Main.cpp
上传用户:whgydz
上传日期:2007-01-12
资源大小:2259k
文件大小:9k
源码类别:

其他书籍

开发平台:

HTML/CSS

  1. #include <d3dx8.h>
  2. LPDIRECT3D8 g_pD3D = NULL;
  3. LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
  4. LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL; // Buffer to hold vertices
  5. struct CUSTOMVERTEX
  6. {
  7.     FLOAT x, y, z;
  8.     DWORD colour;
  9. };
  10. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
  11. #define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}
  12. HRESULT InitialiseD3D(HWND hWnd)
  13. {
  14.     //First of all, create the main D3D object. If it is created successfully we 
  15.     //should get a pointer to an IDirect3D8 interface.
  16.     g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
  17.     if(g_pD3D == NULL)
  18.     {
  19.         return E_FAIL;
  20.     }
  21.     //Get the current display mode
  22.     D3DDISPLAYMODE d3ddm;
  23.     if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
  24.     {
  25.         return E_FAIL;
  26.     }
  27.     //Create a structure to hold the settings for our device
  28.     D3DPRESENT_PARAMETERS d3dpp; 
  29.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  30.     //Fill the structure. 
  31.     //We want our program to be windowed, and set the back buffer to a format
  32.     //that matches our current display mode
  33.     d3dpp.Windowed = TRUE;
  34.     d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
  35.     d3dpp.BackBufferFormat = d3ddm.Format;
  36.     //Create a Direct3D device.
  37.     if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
  38.                                    D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
  39.     {
  40.         return E_FAIL;
  41.     }
  42.     
  43. //Turn on back face culling. This is becuase we want to hide the back of our polygons
  44.     g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
  45. //Turn off lighting becuase we are specifying that our vertices have colour
  46.     g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
  47.     return S_OK;
  48. }
  49. HRESULT InitialiseVertexBuffer()
  50. {
  51.     VOID* pVertices;
  52.     
  53.     //Store each point of the cube together with it's colour
  54. //Make sure that the points of a polygon are specified in a clockwise direction,
  55. //this is because anti-clockwise faces will be culled
  56. //We will use a three triangle strips to render these polygons (Top, Sides, Bottom).
  57.     CUSTOMVERTEX cvVertices[] =
  58.     {
  59. //Top Face
  60. {-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 0 - Blue 
  61.         {-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red 
  62.         {5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 2 - Red 
  63. {5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 3 - Green 
  64. //Face 1
  65. {-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 4 - Red 
  66.         {-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 5 - Blue 
  67.         {5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 6 - Green 
  68. {5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 7 - Red 
  69. //Face 2
  70. {5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 8 - Blue 
  71.         {5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 9 - Green
  72. //Face 3
  73.         {-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 10 - Green 
  74. {-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 11 - Red 
  75. //Face 4
  76.         {-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 12 - Red 
  77.         {-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 13 - Blue
  78. //Bottom Face
  79. {5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 14 - Green 
  80.         {5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 15 - Blue 
  81.         {-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 16 - Red 
  82. {-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 17 - Green
  83.     };
  84.     //Create the vertex buffer from our device.
  85.     if(FAILED(g_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUSTOMVERTEX),
  86.                                                0, D3DFVF_CUSTOMVERTEX,
  87.                                                D3DPOOL_DEFAULT, &g_pVertexBuffer)))
  88.     {
  89.         return E_FAIL;
  90.     }
  91.     //Get a pointer to the vertex buffer vertices and lock the vertex buffer
  92.     if(FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
  93.     {
  94.         return E_FAIL;
  95.     }
  96.     //Copy our stored vertices values into the vertex buffer
  97.     memcpy(pVertices, cvVertices, sizeof(cvVertices));
  98.     //Unlock the vertex buffer
  99.     g_pVertexBuffer->Unlock();
  100.     return S_OK;
  101. }
  102. void SetupRotation()
  103. {
  104.     //Here we will rotate our world around the x, y and z axis.
  105.     D3DXMATRIX matWorld, matWorldX, matWorldY, matWorldZ;
  106.     
  107. //Create the transformation matrices
  108. D3DXMatrixRotationX(&matWorldX, timeGetTime()/400.0f);
  109. D3DXMatrixRotationY(&matWorldY, timeGetTime()/400.0f);
  110. D3DXMatrixRotationZ(&matWorldZ, timeGetTime()/400.0f);
  111. //Combine the transformations by multiplying them together
  112. D3DXMatrixMultiply(&matWorld, &matWorldX, &matWorldY);
  113. D3DXMatrixMultiply(&matWorld, &matWorld, &matWorldZ);
  114. //Apply the tansformation
  115.     g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
  116. }
  117. void SetupCamera()
  118. {
  119. //Here we will setup the camera.
  120. //The camera has three settings: "Camera Position", "Look at Position" and "Up Direction"
  121. //We have set the following:
  122. //Camera Position: (0, 0, -30)
  123. //Look at Position: (0, 0, 0)
  124. //Up direction: Y-Axis.
  125.     D3DXMATRIX matView;
  126.     D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f,-30.0f), //Camera Position
  127.                                  &D3DXVECTOR3(0.0f, 0.0f, 0.0f), //Look At Position
  128.                                  &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction
  129.     g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
  130. }
  131. void SetupPerspective()
  132. {
  133. //Here we specify the field of view, aspect ration and near and far clipping planes.
  134.     D3DXMATRIX matProj;
  135.     D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 500.0f);
  136.     g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
  137. }
  138. void Render()
  139. {
  140.     if(g_pD3DDevice == NULL)
  141.     {
  142.         return;
  143.     }
  144.     //Clear the backbuffer to black
  145.     g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  146.     
  147.     //Begin the scene
  148.     g_pD3DDevice->BeginScene();
  149.     
  150. //Setup the rotation, camera, and perspective matrices
  151.     SetupRotation();
  152. SetupCamera();
  153. SetupPerspective();
  154.     //Rendering our objects
  155.     g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, sizeof(CUSTOMVERTEX));
  156.     g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
  157.     g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //Top
  158. g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 8); //Sides
  159. g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2); //Bottom
  160.     //End the scene
  161.     g_pD3DDevice->EndScene();
  162.     
  163.     //Filp the back and front buffers so that whatever has been rendered on the back buffer
  164.     //will now be visible on screen (front buffer).
  165.     g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
  166. }
  167. void CleanUp()
  168. {
  169.     SafeRelease(g_pVertexBuffer);
  170.     SafeRelease(g_pD3DDevice);
  171.     SafeRelease(g_pD3D);
  172. }
  173. void GameLoop()
  174. {
  175.     //Enter the game loop
  176.     MSG msg; 
  177.     BOOL fMessage;
  178.     PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
  179.     
  180.     while(msg.message != WM_QUIT)
  181.     {
  182.         fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
  183.         if(fMessage)
  184.         {
  185.             //Process message
  186.             TranslateMessage(&msg);
  187.             DispatchMessage(&msg);
  188.         }
  189.         else
  190.         {
  191.             //No message to process, so render the current scene
  192.             Render();
  193.         }
  194.     }
  195. }
  196. //The windows message handler
  197. LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  198. {
  199.     switch(msg)
  200.     {
  201.         case WM_DESTROY:
  202.             PostQuitMessage(0);
  203.             return 0;
  204.         break;
  205.         case WM_KEYUP: 
  206.             switch (wParam)
  207.             { 
  208.                 case VK_ESCAPE:
  209.                     //User has pressed the escape key, so quit
  210.                     DestroyWindow(hWnd);
  211.                     return 0;
  212.                 break;
  213.             } 
  214.         break;
  215.     }
  216.     return DefWindowProc(hWnd, msg, wParam, lParam);
  217. }
  218. //Application entry point
  219. INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
  220. {
  221.     //Register the window class
  222.     WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L, 
  223.                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
  224.                      "DX Project 3", NULL};
  225.     RegisterClassEx(&wc);
  226.     //Create the application's window
  227.     HWND hWnd = CreateWindow("DX Project 3", "www.andypike.com: Tutorial 3", 
  228.                               WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
  229.                               GetDesktopWindow(), NULL, wc.hInstance, NULL);
  230.     //Initialize Direct3D
  231.     if(SUCCEEDED(InitialiseD3D(hWnd)))
  232.     { 
  233.         //Show our window
  234.         ShowWindow(hWnd, SW_SHOWDEFAULT);
  235.         UpdateWindow(hWnd);
  236.         //Initialize Vertex Buffer
  237.         if(SUCCEEDED(InitialiseVertexBuffer()))
  238.         {
  239.             //Start game running: Enter the game loop
  240.             GameLoop();
  241.         }
  242.     }
  243.     
  244.     CleanUp();
  245.     UnregisterClass("DX Project 3", wc.hInstance);
  246.     
  247.     return 0;
  248. }