1 / 83

DirectX 9.0 SDK Tutorial

DirectX 9.0 SDK Tutorial. Tutorial 1: Creating a device. To use Microsoft Direct3D, create an application window. create and initialize Direct3D objects. use the Component Object Model (COM) interfaces

rmooney
Download Presentation

DirectX 9.0 SDK Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DirectX 9.0 SDK Tutorial

  2. Tutorial 1: Creating a device • To use Microsoft Direct3D, • create an application window. • create and initialize Direct3D objects. • use the Component Object Model (COM) interfaces • these objects implement to manipulate them and to create other objects required to render a scene. 컴퓨터게임(DirectX)

  3. Creating a Direct3D device and rendering a blue screen • Step 1: Creating a Window • Step 2: Initializing Direct3D • Step 3: Handling System Messages • Step 4: Rendering and Displaying a Scene • Step 5: Shutting Down 컴퓨터게임(DirectX)

  4. Step 1: Creating a Windows • CreateDevice sample project begins execution at its WinMain function. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // Register the window class. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "Direct3D Tutorial", NULL }; RegisterClassEx( &wc ); // Create the application's window. HWND hWnd = CreateWindow( "Direct3D Tutorial", "Direct3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL ); 컴퓨터게임(DirectX)

  5. Step 2: Initializing Direct3D • After you create an application window, • you are ready to initialize the Direct3D object that you will use to render the scene. • This process includes • creating the object, • setting the presentation parameters, • And finally creating the Direct3D device. 컴퓨터게임(DirectX)

  6. HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice. Most parameters are // zeroed out. We set Windowed to TRUE, since we want to do D3D in a // window, and then set the SwapEffect to "discard", which is the most // efficient method of presenting the back buffer to the display. And // we request a back buffer format that matches the current desktop display // format. 컴퓨터게임(DirectX)

  7. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Create the Direct3D device. Here we are using the default adapter (most // systems only have one, unless they have multiple graphics hardware cards // installed) and requesting the HAL (which is saying we want the hardware // device rather than a software one). Software vertex processing is // specified since we know it will work on all cards. On cards that support // hardware vertex processing, though, we would see a big performance gain // by specifying hardware vertex processing. 컴퓨터게임(DirectX)

  8. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Device state would normally be set here return S_OK; } 컴퓨터게임(DirectX)

  9. Step 3: Handling System Messages • After you have created the application window and initialized Microsoft Direct3D, • you are ready to render the scene. • In most cases, Microsoft Windows applications monitor system messages in their message loop, and they render frames whenever no messages are in the queue. • However, the CreateDevice sample project waits until a WM_PAINT message is in the queue, telling the application that it needs to redraw all or part of its window. 컴퓨터게임(DirectX)

  10. // The message loop. MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } • Each time the loop runs, DispatchMessage calls MsgProc, • which handles messages in the queue. • When WM_PAINT is queued, the application calls Render, • the application-defined function that will redraw the window. • Then the Microsoft Win32 function ValidateRect is called to validate the entire client area. 컴퓨터게임(DirectX)

  11. Message Handling Function: LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: PostQuitMessage( 0 ); return 0; case WM_PAINT: Render(); ValidateRect( hWnd, NULL ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } 컴퓨터게임(DirectX)

  12. Step 4: Rendering and displaying a Scene • To render and display the scene, the sample code in this step • clears the back buffer to a blue color, • transfers the contents of the back buffer to the front buffer, and • presents the front buffer to the screen. • To clear a scene, call the IDirect3DDevice9::Clear method. // Clear the back buffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); 컴퓨터게임(DirectX)

  13. After clearing the viewport, • the CreateDevice sample project informs Direct3D that rendering will begin, • then signals that rendering is complete, // Begin the scene g_pd3dDevice->BeginScene(); // Rendering of scene objects happens here // End the scene g_pd3dDevice->EndScene(); 컴퓨터게임(DirectX)

  14. After rendering the scene, • display it by using the IDirect3DDevice9::Present method. g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); • The first two parameters • a source rectangle and destination rectangle. • The third parameter • sets the destination window for this presentation. • Because this parameter is set to NULL, the hWndDeviceWindow member of D3DPRESENT_PARAMETERS is used. • The fourth parameter • the DirtyRegion parameter and in most cases should be set to NULL. 컴퓨터게임(DirectX)

  15. Step 5: Shutting Down • Shutting down a Microsoft DirectX application • not only means that you destroy the application window, • but you also deallocate any DirectX objects your application uses, and • you invalidate the pointers to them. • The CreateDevice sample project calls • Cleanup, an application-defined function to handle this when it receives a WM_DESTROY message. 컴퓨터게임(DirectX)

  16. VOID Cleanup() { if( g_pd3dDevice != NULL) g_pd3dDevice->Release(); if( g_pD3D != NULL) g_pD3D->Release(); } 컴퓨터게임(DirectX)

  17. Tutorial 2: Rendering Vertices • Applications written in Microsoft Direct3D • use vertices to draw geometric shapes. • Each three-dimensional (3-D) scene • includes one or more of these geometric shapes. • The Vertices sample project • creates the simplest shape, a triangle, and renders it to the display. • This tutorial • shows how to use vertices to create a triangle with the following steps: • Steps • Step 1: Defining a Custom Vertex Type • Step 2: Setting Up the Vertex Buffer • Step 3: Rendering the Display 컴퓨터게임(DirectX)

  18. Step 1: Defining a Custom Vertex Type • The Vertices sample project • renders a 2-D triangle by using three vertices. • This introduces the concept of the vertex buffer, • which is a Microsoft Direct3D object that is used to store and render vertices. • Vertices can be defined in many ways • by specifying a custom vertex structure and corresponding custom flexible vertex format (FVF). • The format of the vertices in the Vertices sample project is shown in the following code fragment: struct CUSTOMVERTEX { FLOAT x, y, z, rhw; // The transformed position for the vertex. DWORD color; // The vertex color. }; 컴퓨터게임(DirectX)

  19. The next step is to define the FVF that describes the contents of the vertices in the vertex buffer. • The following code fragment defines an FVF that corresponds with the custom vertex type created above. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) 컴퓨터게임(DirectX)

  20. Step 2: Setting Up the Vertex Buffer • Now that the custom vertex format is defined, • it is time to initialize the vertices. • The Vertices sample project does this • by calling the application-defined function InitVB after creating the required Microsoft Direct3D objects. • The following code fragment initializes the values for three custom vertices. CUSTOMVERTEX vertices[] = { { 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, }, { 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, }, }; 컴퓨터게임(DirectX)

  21. The next step is to • call IDirect3DDevice9::CreateVertexBuffer to create a vertex buffer as shown in the following code fragment. if( FAILED( g_pd3dDevice->CreateVertexBuffer ( 3*sizeof(CUSTOMVERTEX), 0 /*Usage*/, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) ) return E_FAIL; • The first two parameters • the desired size and usage for the new vertex buffer. • The next two parameters specify • the vector format and memory location for the new buffer. • The vector format here is D3DFVF_CUSTOMVERTEX, which is the flexible vertex format (FVF) • The D3DPOOL_DEFAULT flag • create the vertex buffer in the memory allocation that is most appropriate for this buffer. • The final parameter is the address of the vertex buffer to create. 컴퓨터게임(DirectX)

  22. After creating a vertex buffer, • it is filled with data from the custom vertices. VOID* pVertices; if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, vertices, sizeof(vertices) ); g_pVB->Unlock(); • The vertex buffer is first locked by calling IDirect3DVertexBuffer9::Lock. • The first parameter is the offset into the vertex data to lock, in bytes. • The second parameter is the size of the vertex data to lock, in bytes. • The third parameter is the address of a BYTE pointer, filled with a pointer to vertex data. • The fourth parameter tells the vertex buffer how to lock the data. 컴퓨터게임(DirectX)

  23. The vertices are then copied into the vertex buffer using memcpy. • After the vertices are in the vertex buffer, a call is made to IDirect3DVertexBuffer9::Unlock to unlock the vertex buffer. 컴퓨터게임(DirectX)

  24. Step 3: Rendering the Display • Now that the vertex buffer is filled with vertices, • it is time to render the display. • Rendering the display starts • by clearing the back buffer to a blue color and then calling BeginScene. g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0L ); g_pd3dDevice->BeginScene(); 컴퓨터게임(DirectX)

  25. Rendering vertex data from a vertex buffer requires a few steps. • First, you need to set the stream source; in this case, use stream 0. • The source of the stream is specified by calling IDirect3DDevice9::SetStreamSource. g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); • The first parameter is the stream number and • the second parameter is a pointer to the vertex buffer. • The third parameter is the offset from the beginning of the stream to the beginning of the vertex data, which is 0 in this example. • The last parameter is the number of bytes of each element in the vertex declaration. 컴퓨터게임(DirectX)

  26. The next step is to call IDirect3DDevice9::SetFVF to identify the fixed function vertex shader. g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); • The only parameter for SetFVF is the fixed vertex function code to define the vertex data layout. • The next step is to use IDirect3DDevice9::DrawPrimitive to render the vertices in the vertex buffer. g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 ); • The first parameter a flag that tells Microsoft Direct3D what type of primitives to draw. • This sample uses the flag D3DPT_TRIANGLELIST to specify a list of triangles. • The second parameter is the index of the first vertex to load. • The third parameter tells the number of primitives to draw. 컴퓨터게임(DirectX)

  27. The last steps are to end the scene and then • present the back buffer to the front buffer. g_pd3dDevice->EndScene(); g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); • After the back buffer is presented to the front buffer, • the client window shows a triangle with three different colored points. 컴퓨터게임(DirectX)

  28. Tutorial 3: Using Matrices • This tutorial introduces the concept of matrices and shows how to use them. • you will be working with transformations of vertices in 3-D. • Matrices are also used to set up cameras and viewports. • Before the Matrices sample project renders geometry, • it calls the SetupMatrices application-defined function to create and set the matrix transformations that are used to render the 3-D triangle. • Steps • Step 1: Defining the World Transformation Matrix • Step 2: Defining the View Transformation Matrix • Step 3: Defining the Projection Transformation Matrix 컴퓨터게임(DirectX)

  29. The order in which these transformation matrices are created • does not affect the layout of the objects in a scene. • However, Microsoft Direct3D applies the matrices to the scene in the following order: • World • View • Projection 컴퓨터게임(DirectX)

  30. Step 1: Defining the World Transformation Matrix • The world transformation matrix defines • how to translate, scale, and rotate the geometry in the 3-D model space. • The following code fragment rotates the triangle on the y-axis and • then sets the current world transformation for the Microsoft Direct3D device. D3DXMATRIX matWorld; D3DXMatrixRotationY( &matWorld, timeGetTime()/150.0f ); g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); 컴퓨터게임(DirectX)

  31. The first step is to rotate the triangle around the y-axis by calling the D3DXMatrixRotationY method. • The first parameter is a pointer to a D3DXMATRIX structure that is the result of the operation. • The second parameter is the angle of rotation in radians. • The next step is to call IDirect3DDevice9::SetTransform to set the world transformation for the Direct3D device. • The first parameter accepted by IDirect3DDevice9::SetTransform tells Direct3D which transformation to set. • This sample uses the D3DTS_WORLD macro to specify that the world transformation should be set. • The second parameter is a pointer to a matrix that is set as the current transformation. 컴퓨터게임(DirectX)

  32. Step 2: Defining the View Transformation Matrix • The view transformation matrix defines the position and rotation of the view. • The view matrix is the camera for the scene. • The following code fragment • creates the view transformation matrix and then • sets the current view transformation for the Microsoft Direct3D device. D3DXVECTOR3 vEyePt ( 0.0f, 3.0f,-5.0f ); D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f ); D3DXVECTOR3 vUpVec ( 0.0f, 1.0f, 0.0f ); D3DXMATRIXA16 matView; D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); 컴퓨터게임(DirectX)

  33. The first step is to define the view matrix by calling D3DXMatrixLookAtLH. • The first parameter is a pointer to a D3DXMATRIX structure that is the result of the operation. • The second, third, and fourth parameters define the eye point, look-at point, and "up" direction, respectively. • Here the eye is set back along the z-axis by five units and up three units, the look-at point is set at the origin, and "up" is defined as the y-direction. • The next step is to call IDirect3DDevice9::SetTransform to set the view transformation for the Direct3D device. • The first parameter accepted by IDirect3DDevice9::SetTransform tells Direct3D which transformation to set. • This sample uses the D3DTS_VIEW flag to specify that the view transformation should be set. • The second parameter is a pointer to a matrix that is set as the current transformation. 컴퓨터게임(DirectX)

  34. Step 3: Defining the Projection Transformation Matrix • The projection transformation matrix defines • how geometry is transformed from 3-D view space to 2-D viewport space. • The following code fragment • creates the projection transformation matrix and then • sets the current projection transformation for the Microsoft Direct3D device. D3DXMATRIX matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); 컴퓨터게임(DirectX)

  35. The first step is to call D3DXMatrixPerspectiveFovLH to set up the projection matrix. • The first parameter is a pointer to a D3DXMATRIX structure that is the result of the operation. • The second parameter defines the field of view, which tells how objects in the distance get smaller. • A typical field of view is 1/4 pi, which is what the sample uses. • The third parameter defines the aspect ratio. • The sample uses the typical aspect ratio of 1. • The fourth and fifth parameters define the near and far clipping plane. • This determines the distance at which geometry should no longer be rendered. • The Matrices sample project has its near clipping plane set at 1 and its far clipping plane set at 100. 컴퓨터게임(DirectX)

  36. The next step is to call IDirect3DDevice9::SetTransform to apply the transformation to the Direct3D device. • The first parameter accepted by IDirect3DDevice9::SetTransform tells Direct3D which transformation to set. • This sample uses the D3DTS_PROJECTION flag to specify that the projection transformation should be set. • The second parameter is a pointer to a matrix that is set as the current transformation. 컴퓨터게임(DirectX)

  37. Tutorial 4: Creating and Using Lights • Microsoft Direct3D lights add more realism to 3-D objects. • When used, each geometric object in the scene will be lit based on the location and type of lights that are used. • This tutorial has the following steps to create a material and a light. • Steps • Step 1: Initializing Scene Geometry • Step 2: Setting Up Material and Light 컴퓨터게임(DirectX)

  38. Step 1: Initializing Scene Geometry • One of the requirements of using lights is that • each surface has a normal. • To do this, the Lights sample project uses a different custom vertex type. • The new custom vertex format has a 3-D position and a surface normal. • The surface normal is used internally by Microsoft Direct3D for lighting calculations. struct CUSTOMVERTEX { D3DXVECTOR3 position; // The 3-D position for the vertex. D3DXVECTOR3 normal; // The surface normal for the vertex. }; // Custom flexible vertex format (FVF). #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL) 컴퓨터게임(DirectX)

  39. Now that the correct vector format is defined, the Lights sample project calls InitGeometry, • an application-defined function that creates a cylinder. • The first step is to create a vertex buffer that stores the points of the cylinder. // Create the vertex buffer. if( FAILED( g_pd3dDevice->CreateVertexBuffer( 50*2*sizeof(CUSTOMVERTEX), 0 /*Usage*/, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) ) return E_FAIL; 컴퓨터게임(DirectX)

  40. The next step is to fill the vertex buffer with the points of the cylinder. CUSTOMVERTEX* pVertices; if( FAILED( g_pVB->Lock( 0, 0, (void**)&pVertices, 0 ) ) ) return E_FAIL; for( DWORD i=0; i<50; i++ ) { FLOAT theta = (2*D3DX_PI*i)/(50-1); pVertices[2*i+0].position = D3DXVECTOR3( sinf(theta),-1.0f, cosf(theta) ); pVertices[2*i+0].normal = D3DXVECTOR3( sinf(theta), 0.0f, cosf(theta) ); pVertices[2*i+1].position = D3DXVECTOR3( sinf(theta), 1.0f, cosf(theta) ); pVertices[2*i+1].normal = D3DXVECTOR3( sinf(theta), 0.0f, cosf(theta) ); } 컴퓨터게임(DirectX)

  41. After the preceding sample code fills the vertex buffer with the vertices for a cylinder, the vertex buffer is ready for rendering. • But first, the material and light for this scene must be set up before rendering the cylinder. 컴퓨터게임(DirectX)

  42. Step 2: Setting Up Material and Light • To use lighting in Microsoft Direct3D, • you must create one or more lights. • To determine which color a geometric object reflects, • a material is created that is used to render geometric objects. • Before rendering the scene, the Lights sample project calls SetupLights, • an application-defined function that sets up one material and one directional light. 컴퓨터게임(DirectX)

  43. Creating a Material • A material defines the color that is reflected off the surface of a geometric object when a light hits it. • The following code fragment uses the D3DMATERIAL9 structure to create a material that is yellow. D3DMATERIAL9 mtrl; ZeroMemory( &mtrl, sizeof(mtrl) ); mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f; mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f; mtrl.Diffuse.b = mtrl.Ambient.b = 0.0f; mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f; g_pd3dDevice->SetMaterial( &mtrl ); 컴퓨터게임(DirectX)

  44. The diffuse color and ambient color for the material are set to yellow. • The call to the IDirect3DDevice9::SetMaterial method • applies the material to the Direct3D device used to render the scene. • The only parameter that IDirect3DDevice9::SetMaterial accepts is the address of the material to set. • After this call is made, every primitive will be rendered with this material until another call is made to IDirect3DDevice9::SetMaterial that specifies a different material. 컴퓨터게임(DirectX)

  45. Creating a Light • There are three types of lights available in Direct3D: • point lights • directional lights • Spotlights • The sample code creates a directional light, which is a light that goes in one direction. • The code also oscillates the direction of the light. • The following code fragment uses the D3DLIGHT9 structure to create a directional light. D3DXVECTOR3 vecDir; D3DLIGHT9 light; ZeroMemory( &light, sizeof(light) ); light.Type = D3DLIGHT_DIRECTIONAL; 컴퓨터게임(DirectX)

  46. The following code fragment sets the diffuse color for this light to white. light.Diffuse.r = 1.0f; light.Diffuse.g = 1.0f; light.Diffuse.b = 1.0f; • The following code fragment rotates the direction of the light around in a circle. vecDir = D3DXVECTOR3(cosf(timeGetTime()/360.0f), 0.0f, sinf(timeGetTime()/360.0f) ); D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir ); 컴퓨터게임(DirectX)

  47. The call to D3DXVec3Normalize normalizes the direction vector used to determine the direction of the light. • A range can be specified to tell Direct3D how far the light will have an effect. • This member does not affect directional lights. • The following code fragment assigns a range of 1000 units to this light. light.Range = 1000.0f; 컴퓨터게임(DirectX)

  48. The following code fragment assigns the light to the Direct3D device by calling IDirect3DDevice9::SetLight. g_pd3dDevice->SetLight( 0, &light ); • The first parameter that IDirect3DDevice9::SetLight accepts is the index that this light will be assigned to. • The second parameter is a pointer to the light structure that defines the light. • The Lights sample project places this light at index 0. 컴퓨터게임(DirectX)

  49. The following code fragment enables the light by calling IDirect3DDevice9::LightEnable. g_pd3dDevice->LightEnable( 0, TRUE); • The first parameter that IDirect3DDevice9::LightEnable accepts is the index of the light to enable. • The second parameter is a Boolean value that tells whether to turn the light on (TRUE) or off (FALSE). • In the sample code above, the light at index 0 is turned on. 컴퓨터게임(DirectX)

  50. The following code fragment tells Direct3D to render lights by calling IDirect3DDevice9::SetRenderState. g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); • The first two parameters that IDirect3DDevice9::SetRenderState accepts is • which device state variable to modify and what value to set it to. • This code sample sets the D3DRS_LIGHTING device variable to TRUE, which has the effect of enabling the rendering of lights. 컴퓨터게임(DirectX)

More Related