Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: Direct3D Basics (I) Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology.

Similar presentations


Presentation on theme: "Lecture 7: Direct3D Basics (I) Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology."— Presentation transcript:

1 Lecture 7: Direct3D Basics (I) Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology

2 2 DirectX and GPU (Nvidia-centric) History DirectX 6 Multitexturing Riva TNT DirectX 8 SM 1.x GeForce3 Cg DirectX 9 SM 2.0 GeForceFX DirectX 9.0c SM 3.0 GeForce 6 DirectX 5 Riva 128 1998199920002001200220032004 DirectX 7 T&L GeForce 256 (NV10)(NV4) NVidia’s response to Voodoo2 3dfx demise 1996 3dfx’s first Voodoo chip (NV20)(NV30)(NV40) 2006 DirectX 10 SM 4.0 GeForce 8 (G80) 686 million Transistors 1.5GHz Adapted from David Kirk’s slide DirectX 2

3 3 Direct3D Overview An Application Programming Interface for 3D “HAL device type” provides hardware independence and makes apps compatible –SOFTWARE_VERTEX_PROCESSING (T&L done in CPU) –HARDWARE_VERTEX_PROCESSING (T&L done by GPU) –MIXED_VERTEX_PROCESSING “REF device type” (Reference Rasterizer) supports the entire D3D using emulation for debugging purposes –E.g., use pixel shader on GPU that does not support pixel shader –Need to install D3D SDK 3D Apps Direct3D API REF HAL GPU

4 4 Hardware Abstraction Layer Provide Hardware Independence to the upper layers d3d->CreateDevice(.. –D3DDEVTYPE D3DDEVTYPE_HAL –BehaviorFlags: D3DCREATE_SOFTWARE_VERTEXPROCESSING D3DCREATE_HARDWARE_VERTEXPROCESSING Provided by Graphics Hardware –Vertex Processing –Pixel Processing –Programmable Shaders CreateDevice( Adapter, DeviceType, HWND hWnd, BehaviorFlags, D3DPRESENT_PARAMETERS PresentParameters, IDirect3DDevice9 **ReturnDeviceInterface );

5 5 The Big Picture While (TRUE) { if (PeekMessage(..)) { /* Event Processing */ TranslateMessage(); DispatchMessage(); } else { /* D3D Game Loop */ render_a_frame(); } clean_D3D(); WinMain() hWnd = CreateWindow(…) registerClass(&wc) D3d = Direct3DCreate9(…) D3d->CreateDevice(…, &D3dpp, &gd3dDevice) init_graphics(); init_lighting(); Initialization phase

6 6 Win32 Programming Initialize your program in WinMain() hInstance: a handle for this application hPrevInstance: for pre-32 bit apps that use the same address space for the same app lpCmdLine: Command line arguments nCmdShow: control appearance of a window, we will not use this int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);

7 7 Create A Window Create the Window Class by registering wc class Create a Window using CreateWindow() Return a handle for this newly created window wc.lpfnWndProc = MainWndProc; wc.style = CS_HREDRAW | CS_VREDRAW;... registerClass(&wc); hWnd = CreateWindow(NULL, L"WindowClass”, L"First D3D Program”, WS_OVERLAPPEDWINDOW, 0, 0, 300, 400, NULL, NULL, hInstance, NULL ); Main message handler of the program

8 8 Handling Windows Events and Messages 1.Key Pressed (Enter) 2.Mouse moved (Loc) 3.Window resized (size) 4.… Event Queue Continue PeekMessage() TranslateMessage() DispatchMessage() MainWinProc() Message Recognized? Process Message Adapted from Chris Hanson’s page

9 9 Initialize Direct3D Object Acquire an IDirect3D9 interface Fill in D3D structure parameters (next slide) Create d3d device IDirect3D9* D3d=0; D3DPRESENT_PARAMETERS D3dpp; IDirect3DDevice9* gd3dDevice=0; D3d = Direct3DCreate9(D3D_SDK_VERSION); D3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3dpp, &gd3dDevice, ); CreateDevice( Adapter, DeviceType, HWND hWnd, BehaviorFlags, D3DPRESENT_PARAMETERS PresentPara, IDirect3DDevice9 **RtrnDevInterface );

10 10 Example of Filling D3DPRESENT_PARAMETERS Define basic property of d3d object instance There are more parameters you can specify –Check msdn.microsoft.com for all parameters D3dpp.Windowed = False; // Window mode D3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; D3dpp.hDeviceWindow = hWnd; // Window handle D3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; D3dpp.BackBufferWidth = SCREEN_WIDTH; D3dpp.BackBufferHeight = SCREEN_HEIGHT; D3dpp.BackBufferCount = 1; // Typically 1 D3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;

11 11 Device Capabilities Check what are supported on your graphics hardware IDirect3D9* D3d=0; D3d = Direct3DCreate9(D3D_SDK_VERSION); D3d->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps); if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) devBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING else devBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; D3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, devBehaviorFlags, &D3dpp, &gd3dDevice, );

12 12 Basic D3D Function Call Loop Clear screen to nothing every frame BeginScene() and EndScene() enclose the rendering of one frame Present() presents the content of the next buffer While (TRUE) { gd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, // initialize z buffer (0, 1) 0 // for stencil buffer ); gd3dDevice->BeginScene();.. gd3dDevice->EndScene(); gd3dDevice->Present(0, 0, 0, 0); }

13 13 Clean D3D States Release all COM objects prior to exiting the program While (TRUE) {........ } texture->Release(); vertex_buffer->Release();........ gd3dDevice->Release(); D3d->Release();

14 14 The Big Picture While (TRUE) { if (PeekMessage(..)) { /* Event Processing */ TranslateMessage(); DispatchMessage(); } else { /* D3D Game Loop */ render_a_frame(); } clean_D3D(); WinMain() hWnd = CreateWindow(…) registerClass(&wc) D3d = Direct3DCreate9(…) D3d->CreateDevice(…, &D3dpp, &gd3dDevice) init_graphics(); init_lighting(); Initialization phase

15 15 First Direct3D Example Hello Direct3D (See Demo in Visual Studio) Note that: DirectX 9.0 SDK Required

16 16 D3DX Vectors Various types –D3DXVECTOR3 vec(x, y, z) –D3DXVECTOR4 vec(x, y, z, w) D3DX provides several common vector functions –float D3DXVec3Dot(D3DXVECTOR3*, D3DXVECTOR3*) –D3DXVec3Cross(D3DXVECTOR3 *cross, D3DXVECTOR3 *v1, D3DXVECTOR3 *v2) –D3DXVec3Normalize(D3DXVECTOR3 *out, D3DXVECTOR3*) –float D3DXVec3Length(D3DXVECTOR3*)

17 17 D3DXVector Code Example SimpleVectors (See Demo in Visual Studio)

18 18 D3DX Matrix D3DXMATRIX (4x4) D3DX provides several common matrix manipulations –D3DXVECTOR4 *D3DXVec4Transform (1x4 * 4x4) –D3DXMATRIX *D3DXMatrixMultiply –D3DXMatrixInverse –D3DXMatrixTranspose –D3DXMatrixIdentity

19 19 D3DXMATRIX Example SimpleMatrix (See Demo in Visual Studio)

20 20 Create Vertex Buffer Create a container for allocating vertices IDirect3DVertexBuffer9* v_buffer; gd3dDevice->CreateVertexBuffer( 3* sizeof( Vertex ), 0,// usage 0,// flexible vertex format D3DPOOL_MANAGED, // indicate buffer in video mem &v_buffer, 0); // reserved, not used Struct Vertex { D3DXVECTOR3 pos; }

21 21 Access Vertex Buffer Obtain a pointer to the vertex or index buffer Need to unlock() them when done Vertex* vertices; v_buffer->Lock(0, 0, (void**)&vertices, 0); vertices[0] = VertexPos(-1.0f, 0.0f, 2.0f); vertices[1] = VertexPos( 0.0f, 1.0f, 2.0f); vertices[2] = VertexPos( 1.0f, 0.0f, 2.0f); v_buffer->Unlock(); IDirect3DVertexBuffer9::Lock( UINT OffsetToLock, UINT SizeToLock, BYTE** ppbData, // return a ptr to the locked mem DWORD Flags);

22 22 Preparing to Draw IDirect3DDevice9::SetStreamSource( UNIT StreamNumber, IDirect3DVertexbuffer9* pStreamData, UINT OffetInBytes, UINT Stride); Stream source id Vertex buffer Offset from the start of the stream Size of each element in the vertex buffer

23 23 DrawPrimitive Method IDirect3DDevice9::DrawPrimitive( D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount); gd3dDevice->SetStramSource(0, v_buffer, 0, sizeof(VertexPC)); gd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 3); Example Point, line, trianglelist Index to the element in the vertex buffer The number of primitives to draw

24 24 DrawPrimitve Example DrawFirstTriangle (See Demo in Visual Studio)

25 25 Create Indexed Vertex Buffer To eliminate the repetitive definition of vertices Use a vertex buffer to store unique vertices Use an index buffer to store Index IDirect3DIndexBuffer9* Index_buffer; gd3dDevice->CreateIndexBuffer( 36* sizeof( WORD ), // x86 word D3DUSAGE_WRITEONLY, // usage D3DFMT_INDEX16, // 16-bit indices D3DPOOL_MANAGED, // indicate buffer in video mem &Index_buffer, 0); // reserved, not used

26 26 DrawIndexedPrimitive Method (Recommended) IDirect3DDevice9::DrawIndexedPrimitive( D3DPRIMITIVETYPE PrimitiveType, UINT BaseVertexIndex, UNIT MinIndex, UNIT NumVertices, UNIT Start Index, UINT PrimitiveCount); // Draw a tetrahedron gd3dDevice->SetStreamSource(0, v_buffer, 0, sizeof(VertexPC)); gd3dDevice->SetIndices(i_buffer); gd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12); Example Point, line, trianglelist Used as an offset for drawing same index buffer for a different vertex patch Min vertex index used relative to BaseVertexIndex Number of vertices First Index Number of Primitives

27 27 Indexed Vertex Structure v_buffer->Lock(0,0,(void*)&v, 0); v[0] = VertexPos(-1.0f, -1.0f, -1.0f); v[1] = VertexPos(-1.0f, 1.0f, -1.0f); v[2] = VertexPos( 1.0f, 1.0f, -1.0f); v[3] = VertexPos( 1.0f, -1.0f, -1.0f); v[4] = VertexPos(-1.0f, -1.0f, 1.0f); v[5] = VertexPos(-1.0f, 1.0f, 1.0f); v[6] = VertexPos( 1.0f, 1.0f, 1.0f); v[7] = VertexPos( 1.0f, -1.0f, 1.0f); v_buffer->Unlock(); V[0] V[1]V[2] V[3] V[4] V[5]V[6] V[7] Index_buffer->Lock(0, 0, (void**)&k, 0); k[0] = 0; k[1] = 1; k[2] = 2; // Front face k[3] = 0; k[4] = 2; k[5] = 3; k[6] = 4; k[7] = 6; k[8] = 5; // Back face k[9] = 4; k[10] = 7; k[11] = 6; k[12] = 4; k[13] = 5; k[14] = 1; // Left face k[15] = 4; k[16] = 1; k[17] = 0; k[18] = 3; k[19] = 2; k[20] = 6; // Right face k[21] = 3; k[22] = 6; k[23] = 7; k[24] = 1; k[25] = 5; k[26] = 6; // Top face k[27] = 1; k[28] = 6; k[29] = 2; k[30] = 4; k[31] = 0; k[32] = 3; // Bottom face k[33] = 4; k[34] = 3; k[35] = 7; Index_buffer->Unlock();

28 28 DrawIndexedPrimitive Method 25 26 Index BufferVertex Buffer 0 1 2 3 78 Start Index gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 29, 78, 2); 25 27 28 27 MinIndex NumVertices 28 26 27 25

29 29 Tips Remember to add these libraries in linker input dependencies d3d9.lib d3dx9.lib dxguid.lib dinput.lib dinput8.lib dxerr9.lib Or have the following in the source code #pragma comment(lib, "d3d9.lib") #pragma comment(lib, "d3dx9.lib") #pragma comment(lib, "dxguid.lib") … … …


Download ppt "Lecture 7: Direct3D Basics (I) Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology."

Similar presentations


Ads by Google