Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.03 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <d3d11.h>
  3. #include <d3dcompiler.h>
  4. #include <DirectXMath.h>
  5. #include <stdio.h>
  6. #include <WICTextureLoader.h>
  7.  
  8. //[1] Add the assimp libraries
  9. #include <assimp/Importer.hpp>
  10. #include <assimp/postprocess.h>
  11. #include <assimp/scene.h>
  12.  
  13. using namespace DirectX;
  14.  
  15. #pragma comment(lib, "d3d11.lib")
  16. #pragma comment(lib, "d3dcompiler.lib")
  17. #pragma comment(lib, "DirectXTK.lib")
  18.  
  19. //[2] Add the assimp library
  20. #pragma comment(lib, "assimp-vc140-mt.lib")
  21.  
  22. // Window class name
  23. static wchar_t szAppClassName[] = L"AssImpEx";
  24.  
  25. // Forward definition of Windows procedure, necessary as Windows Procedure is defined last and
  26. // C++ requires identifiers are declared before use
  27. LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  28.  
  29. // DirectX objects
  30. ID3D11Device *d3dDevice;
  31. ID3D11DeviceContext *d3dContext;
  32. IDXGISwapChain *swapChain;
  33. ID3D11RenderTargetView *backBuffer;
  34.  
  35. // Depth buffer texture and object
  36. ID3D11Texture2D* depthStencilBuffer;
  37. ID3D11DepthStencilView* depthStencilView;
  38.  
  39.  
  40. //Model Vertex Template
  41. struct Vertex {
  42. XMFLOAT3 pos;
  43. XMFLOAT2 texCoord;
  44.  
  45. Vertex() {
  46.  
  47. }
  48.  
  49. Vertex(float x, float y, float z, float u, float v) {
  50. pos.x = x;
  51. pos.y = y;
  52. pos.z = z;
  53.  
  54. texCoord.x = u;
  55. texCoord.y = v;
  56. }
  57. };
  58.  
  59. struct ConstantBuffer
  60. {
  61. XMMATRIX mWorld;
  62. XMMATRIX mView;
  63. XMMATRIX mProjection;
  64. };
  65.  
  66. //[3] Dwarf vertex buffer
  67. ID3D11Buffer *dwarfVertexBuffer = NULL;
  68.  
  69.  
  70. //[3c] Car vertex buffer
  71.  
  72.  
  73. //Triangle Direct X data/objects
  74. ID3D11Buffer *constantBuffer = NULL;
  75. ID3D11VertexShader *vertexShader = NULL;
  76. ID3D11PixelShader *pixelShader = NULL;
  77. ID3D11InputLayout *vertexLayout = NULL;
  78.  
  79.  
  80. //[4] Define the dwarf texture object variables
  81. ID3D11ShaderResourceView *dwarfTex;
  82. ID3D11ShaderResourceView *axeTex;
  83.  
  84. //[4c] Define the car texture object variable
  85.  
  86.  
  87.  
  88. // This DirectX object stores the data from the background texture file
  89. // This DirectX object is used to map texture data onto triangle
  90. ID3D11SamplerState *sampler;
  91.  
  92. XMMATRIX matWorld;
  93. XMMATRIX matView;
  94. XMMATRIX matProjection;
  95.  
  96. int mouseXPos;
  97. int mouseYPos;
  98.  
  99. float RotationY = 0.0f;
  100. float RotationX = 0.0f;
  101. float RotFactorX = 0.01f;
  102. float RotFactorY = 0.01f;
  103.  
  104. float scale = 1.0f;
  105.  
  106. bool Rotating = false;
  107.  
  108. //[5] Define a variable to store the number of vertices in each dwarf mesh
  109. int *MeshVertices;
  110.  
  111. //[5c] Define a variable to store the number of vertices in the car mesh
  112.  
  113.  
  114. // Initialise DirectX in the application
  115. // Involves finding a driver suitable for the
  116. // features required by the game
  117. // If there is one, then it is associated
  118. // with the application by use of a back buffer (one or more)
  119.  
  120. BOOL InitialiseDirectX(HWND hMainWnd, HINSTANCE hCurInstance) {
  121. RECT rectDimensions;
  122. GetClientRect(hMainWnd, &rectDimensions);
  123.  
  124. LONG width = rectDimensions.right - rectDimensions.left;
  125. LONG height = rectDimensions.bottom - rectDimensions.top;
  126.  
  127. D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_1 };
  128.  
  129. int numFeatureLevels = sizeof(featureLevels) / sizeof(D3D_FEATURE_LEVEL);
  130.  
  131. DXGI_SWAP_CHAIN_DESC swapChainDesc;
  132. ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
  133. swapChainDesc.BufferCount = 1;
  134. swapChainDesc.BufferDesc.Width = width;
  135. swapChainDesc.BufferDesc.Height = height;
  136. swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  137. swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
  138. swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
  139. swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  140. swapChainDesc.OutputWindow = hMainWnd;
  141. swapChainDesc.Windowed = true;
  142. swapChainDesc.SampleDesc.Count = 1;
  143. swapChainDesc.SampleDesc.Quality = 0;
  144.  
  145. int creationFlags = 0;
  146.  
  147. HRESULT result;
  148.  
  149. // Create the device and swap chain for DirectX 11
  150. result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE,
  151. NULL, NULL, NULL, NULL, D3D11_SDK_VERSION,
  152. &swapChainDesc, &swapChain, &d3dDevice, NULL, &d3dContext);
  153.  
  154. // Check result
  155. if (result != S_OK) {
  156. MessageBox(hMainWnd, TEXT("Failed to initialise DX11!"), szAppClassName, NULL);
  157.  
  158. return false;
  159. }
  160.  
  161. ID3D11Texture2D *backBufferTexture;
  162.  
  163. result = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID *)&backBufferTexture);
  164.  
  165. if (result != S_OK) {
  166. MessageBox(hMainWnd, L"Failed to get back buffer!", szAppClassName, NULL);
  167.  
  168. return false;
  169. }
  170.  
  171. result = d3dDevice->CreateRenderTargetView(backBufferTexture, 0, &backBuffer);
  172.  
  173. if (backBufferTexture != NULL) {
  174. backBufferTexture->Release();
  175. }
  176.  
  177. if (result != S_OK) {
  178. MessageBox(hMainWnd, L"Failed to get render target!", szAppClassName, NULL);
  179.  
  180. return false;
  181. }
  182.  
  183. //Define Depth/Stencil Buffer
  184. D3D11_TEXTURE2D_DESC depthStencilDesc;
  185. ZeroMemory(&depthStencilDesc, sizeof(D3D11_TEXTURE2D_DESC));
  186.  
  187. // DepthStencil buffer has same dimensions as back buffer which is the same as the client window
  188. depthStencilDesc.Width = width;
  189. depthStencilDesc.Height = height;
  190. depthStencilDesc.MipLevels = 1;
  191. depthStencilDesc.ArraySize = 1;
  192.  
  193. depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  194. depthStencilDesc.SampleDesc.Count = 1;
  195. depthStencilDesc.SampleDesc.Quality = 0;
  196.  
  197. depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
  198. depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  199. depthStencilDesc.CPUAccessFlags = 0;
  200. depthStencilDesc.MiscFlags = 0;
  201.  
  202. // Create the texture for the depthstencil buffer
  203. d3dDevice->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
  204.  
  205. d3dDevice->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
  206.  
  207. d3dContext->OMSetRenderTargets(1, &backBuffer, depthStencilView);
  208.  
  209.  
  210. D3D11_VIEWPORT viewport;
  211. viewport.Width = static_cast<float>(width);
  212. viewport.Height = static_cast<float>(height);
  213. viewport.MinDepth = 0.0f;
  214. viewport.MaxDepth = 1.0f;
  215. viewport.TopLeftX = 0.0f;
  216. viewport.TopLeftY = 0.0f;
  217.  
  218. d3dContext->RSSetViewports(1, &viewport);
  219.  
  220. XMVECTOR Eye = XMVectorSet(0.0f, 3.0f, -10.0f, 0.0f);
  221. XMVECTOR At = XMVectorSet(0.0f, 3.0f, 0.0f, 0.0f);
  222. XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  223. matView = XMMatrixLookAtLH(Eye, At, Up);
  224.  
  225. matProjection = XMMatrixPerspectiveFovLH(XM_PIDIV4, width / (FLOAT)height, 0.01f, 100.0f);
  226.  
  227. return true;
  228.  
  229. }
  230.  
  231. bool CreateShaders(HWND hMainWnd) {
  232.  
  233. ID3DBlob *pVSBlob = NULL;
  234. HRESULT hr = D3DReadFileToBlob(L".\\VertexShader.cso", &pVSBlob);
  235. if (hr != S_OK)
  236. {
  237. MessageBox(hMainWnd, L"Problem loading vertex shader. Check shader file (VertexShader.cso) is in the project directory (sub folder of main solution directory) and shader is valid", szAppClassName, MB_OK);
  238.  
  239. return false;
  240. }
  241.  
  242. hr = d3dDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &vertexShader);
  243.  
  244. if (hr != S_OK)
  245. {
  246. pVSBlob->Release();
  247.  
  248. MessageBox(hMainWnd, L"The vertex shader object cannot be created", szAppClassName, MB_OK);
  249.  
  250. return false;
  251. }
  252.  
  253. D3D11_INPUT_ELEMENT_DESC layout[] =
  254. {
  255. {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0} ,
  256. {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
  257. };
  258.  
  259. UINT numElements = 2;
  260.  
  261. hr = d3dDevice->CreateInputLayout(layout, numElements, pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &vertexLayout);
  262.  
  263. pVSBlob->Release();
  264. if (hr != S_OK) {
  265. MessageBox(hMainWnd, L"Problems creating input layout", szAppClassName, MB_OK);
  266. return false;
  267. }
  268.  
  269. d3dContext->IASetInputLayout(vertexLayout);
  270.  
  271. ID3DBlob* pPSBlob = NULL;
  272. hr = D3DReadFileToBlob(L".\\PixelShader.cso", &pPSBlob);
  273.  
  274. if (hr != S_OK)
  275. {
  276. MessageBox(hMainWnd, L"The pixel shader object cannot be created", szAppClassName, MB_OK);
  277.  
  278. return false;
  279. }
  280.  
  281. //[6] Load dwarf texture
  282. hr = CreateWICTextureFromFile(d3dDevice, d3dContext, L".\\dwarf.jpg", NULL, &dwarfTex);
  283. if (hr != S_OK) {
  284. MessageBox(hMainWnd, TEXT("Unable to load dwarf texture"), szAppClassName, MB_OK);
  285.  
  286. return false;
  287. }
  288.  
  289. //[7] Load axe texture
  290. hr = CreateWICTextureFromFile(d3dDevice, d3dContext, L".\\axe.jpg", NULL, &axeTex);
  291. if (hr != S_OK) {
  292. MessageBox(hMainWnd, TEXT("Unable to load axe texture"), szAppClassName, MB_OK);
  293.  
  294. return false;
  295. }
  296.  
  297. //[6c] Load car texture
  298.  
  299. D3D11_SAMPLER_DESC sampDesc;
  300. ZeroMemory(&sampDesc, sizeof(sampDesc));
  301. sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  302. sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  303. sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  304. sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
  305. sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  306. sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
  307.  
  308. hr = d3dDevice->CreateSamplerState(&sampDesc, &sampler);
  309. if (hr != S_OK) {
  310. MessageBox(hMainWnd, TEXT("Unable to create sampler"), szAppClassName, MB_OK);
  311.  
  312. return false;
  313. }
  314.  
  315.  
  316. return true;
  317. }
  318.  
  319. bool LoadDwarfModel(){
  320.  
  321. //[8] Create importer object
  322. Assimp::Importer imp;
  323.  
  324.  
  325. //[9] Load model into scene object
  326. const aiScene *pScene = imp.ReadFile(".\\dwarf.x",
  327. aiProcessPreset_TargetRealtime_Fast | aiProcess_ConvertToLeftHanded);
  328.  
  329. if (!pScene) {
  330. return false;
  331. }
  332.  
  333. //[10] Create an array to store the individual number of vertices in each mesh
  334. MeshVertices = new int[pScene->mNumMeshes];
  335.  
  336.  
  337. //[11] Calculate the total number of vertices from all meshes
  338. int TotalNumVertices = 0;
  339. for (int MeshIdx = 0; MeshIdx < pScene->mNumMeshes; MeshIdx++) {
  340. const aiMesh *mesh = pScene->mMeshes[MeshIdx];
  341.  
  342. MeshVertices[MeshIdx] = mesh->mNumFaces * 3;
  343.  
  344. TotalNumVertices += mesh->mNumFaces * 3;
  345. }
  346.  
  347.  
  348. //[12] Create an array to store all vertices
  349. Vertex *shape = new Vertex[TotalNumVertices];
  350.  
  351.  
  352. //[13] Convert the assimp vertices to ones for DirectX
  353. int vertCount = 0;
  354. for (int MeshIdx = 0; MeshIdx < pScene->mNumMeshes; MeshIdx++) {
  355. const aiMesh *mesh = pScene->mMeshes[MeshIdx];
  356.  
  357. for (int faceIdx = 0; faceIdx < mesh->mNumFaces; faceIdx++) {
  358. const aiFace& face = mesh->mFaces[faceIdx];
  359.  
  360.  
  361. for (int vertIdx = 0; vertIdx < 3; vertIdx++) {
  362. const aiVector3D *pos = &mesh->mVertices[face.mIndices[vertIdx]];
  363. const aiVector3D *tex = &mesh->mTextureCoords[0][face.mIndices[vertIdx]];
  364.  
  365. shape[vertCount] = Vertex(pos->x, pos->y, pos->z, tex->x, tex->y);
  366. vertCount++;
  367. }
  368. }
  369. }
  370.  
  371.  
  372.  
  373. //[14] Define the vertex buffer
  374. D3D11_BUFFER_DESC bd;
  375. ZeroMemory(&bd, sizeof(bd));
  376. bd.Usage = D3D11_USAGE_DEFAULT;
  377. bd.ByteWidth = sizeof(Vertex) * TotalNumVertices;
  378. bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  379. bd.CPUAccessFlags = 0;
  380.  
  381.  
  382. // Define the source of vertex data in RAM
  383. D3D11_SUBRESOURCE_DATA InitData;
  384. ZeroMemory(&InitData, sizeof(InitData));
  385. InitData.pSysMem = shape;
  386. HRESULT hr = d3dDevice->CreateBuffer(&bd, &InitData, &dwarfVertexBuffer);
  387.  
  388. if (hr != S_OK) {
  389. return false;
  390. }
  391.  
  392.  
  393. //[15] Release memory allocated to shape
  394. delete[] shape;
  395.  
  396.  
  397. // Got this far so model loaded okay
  398. return true;
  399. }
  400.  
  401. bool LoadCarModel() {
  402. //[7c] Create importer object
  403.  
  404.  
  405.  
  406. //[8c] Load model into scene object
  407.  
  408.  
  409.  
  410. //[10c] Calculate the total number of vertices from all meshes
  411.  
  412.  
  413. // Store calculated number of vertices in a global variable for drawing later
  414.  
  415. //[11c] Create an array to accommodate all vertices
  416.  
  417.  
  418.  
  419. //[12c] Convert the assimp vertices to ones for DirectX
  420.  
  421.  
  422.  
  423. //[13c] Define the vertex buffer
  424.  
  425.  
  426.  
  427. // Define the source of vertex data in RAM
  428.  
  429.  
  430.  
  431. //[14c] Release memory allocated to shape
  432.  
  433.  
  434. // Got this far so model loaded okay
  435. return true;
  436. }
  437.  
  438. bool CreateMeshes(HWND hMainWnd) {
  439. if (!LoadDwarfModel()) {
  440. MessageBox(hMainWnd, L"Failed to load and create dwarf model", szAppClassName, NULL);
  441.  
  442. return false;
  443. }
  444.  
  445. D3D11_BUFFER_DESC bd;
  446. ZeroMemory(&bd, sizeof(D3D11_BUFFER_DESC));
  447. bd.Usage = D3D11_USAGE_DEFAULT;
  448. bd.ByteWidth = sizeof(ConstantBuffer);
  449. bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  450. bd.CPUAccessFlags = 0;
  451. HRESULT hr = d3dDevice->CreateBuffer(&bd, NULL, &constantBuffer);
  452. if( hr != S_OK){
  453. MessageBox(hMainWnd, L"Failed to craete constant buffer", szAppClassName, NULL);
  454.  
  455. return false;
  456. }
  457.  
  458. return true;
  459. }
  460.  
  461. void Update() {
  462.  
  463. }
  464.  
  465. void Draw() {
  466. if (d3dContext == NULL) {
  467. return;
  468. }
  469.  
  470. matWorld = XMMatrixScaling(0.1f, 0.1f, 0.1f) * XMMatrixRotationY(-RotationY) * XMMatrixTranslation(0.0f, 0.0f, 20.0f);
  471.  
  472. ConstantBuffer cb;
  473. cb.mWorld = XMMatrixTranspose( matWorld );
  474. cb.mView = XMMatrixTranspose( matView );
  475. cb.mProjection = XMMatrixTranspose( matProjection );
  476. d3dContext->UpdateSubresource(constantBuffer, 0, NULL, &cb, 0, 0);
  477.  
  478. float colour[] = { 0.392156f, 0.584313f, 0.929411f, 1.0f };
  479.  
  480. d3dContext->ClearRenderTargetView(backBuffer, colour);
  481.  
  482. d3dContext->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
  483.  
  484. d3dContext->VSSetShader(vertexShader, NULL, 0);
  485. d3dContext->VSSetConstantBuffers(0, 1, &constantBuffer);
  486. d3dContext->PSSetShader(pixelShader, NULL, 0);
  487.  
  488. //[16] Select vertex buffer with all of the mesh vertices
  489. UINT stride = sizeof(Vertex);
  490. UINT offset = 0;
  491. d3dContext->IASetVertexBuffers(0, 1, &dwarfVertexBuffer, &stride, &offset);
  492.  
  493. d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  494.  
  495. d3dContext->PSSetSamplers(0, 1, &sampler);
  496.  
  497.  
  498. ////[17] Select the axe texture first
  499. d3dContext->PSSetShaderResources(1, 1, &axeTex);
  500.  
  501. ////[18] Draw the axe mesh
  502. d3dContext->Draw(MeshVertices[0], 0);
  503.  
  504.  
  505. ////[19] Select the dwarf texture
  506. d3dContext->PSSetShaderResources(1, 1, &dwarfTex);
  507.  
  508.  
  509. ////[20] Draw the dwarf mesh
  510. d3dContext->Draw(MeshVertices[1], MeshVertices[0]);
  511.  
  512.  
  513. //[16c] Select car vertex buffer into pipeline
  514.  
  515.  
  516.  
  517. //[17c] Select car texture into pipeline
  518.  
  519.  
  520.  
  521. //[18c] Draw car
  522.  
  523. //Display frame immediately
  524. swapChain->Present(0, 0);
  525. }
  526.  
  527. void ShutdownDirectX() {
  528. if (dwarfTex) {
  529. dwarfTex->Release();
  530. }
  531.  
  532. if (axeTex) {
  533. axeTex->Release();
  534. }
  535.  
  536. if (dwarfVertexBuffer != NULL)
  537. dwarfVertexBuffer->Release();
  538.  
  539.  
  540. //Direct X Release
  541. if (sampler) {
  542. sampler->Release();
  543. }
  544.  
  545. if (vertexShader != NULL)
  546. vertexShader->Release();
  547.  
  548. if (vertexLayout != NULL)
  549. vertexLayout->Release();
  550.  
  551. if (backBuffer) {
  552. backBuffer->Release();
  553. }
  554.  
  555. if (depthStencilView) {
  556. depthStencilView->Release();
  557. }
  558.  
  559. if (depthStencilBuffer) {
  560. depthStencilBuffer->Release();
  561. }
  562.  
  563. if (swapChain) {
  564. swapChain->Release();
  565. }
  566.  
  567. if (d3dContext) {
  568. d3dContext->Release();
  569. }
  570.  
  571. if (d3dDevice) {
  572. d3dDevice->Release();
  573.  
  574. backBuffer = 0;
  575. swapChain = 0;
  576. d3dContext = 0;
  577. d3dDevice = 0;
  578. }
  579. }
  580.  
  581. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLineArgs, int nInitialWinShowState) {
  582. HWND hMainWnd;
  583. MSG msg = { 0 };
  584.  
  585. WNDCLASS wndclass;
  586. wchar_t fps[64];
  587. ZeroMemory(fps, 64);
  588.  
  589. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  590. wndclass.lpfnWndProc = WinProc;
  591. wndclass.cbClsExtra = 0;
  592. wndclass.cbWndExtra = 0;
  593. wndclass.hInstance = hInstance;
  594. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  595. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  596. wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  597. wndclass.lpszMenuName = NULL;
  598. wndclass.lpszClassName = szAppClassName;
  599.  
  600. if (!RegisterClass(&wndclass)) {
  601. MessageBox(NULL, L"Unable to register class for application", szAppClassName, 0);
  602.  
  603. return 0;
  604. }
  605.  
  606. hMainWnd = CreateWindow(szAppClassName,
  607. L"DirectX Application",
  608. WS_OVERLAPPEDWINDOW,
  609. CW_USEDEFAULT,
  610. CW_USEDEFAULT,
  611. CW_USEDEFAULT,
  612. CW_USEDEFAULT,
  613. NULL,
  614. NULL,
  615. hInstance,
  616. NULL);
  617.  
  618. if (!hMainWnd) {
  619. MessageBox(NULL, L"Unable to create the application's main window", szAppClassName, 0);
  620.  
  621. return 0;
  622. }
  623.  
  624. ShowWindow(hMainWnd, nInitialWinShowState);
  625.  
  626. if (!InitialiseDirectX(hMainWnd, hInstance)) {
  627. MessageBox(NULL, L"Failed to initialise DirectX", szAppClassName, 0);
  628.  
  629. return 0;
  630. }
  631.  
  632. if (!CreateShaders(hMainWnd)) {
  633. return 0;
  634. }
  635.  
  636. if (!CreateMeshes(hMainWnd)) {
  637. return 0;
  638. }
  639.  
  640. DWORD current = GetTickCount();
  641. int count = 0;
  642.  
  643. while (msg.message != WM_QUIT) {
  644. if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  645. TranslateMessage(&msg);
  646. DispatchMessage(&msg);
  647. }
  648. else {
  649. Update();
  650.  
  651. Draw();
  652.  
  653. count++;
  654.  
  655. DWORD now = GetTickCount();
  656. if (now - current > 1000) {
  657. wsprintf(fps, L"FPS = %d", count);
  658.  
  659. SetWindowText(hMainWnd, fps);
  660.  
  661. count = 0;
  662.  
  663. current = now;
  664. }
  665.  
  666. }
  667.  
  668. }
  669.  
  670. ShutdownDirectX();
  671.  
  672. return 0;
  673. }
  674.  
  675. LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  676. int curMouseXPos, curMouseYPos;
  677. int zDelta;
  678.  
  679. curMouseXPos = (short)LOWORD(lParam);
  680. curMouseYPos = (short)HIWORD(lParam);
  681.  
  682. switch (uMsg) {
  683.  
  684. case WM_MOUSEMOVE:
  685. if (Rotating) {
  686. RotationY += -(curMouseXPos - mouseXPos) * RotFactorY;
  687.  
  688. if (RotationY < 0) {
  689. RotationY = 2 * XM_PI - RotationY;
  690. }
  691.  
  692. RotationX += -(curMouseYPos - mouseYPos) * RotFactorX;
  693.  
  694. if (RotationX < 0) {
  695. RotationX = 2 * XM_PI - RotationX;
  696. }
  697. }
  698.  
  699. mouseXPos = curMouseXPos;
  700. mouseYPos = curMouseYPos;
  701. break;
  702.  
  703. case WM_LBUTTONDOWN:
  704. SetCapture(hWnd);
  705. mouseXPos = curMouseXPos;
  706. mouseYPos = curMouseYPos;
  707.  
  708. Rotating = true;
  709. break;
  710.  
  711. case WM_LBUTTONUP:
  712. ReleaseCapture();
  713. Rotating = false;
  714. break;
  715.  
  716. case WM_CLOSE:
  717. PostQuitMessage(0);
  718. return 0;
  719. }
  720.  
  721. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  722. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement