home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / Tutorials / Tut02_Vertices / Vertices.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  8.5 KB  |  243 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Vertices.cpp
  3. //
  4. // Desc: In this tutorial, we are rendering some vertices. This introduces the
  5. //       concept of the vertex buffer, a Direct3D object used to store
  6. //       vertices. Vertices can be defined any way we want by defining a
  7. //       custom structure and a custom FVF (flexible vertex format). In this
  8. //       tutorial, we are using vertices that are transformed (meaning they
  9. //       are already in 2D window coordinates) and lit (meaning we are not
  10. //       using Direct3D lighting, but are supplying our own colors).
  11. //
  12. // Copyright (c) 2000-2001 Microsoft Corporation. All rights reserved.
  13. //-----------------------------------------------------------------------------
  14. #include <d3d8.h>
  15.  
  16.  
  17.  
  18.  
  19. //-----------------------------------------------------------------------------
  20. // Global variables
  21. //-----------------------------------------------------------------------------
  22. LPDIRECT3D8             g_pD3D       = NULL; // Used to create the D3DDevice
  23. LPDIRECT3DDEVICE8       g_pd3dDevice = NULL; // Our rendering device
  24. LPDIRECT3DVERTEXBUFFER8 g_pVB        = NULL; // Buffer to hold vertices
  25.  
  26. // A structure for our custom vertex type
  27. struct CUSTOMVERTEX
  28. {
  29.     FLOAT x, y, z, rhw; // The transformed position for the vertex
  30.     DWORD color;        // The vertex color
  31. };
  32.  
  33. // Our custom FVF, which describes our custom vertex structure
  34. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
  35.  
  36.  
  37.  
  38.  
  39. //-----------------------------------------------------------------------------
  40. // Name: InitD3D()
  41. // Desc: Initializes Direct3D
  42. //-----------------------------------------------------------------------------
  43. HRESULT InitD3D( HWND hWnd )
  44. {
  45.     // Create the D3D object.
  46.     if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
  47.         return E_FAIL;
  48.  
  49.     // Get the current desktop display mode, so we can set up a back
  50.     // buffer of the same format
  51.     D3DDISPLAYMODE d3ddm;
  52.     if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
  53.         return E_FAIL;
  54.  
  55.     // Set up the structure used to create the D3DDevice
  56.     D3DPRESENT_PARAMETERS d3dpp;
  57.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  58.     d3dpp.Windowed = TRUE;
  59.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  60.     d3dpp.BackBufferFormat = d3ddm.Format;
  61.  
  62.     // Create the D3DDevice
  63.     if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  64.                                       D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  65.                                       &d3dpp, &g_pd3dDevice ) ) )
  66.     {
  67.         return E_FAIL;
  68.     }
  69.  
  70.     // Device state would normally be set here
  71.  
  72.     return S_OK;
  73. }
  74.  
  75.  
  76.  
  77.  
  78. //-----------------------------------------------------------------------------
  79. // Name: InitVB()
  80. // Desc: Creates a vertex buffer and fills it with our vertices. The vertex
  81. //       buffer is basically just a chuck of memory that holds vertices. After
  82. //       creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
  83. //       also uses index buffers. The special thing about vertex and index
  84. //       buffers is that they can be created in device memory, allowing some
  85. //       cards to process them in hardware, resulting in a dramatic
  86. //       performance gain.
  87. //-----------------------------------------------------------------------------
  88. HRESULT InitVB()
  89. {
  90.     // Initialize three vertices for rendering a triangle
  91.     CUSTOMVERTEX g_Vertices[] =
  92.     {
  93.         { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
  94.         { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
  95.         {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
  96.     };
  97.  
  98.     // Create the vertex buffer. Here we are allocating enough memory
  99.     // (from the default pool) to hold all our 3 custom vertices. We also
  100.     // specify the FVF, so the vertex buffer knows what data it contains.
  101.     if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
  102.                                                   0, D3DFVF_CUSTOMVERTEX,
  103.                                                   D3DPOOL_DEFAULT, &g_pVB ) ) )
  104.     {
  105.         return E_FAIL;
  106.     }
  107.  
  108.     // Now we fill the vertex buffer. To do this, we need to Lock() the VB to
  109.     // gain access to the vertices. This mechanism is required becuase vertex
  110.     // buffers may be in device memory.
  111.     VOID* pVertices;
  112.     if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (BYTE**)&pVertices, 0 ) ) )
  113.         return E_FAIL;
  114.     memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
  115.     g_pVB->Unlock();
  116.  
  117.     return S_OK;
  118. }
  119.  
  120.  
  121.  
  122.  
  123. //-----------------------------------------------------------------------------
  124. // Name: Cleanup()
  125. // Desc: Releases all previously initialized objects
  126. //-----------------------------------------------------------------------------
  127. VOID Cleanup()
  128. {
  129.     if( g_pVB != NULL )        
  130.         g_pVB->Release();
  131.  
  132.     if( g_pd3dDevice != NULL ) 
  133.         g_pd3dDevice->Release();
  134.  
  135.     if( g_pD3D != NULL )       
  136.         g_pD3D->Release();
  137. }
  138.  
  139.  
  140.  
  141.  
  142. //-----------------------------------------------------------------------------
  143. // Name: Render()
  144. // Desc: Draws the scene
  145. //-----------------------------------------------------------------------------
  146. VOID Render()
  147. {
  148.     // Clear the backbuffer to a blue color
  149.     g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
  150.  
  151.     // Begin the scene
  152.     g_pd3dDevice->BeginScene();
  153.  
  154.     // Draw the triangles in the vertex buffer. This is broken into a few
  155.     // steps. We are passing the vertices down a "stream", so first we need
  156.     // to specify the source of that stream, which is our vertex buffer. Then
  157.     // we need to let D3D know what vertex shader to use. Full, custom vertex
  158.     // shaders are an advanced topic, but in most cases the vertex shader is
  159.     // just the FVF, so that D3D knows what type of vertices we are dealing
  160.     // with. Finally, we call DrawPrimitive() which does the actual rendering
  161.     // of our geometry (in this case, just one triangle).
  162.     g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
  163.     g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
  164.     g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
  165.  
  166.     // End the scene
  167.     g_pd3dDevice->EndScene();
  168.  
  169.     // Present the backbuffer contents to the display
  170.     g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
  171. }
  172.  
  173.  
  174.  
  175.  
  176. //-----------------------------------------------------------------------------
  177. // Name: MsgProc()
  178. // Desc: The window's message handler
  179. //-----------------------------------------------------------------------------
  180. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  181. {
  182.     switch( msg )
  183.     {
  184.         case WM_DESTROY:
  185.             PostQuitMessage( 0 );
  186.             return 0;
  187.     }
  188.  
  189.     return DefWindowProc( hWnd, msg, wParam, lParam );
  190. }
  191.  
  192.  
  193.  
  194.  
  195. //-----------------------------------------------------------------------------
  196. // Name: WinMain()
  197. // Desc: The application's entry point
  198. //-----------------------------------------------------------------------------
  199. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  200. {
  201.     // Register the window class
  202.     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
  203.                       GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
  204.                       "D3D Tutorial", NULL };
  205.     RegisterClassEx( &wc );
  206.  
  207.     // Create the application's window
  208.     HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 02: Vertices",
  209.                               WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
  210.                               GetDesktopWindow(), NULL, wc.hInstance, NULL );
  211.  
  212.     // Initialize Direct3D
  213.     if( SUCCEEDED( InitD3D( hWnd ) ) )
  214.     {
  215.         // Create the vertex buffer
  216.         if( SUCCEEDED( InitVB() ) )
  217.         {
  218.             // Show the window
  219.             ShowWindow( hWnd, SW_SHOWDEFAULT );
  220.             UpdateWindow( hWnd );
  221.  
  222.             // Enter the message loop
  223.             MSG msg;
  224.             ZeroMemory( &msg, sizeof(msg) );
  225.             while( msg.message!=WM_QUIT )
  226.             {
  227.                 if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  228.                 {
  229.                     TranslateMessage( &msg );
  230.                     DispatchMessage( &msg );
  231.                 }
  232.                 else
  233.                     Render();
  234.             }
  235.         }
  236.     }
  237.  
  238.     // Clean up everything and exit the app
  239.     Cleanup();
  240.     UnregisterClass( "D3D Tutorial", wc.hInstance );
  241.     return 0;
  242. }
  243.