Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------
- // Includes
- //---------------------------
- #include "Cube.h"
- #include "Level.h"
- #include "GraphCamera.h"
- //---------------------------
- // Defines
- //---------------------------
- //#define GAME_ENGINE (GameEngine::GetSingleton())
- //---------------------------
- // Constructor & Destructor
- //---------------------------
- Cube::Cube()
- :LevelElement()
- ,m_pVertexLayout(0)
- ,m_pVertexBuffer(0)
- ,m_pIndexBuffer(0)
- ,m_pDefaultEffect(0)
- ,m_pDefaultTechnique(0)
- ,m_pWorldViewProjectionVariable(NULL)
- {
- // nothing to create
- }
- Cube::~Cube()
- {
- SafeRelease(m_pVertexBuffer );
- SafeRelease(m_pVertexLayout);
- }
- void Cube::Initialize(ContentManager *pContentManager)
- {
- CreateEffect(pContentManager);
- DefineInputlayout();
- BuildShape();
- BuildVertexBuffer();
- BuildIndexBuffer();
- }
- void Cube::Draw(const RenderContext* pRenderContext)
- {
- if(!m_pDefaultTechnique )
- {
- MessageBox(0,_T("No Technique"),_T("ERROR"),0);
- return;
- }
- if(!m_pVertexBuffer)
- {
- MessageBox(0,_T("No Vertices"),_T("ERROR"),0);
- return;
- }
- // Update Shader matrix variables
- D3DXMATRIX matView = pRenderContext->GetCamera()->GetView();
- D3DXMATRIX matProj = pRenderContext->GetCamera()->GetProj();
- m_pWorldViewProjectionVariable->SetMatrix((float*)&(m_World*matView*matProj));
- // Set the input layout
- m_pLevel->GetDevice()->IASetInputLayout( m_pVertexLayout );
- // Set vertex buffer(s)
- UINT offset = 0;
- UINT vertexBufferStride = sizeof(VertexPosCol);
- m_pLevel->GetDevice()->IASetVertexBuffers( 0, 1, &m_pVertexBuffer, &vertexBufferStride, &offset );
- // Set index buffer
- m_pLevel->GetDevice()->IASetIndexBuffer(m_pIndexBuffer,DXGI_FORMAT_R32_UINT,0);
- // Set primitive topology
- m_pLevel->GetDevice()->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
- D3D10_TECHNIQUE_DESC techDesc;
- m_pDefaultTechnique->GetDesc( &techDesc );
- for( UINT p = 0; p < techDesc.Passes; ++p )
- {
- m_pDefaultTechnique->GetPassByIndex(p)->Apply(0);
- m_pLevel->GetDevice()->DrawIndexed( m_VecIndices.size(), 0, 0 );
- }
- }
- void Cube::CreateEffect(ContentManager *pContentManager)
- {
- m_pDefaultEffect = pContentManager->GetEffect(m_pLevel->GetDevice(), _T("./Effect/PosCol.fx"));
- //get first technique found
- m_pDefaultTechnique = m_pDefaultEffect->GetTechniqueByIndex(0);
- if(!m_pDefaultTechnique->IsValid())MessageBox(0,_T("Technique not valid"),_T("ERROR"),0);
- GetEffectVariables(m_pDefaultEffect);
- }
- void Cube::GetEffectVariables(ID3D10Effect* pEffect)
- {
- //get effect variables
- m_pWorldViewProjectionVariable = pEffect->GetVariableBySemantic("WorldViewProjection")->AsMatrix();
- if(!m_pWorldViewProjectionVariable->IsValid())MessageBox(0,_T("Getting EffectSemantic WorldViewProjection Failed"),_T("ERROR"),0);
- }
- void Cube::DefineInputlayout()
- {
- // TODO: Define the input layout
- D3D10_INPUT_ELEMENT_DESC layout[] =
- {
- { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, //{Semantic(Position, Normal,Color, etc),p 111 Soort Format hier 32 bit float format zonder Alpha channel
- { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, // Alpha kanaal bij omdat het over transparancy gaat, 12 omdat de Position al 12 B in beslag neemt dus we beginnen vanaf 12
- };
- UINT numElements = sizeof(layout)/sizeof(layout[0]);
- // Create the input layout
- D3D10_PASS_DESC PassDesc;
- // Get the pass decriptor from the effect technique
- m_pDefaultTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
- HR(m_pLevel->GetDevice()->CreateInputLayout( layout,numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &m_pVertexLayout ));
- }
- void Cube::BuildShape()
- {
- VertexPosCol v1(-1,-1,-1,1,1,1,1);
- VertexPosCol v2(-1,1,-1,1,1,1,1);
- VertexPosCol v3(1,1,-1,1,1,1,1);
- VertexPosCol v4(1,-1,-1,1,1,1,1);
- VertexPosCol v5(1,-1,-1,0,0,0,1);
- VertexPosCol v6(1,1,-1,0,0,0,1);
- VertexPosCol v7(1,1,1,0,0,0,1);
- VertexPosCol v8(1,-1,1,0,0,0,1);
- VertexPosCol v9(1,-1,1,1,0,0,1);
- VertexPosCol v10(1,1,1,1,0,0,1);
- VertexPosCol v11(-1,1,1,1,0,0,1);
- VertexPosCol v12(-1,-1,1,1,0,0,1);
- VertexPosCol v13(-1,-1,1,0,1,0,1);
- VertexPosCol v14(-1,1,1,0,1,0,1);
- VertexPosCol v15(-1,1,-1,0,1,0,1);
- VertexPosCol v16(-1,-1,-1,0,1,0,1);
- VertexPosCol v17(-1,1,-1,0,0,1,1);
- VertexPosCol v18(-1,1,1,0,0,1,1);
- VertexPosCol v19(1,1,1,0,0,1,1);
- VertexPosCol v20(1,1,-1,0,0,1,1);
- VertexPosCol v21(1,-1,-1,.5f,.5f,.5f,1);
- VertexPosCol v22(1,-1,1,.5f,.5f,.5f,1);
- VertexPosCol v23(-1,-1,1,.5f,.5f,.5f,1);
- VertexPosCol v24(-1,-1,-1,.5f,.5f,.5f,1);
- m_VecVertices.push_back(v1);
- m_VecVertices.push_back(v2);
- m_VecVertices.push_back(v3);
- m_VecVertices.push_back(v4);
- m_VecVertices.push_back(v5);
- m_VecVertices.push_back(v6);
- m_VecVertices.push_back(v7);
- m_VecVertices.push_back(v8);
- m_VecVertices.push_back(v9);
- m_VecVertices.push_back(v10);
- m_VecVertices.push_back(v11);
- m_VecVertices.push_back(v12);
- m_VecVertices.push_back(v13);
- m_VecVertices.push_back(v14);
- m_VecVertices.push_back(v15);
- m_VecVertices.push_back(v16);
- m_VecVertices.push_back(v17);
- m_VecVertices.push_back(v18);
- m_VecVertices.push_back(v19);
- m_VecVertices.push_back(v20);
- m_VecVertices.push_back(v21);
- m_VecVertices.push_back(v22);
- m_VecVertices.push_back(v23);
- m_VecVertices.push_back(v24);
- AddQuad(0,1,2,3);
- AddQuad(4,5,6,7);
- AddQuad(8,9,10,11);
- AddQuad(12,13,14,15);
- AddQuad(16,17,18,19);
- AddQuad(20,21,22,23);
- }
- void Cube::AddQuad(unsigned int a, unsigned int b, unsigned int c,unsigned int d)
- {
- AddTriangle(a,b,c);
- AddTriangle(a,c,d);
- }
- void Cube::AddTriangle (unsigned int a, unsigned int b, unsigned c)
- {
- m_VecIndices.push_back(a);
- m_VecIndices.push_back(b);
- m_VecIndices.push_back(c);
- }
- void Cube::BuildVertexBuffer()
- {
- //TODO: fill a buffer description to copy the vertexdata into graphics memory
- D3D10_BUFFER_DESC bd;
- bd.Usage = D3D10_USAGE_IMMUTABLE;
- bd.ByteWidth = sizeof(VertexPosCol)*m_VecVertices.size();
- bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
- bd.CPUAccessFlags = 0;
- bd.MiscFlags = 0;
- D3D10_SUBRESOURCE_DATA initData;
- initData.pSysMem = m_VecVertices.data();
- //create a ID3D10Buffer in graphics memory containing the vertex info
- HR(m_pLevel->GetDevice()->CreateBuffer(&bd, &initData, &m_pVertexBuffer));
- }
- void Cube::BuildIndexBuffer()
- {//TODO: fill a buffer description to copy the indexdata into graphics memory
- D3D10_BUFFER_DESC ibd;
- ibd.Usage = D3D10_USAGE_IMMUTABLE;
- ibd.ByteWidth = sizeof(UINT)*m_VecIndices.size();
- ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
- ibd.CPUAccessFlags = 0;
- ibd.MiscFlags = 0;
- D3D10_SUBRESOURCE_DATA initData;
- initData.pSysMem = m_VecIndices.data();
- HR(m_pLevel->GetDevice()->CreateBuffer(&ibd, &initData, &m_pIndexBuffer));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement