Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CubePhysX.h"
- #include "PhysX.h"
- #include "InputMesh.h"
- #include "CookingStream.h"
- #include "Level.h"
- #include "GraphCamera.h"
- #include "math.h"
- #include "StdAfx.h"
- #include "Box.h"
- Box::Box(tstring Soort,PhysX* physx,D3DXVECTOR3 pos,float width,float height,float depth,float UScale, float VScale,int bodyType):
- m_Depth(depth),
- m_Height(height),
- m_Width(width),
- m_pPhysX(physx),
- m_pActor(0),
- m_pVertexLayout(0),
- m_pVertexBuffer(0),
- m_pIndexBuffer(0),
- m_pTextureRV(0),
- m_pDefaultEffect(0),
- m_pDefaultTechnique(0),
- m_pWorldViewProjectionVariable(NULL),
- m_pWorldVariable(0),
- m_pDiffuseMapVariabele(0),
- m_VertexBufferStride(0),
- m_TextureName(_T("")),
- m_NumIndices(0),
- m_NumSlices(m_NumSlices),
- m_NumStacks(m_NumStacks),
- m_UScale(UScale),
- m_VScale(VScale)
- {
- Checksoort(Soort);
- SetBodyType(bodyType);
- Translate(pos);
- }
- Box::~Box(void)
- {
- SAFE_RELEASE(m_pVertexBuffer);
- SAFE_RELEASE(m_pVertexLayout);
- SAFE_RELEASE(m_pIndexBuffer);
- }
- void Box::Checksoort(tstring kind)
- {
- if(kind == _T("normal")){m_TextureName = _T("Crate/Textures/normal.jpg");}
- if(kind == _T("life")){m_TextureName = _T("Crate/Textures/lifecrate.jpg");}
- if(kind == _T("multi")){m_TextureName = _T("Crate/Textures/multicrate.jpg");}
- if(kind == _T("tnt")){m_TextureName = _T("Crate/Textures/tnt.jpg");}
- if(kind == _T("nitro")){m_TextureName = _T("Crate/Textures/nitro.jpg");}
- if(kind == _T("checkpoint")){m_TextureName = _T("Crate/Textures/checkpoint.jpg");}
- if(kind == _T("akuaku")){m_TextureName = _T("Crate/Textures/akuakucrate.jpg");}
- if(kind == _T("question")){m_TextureName = _T("Crate/Textures/questioncrate.jpg");}
- if(kind == _T("steel")){m_TextureName = _T("Crate/Textures/steelcrate.jpg");}
- }
- void Box::Initialize(ContentManager *pContentManager)
- {
- CreateEffect(pContentManager);
- m_pTextureRV = pContentManager->GetTexture(m_pLevel->GetDevice(), m_TextureName);
- DefineInputLayout();
- FillVertexAndIndexBuffer();
- }
- void Box::FillVertexAndIndexBuffer()
- {
- vector<VertexPosNormTex> vertices;
- //vector<DWORD> indices;
- BuildStacks(vertices,m_VecIndices);
- D3D10_BUFFER_DESC vbd;
- vbd.Usage=D3D10_USAGE_IMMUTABLE;
- vbd.ByteWidth=sizeof(VertexPosNormTex) * (UINT)vertices.size();
- vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
- vbd.CPUAccessFlags = 0;
- vbd.MiscFlags = 0;
- D3D10_SUBRESOURCE_DATA vinitData;
- vinitData.pSysMem = &vertices[0];
- HR(m_pLevel->GetDevice()->CreateBuffer(&vbd,&vinitData,&m_pVertexBuffer));
- D3D10_BUFFER_DESC ibd;
- ibd.Usage= D3D10_USAGE_IMMUTABLE;
- ibd.ByteWidth = sizeof(DWORD) * m_VecIndices.size();
- ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
- ibd.CPUAccessFlags = 0;
- ibd.MiscFlags = 0;
- D3D10_SUBRESOURCE_DATA initData;
- initData.pSysMem = &m_VecIndices[0];
- HR(m_pLevel->GetDevice()->CreateBuffer(&ibd,&initData,&m_pIndexBuffer));
- m_NumIndices = (UINT)m_VecIndices.size();
- }
- void Box::Draw(const RenderContext* pRenderContext)
- {
- if(!m_pDefaultTechnique)
- {
- MessageBox(0,_T("GENNEN TECHNIEK KEREL!"),_T("ERROR"),0);
- return;
- }
- if(!m_pVertexBuffer || !m_pIndexBuffer)
- {
- MessageBox(0,_T("No Vertices KEREL"),_T("ERROR"),0);
- return;
- }
- D3DXMATRIX matView = pRenderContext->GetCamera()->GetView();
- D3DXMATRIX matProj = pRenderContext->GetCamera()->GetProj();
- m_pWorldViewProjectionVariable->SetMatrix((float*)&(m_World * matView * matProj));
- m_pDiffuseMapVariabele->SetResource(m_pTextureRV);
- m_pLevel->GetDevice()->IASetInputLayout(m_pVertexLayout);
- UINT offset = 0;
- m_pLevel->GetDevice()->IASetVertexBuffers(0,1,&m_pVertexBuffer,&m_VertexBufferStride, &offset );
- m_pLevel->GetDevice()->IASetIndexBuffer(m_pIndexBuffer,DXGI_FORMAT_R32_UINT,0);
- 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_NumIndices,0,0);
- }
- }
- void Box::BuildStacks(vector<VertexPosNormTex>& vertices, vector<DWORD>& indices)
- {
- float afmeting = 0.5;
- VertexPosNormTex v1(-afmeting,-afmeting,-afmeting,0,0,-1,0,1);
- VertexPosNormTex v2(-afmeting,afmeting,-afmeting,0,0,-1,0,0);
- VertexPosNormTex v3(afmeting,afmeting,-afmeting,0,0,-1,1,0);
- VertexPosNormTex v4(afmeting,-afmeting,-afmeting,0,0,-1,1,1);
- VertexPosNormTex v5(afmeting,-afmeting,-afmeting,1,0,0,0,1);
- VertexPosNormTex v6(afmeting,afmeting,-afmeting,1,0,0,0,0);
- VertexPosNormTex v7(afmeting,afmeting,afmeting,1,0,0,1,0);
- VertexPosNormTex v8(afmeting,-afmeting,afmeting,1,0,0,1,1);
- VertexPosNormTex v9(afmeting,-afmeting,afmeting,0,0,1,0,1);
- VertexPosNormTex v10(afmeting,afmeting,afmeting,0,0,1,0,0);
- VertexPosNormTex v11(-afmeting,afmeting,afmeting,0,0,1,1,0);
- VertexPosNormTex v12(-afmeting,-afmeting,afmeting,0,0,1,1,1);
- VertexPosNormTex v13(-afmeting,-afmeting,afmeting,-1,0,0,0,1);
- VertexPosNormTex v14(-afmeting,afmeting,afmeting,-1,0,0,0,0);
- VertexPosNormTex v15(-afmeting,afmeting,-afmeting,-1,0,0,1,0);
- VertexPosNormTex v16(-afmeting,-afmeting,-afmeting,-1,0,0,1,1);
- VertexPosNormTex v17(-afmeting,afmeting,-afmeting,0,1,0,0,1);
- VertexPosNormTex v18(-afmeting,afmeting,afmeting,0,1,0,0,0);
- VertexPosNormTex v19(afmeting,afmeting,afmeting,0,1,0,1,0);
- VertexPosNormTex v20(afmeting,afmeting,-afmeting,0,1,0,1,1);
- VertexPosNormTex v21(afmeting,-afmeting,-afmeting,0,-1,0,0,1);
- VertexPosNormTex v22(afmeting,-afmeting,afmeting,0,-1,0,0,0);
- VertexPosNormTex v23(-afmeting,-afmeting,afmeting,0,-1,0,1,0);
- VertexPosNormTex v24(-afmeting,-afmeting,-afmeting,0,-1,0,1,1);
- vertices.push_back(v1);
- vertices.push_back(v2);
- vertices.push_back(v3);
- vertices.push_back(v4);
- vertices.push_back(v5);
- vertices.push_back(v6);
- vertices.push_back(v7);
- vertices.push_back(v8);
- vertices.push_back(v9);
- vertices.push_back(v10);
- vertices.push_back(v11);
- vertices.push_back(v12);
- vertices.push_back(v13);
- vertices.push_back(v14);
- vertices.push_back(v15);
- vertices.push_back(v16);
- vertices.push_back(v17);
- vertices.push_back(v18);
- vertices.push_back(v19);
- vertices.push_back(v20);
- vertices.push_back(v21);
- vertices.push_back(v22);
- vertices.push_back(v23);
- vertices.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 Box::AddQuad(unsigned int a, unsigned int b, unsigned int c,unsigned int d)
- {
- AddTriangle(a,b,c);
- AddTriangle(a,c,d);
- }
- void Box::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 Box::Translate(const D3DXVECTOR3 pos)
- {
- LevelElement::Translate(pos);
- NxMat34 mat;
- mat.setColumnMajor44(this->GetWorldMatrix());
- if(m_pActor !=0) m_pActor->setGlobalPose(mat);
- }
- void Box::DefineInputLayout()
- {
- D3D10_INPUT_ELEMENT_DESC layout[] =
- {
- { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
- { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
- { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }
- };
- UINT numElements = sizeof(layout)/sizeof(layout[0]);
- D3D10_PASS_DESC PassDesc;
- HR(m_pDefaultTechnique->GetPassByIndex(0)->GetDesc(&PassDesc));
- HR(m_pLevel->GetDevice()->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &m_pVertexLayout ));
- m_VertexBufferStride = sizeof(VertexPosNormTex);
- }
- void Box::CreateEffect(ContentManager * pContentManager)
- {
- m_pDefaultEffect = pContentManager->GetEffect(m_pLevel->GetDevice(),_T("./Effect/PNTa.fx"));
- m_pDefaultTechnique = m_pDefaultEffect->GetTechniqueByIndex(0);
- GetEffectVariables(m_pDefaultEffect);
- }
- void Box::SetEffect(ID3D10Effect* effect)
- {
- m_pDefaultEffect = effect;
- m_pDefaultTechnique = m_pDefaultEffect->GetTechniqueByIndex(0);
- GetEffectVariables(m_pDefaultEffect);
- }
- void Box::GetEffectVariables(ID3D10Effect* pEffect)
- {
- //get effect variables
- m_pWorldViewProjectionVariable = pEffect->GetVariableByName("gWVP")->AsMatrix();
- if(!m_pWorldViewProjectionVariable->IsValid())MessageBox(0,_T("Getting EffectSemantic WorldViewProjection Failed"),_T("ERROR"),0);
- m_pWorldVariable = pEffect->GetVariableByName("gWorld")->AsMatrix();
- if(!m_pWorldVariable->IsValid())MessageBox(0,_T("Getting EffectSemantic World Failed"),_T("ERROR"),0);
- m_pDiffuseMapVariabele = pEffect->GetVariableByName( "gDiffuseMap" )->AsShaderResource();
- if(!m_pDiffuseMapVariabele->IsValid())MessageBox(0,_T("Getting EffectSemantic DiffuseMap Failed"),_T("ERROR"),0);
- }
- void Box::Tick(const InputState& refInputState)
- {
- if(m_pActor !=0)
- m_pActor->getGlobalPose().getColumnMajor44(m_World);
- }
- void Box::SetBodyType(int bodyType)
- {
- switch(bodyType)
- {
- case RIGID:
- InitActor(bodyType);
- break;
- case STATIC:
- InitActor(bodyType);
- break;
- case GHOST:
- InitActor(bodyType);
- break;
- default:
- ;
- }
- }
- void Box::InitActor(int BodyType)
- {
- double afmeting = 0.5;
- if(m_pActor) m_pPhysX->GetScene()->releaseActor(*m_pActor);
- NxActorDesc ActorDesc;
- NxBoxShapeDesc BoxShapeDesc;
- BoxShapeDesc.dimensions=NxVec3(afmeting,afmeting,afmeting);
- BoxShapeDesc.mass=afmeting*afmeting*afmeting;
- ActorDesc.shapes.push_back(&BoxShapeDesc);
- if ( BodyType == GHOST ){
- BoxShapeDesc.shapeFlags |= NX_TRIGGER_ENABLE;
- }else{
- if(BodyType==RIGID)
- {
- NxBodyDesc bodyDesc;
- bodyDesc.angularDamping = 0.9f;
- bodyDesc.linearVelocity = NxVec3(0,0,0);
- ActorDesc.body = &bodyDesc;
- }
- else ActorDesc.body =0;
- ActorDesc.density = 10.0f;
- }
- NxMat34 mat;
- mat.setColumnMajor44(m_World);
- ActorDesc.globalPose = mat;
- ActorDesc.density = 10.0f;
- m_pActor = m_pPhysX->GetScene()->createActor(ActorDesc);
- }
- void Box::TriggerNormal()
- {
- }
- void Box::TriggerMulti()
- {
- }
- void Box::TriggerLife()
- {
- }
- void Box::TriggerNitro()
- {
- }
- void Box::TriggerTNT()
- {
- }
- void Box::TriggerCheckpoint()
- {
- }
- void Box::TriggerAkuAku()
- {
- }
- void Box::TriggerSteel()
- {
- }
- void Box::TriggerQuestion()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement