Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. // include the basic windows header files and the Direct3D header file
  2. #include <windows.h>
  3. #include <windowsx.h>
  4. #include <d3d9.h>
  5. #include <d3dx9.h>
  6.  
  7. // define the screen resolution
  8. #define SCREEN_WIDTH 800
  9. #define SCREEN_HEIGHT 600
  10.  
  11. // include the Direct3D Library files
  12. #pragma comment (lib, "d3d9.lib")
  13. #pragma comment (lib, "d3dx9.lib")
  14.  
  15. // global declarations
  16. LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
  17. LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
  18. LPDIRECT3DINDEXBUFFER9 i_buffer = NULL; // the pointer to the vertex buffer
  19. LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer
  20.  
  21. // function prototypes
  22. void initD3D(HWND hWnd); // sets up and initializes Direct3D
  23. void render_frame(void); // renders a single frame
  24. void cleanD3D(void); // closes Direct3D and releases memory
  25. void init_graphics(void); // 3D declarations
  26.  
  27. struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR;};
  28. #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)
  29.  
  30. // the WindowProc function prototype
  31. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  32.  
  33.  
  34. // the entry point for any Windows program
  35. int WINAPI WinMain(HINSTANCE hInstance,
  36. HINSTANCE hPrevInstance,
  37. LPSTR lpCmdLine,
  38. int nCmdShow)
  39. {
  40. HWND hWnd;
  41. WNDCLASSEX wc;
  42.  
  43. ZeroMemory(&wc, sizeof(WNDCLASSEX));
  44.  
  45. wc.cbSize = sizeof(WNDCLASSEX);
  46. wc.style = CS_HREDRAW | CS_VREDRAW;
  47. wc.lpfnWndProc = WindowProc;
  48. wc.hInstance = hInstance;
  49. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  50. wc.lpszClassName = L"WindowClass";
  51.  
  52. RegisterClassEx(&wc);
  53.  
  54. hWnd = CreateWindowEx(NULL, L"WindowClass", L"Our Direct3D Program",
  55. WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
  56. NULL, NULL, hInstance, NULL);
  57.  
  58. ShowWindow(hWnd, nCmdShow);
  59.  
  60. // set up and initialize Direct3D
  61. initD3D(hWnd);
  62.  
  63. // enter the main loop:
  64.  
  65. MSG msg;
  66.  
  67. while(TRUE)
  68. {
  69. while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  70. {
  71. TranslateMessage(&msg);
  72. DispatchMessage(&msg);
  73. }
  74.  
  75. if(msg.message == WM_QUIT)
  76. break;
  77.  
  78. render_frame();
  79. }
  80.  
  81. // clean up DirectX and COM
  82. cleanD3D();
  83.  
  84. return msg.wParam;
  85. }
  86.  
  87.  
  88. // this is the main message handler for the program
  89. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  90. {
  91. switch(message)
  92. {
  93. case WM_DESTROY:
  94. {
  95. PostQuitMessage(0);
  96. return 0;
  97. } break;
  98. }
  99.  
  100. return DefWindowProc (hWnd, message, wParam, lParam);
  101. }
  102.  
  103.  
  104. // this function initializes and prepares Direct3D for use
  105. void initD3D(HWND hWnd)
  106. {
  107. d3d = Direct3DCreate9(D3D_SDK_VERSION);
  108.  
  109. D3DPRESENT_PARAMETERS d3dpp;
  110.  
  111. ZeroMemory(&d3dpp, sizeof(d3dpp));
  112. d3dpp.Windowed = TRUE;
  113. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  114. d3dpp.hDeviceWindow = hWnd;
  115. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  116. d3dpp.BackBufferWidth = SCREEN_WIDTH;
  117. d3dpp.BackBufferHeight = SCREEN_HEIGHT;
  118.  
  119. // create a device class using this information and the info from the d3dpp stuct
  120. d3d->CreateDevice(D3DADAPTER_DEFAULT,
  121. D3DDEVTYPE_HAL,
  122. hWnd,
  123. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  124. &d3dpp,
  125. &d3ddev);
  126.  
  127. init_graphics(); // call the function to initialize the triangle
  128.  
  129. d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); // turn off the 3D lighting
  130. }
  131.  
  132.  
  133. // this is the function used to render a single frame
  134. void render_frame(void)
  135. {
  136. d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  137.  
  138. d3ddev->BeginScene();
  139.  
  140. // select which vertex format we are using
  141. d3ddev->SetFVF(CUSTOMFVF);
  142.  
  143. // SET UP THE PIPELINE
  144.  
  145. D3DXMATRIX matRotateY; // a matrix to store the rotation information
  146. D3DXMATRIX matRotateX; // a matrix to store the rotation information
  147. D3DXMATRIX transMat;
  148.  
  149. static float index = 0.0f; index+=0.05f; // an ever-increasing float value
  150.  
  151. // build a matrix to rotate the model based on the increasing float value
  152. D3DXMatrixRotationY(&matRotateY, index);
  153. D3DXMatrixRotationX(&matRotateX, index);
  154.  
  155. // tell Direct3D about our matrix
  156.  
  157.  
  158. //draw triangle 1
  159. /*D3DXMatrixTranslation(&transMat,3,0,0);
  160. d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY*transMat));
  161. d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);*/
  162.  
  163. ////draw Triangle 2
  164. //D3DXMatrixTranslation(&transMat,3,60,0);
  165. //d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY*transMat));
  166. //d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
  167.  
  168.  
  169. D3DXMATRIX matView; // the view transform matrix
  170.  
  171. D3DXMatrixLookAtLH(&matView,
  172. &D3DXVECTOR3 (0.0f, 0.0f, 400.0f), // the camera position
  173. &D3DXVECTOR3 (0.0f, 1.0f, 0.0f), // the look-at position
  174. &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
  175.  
  176. d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
  177.  
  178. D3DXMATRIX matProjection; // the projection transform matrix
  179.  
  180. D3DXMatrixPerspectiveFovLH(&matProjection,
  181. D3DXToRadian(45), // the horizontal field of view
  182. (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
  183. 1.0f, // the near view-plane
  184. 1000.0f); // the far view-plane
  185.  
  186. d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
  187.  
  188. // select the vertex buffer to display
  189. d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
  190.  
  191.  
  192. d3ddev->SetIndices(i_buffer);
  193.  
  194. d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 5, 0, 6);
  195. // copy the vertex buffer to the back buffer
  196.  
  197.  
  198.  
  199. d3ddev->EndScene();
  200.  
  201. d3ddev->Present(NULL, NULL, NULL, NULL);
  202. }
  203.  
  204.  
  205. // this is the function that cleans up Direct3D and COM
  206. void cleanD3D(void)
  207. {
  208. v_buffer->Release(); // close and release the vertex buffer
  209. d3ddev->Release(); // close and release the 3D device
  210. d3d->Release(); // close and release Direct3D
  211. }
  212.  
  213.  
  214. // this is the function that puts the 3D models into video RAM
  215. void init_graphics(void)
  216. {
  217. // create the vertices using the CUSTOMVERTEX struct
  218. CUSTOMVERTEX vertices[] =
  219. {
  220. /* { -30.0f, 30.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
  221. { 30.0f, 30.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
  222. { -30.0f, -30.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
  223. { 30.0f, -30.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
  224.  
  225. { 40.0f, 60.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
  226. */
  227. /*{ -30.0f, -30.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
  228. { 30.0f, -30.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
  229. { 30.0f, 30.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
  230. { 30.0f, -30.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
  231.  
  232. { -40.0f, 40.0f,40.0f, D3DCOLOR_XRGB(0, 255, 0), },*/
  233.  
  234. // base
  235. { -30.0f, 0.0f, 30.0f, D3DCOLOR_XRGB(0, 255, 0), },
  236. { 30.0f, 0.0f, 30.0f, D3DCOLOR_XRGB(0, 0, 255), },
  237. { -30.0f, 0.0f, -30.0f, D3DCOLOR_XRGB(255, 0, 0), },
  238. { 30.0f, 0.0f, -30.0f, D3DCOLOR_XRGB(0, 255, 255), },
  239.  
  240. // top
  241. { 0.0f, 70.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
  242.  
  243.  
  244. };
  245. short indices[] =
  246. {
  247. 0,1,4,
  248. 1,2,4,
  249. 2,3,4,
  250. 3,0,4,
  251. //square
  252. 0,1,2,
  253. 0,2,3,
  254.  
  255.  
  256. };
  257. d3ddev->CreateIndexBuffer(18*sizeof(short),
  258. 0,
  259. D3DFMT_INDEX16,
  260. D3DPOOL_MANAGED,
  261. &i_buffer,
  262. NULL);
  263.  
  264. // create a vertex buffer i8nterface called v_buffer
  265. d3ddev->CreateVertexBuffer(5*sizeof(CUSTOMVERTEX),
  266. 0,
  267. CUSTOMFVF,
  268. D3DPOOL_MANAGED,
  269. &v_buffer,
  270. NULL);
  271.  
  272. VOID* pVoid; // a void pointer
  273.  
  274. // lock v_buffer and load the vertices into it
  275. v_buffer->Lock(0, 0, (void**)&pVoid, 0);
  276. memcpy(pVoid, vertices, sizeof(vertices));
  277. v_buffer->Unlock();
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement