Advertisement
Guest User

Untitled

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