Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.08 KB | None | 0 0
  1. #include "CubePhysX.h"
  2. #include "PhysX.h"
  3. #include "InputMesh.h"
  4. #include "CookingStream.h"
  5. #include "Level.h"
  6. #include "GraphCamera.h"
  7. #include "math.h"
  8. #include "StdAfx.h"
  9. #include "Box.h"
  10.  
  11. Box::Box(tstring Soort,PhysX* physx,D3DXVECTOR3 pos,float width,float height,float depth,float UScale, float VScale,int bodyType):
  12. m_Depth(depth),
  13. m_Height(height),
  14. m_Width(width),
  15. m_pPhysX(physx),
  16. m_pActor(0),
  17. m_pVertexLayout(0),
  18. m_pVertexBuffer(0),
  19. m_pIndexBuffer(0),
  20. m_pTextureRV(0),
  21. m_pDefaultEffect(0),
  22. m_pDefaultTechnique(0),
  23. m_pWorldViewProjectionVariable(NULL),
  24. m_pWorldVariable(0),
  25. m_pDiffuseMapVariabele(0),
  26. m_VertexBufferStride(0),
  27. m_TextureName(_T("")),
  28. m_NumIndices(0),
  29. m_NumSlices(m_NumSlices),
  30. m_NumStacks(m_NumStacks),
  31. m_UScale(UScale),
  32. m_VScale(VScale)
  33. {
  34.     Checksoort(Soort);
  35.     SetBodyType(bodyType);
  36.     Translate(pos);
  37. }
  38. Box::~Box(void)
  39. {
  40.     SAFE_RELEASE(m_pVertexBuffer);
  41.     SAFE_RELEASE(m_pVertexLayout);
  42.     SAFE_RELEASE(m_pIndexBuffer);
  43. }
  44. void Box::Checksoort(tstring kind)
  45. {
  46.     if(kind == _T("normal")){m_TextureName = _T("Crate/Textures/normal.jpg");}
  47.     if(kind == _T("life")){m_TextureName = _T("Crate/Textures/lifecrate.jpg");}
  48.     if(kind == _T("multi")){m_TextureName = _T("Crate/Textures/multicrate.jpg");}
  49.     if(kind == _T("tnt")){m_TextureName = _T("Crate/Textures/tnt.jpg");}
  50.     if(kind == _T("nitro")){m_TextureName = _T("Crate/Textures/nitro.jpg");}
  51.     if(kind == _T("checkpoint")){m_TextureName = _T("Crate/Textures/checkpoint.jpg");}
  52.     if(kind == _T("akuaku")){m_TextureName = _T("Crate/Textures/akuakucrate.jpg");}
  53.     if(kind == _T("question")){m_TextureName = _T("Crate/Textures/questioncrate.jpg");}
  54.     if(kind == _T("steel")){m_TextureName = _T("Crate/Textures/steelcrate.jpg");}
  55.  
  56. }
  57. void Box::Initialize(ContentManager *pContentManager)
  58. {
  59.     CreateEffect(pContentManager);
  60.     m_pTextureRV = pContentManager->GetTexture(m_pLevel->GetDevice(), m_TextureName);
  61.     DefineInputLayout();
  62.     FillVertexAndIndexBuffer();
  63. }
  64. void Box::FillVertexAndIndexBuffer()
  65. {
  66.     vector<VertexPosNormTex> vertices;
  67.     //vector<DWORD> indices;
  68.     BuildStacks(vertices,m_VecIndices);
  69.  
  70.     D3D10_BUFFER_DESC vbd;
  71.     vbd.Usage=D3D10_USAGE_IMMUTABLE;
  72.     vbd.ByteWidth=sizeof(VertexPosNormTex) * (UINT)vertices.size();
  73.     vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  74.     vbd.CPUAccessFlags = 0;
  75.     vbd.MiscFlags = 0;
  76.  
  77.     D3D10_SUBRESOURCE_DATA vinitData;
  78.     vinitData.pSysMem = &vertices[0];
  79.     HR(m_pLevel->GetDevice()->CreateBuffer(&vbd,&vinitData,&m_pVertexBuffer));
  80.  
  81.     D3D10_BUFFER_DESC  ibd;
  82.     ibd.Usage= D3D10_USAGE_IMMUTABLE;
  83.     ibd.ByteWidth = sizeof(DWORD) * m_VecIndices.size();
  84.     ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
  85.     ibd.CPUAccessFlags = 0;
  86.     ibd.MiscFlags = 0;
  87.  
  88.     D3D10_SUBRESOURCE_DATA initData;
  89.     initData.pSysMem = &m_VecIndices[0];
  90.     HR(m_pLevel->GetDevice()->CreateBuffer(&ibd,&initData,&m_pIndexBuffer));
  91.  
  92.     m_NumIndices = (UINT)m_VecIndices.size();
  93. }
  94. void Box::Draw(const RenderContext* pRenderContext)
  95. {
  96.     if(!m_pDefaultTechnique)
  97.     {
  98.         MessageBox(0,_T("GENNEN TECHNIEK KEREL!"),_T("ERROR"),0);
  99.         return;
  100.     }
  101.     if(!m_pVertexBuffer || !m_pIndexBuffer)
  102.     {
  103.         MessageBox(0,_T("No Vertices KEREL"),_T("ERROR"),0);
  104.         return;
  105.     }
  106.  
  107.     D3DXMATRIX matView = pRenderContext->GetCamera()->GetView();
  108.     D3DXMATRIX matProj = pRenderContext->GetCamera()->GetProj();
  109.     m_pWorldViewProjectionVariable->SetMatrix((float*)&(m_World * matView * matProj));
  110.  
  111.     m_pDiffuseMapVariabele->SetResource(m_pTextureRV);
  112.  
  113.     m_pLevel->GetDevice()->IASetInputLayout(m_pVertexLayout);
  114.  
  115.     UINT offset = 0;
  116.     m_pLevel->GetDevice()->IASetVertexBuffers(0,1,&m_pVertexBuffer,&m_VertexBufferStride, &offset );
  117.     m_pLevel->GetDevice()->IASetIndexBuffer(m_pIndexBuffer,DXGI_FORMAT_R32_UINT,0);
  118.     m_pLevel->GetDevice()->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  119.  
  120.     D3D10_TECHNIQUE_DESC techDesc;
  121.     m_pDefaultTechnique->GetDesc(&techDesc);
  122.     for(UINT p = 0;p < techDesc.Passes;++p)
  123.     {
  124.         m_pDefaultTechnique->GetPassByIndex(p)->Apply(0);
  125.         m_pLevel->GetDevice()->DrawIndexed(m_NumIndices,0,0);
  126.     }
  127. }
  128. void Box::BuildStacks(vector<VertexPosNormTex>& vertices, vector<DWORD>& indices)
  129. {
  130.     float afmeting = 0.5;
  131.  
  132.     VertexPosNormTex v1(-afmeting,-afmeting,-afmeting,0,0,-1,0,1);
  133.     VertexPosNormTex v2(-afmeting,afmeting,-afmeting,0,0,-1,0,0);
  134.     VertexPosNormTex v3(afmeting,afmeting,-afmeting,0,0,-1,1,0);
  135.     VertexPosNormTex v4(afmeting,-afmeting,-afmeting,0,0,-1,1,1);
  136.  
  137.     VertexPosNormTex v5(afmeting,-afmeting,-afmeting,1,0,0,0,1);
  138.     VertexPosNormTex v6(afmeting,afmeting,-afmeting,1,0,0,0,0);
  139.     VertexPosNormTex v7(afmeting,afmeting,afmeting,1,0,0,1,0);
  140.     VertexPosNormTex v8(afmeting,-afmeting,afmeting,1,0,0,1,1);
  141.  
  142.     VertexPosNormTex v9(afmeting,-afmeting,afmeting,0,0,1,0,1);
  143.     VertexPosNormTex v10(afmeting,afmeting,afmeting,0,0,1,0,0);
  144.     VertexPosNormTex v11(-afmeting,afmeting,afmeting,0,0,1,1,0);
  145.     VertexPosNormTex v12(-afmeting,-afmeting,afmeting,0,0,1,1,1);
  146.  
  147.     VertexPosNormTex v13(-afmeting,-afmeting,afmeting,-1,0,0,0,1);
  148.     VertexPosNormTex v14(-afmeting,afmeting,afmeting,-1,0,0,0,0);
  149.     VertexPosNormTex v15(-afmeting,afmeting,-afmeting,-1,0,0,1,0);
  150.     VertexPosNormTex v16(-afmeting,-afmeting,-afmeting,-1,0,0,1,1);
  151.  
  152.     VertexPosNormTex v17(-afmeting,afmeting,-afmeting,0,1,0,0,1);
  153.     VertexPosNormTex v18(-afmeting,afmeting,afmeting,0,1,0,0,0);
  154.     VertexPosNormTex v19(afmeting,afmeting,afmeting,0,1,0,1,0);
  155.     VertexPosNormTex v20(afmeting,afmeting,-afmeting,0,1,0,1,1);
  156.  
  157.     VertexPosNormTex v21(afmeting,-afmeting,-afmeting,0,-1,0,0,1);
  158.     VertexPosNormTex v22(afmeting,-afmeting,afmeting,0,-1,0,0,0);
  159.     VertexPosNormTex v23(-afmeting,-afmeting,afmeting,0,-1,0,1,0);
  160.     VertexPosNormTex v24(-afmeting,-afmeting,-afmeting,0,-1,0,1,1);
  161.  
  162.     vertices.push_back(v1);
  163.     vertices.push_back(v2);
  164.     vertices.push_back(v3);
  165.     vertices.push_back(v4);
  166.     vertices.push_back(v5);
  167.     vertices.push_back(v6);
  168.     vertices.push_back(v7);
  169.     vertices.push_back(v8);
  170.     vertices.push_back(v9);
  171.     vertices.push_back(v10);
  172.     vertices.push_back(v11);
  173.     vertices.push_back(v12);
  174.     vertices.push_back(v13);
  175.     vertices.push_back(v14);
  176.     vertices.push_back(v15);
  177.     vertices.push_back(v16);
  178.     vertices.push_back(v17);
  179.     vertices.push_back(v18);
  180.     vertices.push_back(v19);
  181.     vertices.push_back(v20);
  182.     vertices.push_back(v21);
  183.     vertices.push_back(v22);
  184.     vertices.push_back(v23);
  185.     vertices.push_back(v24);
  186.  
  187.     AddQuad(0,1,2,3);
  188.     AddQuad(4,5,6,7);
  189.     AddQuad(8,9,10,11);
  190.     AddQuad(12,13,14,15);
  191.     AddQuad(16,17,18,19);
  192.     AddQuad(20,21,22,23);
  193. }
  194. void Box::AddQuad(unsigned int a, unsigned int b, unsigned int c,unsigned int d)
  195. {
  196.     AddTriangle(a,b,c);
  197.     AddTriangle(a,c,d);
  198. }
  199. void Box::AddTriangle (unsigned int a, unsigned int b, unsigned c)
  200. {
  201.     m_VecIndices.push_back(a);
  202.     m_VecIndices.push_back(b);
  203.     m_VecIndices.push_back(c);
  204. }
  205. void Box::Translate(const D3DXVECTOR3 pos)
  206. {
  207.     LevelElement::Translate(pos);
  208.     NxMat34 mat;
  209.     mat.setColumnMajor44(this->GetWorldMatrix());
  210.     if(m_pActor !=0) m_pActor->setGlobalPose(mat);
  211.  
  212. }
  213. void Box::DefineInputLayout()
  214. {
  215.     D3D10_INPUT_ELEMENT_DESC layout[] =
  216.     {
  217.         { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },  
  218.         { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
  219.         { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }
  220.     };
  221.  
  222.     UINT numElements = sizeof(layout)/sizeof(layout[0]);
  223.  
  224.     D3D10_PASS_DESC PassDesc;
  225.  
  226.     HR(m_pDefaultTechnique->GetPassByIndex(0)->GetDesc(&PassDesc));
  227.     HR(m_pLevel->GetDevice()->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &m_pVertexLayout ));
  228.  
  229.     m_VertexBufferStride = sizeof(VertexPosNormTex);
  230.  
  231. }
  232. void Box::CreateEffect(ContentManager * pContentManager)
  233. {
  234.     m_pDefaultEffect = pContentManager->GetEffect(m_pLevel->GetDevice(),_T("./Effect/PNTa.fx"));
  235.  
  236.     m_pDefaultTechnique  = m_pDefaultEffect->GetTechniqueByIndex(0);
  237.  
  238.     GetEffectVariables(m_pDefaultEffect);
  239.  
  240. }
  241. void Box::SetEffect(ID3D10Effect* effect)
  242. {
  243.     m_pDefaultEffect = effect;
  244.  
  245.     m_pDefaultTechnique = m_pDefaultEffect->GetTechniqueByIndex(0);
  246.  
  247.     GetEffectVariables(m_pDefaultEffect);
  248. }
  249. void Box::GetEffectVariables(ID3D10Effect* pEffect)
  250. {
  251.     //get effect variables
  252.     m_pWorldViewProjectionVariable = pEffect->GetVariableByName("gWVP")->AsMatrix();
  253.     if(!m_pWorldViewProjectionVariable->IsValid())MessageBox(0,_T("Getting EffectSemantic WorldViewProjection Failed"),_T("ERROR"),0);
  254.  
  255.     m_pWorldVariable = pEffect->GetVariableByName("gWorld")->AsMatrix();
  256.     if(!m_pWorldVariable->IsValid())MessageBox(0,_T("Getting EffectSemantic World Failed"),_T("ERROR"),0);
  257.    
  258.     m_pDiffuseMapVariabele = pEffect->GetVariableByName( "gDiffuseMap" )->AsShaderResource();
  259.     if(!m_pDiffuseMapVariabele->IsValid())MessageBox(0,_T("Getting EffectSemantic DiffuseMap Failed"),_T("ERROR"),0);
  260. }
  261. void Box::Tick(const InputState& refInputState)
  262. {
  263.     if(m_pActor !=0)
  264.         m_pActor->getGlobalPose().getColumnMajor44(m_World);
  265. }
  266. void Box::SetBodyType(int bodyType)
  267. {
  268.     switch(bodyType)
  269.     {
  270.     case RIGID:
  271.         InitActor(bodyType);
  272.         break;
  273.     case STATIC:
  274.         InitActor(bodyType);
  275.         break;
  276.     case GHOST:
  277.         InitActor(bodyType);
  278.         break;
  279.     default:
  280.         ;
  281.     }
  282. }
  283. void Box::InitActor(int BodyType)
  284. {
  285.     double afmeting = 0.5;
  286.     if(m_pActor) m_pPhysX->GetScene()->releaseActor(*m_pActor);
  287.     NxActorDesc ActorDesc;
  288.  
  289.     NxBoxShapeDesc BoxShapeDesc;
  290.     BoxShapeDesc.dimensions=NxVec3(afmeting,afmeting,afmeting);
  291.     BoxShapeDesc.mass=afmeting*afmeting*afmeting;
  292.     ActorDesc.shapes.push_back(&BoxShapeDesc);
  293.  
  294.     if ( BodyType == GHOST ){
  295.         BoxShapeDesc.shapeFlags |= NX_TRIGGER_ENABLE;
  296.     }else{
  297.         if(BodyType==RIGID)
  298.         {
  299.        
  300.             NxBodyDesc bodyDesc;
  301.             bodyDesc.angularDamping = 0.9f;
  302.             bodyDesc.linearVelocity = NxVec3(0,0,0);
  303.             ActorDesc.body = &bodyDesc;
  304.         }
  305.         else ActorDesc.body =0;
  306.    
  307.         ActorDesc.density = 10.0f;
  308.     }
  309.  
  310.     NxMat34 mat;
  311.     mat.setColumnMajor44(m_World);
  312.     ActorDesc.globalPose = mat;
  313.     ActorDesc.density = 10.0f;
  314.     m_pActor = m_pPhysX->GetScene()->createActor(ActorDesc);
  315.    
  316. }
  317. void Box::TriggerNormal()
  318. {
  319. }
  320. void Box::TriggerMulti()
  321. {
  322. }
  323. void Box::TriggerLife()
  324. {
  325. }
  326. void Box::TriggerNitro()
  327. {
  328. }
  329. void Box::TriggerTNT()
  330. {
  331. }
  332. void Box::TriggerCheckpoint()
  333. {
  334. }
  335. void Box::TriggerAkuAku()
  336. {
  337. }
  338. void Box::TriggerSteel()
  339. {
  340. }
  341. void Box::TriggerQuestion()
  342. {
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement