DOC PREVIEW
GT ECE 4893 - Lecture 7: Direct3D Basics (I)
School name Georgia Tech
Pages 29

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 7: Direct3D Basics (I)DirectX and GPU (Nvidia-centric) HistoryDirect3D OverviewHardware Abstraction LayerThe Big PictureWin32 ProgrammingCreate A WindowHandling Windows Events and MessagesInitialize Direct3D ObjectExample of Filling D3DPRESENT_PARAMETERSDevice CapabilitiesBasic D3D Function Call LoopClean D3D StatesSlide 14Slide 15D3DX VectorsSlide 17D3DX MatrixSlide 19Create Vertex BufferAccess Vertex BufferPreparing to DrawDrawPrimitive MethodSlide 24Create Indexed Vertex BufferDrawIndexedPrimitive Method (Recommended)Indexed Vertex StructureDrawIndexedPrimitive MethodTipsLecture 7: Direct3D Basics (I)Prof. Hsien-Hsin Sean LeeSchool of Electrical and Computer EngineeringGeorgia Institute of Technology2DirectX and GPU (Nvidia-centric) HistoryDirectX 6MultitexturingRiva TNTDirectX 8SM 1.xGeForce3CgDirectX 9SM 2.0GeForceFXDirectX 9.0cSM 3.0GeForce 6DirectX 5Riva 1281998 1999 2000 2001 2002 2003 2004DirectX 7T&L GeForce 256(NV10)(NV4)NVidia’sresponse to Voodoo23dfx demise19963dfx’sfirstVoodoochip(NV20) (NV30) (NV40)2006DirectX 10SM 4.0GeForce 8(G80)686 millionTransistors1.5GHzAdapted from David Kirk’s slide DirectX 23Direct3D 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 SDK3D AppsDirect3DAPIREFHALGPU4Hardware Abstraction Layer•Provide Hardware Independence to the upper layersd3d->CreateDevice(..–D3DDEVTYPE•D3DDEVTYPE_HAL–BehaviorFlags:•D3DCREATE_SOFTWARE_VERTEXPROCESSING•D3DCREATE_HARDWARE_VERTEXPROCESSING•Provided by Graphics Hardware–Vertex Processing–Pixel Processing–Programmable ShadersCreateDevice( Adapter, DeviceType, HWND hWnd, BehaviorFlags, D3DPRESENT_PARAMETERS PresentParameters, IDirect3DDevice9 **ReturnDeviceInterface);5The Big PictureWhile (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 phase6Win32 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);7Create A Window•Create the Window Class by registering wc class•Create a Window using CreateWindow()•Return a handle for this newly created windowwc.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 program8Handling Windows Events and Messages1. Key Pressed (Enter)2. Mouse moved (Loc)3. Window resized (size)4. … Event QueueContinuePeekMessage()TranslateMessage()DispatchMessage()MainWinProc()MessageRecognized?ProcessMessageAdapted from Chris Hanson’s page9Initialize Direct3D Object•Acquire an IDirect3D9 interface•Fill in D3D structure parameters (next slide)•Create d3d deviceIDirect3D9* 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);10Example of Filling D3DPRESENT_PARAMETERS•Define basic property of d3d object instance•There are more parameters you can specify–Check msdn.microsoft.com for all parametersD3dpp.Windowed = False; // Window modeD3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;D3dpp.hDeviceWindow = hWnd; // Window handleD3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;D3dpp.BackBufferWidth = SCREEN_WIDTH;D3dpp.BackBufferHeight = SCREEN_HEIGHT;D3dpp.BackBufferCount = 1; // Typically 1D3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;11Device Capabilities•Check what are supported on your graphics hardwareIDirect3D9* D3d=0;D3d = Direct3DCreate9(D3D_SDK_VERSION); D3d->GetDeviceCaps(D3DADAPTER_DEFAULT,deviceType,&caps);if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) devBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSINGelse devBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;D3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,devBehaviorFlags,&D3dpp,&gd3dDevice,);12Basic 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);}13Clean D3D States•Release all COM objects prior to exiting the programWhile (TRUE) {... ... ..}texture->Release();vertex_buffer->Release();... ... ..gd3dDevice->Release();D3d->Release();14The Big PictureWhile (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 phase15First Direct3D ExampleHello Direct3D(See Demo in Visual Studio)Note that: DirectX 9.0 SDK Required16D3DX 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*)17D3DXVector Code ExampleSimpleVectors(See


View Full Document

GT ECE 4893 - Lecture 7: Direct3D Basics (I)

Documents in this Course
Load more
Download Lecture 7: Direct3D Basics (I)
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7: Direct3D Basics (I) and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7: Direct3D Basics (I) 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?