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(0, 1.6f, 0); // 1 KAIRĖ PUSĖ KARDO
- vertices[0].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[1].position = D3DXVECTOR3(0, 1.4f, 0.2f); // 2
- vertices[1].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[2].position = D3DXVECTOR3(0, 0.6f, 0.2f); // 3
- vertices[2].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[3].position = D3DXVECTOR3(0, 0.6f, 0.4f); // 4
- vertices[3].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[4].position = D3DXVECTOR3(0, 0.5f, 0.3f); // 5
- vertices[4].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[5].position = D3DXVECTOR3(0, 0.5f, 0.2f); // 6
- vertices[5].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[6].position = D3DXVECTOR3(0, 0.4f, 0.1f); // 7
- vertices[6].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[7].position = D3DXVECTOR3(0, 0.2f, 0.1f); // 8
- vertices[7].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[8].position = D3DXVECTOR3(0, 0, 0); // 9
- vertices[8].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f); // KAIRĖ PUSĖ BAIGTA
- vertices[9].position = D3DXVECTOR3(0, 1.4f, -0.2f); // 9 DEŠINĖ PUSĖ
- vertices[9].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[10].position = D3DXVECTOR3(0, 0.6f, -0.2f); // 10
- vertices[10].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[11].position = D3DXVECTOR3(0, 0.6f, -0.4f); // 11
- vertices[11].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[12].position = D3DXVECTOR3(0, 0.5f, -0.3f); // 12
- vertices[12].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[13].position = D3DXVECTOR3(0, 0.5f, -0.2f); // 13
- vertices[13].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[14].position = D3DXVECTOR3(0, 0.4f, -0.1f); // 14
- vertices[14].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[15].position = D3DXVECTOR3(0, 0.2f, -0.1f); // 15
- vertices[15].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f); // 15 DEŠINĖ PUSĖ BAIGTA
- vertices[16].position = D3DXVECTOR3(2.5f, 1.6f, 0); // 1 DAROMA ANTRA PUSĖ KARDO
- vertices[16].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[17].position = D3DXVECTOR3(2.5f, 1.4f, 0.2f); // 2
- vertices[17].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[18].position = D3DXVECTOR3(2.5f, 0.6f, 0.2f); // 3
- vertices[18].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[19].position = D3DXVECTOR3(2.5f, 0.6f, 0.4f); // 4
- vertices[19].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[20].position = D3DXVECTOR3(2.5f, 0.5f, 0.3f); // 5
- vertices[20].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[21].position = D3DXVECTOR3(2.5f, 0.5f, 0.2f); // 6
- vertices[21].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[22].position = D3DXVECTOR3(2.5f, 0.4f, 0.1f); // 7
- vertices[22].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[23].position = D3DXVECTOR3(2.5f, 0.2f, 0.1f); // 8
- vertices[23].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[24].position = D3DXVECTOR3(2.5f, 0, 0); // 9
- vertices[24].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[25].position = D3DXVECTOR3(2.5f, 1.4f, -0.2f);
- vertices[25].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[26].position = D3DXVECTOR3(2.5f, 0.6f, -0.2f); // 11
- vertices[26].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[27].position = D3DXVECTOR3(2.5f, 0.6f, -0.4f); // 12
- vertices[27].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[28].position = D3DXVECTOR3(2.5f, 0.5f, -0.3f); // 13
- vertices[28].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[29].position = D3DXVECTOR3(2.5f, 0.5f, -0.2f); // 14
- vertices[29].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 1.0f);
- vertices[30].position = D3DXVECTOR3(2.5f, 0.4f, -0.1f); // 15
- vertices[30].color = D3DXVECTOR4(0.15f, 0.65f, 0.95f, 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] = 1;
- indices[2] = 7;
- indices[3] = 1;
- indices[4] = 2;
- indices[5] = 0;
- indices[6] = 2;
- indices[7] = 3;
- indices[8] = 4;
- indices[9] = 2;
- indices[10] = 4;
- indices[11] = 5;
- indices[12] = 5;
- indices[13] = 6;
- indices[14] = 0;
- indices[15] = 6;
- indices[16] = 7;
- indices[17] = 8;
- indices[18] = 8;
- indices[19] = 0;
- indices[20] = 6;
- indices[21] = 2;
- indices[22] = 5;
- indices[23] = 6; // KAIRĖ PUSĖ BAIGTA
- indices[24] = 0;
- indices[25] = 9;
- indices[26] = 7;
- indices[27] = 9;
- indices[28] = 10;
- indices[29] = 7;
- indices[30] = 10;
- indices[31] = 11;
- indices[32] = 12;
- indices[33] = 10;
- indices[34] = 12;
- indices[35] = 13;
- indices[36] = 13;
- indices[37] = 14;
- indices[38] = 0;
- indices[39] = 14;
- indices[40] = 15;
- indices[41] = 8;
- indices[42] = 8;
- indices[43] = 0;
- indices[44] = 14;
- indices[45] = 10;
- indices[46] = 13;
- indices[47] = 14;
- indices[48] = 6;
- indices[49] = 0;
- indices[50] = 2;
- // 2 DALIS KARO
- indices[51] = 0;
- indices[52] = 18;
- indices[53] = 24;
- indices[54] = 18;
- indices[55] = 19;
- indices[56] = 0;
- indices[57] = 19;
- indices[58] = 20;
- indices[59] = 21;
- indices[60] = 19;
- indices[61] = 21;
- indices[62] = 22;
- indices[63] = 22;
- indices[64] = 23;
- indices[65] = 0;
- indices[66] = 23;
- indices[67] = 24;
- indices[68] = 8;
- indices[69] = 8;
- indices[70] = 0;
- indices[71] = 23;
- indices[72] = 19;
- indices[73] = 22;
- indices[74] = 23;
- // 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;
- }
- void TriangleModelClass::Render(ID3D11DeviceContext* deviceContext)
- {
- //Update the world transformations
- UINT iTime = timeGetTime() % 2000;
- float fAngle = iTime * 2 * D3DX_PI / 2000.0f;
- D3DXMATRIX rotMatrix;
- D3DXMatrixRotationY(&rotMatrix, fAngle);
- //D3DXMatrixRotationAxis(&rotMatrix, 1.0f, 1.0f,1.0f, fAngle );
- SetModelWorldMatrix(rotMatrix);
- //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