Guest User

Untitled

a guest
Oct 24th, 2020
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.03 KB | None | 0 0
  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <d3d11.h>
  4. #include <D3DCSX.h>
  5. #include <DirectXMath.h>
  6. #include <d3dcompiler.h>
  7. #include <cmath>
  8. #include <stdio.h>
  9. #include <winrt/base.h>
  10. #include <sstream>
  11. #include <iostream>
  12. #include <string>
  13. #include <vector>
  14. #include <fstream>
  15. #include <math.h>
  16.  
  17. #pragma comment (lib, "d3d11.lib")
  18. #pragma comment (lib, "kernel32.lib")
  19. #pragma comment (lib, "d3dcsx.lib")
  20. #pragma comment (lib, "user32.lib")
  21. #pragma comment (lib, "D3DCompiler.lib")
  22. #pragma comment (lib, "Dxgi.lib")
  23. #pragma comment (lib, "dxguid.lib")
  24.  
  25. #define SCREEN_WIDTH 1000
  26. #define SCREEN_HEIGHT 750
  27.  
  28. IDXGISwapChain *swapchain;
  29. ID3D11Device *dev;
  30. ID3D11DeviceContext *devcon;
  31. ID3D11RenderTargetView *backbuffer;
  32. ID3D11InputLayout *pLayout;
  33. ID3D11VertexShader *pVS;
  34. ID3D11PixelShader *pPS;
  35. ID3D11Buffer *pConstantBuffer;
  36. D3D11_BUFFER_DESC cbd;
  37. D3D11_SUBRESOURCE_DATA csd = {};
  38.  
  39. using namespace std;
  40. namespace dx = DirectX;
  41.  
  42. struct Vertex{
  43. float x;
  44. float y;
  45. float z;
  46. unsigned char r;
  47. unsigned char g;
  48. unsigned char b;
  49. unsigned char a;
  50. };
  51.  
  52. struct Index{
  53. unsigned int a, b, c;
  54. };
  55.  
  56. vector<Index> indices;
  57. vector<Vertex> vertices;
  58. //Index indices[4309];
  59. //Vertex vertices[4727];
  60. unsigned int check = 0;
  61. string curline;
  62.  
  63. Vertex getFloats(string in)
  64. {
  65. Vertex fOut;
  66. string strV[3];
  67. in.erase(0,2);
  68. int spc;
  69.  
  70.  
  71. spc = in.find(" ") + 1;
  72. strV[0] = in.substr(0, spc);
  73. in.erase(0, spc);
  74. cout << "strv0: " << strV[0] << endl;
  75.  
  76.  
  77. spc = in.find(" ") + 1;
  78. strV[1] = in.substr(0, spc);
  79. in.erase(0, spc);
  80. cout << "strv1: " << strV[1] << endl;
  81.  
  82. strV[2] = in;
  83. cout << "strv2:" << strV[2] << endl;
  84.  
  85. fOut.x = stof(strV[0]);
  86. fOut.y = stof(strV[1]);
  87. fOut.z = stof(strV[2]);
  88.  
  89. unsigned char r = 0;
  90. unsigned char g = 255;
  91. unsigned char b = 0;
  92. unsigned char a = 0;
  93.  
  94. fOut.r = r;
  95. fOut.g = g;
  96. fOut.b = b;
  97. fOut.a = a;
  98.  
  99. return fOut;
  100. }
  101.  
  102. Index getUint(string in)
  103. {
  104. Index out;
  105. unsigned int uint[3];
  106. int spc;
  107. int sls;
  108. string strV[3];
  109.  
  110. in.erase(0,2);
  111.  
  112. sls = in.find("/");
  113. spc = in.find(" ") + 1;
  114. strV[0] = in.substr(0,sls);
  115. in.erase(0,spc);
  116.  
  117.  
  118. sls = in.find("/");
  119. spc = in.find(" ") + 1;
  120. strV[1] = in.substr(0,sls);
  121. in.erase(0,spc);
  122.  
  123.  
  124. sls = in.find("/");
  125. spc = in.find(" ") + 1;
  126. strV[2] = in.substr(0,sls);
  127. in.erase(0,spc);
  128.  
  129. cout << "0: " << strV[0] << " 1: " << strV[1] << " 2: " << strV[2] << endl;
  130.  
  131. out.a = stoi(strV[0]);
  132. out.b = stoi(strV[1]);
  133. out.c = stoi(strV[2]);
  134. return out;
  135. }
  136.  
  137.  
  138.  
  139. int loadObj()
  140. {
  141. cout << "start:" << endl;
  142.  
  143. ifstream file("P-51 Mustang.obj");
  144.  
  145. if(!file.is_open())
  146. return false;
  147.  
  148. int vertex = 0;
  149. int indice = 0;
  150.  
  151.  
  152. curline.clear();
  153. while (getline(file, curline))
  154. {
  155. char first = curline[0];
  156. char second = curline[1];
  157. char vCheck = 'v';
  158. char fCheck = 'f';
  159. char space = ' ';
  160. char currnum[20];
  161. cout << first << endl;
  162.  
  163. if(first == vCheck && second == space){
  164. //vertices[vertex] = getFloats(curline);
  165. vertices.push_back(getFloats(curline));
  166. vertex += 1;
  167. }
  168.  
  169. if(first == fCheck && second == space){
  170. //indices[indice] = getUint(curline);
  171. indices.push_back(getUint(curline));
  172. indice += 1;
  173. }
  174.  
  175. curline.clear();
  176. }
  177. cout << "Vertex count : " << vertices.size() << "\n";
  178.  
  179. cout << "Indice count : " << indices.size() << "\n";
  180.  
  181. return 1;
  182. }
  183.  
  184.  
  185.  
  186.  
  187. float angle = 0.0f;
  188. float x = 0.0f;
  189. float y = 0.0f;
  190.  
  191. tagPOINT mouse;
  192.  
  193.  
  194. float s = 1.0f;
  195. float ns = s * -1;
  196.  
  197.  
  198. struct ConstantBuffer
  199. {
  200. dx::XMMATRIX transform;
  201. } cb;
  202.  
  203. void RenderFrame(HWND hWnd);
  204. void CleanD3D(void);
  205. void initD3d(HWND hWnd);
  206.  
  207.  
  208. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  209.  
  210. int WINAPI WinMain(HINSTANCE hInstance,
  211. HINSTANCE hPrevInstance,
  212. LPSTR lpCmdLine,
  213. int nCmdShow)
  214. {
  215.  
  216. std::cout << "start\n" ;
  217.  
  218. loadObj();
  219.  
  220. HWND hWnd;
  221. WNDCLASSEX wc;
  222.  
  223. ZeroMemory(&wc, sizeof(WNDCLASSEX));
  224.  
  225. wc.cbSize = sizeof(WNDCLASSEX);
  226. wc.style = CS_HREDRAW | CS_VREDRAW;
  227. wc.lpfnWndProc = WindowProc;
  228. wc.hInstance = hInstance;
  229. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  230. wc.lpszClassName = "WindowClass";
  231.  
  232. RegisterClassEx(&wc);
  233.  
  234. RECT wr = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
  235. AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
  236.  
  237. hWnd = CreateWindowEx(NULL,
  238. "WindowClass",
  239. "Typhoon Simulation",
  240. WS_OVERLAPPEDWINDOW,
  241. 300,
  242. 300,
  243. wr.right - wr.left,
  244. wr.bottom - wr.top,
  245. NULL,
  246. NULL,
  247. hInstance,
  248. NULL);
  249.  
  250. ShowWindow(hWnd, nCmdShow);
  251.  
  252. initD3d(hWnd);
  253.  
  254. MSG msg;
  255.  
  256. while(TRUE)
  257. {
  258. if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  259. {
  260. TranslateMessage(&msg);
  261. DispatchMessage(&msg);
  262.  
  263. if(msg.message == WM_QUIT){
  264. MessageBox(hWnd, "Quitting", "Quitting", 0);
  265. break;
  266. }
  267. }
  268. RenderFrame(hWnd);
  269. }
  270.  
  271. CleanD3D();
  272.  
  273. return msg.wParam;
  274. }
  275.  
  276.  
  277.  
  278. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  279. {
  280. switch(message)
  281. {
  282. case WM_DESTROY:
  283. {
  284. PostQuitMessage(0);
  285. return 0;
  286. } break;
  287. }
  288. return DefWindowProc (hWnd, message, wParam, lParam);
  289. }
  290.  
  291.  
  292.  
  293. void initD3d(HWND hWnd){
  294. DXGI_SWAP_CHAIN_DESC scd;
  295.  
  296. ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
  297.  
  298. scd.BufferCount = 1;
  299. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  300. scd.BufferDesc.Width = SCREEN_WIDTH;
  301. scd.BufferDesc.Height = SCREEN_HEIGHT;
  302. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  303. scd.OutputWindow = hWnd;
  304. scd.SampleDesc.Count = 4;
  305. scd.Windowed = TRUE;
  306. scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  307.  
  308.  
  309. D3D11CreateDeviceAndSwapChain(NULL,
  310. D3D_DRIVER_TYPE_HARDWARE,
  311. NULL,
  312. NULL,
  313. NULL,
  314. NULL,
  315. D3D11_SDK_VERSION,
  316. &scd,
  317. &swapchain,
  318. &dev,
  319. NULL,
  320. &devcon);
  321.  
  322. winrt::com_ptr<ID3D11Texture2D> pBackBuffer;
  323. swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
  324.  
  325. dev->CreateRenderTargetView(pBackBuffer.get(), NULL, &backbuffer);
  326. pBackBuffer->Release();
  327.  
  328. devcon->OMSetRenderTargets(1, &backbuffer, NULL);
  329.  
  330. D3D11_VIEWPORT viewport;
  331. ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  332.  
  333. viewport.TopLeftX = 0;
  334. viewport.TopLeftY = 0;
  335. viewport.MinDepth = 0.0f;
  336. viewport.MaxDepth = 1.0f;
  337. viewport.Width = SCREEN_WIDTH;
  338. viewport.Height = SCREEN_HEIGHT;
  339.  
  340. devcon->RSSetViewports(1, &viewport);
  341.  
  342. ID3DBlob *VS, *PS;
  343.  
  344. D3DCompileFromFile(L"vertexShader.hlsl", NULL, NULL, "main", "vs_4_0", 0, 0, &VS, &VS);
  345. D3DCompileFromFile(L"pixelShader.hlsl", NULL, NULL, "main", "ps_4_0", 0, 0, &PS, &PS);
  346.  
  347. dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
  348. dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);
  349.  
  350. devcon->VSSetShader(pVS, 0, 0);
  351. devcon->PSSetShader(pPS, 0, 0);
  352.  
  353. const D3D11_INPUT_ELEMENT_DESC ied[] =
  354. {
  355. {"Position", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
  356. {"Color", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
  357. };
  358.  
  359. dev->CreateInputLayout(
  360. ied, (UINT)std::size(ied),
  361. VS->GetBufferPointer(),
  362. VS->GetBufferSize(),
  363. &pLayout);
  364.  
  365. devcon->IASetInputLayout(pLayout);
  366.  
  367. ID3D11Buffer *pVertexBuffer;
  368. D3D11_BUFFER_DESC bd;
  369. bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  370. bd.ByteWidth = vertices.size() * sizeof(Vertex);
  371. bd.CPUAccessFlags = 0u;
  372. bd.MiscFlags = 0u;
  373. bd.Usage = D3D11_USAGE_DEFAULT;
  374. bd.StructureByteStride = sizeof(Vertex);
  375. D3D11_SUBRESOURCE_DATA sd = {};
  376. sd.pSysMem = vertices.data();
  377. dev->CreateBuffer(&bd, &sd, &pVertexBuffer);
  378. const UINT stride = sizeof(Vertex);
  379. const UINT offset = 0u;
  380. devcon->IASetVertexBuffers(0u, 1u, &pVertexBuffer, &stride, &offset);
  381.  
  382. ID3D11Buffer *pIndexBuffer;
  383. D3D11_BUFFER_DESC ibd;
  384. ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
  385. ibd.ByteWidth = indices.size() * sizeof(Index);
  386. ibd.CPUAccessFlags = 0u;
  387. ibd.MiscFlags = 0u;
  388. ibd.Usage = D3D11_USAGE_DEFAULT;
  389. ibd.StructureByteStride = sizeof(Index);
  390. D3D11_SUBRESOURCE_DATA isd = {};
  391. isd.pSysMem = indices.data();
  392. dev->CreateBuffer(&ibd, &isd, &pIndexBuffer);
  393. devcon->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0u);
  394.  
  395. cb = {
  396. {
  397. dx::XMMatrixTranspose(
  398. //dx::XMMatrixRotationX(angle) *
  399. dx::XMMatrixRotationY(angle) *
  400. //dx::XMMatrixRotationZ(angle) *
  401. dx::XMMatrixScaling(0.00005f, 0.00005f, 0.00005f) *
  402. dx::XMMatrixTranslation(mouse.x/400.0f - 1.0f, -mouse.y/300.0f + 1.0f, 0.5f)
  403. //dx::XMMatrixTranslation(0.0f, 0.5f, 0.1f)
  404. //dx::XMMatrixPerspectiveLH(1.0f, 3.0f/4.0f, 0.5f, 10.0f)
  405. )
  406. }
  407. };
  408.  
  409. ZeroMemory(&cbd, sizeof(cbd));
  410. cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  411. cbd.Usage = D3D11_USAGE_DYNAMIC;
  412. cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  413. cbd.MiscFlags = 0u;
  414. cbd.ByteWidth = sizeof(cb);
  415. cbd.StructureByteStride = 0u;
  416. csd.pSysMem = &cb;
  417. dev->CreateBuffer(&cbd, &csd, &pConstantBuffer);
  418. devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  419. }
  420.  
  421.  
  422.  
  423. void RenderFrame(HWND hWnd)
  424. {
  425. check += 1;
  426. FLOAT ColorRGBA[4] = {0.3f, 0.2f, 0.4f, 1.0f};
  427. devcon->ClearRenderTargetView(backbuffer, ColorRGBA);
  428.  
  429. GetCursorPos(&mouse);
  430. ScreenToClient(hWnd, &mouse);
  431.  
  432. angle += 0.001f;
  433. if (angle > 10.0f)
  434. angle = 0.0f;
  435.  
  436. cb = {
  437. {
  438. dx::XMMatrixTranspose(
  439. //dx::XMMatrixRotationX(angle) *
  440. dx::XMMatrixRotationY(angle) *
  441. //dx::XMMatrixRotationZ(angle) *
  442. dx::XMMatrixScaling(0.00005f, 0.00005f, 0.00005f) *
  443. //dx::XMMatrixTranslation(mouse.x/400.0f - 1.0f, -mouse.y/300.0f + 1.0f, 0.5f)
  444. dx::XMMatrixTranslation(0.0f, -0.5f, 0.1f)
  445. //dx::XMMatrixPerspectiveLH(1.0f, 3.0f/4.0f, 0.5f, 10.0f)
  446. )
  447. }
  448. };
  449. dev->CreateBuffer(&cbd, &csd, &pConstantBuffer);
  450. int res;
  451. devcon->VSSetConstantBuffers(0u, 1u, &pConstantBuffer);
  452. devcon->DrawIndexed((UINT)indices.size(), 0u, 0u);
  453. pConstantBuffer->Release();
  454.  
  455. cb = {
  456. {
  457. dx::XMMatrixTranspose(
  458. //dx::XMMatrixRotationX(angle) *
  459. dx::XMMatrixRotationY(angle) *
  460. //dx::XMMatrixRotationZ(angle) *
  461. dx::XMMatrixScaling(0.00005f, 0.00005f, 0.00005f) *
  462. //dx::XMMatrixTranslation(mouse.x/400.0f - 1.0f, -mouse.y/300.0f + 1.0f, 0.5f)
  463. dx::XMMatrixTranslation(0.0f, 0.5f, 0.1f)
  464. //dx::XMMatrixPerspectiveLH(1.0f, 3.0f/4.0f, 0.5f, 10.0f)
  465. )
  466. }
  467. };
  468. dev->CreateBuffer(&cbd, &csd, &pConstantBuffer);
  469. devcon->VSSetConstantBuffers(0u, 1u, &pConstantBuffer);
  470. devcon->DrawIndexed((UINT)indices.size(), 0u, 0u);
  471. pConstantBuffer->Release();
  472.  
  473. cb = {
  474. {
  475. dx::XMMatrixTranspose(
  476. //dx::XMMatrixRotationX(angle) *
  477. dx::XMMatrixRotationY(angle) *
  478. //dx::XMMatrixRotationZ(angle) *
  479. dx::XMMatrixScaling(0.00005f, 0.00005f, 0.00005f) *
  480. //dx::XMMatrixTranslation(mouse.x/400.0f - 1.0f, -mouse.y/300.0f + 1.0f, 0.5f)
  481. dx::XMMatrixTranslation(0.0f, 0.0f, 0.1f)
  482. //dx::XMMatrixPerspectiveLH(1.0f, 3.0f/4.0f, 0.5f, 10.0f)
  483. )
  484. }
  485. };
  486. dev->CreateBuffer(&cbd, &csd, &pConstantBuffer);
  487. devcon->VSSetConstantBuffers(0u, 1u, &pConstantBuffer);
  488. devcon->DrawIndexed((UINT)indices.size(), 0u, 0u);
  489. pConstantBuffer->Release();
  490.  
  491. cb = {
  492. {
  493. dx::XMMatrixTranspose(
  494. //dx::XMMatrixRotationX(angle) *
  495. dx::XMMatrixRotationY(angle) *
  496. //dx::XMMatrixRotationZ(angle) *
  497. dx::XMMatrixScaling(0.00005f, 0.00005f, 0.00005f) *
  498. //dx::XMMatrixTranslation(mouse.x/400.0f - 1.0f, -mouse.y/300.0f + 1.0f, 0.5f)
  499. dx::XMMatrixTranslation(-0.5f, 0.0f, 0.1f)
  500. //dx::XMMatrixPerspectiveLH(1.0f, 3.0f/4.0f, 0.5f, 10.0f)
  501. )
  502. }
  503. };
  504. dev->CreateBuffer(&cbd, &csd, &pConstantBuffer);
  505. devcon->VSSetConstantBuffers(0u, 1u, &pConstantBuffer);
  506. devcon->DrawIndexed((UINT)indices.size(), 0u, 0u);
  507. pConstantBuffer->Release();
  508.  
  509. cb = {
  510. {
  511. dx::XMMatrixTranspose(
  512. //dx::XMMatrixRotationX(angle) *
  513. dx::XMMatrixRotationY(angle) *
  514. //dx::XMMatrixRotationZ(angle) *
  515. dx::XMMatrixScaling(0.00005f, 0.00005f, 0.00005f) *
  516. //dx::XMMatrixTranslation(mouse.x/400.0f - 1.0f, -mouse.y/300.0f + 1.0f, 0.5f)
  517. dx::XMMatrixTranslation(0.5f, 0.0f, 0.1f)
  518. //dx::XMMatrixPerspectiveLH(1.0f, 3.0f/4.0f, 0.5f, 10.0f)
  519. )
  520. }
  521. };
  522. dev->CreateBuffer(&cbd, &csd, &pConstantBuffer);
  523. devcon->VSSetConstantBuffers(0u, 1u, &pConstantBuffer);
  524. //Exception thrown at 0x76CF4662 in main.exe: Microsoft C++ exception: _com_error at memory location 0x010FDE44.
  525. //check = 134791
  526. devcon->DrawIndexed((UINT)indices.size(), 0u, 0u);
  527. pConstantBuffer->Release();
  528.  
  529. swapchain->Present(0, 0);
  530.  
  531. if(check == 100000)
  532. cout << check << endl;
  533. if(check == 200000)
  534. cout << check << endl;
  535. if(check == 300000)
  536. cout << check << endl;
  537. if(check == 400000)
  538. cout << check << endl;
  539. if(check == 500000)
  540. cout << check << endl;
  541. if(check == 600000)
  542. cout << check << endl;
  543. if(check == 700000)
  544. cout << check << endl;
  545. if(check == 800000)
  546. cout << check << endl;
  547. if(check == 900000)
  548. cout << check << endl;
  549. if(check == 1000000)
  550. cout << check << endl;
  551. if(check == 1100000)
  552. cout << check << endl;
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559. }
  560.  
  561.  
  562. void CleanD3D(void)
  563. {
  564. swapchain->SetFullscreenState(FALSE, NULL);
  565. pLayout->Release();
  566. pVS->Release();
  567. pPS->Release();
  568. swapchain->Release();
  569. backbuffer->Release();
  570. dev->Release();
  571. devcon->Release();
  572. pConstantBuffer->Release();
  573. }
  574.  
  575.  
  576.  
  577.  
  578.  
Advertisement
Add Comment
Please, Sign In to add comment