Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.82 KB | None | 0 0
  1. //---------------------------
  2. // Includes
  3. //---------------------------
  4. #include "Cube.h"
  5. #include "Level.h"
  6. #include "GraphCamera.h"
  7. //---------------------------
  8. // Defines
  9. //---------------------------
  10. //#define GAME_ENGINE (GameEngine::GetSingleton())
  11.  
  12. //---------------------------
  13. // Constructor & Destructor
  14. //---------------------------
  15. Cube::Cube()
  16. :LevelElement()
  17.     ,m_pVertexLayout(0)
  18.     ,m_pVertexBuffer(0)
  19.     ,m_pIndexBuffer(0)
  20.     ,m_pDefaultEffect(0)   
  21.     ,m_pDefaultTechnique(0)
  22.     ,m_pWorldViewProjectionVariable(NULL)
  23. {
  24.     // nothing to create
  25. }
  26.  
  27. Cube::~Cube()
  28. {
  29.     SafeRelease(m_pVertexBuffer );
  30.     SafeRelease(m_pVertexLayout);
  31. }
  32.  
  33. void Cube::Initialize(ContentManager *pContentManager)
  34. {
  35.     CreateEffect(pContentManager);
  36.     DefineInputlayout();
  37.     BuildShape();
  38.     BuildVertexBuffer();
  39.     BuildIndexBuffer();
  40. }
  41.  
  42. void Cube::Draw(const RenderContext* pRenderContext)
  43. {
  44.     if(!m_pDefaultTechnique )
  45.     {
  46.         MessageBox(0,_T("No Technique"),_T("ERROR"),0);
  47.         return;
  48.     }
  49.     if(!m_pVertexBuffer)
  50.     {
  51.         MessageBox(0,_T("No Vertices"),_T("ERROR"),0);
  52.         return;
  53.     }  
  54.     // Update Shader matrix variables
  55.     D3DXMATRIX matView = pRenderContext->GetCamera()->GetView();
  56.     D3DXMATRIX matProj = pRenderContext->GetCamera()->GetProj();
  57.     m_pWorldViewProjectionVariable->SetMatrix((float*)&(m_World*matView*matProj));
  58.  
  59.     // Set the input layout
  60.     m_pLevel->GetDevice()->IASetInputLayout( m_pVertexLayout );
  61.  
  62.     // Set vertex buffer(s)
  63.     UINT offset = 0;
  64.     UINT vertexBufferStride = sizeof(VertexPosCol);
  65.     m_pLevel->GetDevice()->IASetVertexBuffers( 0, 1, &m_pVertexBuffer, &vertexBufferStride, &offset );
  66.    
  67.     // Set index buffer
  68.     m_pLevel->GetDevice()->IASetIndexBuffer(m_pIndexBuffer,DXGI_FORMAT_R32_UINT,0);
  69.  
  70.     // Set primitive topology
  71.     m_pLevel->GetDevice()->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  72.  
  73.     D3D10_TECHNIQUE_DESC techDesc;
  74.     m_pDefaultTechnique->GetDesc( &techDesc );
  75.     for( UINT p = 0; p < techDesc.Passes; ++p )
  76.     {
  77.         m_pDefaultTechnique->GetPassByIndex(p)->Apply(0);
  78.         m_pLevel->GetDevice()->DrawIndexed( m_VecIndices.size(), 0, 0 );
  79.     }
  80. }
  81. void Cube::CreateEffect(ContentManager *pContentManager)
  82. {
  83.     m_pDefaultEffect = pContentManager->GetEffect(m_pLevel->GetDevice(),  _T("./Effect/PosCol.fx"));
  84.    
  85.     //get first technique found
  86.     m_pDefaultTechnique = m_pDefaultEffect->GetTechniqueByIndex(0);
  87.     if(!m_pDefaultTechnique->IsValid())MessageBox(0,_T("Technique not valid"),_T("ERROR"),0);
  88.  
  89.     GetEffectVariables(m_pDefaultEffect);
  90. }
  91. void Cube::GetEffectVariables(ID3D10Effect* pEffect)
  92. {
  93.     //get effect variables
  94.     m_pWorldViewProjectionVariable = pEffect->GetVariableBySemantic("WorldViewProjection")->AsMatrix();
  95.     if(!m_pWorldViewProjectionVariable->IsValid())MessageBox(0,_T("Getting EffectSemantic WorldViewProjection Failed"),_T("ERROR"),0);
  96. }
  97. void Cube::DefineInputlayout()
  98. {
  99.     // TODO: Define the input layout
  100.     D3D10_INPUT_ELEMENT_DESC layout[] =
  101.     {
  102.         { "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
  103.         { "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
  104.  
  105.     };
  106.     UINT numElements = sizeof(layout)/sizeof(layout[0]);
  107.  
  108.     // Create the input layout
  109.     D3D10_PASS_DESC PassDesc;
  110.     // Get the pass decriptor from the effect technique
  111.     m_pDefaultTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
  112.     HR(m_pLevel->GetDevice()->CreateInputLayout( layout,numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &m_pVertexLayout ));
  113. }
  114.  
  115. void Cube::BuildShape()
  116. {
  117.  
  118.     VertexPosCol v1(-1,-1,-1,1,1,1,1);
  119.     VertexPosCol v2(-1,1,-1,1,1,1,1);
  120.     VertexPosCol v3(1,1,-1,1,1,1,1);
  121.     VertexPosCol v4(1,-1,-1,1,1,1,1);
  122.  
  123.     VertexPosCol v5(1,-1,-1,0,0,0,1);
  124.     VertexPosCol v6(1,1,-1,0,0,0,1);
  125.     VertexPosCol v7(1,1,1,0,0,0,1);
  126.     VertexPosCol v8(1,-1,1,0,0,0,1);
  127.  
  128.     VertexPosCol v9(1,-1,1,1,0,0,1);
  129.     VertexPosCol v10(1,1,1,1,0,0,1);
  130.     VertexPosCol v11(-1,1,1,1,0,0,1);
  131.     VertexPosCol v12(-1,-1,1,1,0,0,1);
  132.  
  133.     VertexPosCol v13(-1,-1,1,0,1,0,1);
  134.     VertexPosCol v14(-1,1,1,0,1,0,1);
  135.     VertexPosCol v15(-1,1,-1,0,1,0,1);
  136.     VertexPosCol v16(-1,-1,-1,0,1,0,1);
  137.  
  138.     VertexPosCol v17(-1,1,-1,0,0,1,1);
  139.     VertexPosCol v18(-1,1,1,0,0,1,1);
  140.     VertexPosCol v19(1,1,1,0,0,1,1);
  141.     VertexPosCol v20(1,1,-1,0,0,1,1);
  142.  
  143.     VertexPosCol v21(1,-1,-1,.5f,.5f,.5f,1);
  144.     VertexPosCol v22(1,-1,1,.5f,.5f,.5f,1);
  145.     VertexPosCol v23(-1,-1,1,.5f,.5f,.5f,1);
  146.     VertexPosCol v24(-1,-1,-1,.5f,.5f,.5f,1);
  147.  
  148.     m_VecVertices.push_back(v1);
  149.     m_VecVertices.push_back(v2);
  150.     m_VecVertices.push_back(v3);
  151.     m_VecVertices.push_back(v4);
  152.     m_VecVertices.push_back(v5);
  153.     m_VecVertices.push_back(v6);
  154.     m_VecVertices.push_back(v7);
  155.     m_VecVertices.push_back(v8);
  156.     m_VecVertices.push_back(v9);
  157.     m_VecVertices.push_back(v10);
  158.     m_VecVertices.push_back(v11);
  159.     m_VecVertices.push_back(v12);
  160.     m_VecVertices.push_back(v13);
  161.     m_VecVertices.push_back(v14);
  162.     m_VecVertices.push_back(v15);
  163.     m_VecVertices.push_back(v16);
  164.     m_VecVertices.push_back(v17);
  165.     m_VecVertices.push_back(v18);
  166.     m_VecVertices.push_back(v19);
  167.     m_VecVertices.push_back(v20);
  168.     m_VecVertices.push_back(v21);
  169.     m_VecVertices.push_back(v22);
  170.     m_VecVertices.push_back(v23);
  171.     m_VecVertices.push_back(v24);
  172.  
  173.     AddQuad(0,1,2,3);
  174.     AddQuad(4,5,6,7);
  175.     AddQuad(8,9,10,11);
  176.     AddQuad(12,13,14,15);
  177.     AddQuad(16,17,18,19);
  178.     AddQuad(20,21,22,23);
  179. }  
  180. void Cube::AddQuad(unsigned int a, unsigned int b, unsigned int c,unsigned int d)
  181. {
  182.     AddTriangle(a,b,c);
  183.     AddTriangle(a,c,d);
  184. }
  185. void Cube::AddTriangle (unsigned int a, unsigned int b, unsigned c)
  186. {
  187.     m_VecIndices.push_back(a);
  188.     m_VecIndices.push_back(b);
  189.     m_VecIndices.push_back(c);
  190. }
  191.  
  192.  
  193.  
  194. void Cube::BuildVertexBuffer()
  195. {
  196.     //TODO: fill a buffer description to copy the vertexdata into graphics memory
  197.     D3D10_BUFFER_DESC bd;
  198.     bd.Usage = D3D10_USAGE_IMMUTABLE;
  199.     bd.ByteWidth = sizeof(VertexPosCol)*m_VecVertices.size();
  200.     bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  201.     bd.CPUAccessFlags = 0;
  202.     bd.MiscFlags = 0;
  203.  
  204.     D3D10_SUBRESOURCE_DATA initData;
  205.     initData.pSysMem = m_VecVertices.data();
  206.     //create a ID3D10Buffer in graphics memory containing the vertex info
  207.     HR(m_pLevel->GetDevice()->CreateBuffer(&bd, &initData, &m_pVertexBuffer));
  208. }
  209. void Cube::BuildIndexBuffer()
  210. {//TODO: fill a buffer description to copy the indexdata into graphics memory
  211.     D3D10_BUFFER_DESC ibd;
  212.     ibd.Usage = D3D10_USAGE_IMMUTABLE;
  213.     ibd.ByteWidth = sizeof(UINT)*m_VecIndices.size();
  214.     ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
  215.     ibd.CPUAccessFlags = 0;
  216.     ibd.MiscFlags = 0;
  217.     D3D10_SUBRESOURCE_DATA initData;
  218.     initData.pSysMem = m_VecIndices.data();
  219.     HR(m_pLevel->GetDevice()->CreateBuffer(&ibd, &initData, &m_pIndexBuffer));
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement