Advertisement
Guest User

save7

a guest
Mar 19th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Filename: triangle_modelclass.cpp
  3. ////////////////////////////////////////////////////////////////////////////////
  4. #include "triangle_modelclass.h"
  5.  
  6.  
  7. TriangleModelClass::TriangleModelClass() :IndexModelClass()
  8. {
  9. }
  10.  
  11.  
  12. TriangleModelClass::TriangleModelClass(const TriangleModelClass& other)
  13. {
  14. }
  15.  
  16.  
  17. TriangleModelClass::~TriangleModelClass()
  18. {
  19. }
  20.  
  21.  
  22. bool TriangleModelClass::InitializeBuffers(ID3D11Device* device)
  23. {
  24. HRESULT result;
  25. //
  26. // Process the vertex buffer
  27. //
  28. VertexType* vertices;
  29. D3D11_BUFFER_DESC vertexBufferDesc;
  30. D3D11_SUBRESOURCE_DATA vertexData;
  31.  
  32. // Set the number of vertices in the vertex array.
  33. int vCount = 40;
  34. SetVertexCount(vCount);
  35.  
  36. // Create the vertex array.
  37. vertices = new VertexType[vCount];
  38. if (!vertices)
  39. {
  40. return false;
  41. }
  42.  
  43. // Load the vertex array with data.
  44. vertices[0].position = D3DXVECTOR3(0, 1.6f, 0); // 1 KAIRĖ PUSĖ KARDO
  45. vertices[0].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  46.  
  47. vertices[1].position = D3DXVECTOR3(0, 1.4f, 0.2f); // 2
  48. vertices[1].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  49.  
  50. vertices[2].position = D3DXVECTOR3(0, 0.6f, 0.2f); // 3
  51. vertices[2].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  52.  
  53. vertices[3].position = D3DXVECTOR3(0, 0.6f, 0.4f); // 4
  54. vertices[3].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  55.  
  56. vertices[4].position = D3DXVECTOR3(0, 0.5f, 0.3f); // 5
  57. vertices[4].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  58.  
  59. vertices[5].position = D3DXVECTOR3(0, 0.5f, 0.2f); // 6
  60. vertices[5].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  61.  
  62. vertices[6].position = D3DXVECTOR3(0, 0.4f, 0.1f); // 7
  63. vertices[6].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  64.  
  65. vertices[7].position = D3DXVECTOR3(0, 0.2f, 0.1f); // 8
  66. vertices[7].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  67.  
  68. vertices[8].position = D3DXVECTOR3(0, 0, 0); // 9
  69. vertices[8].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f); // BAIGTA
  70.  
  71.  
  72. vertices[9].position = D3DXVECTOR3(0, 1.4f, -0.2f); // 9 DEŠINĖ PUSĖ
  73. vertices[9].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  74.  
  75. vertices[10].position = D3DXVECTOR3(0, 0.6f, -0.2f); // 10
  76. vertices[10].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  77.  
  78. vertices[11].position = D3DXVECTOR3(0, 0.6f, -0.4f); // 11
  79. vertices[11].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  80.  
  81. vertices[12].position = D3DXVECTOR3(0, 0.5f, -0.3f); // 12
  82. vertices[12].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  83.  
  84. vertices[13].position = D3DXVECTOR3(0, 0.5f, -0.2f); // 13
  85. vertices[13].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  86.  
  87. vertices[14].position = D3DXVECTOR3(0, 0.4f, -0.1f); // 14
  88. vertices[14].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  89.  
  90. vertices[15].position = D3DXVECTOR3(0, 0.2f, -0.1f); // 15
  91. vertices[15].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f); // BAIGTA
  92.  
  93. // ANTRAS KARDAS //////////////////////////////////////////////////////////////////////////////////////////
  94.  
  95. vertices[16].position = D3DXVECTOR3(0.15f, 1.6f, 0); // 1 KAIRĖ PUSĖ KARDO
  96. vertices[16].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  97.  
  98. vertices[17].position = D3DXVECTOR3(0.15f, 1.4f, 0.2f); // 2
  99. vertices[17].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  100.  
  101. vertices[18].position = D3DXVECTOR3(0.15f, 0.6f, 0.2f); // 3
  102. vertices[18].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  103.  
  104. vertices[19].position = D3DXVECTOR3(0.15f, 0.6f, 0.4f); // 4
  105. vertices[19].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  106.  
  107. vertices[20].position = D3DXVECTOR3(0.15f, 0.5f, 0.3f); // 5
  108. vertices[20].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  109.  
  110. vertices[21].position = D3DXVECTOR3(0.15f, 0.5f, 0.2f); // 6
  111. vertices[21].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  112.  
  113. vertices[22].position = D3DXVECTOR3(0.15f, 0.4f, 0.1f); // 7
  114. vertices[22].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  115.  
  116. vertices[23].position = D3DXVECTOR3(0.15f, 0.2f, 0.1f); // 8
  117. vertices[23].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  118.  
  119. vertices[24].position = D3DXVECTOR3(0.15f, 0, 0); // 9
  120. vertices[24].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  121.  
  122.  
  123. vertices[25].position = D3DXVECTOR3(0.15f, 1.4f, -0.2f); // 9 DEŠINĖ PUSĖ
  124. vertices[25].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  125.  
  126. vertices[26].position = D3DXVECTOR3(0.15f, 0.6f, -0.2f); // 10
  127. vertices[26].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  128.  
  129. vertices[27].position = D3DXVECTOR3(0.15f, 0.6f, -0.4f); // 11
  130. vertices[27].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  131.  
  132. vertices[28].position = D3DXVECTOR3(0.15f, 0.5f, -0.3f); // 12
  133. vertices[28].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  134.  
  135. vertices[29].position = D3DXVECTOR3(0.15f, 0.5f, -0.2f); // 13
  136. vertices[29].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  137.  
  138. vertices[30].position = D3DXVECTOR3(0.15f, 0.4f, -0.1f); // 14
  139. vertices[30].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  140.  
  141. vertices[31].position = D3DXVECTOR3(0.15f, 0.2f, -0.1f); // 15
  142. vertices[31].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. // Set up the description of the static vertex buffer.
  151. vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  152. vertexBufferDesc.ByteWidth = sizeof(VertexType) * vCount;
  153. vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  154. vertexBufferDesc.CPUAccessFlags = 0;
  155. vertexBufferDesc.MiscFlags = 0;
  156. vertexBufferDesc.StructureByteStride = 0;
  157.  
  158. // Give the subresource structure a pointer to the vertex data.
  159. ZeroMemory(&vertexData, sizeof(vertexData));
  160. vertexData.pSysMem = vertices;
  161. vertexData.SysMemPitch = 0;
  162. vertexData.SysMemSlicePitch = 0;
  163.  
  164. // Now create the vertex buffer.
  165. result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
  166. if (FAILED(result))
  167. {
  168. return false;
  169. }
  170.  
  171. // Release the array now that the vertex buffer has been created and loaded.
  172. delete[] vertices;
  173. vertices = 0;
  174.  
  175. //
  176. // Process the index buffer
  177. //
  178. unsigned long* indices;
  179. D3D11_BUFFER_DESC indexBufferDesc;
  180. D3D11_SUBRESOURCE_DATA indexData;
  181.  
  182. // Set the number of indices in the index array.
  183. int iCount = 220;
  184. SetIndexCount(iCount);
  185.  
  186. // Create the index array.
  187. indices = new unsigned long[iCount];
  188. if (!indices)
  189. {
  190. return false;
  191. }
  192. // Load the index array with data.
  193. indices[0] = 0;
  194. indices[1] = 1;
  195. indices[2] = 7;
  196.  
  197. indices[3] = 1;
  198. indices[4] = 2;
  199. indices[5] = 0;
  200.  
  201. indices[6] = 2;
  202. indices[7] = 3;
  203. indices[8] = 4;
  204.  
  205. indices[9] = 2;
  206. indices[10] = 4;
  207. indices[11] = 5;
  208.  
  209. indices[12] = 5;
  210. indices[13] = 6;
  211. indices[14] = 0;
  212.  
  213. indices[15] = 6;
  214. indices[16] = 7;
  215. indices[17] = 8;
  216.  
  217. indices[18] = 8;
  218. indices[19] = 0;
  219. indices[20] = 6;
  220.  
  221. indices[21] = 2;
  222. indices[22] = 5;
  223. indices[23] = 6; // KAIRĖ PUSĖ BAIGTA
  224.  
  225. indices[24] = 0;
  226. indices[25] = 9;
  227. indices[26] = 7;
  228.  
  229. indices[27] = 9;
  230. indices[28] = 10;
  231. indices[29] = 7;
  232.  
  233. indices[30] = 10;
  234. indices[31] = 11;
  235. indices[32] = 12;
  236.  
  237. indices[33] = 10;
  238. indices[34] = 12;
  239. indices[35] = 13;
  240.  
  241. indices[36] = 13;
  242. indices[37] = 14;
  243. indices[38] = 0;
  244.  
  245. indices[39] = 14;
  246. indices[40] = 15;
  247. indices[41] = 8;
  248.  
  249. indices[42] = 8;
  250. indices[43] = 0;
  251. indices[44] = 14;
  252.  
  253. indices[45] = 10;
  254. indices[46] = 13;
  255. indices[47] = 14;
  256.  
  257. indices[48] = 6;
  258. indices[49] = 0;
  259. indices[50] = 2; // DEŠINĖ PUSĖ BAIGTA
  260.  
  261. // 2 DALIS KARDO
  262.  
  263. indices[51] = 16;
  264. indices[52] = 17;
  265. indices[53] = 23;
  266.  
  267. indices[54] = 17;
  268. indices[55] = 18;
  269. indices[56] = 16;
  270.  
  271. indices[57] = 18;
  272. indices[58] = 19;
  273. indices[59] = 20;
  274.  
  275. indices[60] = 18;
  276. indices[61] = 20;
  277. indices[62] = 21;
  278.  
  279. indices[63] = 21;
  280. indices[64] = 22;
  281. indices[65] = 16;
  282.  
  283. indices[66] = 22;
  284. indices[67] = 23;
  285. indices[68] = 24;
  286.  
  287. indices[69] = 24;
  288. indices[70] = 16;
  289. indices[71] = 22;
  290.  
  291. indices[72] = 18;
  292. indices[73] = 21;
  293. indices[74] = 22; // KAIRĖ PUSĖ BAIGTA
  294.  
  295. indices[75] = 16;
  296. indices[76] = 25;
  297. indices[77] = 23;
  298.  
  299. indices[78] = 25;
  300. indices[79] = 26;
  301. indices[80] = 23;
  302.  
  303. indices[81] = 26;
  304. indices[82] = 27;
  305. indices[83] = 28;
  306.  
  307. indices[84] = 26;
  308. indices[85] = 28;
  309. indices[86] = 29;
  310.  
  311. indices[87] = 29;
  312. indices[88] = 30;
  313. indices[89] = 16;
  314.  
  315. indices[90] = 30;
  316. indices[91] = 31;
  317. indices[92] = 24;
  318.  
  319. indices[93] = 24;
  320. indices[94] = 16;
  321. indices[95] = 30;
  322.  
  323. indices[96] = 26;
  324. indices[97] = 29;
  325. indices[98] = 30;
  326.  
  327. indices[99] = 22;
  328. indices[100] = 16;
  329. indices[101] = 18; // DEŠINĖ PUSĖ BAIGTA
  330.  
  331. indices[102] = 16;
  332. indices[103] = 0;
  333. indices[104] = 9;
  334.  
  335. indices[105] = 16;
  336. indices[106] = 17;
  337. indices[107] = 0; // 1 - virsus
  338.  
  339. indices[108] = 0;
  340. indices[109] = 1;
  341. indices[110] = 16;
  342.  
  343. indices[111] = 17;
  344. indices[112] = 16;
  345. indices[113] = 1; // 1 - virsus
  346.  
  347. indices[114] = 17;
  348. indices[115] = 1;
  349. indices[116] = 2;
  350.  
  351. indices[117] = 18;
  352. indices[118] = 17;
  353. indices[119] = 2; // 2 - sonai
  354.  
  355. indices[120] = 31;
  356. indices[121] = 30;
  357. indices[122] = 10;
  358.  
  359. indices[123] = 9;
  360. indices[124] = 31;
  361. indices[125] = 10; // 2 - sonai
  362.  
  363. indices[126] = 2;
  364. indices[127] = 3;
  365. indices[128] = 18;
  366.  
  367. indices[129] = 19;
  368. indices[130] = 18;
  369. indices[131] = 3; // 3 - mazi sonai virsus
  370.  
  371. indices[132] = 30;
  372. indices[133] = 29;
  373. indices[134] = 11;
  374.  
  375. indices[135] = 10;
  376. indices[136] = 30;
  377. indices[137] = 11; // 3 - mazi sonai virsus
  378.  
  379. indices[138] = 3;
  380. indices[139] = 4;
  381. indices[140] = 20;
  382.  
  383. indices[141] = 19;
  384. indices[142] = 20;
  385. indices[143] = 3; // 4 mazi sonai apacia
  386.  
  387. indices[144] = 29;
  388. indices[145] = 28;
  389. indices[146] = 11;
  390.  
  391. indices[147] = 21;
  392. indices[148] = 11;
  393. indices[149] = 28; // 4 mazi sonai apacia
  394.  
  395. indices[150] = 4;
  396. indices[151] = 5;
  397. indices[152] = 20;
  398.  
  399. indices[153] = 21;
  400. indices[154] = 20;
  401. indices[155] = 5; // 4 mazi sonai apacia apacia
  402.  
  403. indices[156] = 27;
  404. indices[157] = 28;
  405. indices[158] = 13;
  406.  
  407. indices[159] = 12;
  408. indices[160] = 13;
  409. indices[161] = 28; // 4 mazi sonai apacia apacia
  410.  
  411. indices[162] = 4;
  412. indices[163] = 5;
  413. indices[164] = 20;
  414.  
  415. indices[165] = 5;
  416. indices[166] = 6;
  417. indices[167] = 21; // 4 mazi sonai apacia apacia
  418.  
  419. indices[168] = 22;
  420. indices[169] = 21;
  421. indices[170] = 6;
  422.  
  423. indices[171] = 12;
  424. indices[172] = 13;
  425. indices[173] = 28; // apacia lenkta
  426.  
  427. indices[174] = 26;
  428. indices[175] = 27;
  429. indices[176] = 13;
  430.  
  431. indices[177] = 14;
  432. indices[178] = 26;
  433. indices[179] = 13; // apacia lenkta
  434.  
  435. indices[180] = 6;
  436. indices[181] = 7;
  437. indices[182] = 23;
  438.  
  439. indices[183] = 22;
  440. indices[184] = 23;
  441. indices[185] = 6; // apacia sonai
  442.  
  443. indices[186] = 26;
  444. indices[187] = 25;
  445. indices[188] = 15;
  446.  
  447. indices[189] = 14;
  448. indices[190] = 15;
  449. indices[191] = 26; // apacia sonai
  450.  
  451. indices[192] = 7;
  452. indices[193] = 8;
  453. indices[194] = 24;
  454.  
  455. indices[195] = 23;
  456. indices[196] = 24;
  457. indices[197] = 7; // apacia
  458.  
  459. indices[198] = 25;
  460. indices[199] = 24;
  461. indices[200] = 8;
  462.  
  463. indices[201] = 15;
  464. indices[202] = 8;
  465. indices[203] = 25; // apacia
  466.  
  467.  
  468.  
  469.  
  470.  
  471. // Set up the description of the static index buffer.
  472. indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  473. indexBufferDesc.ByteWidth = sizeof(unsigned long) * iCount;
  474. indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  475. indexBufferDesc.CPUAccessFlags = 0;
  476. indexBufferDesc.MiscFlags = 0;
  477. indexBufferDesc.StructureByteStride = 0;
  478.  
  479. // Give the subresource structure a pointer to the index data.
  480. indexData.pSysMem = indices;
  481. indexData.SysMemPitch = 0;
  482. indexData.SysMemSlicePitch = 0;
  483.  
  484. // Create the index buffer.
  485. result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
  486. if (FAILED(result))
  487. {
  488. return false;
  489. }
  490.  
  491. // Release the array now that the vertex buffer has been created and loaded.
  492. delete[] indices;
  493. indices = 0;
  494.  
  495. return true;
  496. }
  497.  
  498.  
  499. void TriangleModelClass::Render(ID3D11DeviceContext* deviceContext)
  500. {
  501. //Update the world transformations
  502. UINT iTime = timeGetTime() % 2000;
  503. float fAngle = iTime * 2 * D3DX_PI / 2000.0f;
  504. D3DXMATRIX rotMatrix;
  505. D3DXMatrixRotationY(&rotMatrix, fAngle);
  506. //D3DXMatrixRotationAxis(&rotMatrix, 1.0f, 1.0f,1.0f, fAngle );
  507. SetModelWorldMatrix(rotMatrix);
  508. //SetModelWorldMatrix(XMMatrixIdentity());
  509.  
  510. // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
  511. RenderBuffers(deviceContext);
  512.  
  513. return;
  514. }
  515.  
  516.  
  517. void TriangleModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
  518. {
  519. unsigned int stride;
  520. unsigned int offset;
  521.  
  522. // Set vertex buffer stride and offset.
  523. stride = sizeof(VertexType);
  524. offset = 0;
  525.  
  526. // Set the vertex buffer to active in the input assembler so it can be rendered.
  527. deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
  528.  
  529. // Set the index buffer to active in the input assembler so it can be rendered.
  530. deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
  531.  
  532. // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
  533. deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  534.  
  535. // render primitives
  536. deviceContext->DrawIndexed(GetIndexCount(), 0, 0);
  537.  
  538. return;
  539. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement