Guest User

Untitled

a guest
Oct 20th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.76 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 *pVBuffer;
  36. ID3D11Buffer *pConstantBuffer;
  37. D3D11_BUFFER_DESC cbd;
  38. D3D11_SUBRESOURCE_DATA csd = {};
  39.  
  40. using namespace std;
  41. namespace dx = DirectX;
  42.  
  43. struct Vertex{
  44. float x;
  45. float y;
  46. float z;
  47. unsigned char r;
  48. unsigned char g;
  49. unsigned char b;
  50. unsigned char a;
  51. };
  52.  
  53. struct Index{
  54. unsigned int a, b, c;
  55. };
  56.  
  57. Index indices[4309];
  58. Vertex vertices[4727];
  59.  
  60. string curline;
  61.  
  62. Vertex getFloats(string in)
  63. {
  64. Vertex fOut;
  65. string strV[3];
  66. in.erase(0,2);
  67. int spc;
  68.  
  69.  
  70. spc = in.find(" ") + 1;
  71. strV[0] = in.substr(0, spc);
  72. in.erase(0, spc);
  73. cout << "strv0: " << strV[0] << endl;
  74.  
  75.  
  76. spc = in.find(" ") + 1;
  77. strV[1] = in.substr(0, spc);
  78. in.erase(0, spc);
  79. cout << "strv1: " << strV[1] << endl;
  80.  
  81. strV[2] = in;
  82. cout << "strv2:" << strV[2] << endl;
  83.  
  84. fOut.x = stof(strV[0]);
  85. fOut.y = stof(strV[1]);
  86. fOut.z = stof(strV[2]);
  87.  
  88. unsigned char r = 0;
  89. unsigned char g = 255;
  90. unsigned char b = 0;
  91. unsigned char a = 0;
  92.  
  93. fOut.r = r;
  94. fOut.g = g;
  95. fOut.b = b;
  96. fOut.a = a;
  97.  
  98. return fOut;
  99. }
  100.  
  101. Index getUint(string in)
  102. {
  103. Index out;
  104. unsigned int uint[3];
  105. int spc;
  106. int sls;
  107. string strV[3];
  108.  
  109. in.erase(0,2);
  110.  
  111. sls = in.find("/");
  112. spc = in.find(" ") + 1;
  113. strV[0] = in.substr(0,sls);
  114. in.erase(0,spc);
  115.  
  116.  
  117. sls = in.find("/");
  118. spc = in.find(" ") + 1;
  119. strV[1] = in.substr(0,sls);
  120. in.erase(0,spc);
  121.  
  122.  
  123. sls = in.find("/");
  124. spc = in.find(" ") + 1;
  125. strV[2] = in.substr(0,sls);
  126. in.erase(0,spc);
  127.  
  128. cout << "0: " << strV[0] << " 1: " << strV[1] << " 2: " << strV[2] << endl;
  129.  
  130. out.a = stoi(strV[0]);
  131. out.b = stoi(strV[1]);
  132. out.c = stoi(strV[2]);
  133. return out;
  134. }
  135.  
  136.  
  137.  
  138. int loadObj()
  139. {
  140. cout << "start:" << endl;
  141.  
  142. ifstream file("P-51 Mustang.obj");
  143.  
  144. if(!file.is_open())
  145. return false;
  146.  
  147. int vertex = 0;
  148. int indice = 0;
  149.  
  150.  
  151. curline.clear();
  152. while (getline(file, curline))
  153. {
  154. char first = curline[0];
  155. char second = curline[1];
  156. char vCheck = 'v';
  157. char fCheck = 'f';
  158. char space = ' ';
  159. char currnum[20];
  160. cout << first << endl;
  161.  
  162. if(first == vCheck && second == space){
  163. vertices[vertex] = getFloats(curline);
  164. vertex += 1;
  165. }
  166.  
  167. if(first == fCheck && second == space){
  168. indices[indice] = getUint(curline);
  169. indice += 1;
  170. }
  171.  
  172. curline.clear();
  173. }
  174. cout << "Vertex count : " << vertex << "\n";
  175. cout << "Indice count : " << indice << "\n";
  176.  
  177. return 1;
  178. }
  179.  
  180.  
  181.  
  182.  
  183. float angle = 0.0f;
  184. float x = 0.0f;
  185. float y = 0.0f;
  186.  
  187. tagPOINT mouse;
  188.  
  189.  
  190. float s = 1.0f;
  191. float ns = s * -1;
  192.  
  193.  
  194. struct ConstantBuffer
  195. {
  196. dx::XMMATRIX transform;
  197. } cb;
  198.  
  199. void RenderFrame(HWND hWnd);
  200. void CleanD3D(void);
  201. void initD3d(HWND hWnd);
  202.  
  203.  
  204. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  205.  
  206. int WINAPI WinMain(HINSTANCE hInstance,
  207. HINSTANCE hPrevInstance,
  208. LPSTR lpCmdLine,
  209. int nCmdShow)
  210. {
  211.  
  212. std::cout << "start\n" ;
  213.  
  214. loadObj();
  215.  
  216. HWND hWnd;
  217. WNDCLASSEX wc;
  218.  
  219. ZeroMemory(&wc, sizeof(WNDCLASSEX));
  220.  
  221. wc.cbSize = sizeof(WNDCLASSEX);
  222. wc.style = CS_HREDRAW | CS_VREDRAW;
  223. wc.lpfnWndProc = WindowProc;
  224. wc.hInstance = hInstance;
  225. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  226. wc.lpszClassName = "WindowClass";
  227.  
  228. RegisterClassEx(&wc);
  229.  
  230. RECT wr = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
  231. AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
  232.  
  233. hWnd = CreateWindowEx(NULL,
  234. "WindowClass",
  235. "My First Direct3D Program",
  236. WS_OVERLAPPEDWINDOW,
  237. 300,
  238. 300,
  239. wr.right - wr.left,
  240. wr.bottom - wr.top,
  241. NULL,
  242. NULL,
  243. hInstance,
  244. NULL);
  245.  
  246. ShowWindow(hWnd, nCmdShow);
  247.  
  248. initD3d(hWnd);
  249.  
  250. MSG msg;
  251.  
  252. while(TRUE)
  253. {
  254. if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  255. {
  256. TranslateMessage(&msg);
  257. DispatchMessage(&msg);
  258.  
  259. if(msg.message == WM_QUIT){
  260. MessageBox(hWnd, "Quitting", "Quitting", 0);
  261. break;
  262. }
  263. }
  264. RenderFrame(hWnd);
  265. }
  266.  
  267. CleanD3D();
  268.  
  269. return msg.wParam;
  270. }
  271.  
  272.  
  273.  
  274. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  275. {
  276. switch(message)
  277. {
  278. case WM_DESTROY:
  279. {
  280. PostQuitMessage(0);
  281. return 0;
  282. } break;
  283. }
  284. return DefWindowProc (hWnd, message, wParam, lParam);
  285. }
  286.  
  287.  
  288.  
  289. void initD3d(HWND hWnd){
  290. DXGI_SWAP_CHAIN_DESC scd;
  291.  
  292. ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
  293.  
  294. scd.BufferCount = 1;
  295. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  296. scd.BufferDesc.Width = SCREEN_WIDTH;
  297. scd.BufferDesc.Height = SCREEN_HEIGHT;
  298. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  299. scd.OutputWindow = hWnd;
  300. scd.SampleDesc.Count = 4;
  301. scd.Windowed = TRUE;
  302. scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  303.  
  304.  
  305. D3D11CreateDeviceAndSwapChain(NULL,
  306. D3D_DRIVER_TYPE_HARDWARE,
  307. NULL,
  308. NULL,
  309. NULL,
  310. NULL,
  311. D3D11_SDK_VERSION,
  312. &scd,
  313. &swapchain,
  314. &dev,
  315. NULL,
  316. &devcon);
  317.  
  318. winrt::com_ptr<ID3D11Texture2D> pBackBuffer;
  319. swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
  320.  
  321. dev->CreateRenderTargetView(pBackBuffer.get(), NULL, &backbuffer);
  322. pBackBuffer->Release();
  323.  
  324. devcon->OMSetRenderTargets(1, &backbuffer, NULL);
  325.  
  326. D3D11_VIEWPORT viewport;
  327. ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  328.  
  329. viewport.TopLeftX = 0;
  330. viewport.TopLeftY = 0;
  331. viewport.MinDepth = 0.0f;
  332. viewport.MaxDepth = 1.0f;
  333. viewport.Width = SCREEN_WIDTH;
  334. viewport.Height = SCREEN_HEIGHT;
  335.  
  336. devcon->RSSetViewports(1, &viewport);
  337.  
  338. ID3DBlob *VS, *PS;
  339.  
  340. D3DCompileFromFile(L"vertexShader.hlsl", NULL, NULL, "main", "vs_4_0", 0, 0, &VS, &VS);
  341. D3DCompileFromFile(L"pixelShader.hlsl", NULL, NULL, "main", "ps_4_0", 0, 0, &PS, &PS);
  342.  
  343. dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
  344. dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);
  345.  
  346. devcon->VSSetShader(pVS, 0, 0);
  347. devcon->PSSetShader(pPS, 0, 0);
  348.  
  349. const D3D11_INPUT_ELEMENT_DESC ied[] =
  350. {
  351. {"Position", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
  352. {"Color", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
  353. };
  354.  
  355. dev->CreateInputLayout(
  356. ied, (UINT)std::size(ied),
  357. VS->GetBufferPointer(),
  358. VS->GetBufferSize(),
  359. &pLayout);
  360.  
  361. devcon->IASetInputLayout(pLayout);
  362.  
  363. ID3D11Buffer *pVertexBuffer;
  364. D3D11_BUFFER_DESC bd;
  365. bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  366. bd.ByteWidth = sizeof(vertices);
  367. bd.CPUAccessFlags = 0u;
  368. bd.MiscFlags = 0u;
  369. bd.Usage = D3D11_USAGE_DEFAULT;
  370. bd.StructureByteStride = sizeof(Vertex);
  371. D3D11_SUBRESOURCE_DATA sd = {};
  372. sd.pSysMem = vertices;
  373. dev->CreateBuffer(&bd, &sd, &pVertexBuffer);
  374. const UINT stride = sizeof(Vertex);
  375. const UINT offset = 0u;
  376. devcon->IASetVertexBuffers(0u, 1u, &pVertexBuffer, &stride, &offset);
  377.  
  378. ID3D11Buffer *pIndexBuffer;
  379. D3D11_BUFFER_DESC ibd;
  380. ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
  381. ibd.ByteWidth = sizeof(indices);
  382. ibd.CPUAccessFlags = 0u;
  383. ibd.MiscFlags = 0u;
  384. ibd.Usage = D3D11_USAGE_DEFAULT;
  385. ibd.StructureByteStride = sizeof(Index);
  386. D3D11_SUBRESOURCE_DATA isd = {};
  387. isd.pSysMem = indices;
  388. dev->CreateBuffer(&ibd, &isd, &pIndexBuffer);
  389. devcon->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0u);
  390.  
  391. cb = {
  392. {
  393. dx::XMMatrixTranspose(
  394. //dx::XMMatrixRotationX(angle) *
  395. dx::XMMatrixRotationY(angle) *
  396. //dx::XMMatrixRotationZ(angle) *
  397. dx::XMMatrixScaling(0.0001f, 0.0001f, 0.0001f) *
  398. dx::XMMatrixTranslation(mouse.x/400.0f - 1.0f, -mouse.y/300.0f + 1.0f, 0.5f)
  399. //dx::XMMatrixTranslation(0.0f, 0.5f, 0.1f)
  400. //dx::XMMatrixPerspectiveLH(1.0f, 3.0f/4.0f, 0.5f, 10.0f)
  401. )
  402. }
  403. };
  404.  
  405. ZeroMemory(&cbd, sizeof(cbd));
  406. cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  407. cbd.Usage = D3D11_USAGE_DYNAMIC;
  408. cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  409. cbd.MiscFlags = 0u;
  410. cbd.ByteWidth = sizeof(cb);
  411. cbd.StructureByteStride = 0u;
  412. csd.pSysMem = &cb;
  413. dev->CreateBuffer(&cbd, &csd, &pConstantBuffer);
  414. devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  415. }
  416.  
  417.  
  418.  
  419. void RenderFrame(HWND hWnd)
  420. {
  421. FLOAT ColorRGBA[4] = {0.3f, 0.2f, 0.4f, 1.0f};
  422. devcon->ClearRenderTargetView(backbuffer, ColorRGBA);
  423.  
  424. GetCursorPos(&mouse);
  425. ScreenToClient(hWnd, &mouse);
  426.  
  427. angle += 0.001f;
  428. if (angle > 10.0f)
  429. angle = 0.0f;
  430.  
  431. cb = {
  432. {
  433. dx::XMMatrixTranspose(
  434. //dx::XMMatrixRotationX(angle) *
  435. dx::XMMatrixRotationY(angle) *
  436. //dx::XMMatrixRotationZ(angle) *
  437. dx::XMMatrixScaling(0.0001f, 0.0001f, 0.0001f) *
  438. dx::XMMatrixTranslation(mouse.x/400.0f - 1.0f, -mouse.y/300.0f + 1.0f, 0.5f)
  439. //dx::XMMatrixTranslation(0.0f, 0.5f, 0.1f)
  440. //dx::XMMatrixPerspectiveLH(1.0f, 3.0f/4.0f, 0.5f, 10.0f)
  441. )
  442. }
  443. };
  444. dev->CreateBuffer(&cbd, &csd, &pConstantBuffer);
  445. devcon->VSSetConstantBuffers(0u, 1u, &pConstantBuffer);
  446.  
  447. devcon->DrawIndexed((UINT)sizeof(indices), 0u, 0u);
  448. swapchain->Present(0, 0);
  449.  
  450. }
  451.  
  452.  
  453. void CleanD3D(void)
  454. {
  455. swapchain->SetFullscreenState(FALSE, NULL);
  456. pLayout->Release();
  457. pVS->Release();
  458. pPS->Release();
  459. pVBuffer->Release();
  460. swapchain->Release();
  461. backbuffer->Release();
  462. dev->Release();
  463. devcon->Release();
  464. }
  465.  
  466.  
  467.  
  468.  
Advertisement
Add Comment
Please, Sign In to add comment