Advertisement
Guest User

main.cpp

a guest
May 12th, 2011
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.13 KB | None | 0 0
  1. #include "vars.h"
  2.  
  3. // the WindowProc function prototype
  4. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  5.  
  6.  
  7. // the entry point for any Windows program
  8. int WINAPI WinMain(HINSTANCE hInstance,
  9. HINSTANCE hPrevInstance,
  10. LPSTR lpCmdLine,
  11. int nCmdShow)
  12. {
  13. HWND hWnd;
  14. WNDCLASSEX wc;
  15.  
  16. ZeroMemory(&wc, sizeof(WNDCLASSEX));
  17.  
  18. wc.cbSize = sizeof(WNDCLASSEX);
  19. wc.style = CS_HREDRAW | CS_VREDRAW;
  20. wc.lpfnWndProc = WindowProc;
  21. wc.hInstance = hInstance;
  22. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  23. wc.lpszClassName = L"WindowClass";
  24.  
  25. RegisterClassEx(&wc);
  26.  
  27. RECT wr = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
  28. AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
  29.  
  30. hWnd = CreateWindowEx(NULL,
  31. L"WindowClass",
  32. L"Our First Direct3D Program",
  33. WS_OVERLAPPEDWINDOW,
  34. 300,
  35. 300,
  36. wr.right - wr.left,
  37. wr.bottom - wr.top,
  38. NULL,
  39. NULL,
  40. hInstance,
  41. NULL);
  42.  
  43. ShowWindow(hWnd, nCmdShow);
  44.  
  45. // set up and initialize Direct3D
  46. InitD3D(hWnd);
  47.  
  48. // enter the main loop:
  49.  
  50. MSG msg;
  51.  
  52. while(TRUE)
  53. {
  54. if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  55. {
  56. TranslateMessage(&msg);
  57. DispatchMessage(&msg);
  58.  
  59. if(msg.message == WM_QUIT)
  60. break;
  61. }
  62.  
  63. RenderFrame();
  64. }
  65.  
  66. // clean up DirectX and COM
  67. CleanD3D();
  68.  
  69. return msg.wParam;
  70. }
  71.  
  72.  
  73. // this is the main message handler for the program
  74. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  75. {
  76. switch(message)
  77. {
  78. case WM_DESTROY:
  79. {
  80. PostQuitMessage(0);
  81. return 0;
  82. } break;
  83. }
  84.  
  85. return DefWindowProc (hWnd, message, wParam, lParam);
  86. }
  87.  
  88.  
  89. // this function initializes and prepares Direct3D for use
  90. void InitD3D(HWND hWnd)
  91. {
  92. // create a struct to hold information about the swap chain
  93. DXGI_SWAP_CHAIN_DESC scd;
  94.  
  95. // clear out the struct for use
  96. ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
  97.  
  98. // fill the swap chain description struct
  99. scd.BufferCount = 1; // one back buffer
  100. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
  101. scd.BufferDesc.Width = SCREEN_WIDTH; // set the back buffer width
  102. scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back buffer height
  103. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
  104. scd.OutputWindow = hWnd; // the window to be used
  105. scd.SampleDesc.Count = 4; // how many multisamples
  106. scd.Windowed = TRUE; // windowed/full-screen mode
  107. scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow full-screen switching
  108.  
  109. // create a device, device context and swap chain using the information in the scd struct
  110. D3D11CreateDeviceAndSwapChain(NULL,
  111. D3D_DRIVER_TYPE_HARDWARE,
  112. NULL,
  113. NULL,
  114. NULL,
  115. NULL,
  116. D3D11_SDK_VERSION,
  117. &scd,
  118. &swapchain,
  119. &dev,
  120. NULL,
  121. &devcon);
  122.  
  123.  
  124. // create the depth buffer texture
  125. D3D11_TEXTURE2D_DESC texd;
  126. ZeroMemory(&texd, sizeof(texd));
  127.  
  128. texd.Width = SCREEN_WIDTH;
  129. texd.Height = SCREEN_HEIGHT;
  130. texd.ArraySize = 1;
  131. texd.MipLevels = 1;
  132. texd.SampleDesc.Count = 4;
  133. texd.Format = DXGI_FORMAT_D32_FLOAT;
  134. texd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  135.  
  136. ID3D11Texture2D *pDepthBuffer;
  137. dev->CreateTexture2D(&texd, NULL, &pDepthBuffer);
  138.  
  139. // create the depth buffer
  140. D3D11_DEPTH_STENCIL_VIEW_DESC dsvd;
  141. ZeroMemory(&dsvd, sizeof(dsvd));
  142.  
  143. dsvd.Format = DXGI_FORMAT_D32_FLOAT;
  144. dsvd.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
  145.  
  146. dev->CreateDepthStencilView(pDepthBuffer, &dsvd, &zbuffer);
  147. pDepthBuffer->Release();
  148.  
  149. // get the address of the back buffer
  150. ID3D11Texture2D *pBackBuffer;
  151. swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
  152.  
  153. // use the back buffer address to create the render target
  154. dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
  155. pBackBuffer->Release();
  156.  
  157. // set the render target as the back buffer
  158. devcon->OMSetRenderTargets(1, &backbuffer, zbuffer);
  159.  
  160.  
  161. // Set the viewport
  162. D3D11_VIEWPORT viewport;
  163. ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  164.  
  165. viewport.TopLeftX = 0; // set the left to 0
  166. viewport.TopLeftY = 0; // set the top to 0
  167. viewport.Width = SCREEN_WIDTH; // set the width to the window's width
  168. viewport.Height = SCREEN_HEIGHT; // set the height to the window's height
  169. viewport.MinDepth = 0; // the closest an object can be on the depth buffer is 0.0
  170. viewport.MaxDepth = 1; // the farthest an object can be on the depth buffer is 1.0
  171.  
  172. devcon->RSSetViewports(1, &viewport);
  173.  
  174. InitPipeline();
  175. InitGraphics();
  176. }
  177.  
  178.  
  179. // this is the function used to render a single frame
  180. void RenderFrame(void)
  181. {
  182.  
  183.  
  184. D3DXMATRIX matRotate, matView, matProjection;
  185. D3DXMATRIX matFinal;
  186.  
  187. static float Time = 0.0f; Time += 0.0003f;
  188.  
  189. // create a world matrices
  190. D3DXMatrixRotationY(&matRotate, Time);
  191.  
  192. // create a view matrix
  193. D3DXMatrixLookAtLH(&matView,
  194. &D3DXVECTOR3(0.0f, 7.0f, 5.0f), // the camera position
  195. &D3DXVECTOR3(0.0f, 0.0f, 0.0f), // the look-at position
  196. &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); // the up direction
  197.  
  198. // create a projection matrix
  199. D3DXMatrixPerspectiveFovLH(&matProjection,
  200. (FLOAT)D3DXToRadian(45), // field of view
  201. (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
  202. 1.0f, // near view-plane
  203. 1000.0f); // far view-plane
  204.  
  205. // load the matrices into the constant buffer
  206. matFinal = matRotate * matView * matProjection;
  207.  
  208.  
  209. // clear the back buffer to a deep blue
  210. devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f));
  211.  
  212. // clear the depth buffer
  213. devcon->ClearDepthStencilView(zbuffer, D3D11_CLEAR_DEPTH, 1.0f, 0);
  214.  
  215. // select which vertex buffer to display
  216. UINT stride = sizeof(VertexType);
  217. UINT offset = 0;
  218. devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
  219. devcon->IASetIndexBuffer(pIBuffer, DXGI_FORMAT_R32_UINT, 0);
  220.  
  221. // select which primtive type we are using
  222. devcon->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINELIST);
  223.  
  224. //Draw
  225. devcon->DrawIndexed(m_indexCount, 0, 0);
  226.  
  227. // switch the back buffer and the front buffer
  228. swapchain->Present(0, 0);
  229. }
  230.  
  231.  
  232. // this is the function that cleans up Direct3D and COM
  233. void CleanD3D(void)
  234. {
  235. swapchain->SetFullscreenState(FALSE, NULL); // switch to windowed mode
  236.  
  237. // close and release all existing COM objects
  238. zbuffer->Release();
  239. pLayout->Release();
  240. pVS->Release();
  241. pPS->Release();
  242. pVBuffer->Release();
  243. pIBuffer->Release();
  244. swapchain->Release();
  245. backbuffer->Release();
  246. dev->Release();
  247. devcon->Release();
  248. }
  249.  
  250.  
  251. // this is the function that creates the shape to render
  252. void InitGraphics()
  253. {
  254. VertexType* vertices;
  255. unsigned long* indices;
  256. int i, j, index;
  257. float positionX, positionZ;
  258.  
  259. // Calculate the number of vertices in the terrain mesh.
  260. m_vertexCount = (m_terrainWidth - 1) * (m_terrainHeight - 1) * 8;
  261.  
  262. // Set the index count to the same as the vertex count.
  263. m_indexCount = m_vertexCount;
  264.  
  265. vertices = new VertexType[m_vertexCount];
  266. indices = new unsigned long[m_indexCount];
  267.  
  268. // Initialize the index to the vertex buffer.
  269. index = 0;
  270.  
  271. // Load the vertex and index array with the terrain data.
  272. for(j=0; j<(m_terrainHeight-1); j++)
  273. {
  274. for(i=0; i<(m_terrainWidth-1); i++)
  275. {
  276. // LINE 1
  277. // Upper left.
  278. positionX = (float)i;
  279. positionZ = (float)(j+1);
  280.  
  281. vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ);
  282. vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
  283. indices[index] = index;
  284. index++;
  285.  
  286. // Upper right.
  287. positionX = (float)(i+1);
  288. positionZ = (float)(j+1);
  289.  
  290. vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ);
  291. vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
  292. indices[index] = index;
  293. index++;
  294.  
  295. // LINE 2
  296. // Upper right.
  297. positionX = (float)(i+1);
  298. positionZ = (float)(j+1);
  299.  
  300. vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ);
  301. vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
  302. indices[index] = index;
  303. index++;
  304.  
  305. // Bottom right.
  306. positionX = (float)(i+1);
  307. positionZ = (float)j;
  308.  
  309. vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ);
  310. vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
  311. indices[index] = index;
  312. index++;
  313.  
  314. // LINE 3
  315. // Bottom right.
  316. positionX = (float)(i+1);
  317. positionZ = (float)j;
  318.  
  319. vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ);
  320. vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
  321. indices[index] = index;
  322. index++;
  323.  
  324. // Bottom left.
  325. positionX = (float)i;
  326. positionZ = (float)j;
  327.  
  328. vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ);
  329. vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
  330. indices[index] = index;
  331. index++;
  332.  
  333. // LINE 4
  334. // Bottom left.
  335. positionX = (float)i;
  336. positionZ = (float)j;
  337.  
  338. vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ);
  339. vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
  340. indices[index] = index;
  341. index++;
  342.  
  343. // Upper left.
  344. positionX = (float)i;
  345. positionZ = (float)(j+1);
  346.  
  347. vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ);
  348. vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);
  349. indices[index] = index;
  350. index++;
  351. }
  352. }
  353.  
  354. // create the vertex buffer
  355. D3D11_BUFFER_DESC bd;
  356. ZeroMemory(&bd, sizeof(bd));
  357.  
  358. bd.Usage = D3D11_USAGE_DYNAMIC;
  359. bd.ByteWidth = sizeof(VertexType) * m_vertexCount;
  360. bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  361. bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  362.  
  363. dev->CreateBuffer(&bd, NULL, &pVBuffer);
  364.  
  365. // copy the vertices into the buffer
  366. D3D11_MAPPED_SUBRESOURCE ms;
  367. devcon->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms); // map the buffer
  368. memcpy(ms.pData, vertices, sizeof(vertices)); // copy the data
  369. devcon->Unmap(pVBuffer, NULL);
  370.  
  371. // create the index buffer
  372. bd.Usage = D3D11_USAGE_DYNAMIC;
  373. bd.ByteWidth = sizeof(unsigned long) * m_indexCount;
  374. bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
  375. bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  376. bd.MiscFlags = 0;
  377.  
  378. dev->CreateBuffer(&bd, NULL, &pIBuffer);
  379.  
  380. devcon->Map(pIBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms); // map the buffer
  381. memcpy(ms.pData, indices, sizeof(indices)); // copy the data
  382. devcon->Unmap(pIBuffer, NULL);
  383. /*
  384. D3DX11CreateShaderResourceViewFromFile(dev, // the Direct3D device
  385. L"Wood.png", // load Wood.png in the local folder
  386. NULL, // no additional information
  387. NULL, // no multithreading
  388. &pTexture, // address of the shader-resource-view
  389. NULL); // no multithreading
  390. */
  391.  
  392.  
  393. delete [] vertices;
  394. vertices = 0;
  395.  
  396. delete [] indices;
  397. indices = 0;
  398. }
  399.  
  400.  
  401.  
  402.  
  403. // this function loads and prepares the shaders
  404. void InitPipeline()
  405. {
  406. // compile the shaders
  407. ID3D10Blob *VS, *PS;
  408. D3DX11CompileFromFile(L"shaders.hlsl", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
  409. D3DX11CompileFromFile(L"shaders.hlsl", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0);
  410.  
  411. // create the shader objects
  412. dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
  413. dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);
  414.  
  415. // set the shader objects
  416. devcon->VSSetShader(pVS, 0, 0);
  417. devcon->PSSetShader(pPS, 0, 0);
  418.  
  419. // create the input element object
  420. D3D11_INPUT_ELEMENT_DESC ied[] =
  421. {
  422. {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
  423. {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
  424. };
  425.  
  426. // use the input element descriptions to create the input layout
  427. dev->CreateInputLayout(ied, 3, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
  428. devcon->IASetInputLayout(pLayout);
  429.  
  430.  
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement