Advertisement
Guest User

SAVE2

a guest
Mar 18th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.69 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 = 24;
  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); // KAIRĖ PUSĖ 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); // 15 DEŠINĖ PUSĖ BAIGTA
  92.  
  93.  
  94. vertices[16].position = D3DXVECTOR3(2.5f, 1.6f, 0); // 1 DAROMA ANTRA PUSĖ KARDO
  95. vertices[16].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  96.  
  97. vertices[17].position = D3DXVECTOR3(2.5f, 1.4f, 0.2f); // 2
  98. vertices[17].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  99.  
  100. vertices[18].position = D3DXVECTOR3(2.5f, 0.6f, 0.2f); // 3
  101. vertices[18].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  102.  
  103. vertices[19].position = D3DXVECTOR3(2.5f, 0.6f, 0.4f); // 4
  104. vertices[19].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  105.  
  106. vertices[20].position = D3DXVECTOR3(2.5f, 0.5f, 0.3f); // 5
  107. vertices[20].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  108.  
  109. vertices[21].position = D3DXVECTOR3(2.5f, 0.5f, 0.2f); // 6
  110. vertices[21].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  111.  
  112. vertices[22].position = D3DXVECTOR3(2.5f, 0.4f, 0.1f); // 7
  113. vertices[22].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  114.  
  115. vertices[23].position = D3DXVECTOR3(2.5f, 0.2f, 0.1f); // 8
  116. vertices[23].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  117.  
  118. vertices[24].position = D3DXVECTOR3(2.5f, 0, 0); // 9
  119. vertices[24].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  120.  
  121.  
  122. vertices[25].position = D3DXVECTOR3(2.5f, 1.4f, -0.2f);
  123. vertices[25].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  124.  
  125. vertices[26].position = D3DXVECTOR3(2.5f, 0.6f, -0.2f); // 11
  126. vertices[26].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  127.  
  128. vertices[27].position = D3DXVECTOR3(2.5f, 0.6f, -0.4f); // 12
  129. vertices[27].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  130.  
  131. vertices[28].position = D3DXVECTOR3(2.5f, 0.5f, -0.3f); // 13
  132. vertices[28].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  133.  
  134. vertices[29].position = D3DXVECTOR3(2.5f, 0.5f, -0.2f); // 14
  135. vertices[29].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  136.  
  137. vertices[30].position = D3DXVECTOR3(2.5f, 0.4f, -0.1f); // 15
  138. vertices[30].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. // Set up the description of the static vertex buffer.
  147. vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  148. vertexBufferDesc.ByteWidth = sizeof(VertexType) * vCount;
  149. vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  150. vertexBufferDesc.CPUAccessFlags = 0;
  151. vertexBufferDesc.MiscFlags = 0;
  152. vertexBufferDesc.StructureByteStride = 0;
  153.  
  154. // Give the subresource structure a pointer to the vertex data.
  155. ZeroMemory(&vertexData, sizeof(vertexData));
  156. vertexData.pSysMem = vertices;
  157. vertexData.SysMemPitch = 0;
  158. vertexData.SysMemSlicePitch = 0;
  159.  
  160. // Now create the vertex buffer.
  161. result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
  162. if (FAILED(result))
  163. {
  164. return false;
  165. }
  166.  
  167. // Release the array now that the vertex buffer has been created and loaded.
  168. delete[] vertices;
  169. vertices = 0;
  170.  
  171. //
  172. // Process the index buffer
  173. //
  174. unsigned long* indices;
  175. D3D11_BUFFER_DESC indexBufferDesc;
  176. D3D11_SUBRESOURCE_DATA indexData;
  177.  
  178. // Set the number of indices in the index array.
  179. int iCount = 108;
  180. SetIndexCount(iCount);
  181.  
  182. // Create the index array.
  183. indices = new unsigned long[iCount];
  184. if (!indices)
  185. {
  186. return false;
  187. }
  188. // Load the index array with data.
  189. indices[0] = 0;
  190. indices[1] = 1;
  191. indices[2] = 7;
  192.  
  193. indices[3] = 1;
  194. indices[4] = 2;
  195. indices[5] = 0;
  196.  
  197. indices[6] = 2;
  198. indices[7] = 3;
  199. indices[8] = 4;
  200.  
  201. indices[9] = 2;
  202. indices[10] = 4;
  203. indices[11] = 5;
  204.  
  205. indices[12] = 5;
  206. indices[13] = 6;
  207. indices[14] = 0;
  208.  
  209. indices[15] = 6;
  210. indices[16] = 7;
  211. indices[17] = 8;
  212.  
  213. indices[18] = 8;
  214. indices[19] = 0;
  215. indices[20] = 6;
  216.  
  217. indices[21] = 2;
  218. indices[22] = 5;
  219. indices[23] = 6; // KAIRĖ PUSĖ BAIGTA
  220.  
  221. indices[24] = 0;
  222. indices[25] = 9;
  223. indices[26] = 7;
  224.  
  225. indices[27] = 9;
  226. indices[28] = 10;
  227. indices[29] = 7;
  228.  
  229. indices[30] = 10;
  230. indices[31] = 11;
  231. indices[32] = 12;
  232.  
  233. indices[33] = 10;
  234. indices[34] = 12;
  235. indices[35] = 13;
  236.  
  237. indices[36] = 13;
  238. indices[37] = 14;
  239. indices[38] = 0;
  240.  
  241. indices[39] = 14;
  242. indices[40] = 15;
  243. indices[41] = 8;
  244.  
  245. indices[42] = 8;
  246. indices[43] = 0;
  247. indices[44] = 14;
  248.  
  249. indices[45] = 10;
  250. indices[46] = 13;
  251. indices[47] = 14;
  252.  
  253. indices[48] = 6;
  254. indices[49] = 0;
  255. indices[50] = 2;
  256.  
  257.  
  258. // 2 DALIS KARO
  259.  
  260. indices[51] = 0;
  261. indices[52] = 18;
  262. indices[53] = 24;
  263.  
  264. indices[54] = 18;
  265. indices[55] = 19;
  266. indices[56] = 0;
  267.  
  268. indices[57] = 19;
  269. indices[58] = 20;
  270. indices[59] = 21;
  271.  
  272. indices[60] = 19;
  273. indices[61] = 21;
  274. indices[62] = 22;
  275.  
  276. indices[63] = 22;
  277. indices[64] = 23;
  278. indices[65] = 0;
  279.  
  280. indices[66] = 23;
  281. indices[67] = 24;
  282. indices[68] = 8;
  283.  
  284. indices[69] = 8;
  285. indices[70] = 0;
  286. indices[71] = 23;
  287.  
  288. indices[72] = 19;
  289. indices[73] = 22;
  290. indices[74] = 23;
  291.  
  292.  
  293. // Set up the description of the static index buffer.
  294. indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  295. indexBufferDesc.ByteWidth = sizeof(unsigned long) * iCount;
  296. indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  297. indexBufferDesc.CPUAccessFlags = 0;
  298. indexBufferDesc.MiscFlags = 0;
  299. indexBufferDesc.StructureByteStride = 0;
  300.  
  301. // Give the subresource structure a pointer to the index data.
  302. indexData.pSysMem = indices;
  303. indexData.SysMemPitch = 0;
  304. indexData.SysMemSlicePitch = 0;
  305.  
  306. // Create the index buffer.
  307. result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
  308. if (FAILED(result))
  309. {
  310. return false;
  311. }
  312.  
  313. // Release the array now that the vertex buffer has been created and loaded.
  314. delete[] indices;
  315. indices = 0;
  316.  
  317. return true;
  318. }
  319.  
  320.  
  321. void TriangleModelClass::Render(ID3D11DeviceContext* deviceContext)
  322. {
  323. //Update the world transformations
  324. UINT iTime = timeGetTime() % 2000;
  325. float fAngle = iTime * 2 * D3DX_PI / 2000.0f;
  326. D3DXMATRIX rotMatrix;
  327. D3DXMatrixRotationY(&rotMatrix, fAngle);
  328. //D3DXMatrixRotationAxis(&rotMatrix, 1.0f, 1.0f,1.0f, fAngle );
  329. SetModelWorldMatrix(rotMatrix);
  330. //SetModelWorldMatrix(XMMatrixIdentity());
  331.  
  332. // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
  333. RenderBuffers(deviceContext);
  334.  
  335. return;
  336. }
  337.  
  338.  
  339. void TriangleModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
  340. {
  341. unsigned int stride;
  342. unsigned int offset;
  343.  
  344. // Set vertex buffer stride and offset.
  345. stride = sizeof(VertexType);
  346. offset = 0;
  347.  
  348. // Set the vertex buffer to active in the input assembler so it can be rendered.
  349. deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
  350.  
  351. // Set the index buffer to active in the input assembler so it can be rendered.
  352. deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
  353.  
  354. // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
  355. deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  356.  
  357. // render primitives
  358. deviceContext->DrawIndexed(GetIndexCount(), 0, 0);
  359.  
  360. return;
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement