Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////
- // Filename: triangle_modelclass.cpp
- ////////////////////////////////////////////////////////////////////////////////
- #include "triangle_modelclass.h"
- TriangleModelClass::TriangleModelClass():IndexModelClass()
- {
- }
- TriangleModelClass::TriangleModelClass(const TriangleModelClass& other)
- {
- }
- TriangleModelClass::~TriangleModelClass()
- {
- }
- bool TriangleModelClass::InitializeBuffers(ID3D11Device* device)
- {
- HRESULT result;
- //
- // Process the vertex buffer
- //
- VertexType* vertices;
- D3D11_BUFFER_DESC vertexBufferDesc;
- D3D11_SUBRESOURCE_DATA vertexData;
- // Set the number of vertices in the vertex array.
- int vCount = 24;
- SetVertexCount(vCount);
- // Create the vertex array.
- vertices = new VertexType[vCount];
- if(!vertices)
- {
- return false;
- }
- // Load the vertex array with data.
- vertices[0].position = D3DXVECTOR3(-1.0f, 1.5f, -1.0f);
- vertices[0].color = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
- vertices[1].position = D3DXVECTOR3(1.0f, 1.5f, -1.0f);
- vertices[1].color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
- vertices[2].position = D3DXVECTOR3(1.0f, 1.5f, 1.0f);
- vertices[2].color = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
- vertices[3].position = D3DXVECTOR3(-1.0f, 1.5f, 1.0f);
- vertices[3].color = D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f);
- vertices[4].position = D3DXVECTOR3(-1.0f, 0.5f, -1.0f);
- vertices[4].color = D3DXVECTOR4(0.0f, 1.0f, 01.0f, 1.0f);
- /////////////////////////////////
- vertices[5].position = D3DXVECTOR3(1.0f, 0.5f, -1.0f);
- vertices[5].color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
- vertices[6].position = D3DXVECTOR3(1.0f, 0.5f, 1.0f);
- vertices[6].color = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
- vertices[7].position = D3DXVECTOR3(-1.0f, 0.5f, 1.0f);
- vertices[7].color = D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f);
- vertices[8].position = D3DXVECTOR3(-2.0f, 0.5f, -1.0f);
- vertices[8].color = D3DXVECTOR4(0.0f, 1.0f, 01.0f, 1.0f);
- vertices[9].position = D3DXVECTOR3(2.0f, 0.5f, -1.0f);
- vertices[9].color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
- vertices[10].position = D3DXVECTOR3(2.0f, 0.5f, 1.0f);
- vertices[10].color = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
- vertices[11].position = D3DXVECTOR3(-2.0f, 0.5f, 1.0f);
- vertices[11].color = D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f);
- vertices[12].position = D3DXVECTOR3(-2.0f, -0.5f, -1.0f);
- vertices[12].color = D3DXVECTOR4(0.0f, 1.0f, 01.0f, 1.0f);
- vertices[13].position = D3DXVECTOR3(2.0f, -0.5f, -1.0f);
- vertices[13].color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
- vertices[14].position = D3DXVECTOR3(2.0f, -0.5f, 1.0f);
- vertices[14].color = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
- vertices[15].position = D3DXVECTOR3(-2.0f, -0.5f, 1.0f);
- vertices[15].color = D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f);
- vertices[16].position = D3DXVECTOR3(-3.0f, -0.5f, -1.0f);
- vertices[16].color = D3DXVECTOR4(0.0f, 1.0f, 01.0f, 1.0f);
- vertices[17].position = D3DXVECTOR3(3.0f, -0.5f, -1.0f);
- vertices[17].color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
- vertices[18].position = D3DXVECTOR3(3.0f, -0.5f, 1.0f);
- vertices[18].color = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
- vertices[19].position = D3DXVECTOR3(-3.0f, -0.5f, 1.0f);
- vertices[19].color = D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f);
- vertices[20].position = D3DXVECTOR3(-3.0f, -1.5f, -1.0f);
- vertices[20].color = D3DXVECTOR4(0.0f, 1.0f, 01.0f, 1.0f);
- vertices[21].position = D3DXVECTOR3(3.0f, -1.5f, -1.0f);
- vertices[21].color = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
- vertices[22].position = D3DXVECTOR3(3.0f, -1.5f, 1.0f);
- vertices[22].color = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
- vertices[23].position = D3DXVECTOR3(-3.0f, -1.5f, 1.0f);
- vertices[23].color = D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f);
- // Set up the description of the static vertex buffer.
- vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- vertexBufferDesc.ByteWidth = sizeof(VertexType) * vCount;
- vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
- vertexBufferDesc.CPUAccessFlags = 0;
- vertexBufferDesc.MiscFlags = 0;
- vertexBufferDesc.StructureByteStride = 0;
- // Give the subresource structure a pointer to the vertex data.
- ZeroMemory( &vertexData, sizeof( vertexData ) );
- vertexData.pSysMem = vertices;
- vertexData.SysMemPitch = 0;
- vertexData.SysMemSlicePitch = 0;
- // Now create the vertex buffer.
- result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
- if(FAILED(result))
- {
- return false;
- }
- // Release the array now that the vertex buffer has been created and loaded.
- delete [] vertices;
- vertices = 0;
- //
- // Process the index buffer
- //
- unsigned long* indices;
- D3D11_BUFFER_DESC indexBufferDesc;
- D3D11_SUBRESOURCE_DATA indexData;
- // Set the number of indices in the index array.
- int iCount = 108;
- SetIndexCount(iCount);
- // Create the index array.
- indices = new unsigned long[iCount];
- if(!indices)
- {
- return false;
- }
- // Load the index array with data.
- indices[0] = 0;
- indices[1] = 2;
- indices[2] = 1;
- indices[3] = 0;
- indices[4] = 3;
- indices[5] = 2;
- indices[6] = 0;
- indices[7] = 1;
- indices[8] = 5;
- indices[9] = 0;
- indices[10] = 4;
- indices[11] = 5;
- indices[12] = 6;
- indices[13] = 3;
- indices[14] = 2;
- indices[15] = 3;
- indices[16] = 7;
- indices[17] = 6;
- indices[18] = 5;
- indices[19] = 1;
- indices[20] = 2;
- indices[21] = 2;
- indices[22] = 5;
- indices[23] = 6;
- indices[24] = 0;
- indices[25] = 3;
- indices[26] = 4;
- indices[27] = 3;
- indices[28] = 4;
- indices[29] = 7;
- indices[30] = 5;
- indices[31] = 9;
- indices[32] = 10;
- indices[33] = 5;
- indices[34] = 6;
- indices[35] = 10;
- indices[36] = 8;
- indices[37] = 7;
- indices[38] = 4;
- indices[39] = 11;
- indices[40] = 7;
- indices[41] = 8;
- indices[42] = 9;
- indices[43] = 10;
- indices[44] = 13;
- indices[45] = 10;
- indices[46] = 14;
- indices[47] = 13;
- indices[48] = 8;
- indices[49] = 11;
- indices[50] = 12;
- indices[51] = 11;
- indices[52] = 12;
- indices[53] = 15;
- indices[54] = 8;
- indices[55] = 9;
- indices[56] = 13;
- indices[57] = 8;
- indices[58] = 12;
- indices[59] = 13;
- //
- indices[60] = 11;
- indices[61] = 10;
- indices[62] = 14;
- indices[63] = 11;
- indices[64] = 15;
- indices[65] = 14;
- indices[66] = 13;
- indices[67] = 17;
- indices[68] = 18;
- indices[69] = 13;
- indices[70] = 18;
- indices[71] = 14;
- indices[72] = 12;
- indices[73] = 16;
- indices[74] = 15;
- indices[75] = 16;
- indices[76] = 19;
- indices[77] = 15;
- indices[78] = 16;
- indices[79] = 17;
- indices[80] = 21;
- indices[81] = 16;
- indices[82] = 20;
- indices[83] = 21;
- indices[84] = 17;
- indices[85] = 18;
- indices[86] = 21;
- indices[87] = 18;
- indices[88] = 22;
- indices[89] = 21;
- //dugnas
- indices[90] = 20;
- indices[91] = 21;
- indices[92] = 22;
- indices[93] = 20;
- indices[94] = 23;
- indices[95] = 22;
- //
- indices[96] = 16;
- indices[97] = 19;
- indices[98] = 20;
- indices[99] = 19;
- indices[100] = 20;
- indices[101] = 23;
- //priekis apacia
- indices[102] = 19;
- indices[103] = 18;
- indices[104] = 22;
- indices[105] = 19;
- indices[106] = 23;
- indices[107] = 22;
- // Set up the description of the static index buffer.
- indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- indexBufferDesc.ByteWidth = sizeof(unsigned long) * iCount;
- indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
- indexBufferDesc.CPUAccessFlags = 0;
- indexBufferDesc.MiscFlags = 0;
- indexBufferDesc.StructureByteStride = 0;
- // Give the subresource structure a pointer to the index data.
- indexData.pSysMem = indices;
- indexData.SysMemPitch = 0;
- indexData.SysMemSlicePitch = 0;
- // Create the index buffer.
- result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
- if(FAILED(result))
- {
- return false;
- }
- // Release the array now that the vertex buffer has been created and loaded.
- delete [] indices;
- indices = 0;
- return true;
- }
- 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;
- void TriangleModelClass::Render(ID3D11DeviceContext* deviceContext)
- {
- /*
- fAngle – pradinis kampas
- sAngle – sukimosi žingnis
- fScale – pradinė reikšmė
- sScale – scale žingnis
- xTrans – translate pradinė reikšmė x ašyje
- dxTrans – translate pradinės reikšmės x ašyje pokytis
- zTrans – translate pradinė reikšmė z ašyje
- dzTrans – translate pradinės reikšmės z ašyje pokytis
- */
- //Update the world transformations
- //UINT iTime = timeGetTime() % 2000;
- //float fAngle = iTime * 2 * D3DX_PI / 2000.0f;
- D3DXMATRIX rotMatrix, scaMatrix, transMatrix;
- D3DXMatrixRotationY(&rotMatrix, fAngle * D3DX_PI/180);
- if (fAngle < -90.0f) sAngle = 0.05f;
- if (fAngle > 90.0f) sAngle = -0.05f;
- fAngle = fAngle + sAngle;
- D3DXMatrixScaling(&scaMatrix, 1.0f, fScale, 1.0f);
- if (fScale < 1.0f) sScale = 0.001f;
- if (fScale > 2.0f) sScale = -0.001f;
- fScale = fScale + sScale;
- D3DXMatrixTranslation(&transMatrix, xTrans, 0.0f, zTrans);
- if (xTrans < -2.0f) dxTrans = 0.001f;
- if (xTrans > 2.0f) dxTrans = -0.001f;
- xTrans = xTrans + dxTrans;
- if (zTrans < -4.0f) dzTrans = 0.001f;
- if (zTrans > 4.0f) dzTrans = -0.001f;
- zTrans = zTrans + dzTrans;
- //D3DXMatrixRotationAxis(&rotMatrix, 1.0f, 1.0f,1.0f, fAngle );
- SetModelWorldMatrix(rotMatrix*scaMatrix*transMatrix);
- //SetModelWorldMatrix(XMMatrixIdentity());
- // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
- RenderBuffers(deviceContext);
- return;
- }
- void TriangleModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
- {
- unsigned int stride;
- unsigned int offset;
- // Set vertex buffer stride and offset.
- stride = sizeof(VertexType);
- offset = 0;
- // Set the vertex buffer to active in the input assembler so it can be rendered.
- deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
- // Set the index buffer to active in the input assembler so it can be rendered.
- deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
- // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
- deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
- // render primitives
- deviceContext->DrawIndexed(GetIndexCount(), 0, 0);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement