void PhysXPhysics::VSyncVisibleScene() { for (ActorIdToPysXRigidBodyTable::const_iterator it = m_actorRigidBodyMap.begin(); it != m_actorRigidBodyMap.end(); it++) { ActorId const id = it->first; PxTransform pxLoc = it->second->getGlobalPose(); Mat4x4 loc; PxMatrixToMat4x4(PxMat44(pxLoc), &loc); Actor* pActor = g_pApp->m_pGame->VGetActor(id); if (pActor) { TransformComponent* pTransformComponent = pActor->GetComponent<TransformComponent>(TransformComponent::g_Name); if (pTransformComponent) { if (pTransformComponent->GetTransform() != loc) { Vec3 rot = loc.GetYawPitchRoll(); pTransformComponent->SetPosition(loc.GetPosition()); pTransformComponent->SetRotation(Vec3(XMConvertToDegrees(rot.x), XMConvertToDegrees(rot.y), XMConvertToDegrees(rot.z))); EventDataPtr pEvent(BE_NEW EvtData_Move_Actor(id, loc)); IEventManager::Get()->VQueueEvent(pEvent); } } } } }
PxMat44 Camera3D::getViewProjection() const { //This comes from the conjugate rotation because the world should appear to rotate //opposite to the camera's rotation. PxMat44 cameraRotation = PxMat44(getTransform().getTransformedRot().getConjugate()); PxMat44 cameraTranslation; //Similarly, the translation is inverted because the world appears to move opposite //to the camera's movement. cameraTranslation = Utility::initTranslation(getTransform().getTransformedPos() * -1); return m_projection * cameraRotation * cameraTranslation; }
//------------------------------------------------------------------------- // void ModelRendering::render(const GameObjectRef &iGameObject) { LPDIRECT3DDEVICE9 wD3dDevice = Game<IRenderManager>()->d3dDevice(); PxMat44 wMatrixTransfo(PxIdentity); IPoseInterfaceRef wGOPose = iGameObject->as<IPoseInterface>(); if (wGOPose) { wMatrixTransfo = PxMat44(wGOPose->pose()); } wD3dDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)&wMatrixTransfo); _model.render(); }
PhysXRigidBody::PhysXRigidBody( PxPhysics* PxDevice, PhysXMaterial* Material, const ERigidBodies Type, scene::SceneNode* RootNode, const SRigidBodyConstruction &Construct) : BaseObject ( ), PhysicsBaseObject ( ), RigidBody (Type, Construct), PhysXBaseObject (Material ), PxActor_ (0 ) { if (!PxDevice || !RootNode || !Material) throw io::stringc("Invalid arguments for rigid body"); /* Create dynamic rigid body */ PxBaseActor_ = PxActor_ = PxDevice->createRigidDynamic( PxTransform(PxMat44(RootNode->getTransformMatrix(true).getArray())) ); PxBaseActor_->userData = RootNode; if (!PxActor_) throw io::stringc("Could not create PhysX actor for rigid body"); /* Create base shape */ switch (Type) { case RIGIDBODY_BOX: createBox (Construct); break; case RIGIDBODY_SPHERE: createSphere (Construct); break; case RIGIDBODY_CAPSULE: createCapsule (Construct); break; default: throw io::stringc("Unsupported rigid body type"); } /* Initialize root node and actor */ setRootNode(RootNode); setMass(1.0f); }
PhysXStaticObject::PhysXStaticObject( PxPhysics* PxDevice, PxCooking* PxCookDevice, PhysXMaterial* Material, scene::Mesh* Mesh) : BaseObject ( ), PhysicsBaseObject ( ), StaticPhysicsObject (Mesh ), PhysXBaseObject (Material ), PxActor_ (0 ) { if (!PxDevice || !Mesh || !Material) throw io::stringc("Invalid arguments for static physics object"); /* Create dynamic rigid body */ PxBaseActor_ = PxActor_ = PxDevice->createRigidStatic( PxTransform(PxMat44(Mesh->getTransformMatrix(true).getArray())) //PxTransform(PxVec3(0.0f), PxQuat(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f))) ); if (!PxActor_) throw io::stringc("Could not create PhysX actor for static object"); /* Create base shape */ createMesh(PxDevice, PxCookDevice, Mesh); //addShape(PxPlaneGeometry()); }
void GLES2Renderer::bindMeshContext(const RendererMeshContext &context) { physx::PxMat44 model = physx::PxMat44::createIdentity(); physx::PxMat44 modelView; if(context.transform) model = *context.transform; GLfloat glmvp[16]; GLfloat glmodel[16]; GLfloat glview[16]; GLfloat glmodelview[16]; GLES2RendererMaterial::shaderProgram& program = g_hackCurrentMat->m_program[g_hackCurrentMat->m_currentPass]; // if we don't use MojoShader if (program.modelMatrixUniform != -1) { PxToGL(glmodel, model); glUniformMatrix4fv(program.modelMatrixUniform, 1, GL_FALSE, glmodel); } if (program.viewMatrixUniform != -1) { PxToGL(glview, m_viewMatrix); glUniformMatrix4fv(program.viewMatrixUniform, 1, GL_FALSE, glview); } if (program.projMatrixUniform != -1) { glUniformMatrix4fv(program.projMatrixUniform, 1, GL_FALSE, m_glProjectionMatrix); } if (program.modelViewMatrixUniform != -1) { modelView = m_viewMatrix * model; PxToGL(glmodelview, modelView); glUniformMatrix4fv(program.modelViewMatrixUniform, 1, GL_FALSE, glmodelview); } if (program.modelViewProjMatrixUniform != -1) { modelView = m_viewMatrix * model; physx::PxMat44 MVP = PxMat44(m_glProjectionMatrix).getTranspose(); MVP = MVP * modelView; PxToGLColumnMajor(glmvp, MVP); glUniformMatrix4fv(program.modelViewProjMatrixUniform, 1, GL_FALSE, glmvp); } // if we use MojoShader if(program.vertexMojoResult || program.fragmentMojoResult) { // model matrix { const RendererMaterial::Variable* var = g_hackCurrentMat->findVariable("g_modelMatrix", RendererMaterial::VARIABLE_FLOAT4x4); if(var) { if (context.shaderData) { memcpy(&glmodel, context.shaderData, 16 * sizeof(PxF32)); } else { PxToGL(glmodel, model); } g_hackCurrentMat->bindVariable(g_hackCurrentMat->m_currentPass, *var, glmodel); } } // model-view matrix { const RendererMaterial::Variable* var = g_hackCurrentMat->findVariable("g_modelViewMatrix", RendererMaterial::VARIABLE_FLOAT4x4); if(var) { modelView = m_viewMatrix * model; PxToGL(glmodelview, modelView); g_hackCurrentMat->bindVariable(g_hackCurrentMat->m_currentPass, *var, glmodelview); } } // model-view-project matrix { const RendererMaterial::Variable* var = g_hackCurrentMat->findVariable("g_MVP", RendererMaterial::VARIABLE_FLOAT4x4); if(var) { physx::PxMat44 MVP = physx::PxMat44(m_glProjectionMatrix); modelView = m_viewMatrix * model; PxToGL(glmodelview, modelView); physx::PxMat44 MV(glmodelview); MVP = MV * MVP; memcpy(glmvp, MVP.front(), sizeof(MVP)); g_hackCurrentMat->bindVariable(g_hackCurrentMat->m_currentPass, *var, glmvp); } } } g_hackCurrentMat->submitUniforms(); switch(context.cullMode) { case RendererMeshContext::CLOCKWISE: glFrontFace( GL_CW ); glCullFace( GL_BACK ); glEnable( GL_CULL_FACE ); break; case RendererMeshContext::COUNTER_CLOCKWISE: glFrontFace( GL_CCW ); glCullFace( GL_BACK ); glEnable( GL_CULL_FACE ); break; case RendererMeshContext::NONE: glDisable( GL_CULL_FACE ); break; default: RENDERER_ASSERT(0, "Invalid Cull Mode"); } RENDERER_ASSERT(context.numBones <= RENDERER_MAX_BONES, "Too many bones."); if(context.boneMatrices && context.numBones>0 && context.numBones <= RENDERER_MAX_BONES && program.boneMatricesUniform != -1) { GLfloat glbonematrix[16*RENDERER_MAX_BONES]; for(PxU32 i=0; i<context.numBones; i++) { PxToGL(glbonematrix+(16*i), context.boneMatrices[i]); } glUniformMatrix4fv(program.boneMatricesUniform, context.numBones, GL_FALSE, glbonematrix); } }
osg::Node* createNodeForActor( PxRigidActor* actor ) { if ( !actor ) return NULL; std::vector<PxShape*> shapes( actor->getNbShapes() ); osg::ref_ptr<osg::MatrixTransform> transform = new osg::MatrixTransform; transform->setMatrix( toMatrix(PxMat44(actor->getGlobalPose())) ); osg::ref_ptr<osg::Geode> geode = new osg::Geode; transform->addChild( geode.get() ); PxU32 num = actor->getShapes( &(shapes[0]), actor->getNbShapes() ); for ( PxU32 i=0; i<num; ++i ) { PxShape* shape = shapes[i]; osg::Matrix localMatrix = toMatrix( PxMat44(actor->getGlobalPose()) ); osg::Vec3 localPos = toVec3( shape->getLocalPose().p ); osg::Quat localQuat(shape->getLocalPose().q.x, shape->getLocalPose().q.y, shape->getLocalPose().q.z, shape->getLocalPose().q.w); switch ( shape->getGeometryType() ) { case PxGeometryType::eSPHERE: { PxSphereGeometry sphere; shape->getSphereGeometry( sphere ); osg::Sphere* sphereShape = new osg::Sphere(localPos, sphere.radius); geode->addDrawable( new osg::ShapeDrawable(sphereShape) ); } break; case PxGeometryType::ePLANE: // TODO break; case PxGeometryType::eCAPSULE: { PxCapsuleGeometry capsule; shape->getCapsuleGeometry( capsule ); osg::Capsule* capsuleShape = new osg::Capsule( localPos, capsule.radius, capsule.halfHeight * 2.0f); capsuleShape->setRotation( localQuat ); geode->addDrawable( new osg::ShapeDrawable(capsuleShape) ); } break; case PxGeometryType::eBOX: { PxBoxGeometry box; shape->getBoxGeometry( box ); osg::Box* boxShape = new osg::Box(localPos, box.halfExtents[0] * 2.0f, box.halfExtents[1] * 2.0f, box.halfExtents[2] * 2.0f); boxShape->setRotation( localQuat ); geode->addDrawable( new osg::ShapeDrawable(boxShape) ); } break; case PxGeometryType::eCONVEXMESH: { PxConvexMeshGeometry convexMeshGeom; shape->getConvexMeshGeometry( convexMeshGeom ); // TODO: consider convexMeshGeom.scale PxConvexMesh* convexMesh = convexMeshGeom.convexMesh; if ( convexMesh ) { /*for ( unsigned int i=0; i<convexMesh->getNbPolygons(); ++i ) { }*/ // TODO } } break; case PxGeometryType::eTRIANGLEMESH: { PxTriangleMeshGeometry triangleMeshGeom; shape->getTriangleMeshGeometry( triangleMeshGeom ); // TODO: consider triangleMeshGeom.scale PxTriangleMesh* triangleMesh = triangleMeshGeom.triangleMesh; if ( triangleMesh ) { osg::ref_ptr<osg::Vec3Array> va = new osg::Vec3Array( triangleMesh->getNbVertices() ); for ( unsigned int i=0; i<va->size(); ++i ) (*va)[i] = toVec3( *(triangleMesh->getVertices() + i) ) * localMatrix; osg::ref_ptr<osg::DrawElements> de; if ( triangleMesh->getTriangleMeshFlags()&PxTriangleMeshFlag::eHAS_16BIT_TRIANGLE_INDICES ) { osg::DrawElementsUShort* de16 = new osg::DrawElementsUShort(GL_TRIANGLES); de = de16; const PxU16* indices = (const PxU16*)triangleMesh->getTriangles(); for ( unsigned int i=0; i<triangleMesh->getNbTriangles(); ++i ) { de16->push_back( indices[3 * i + 0] ); de16->push_back( indices[3 * i + 1] ); de16->push_back( indices[3 * i + 2] ); } } else { osg::DrawElementsUInt* de32 = new osg::DrawElementsUInt(GL_TRIANGLES); de = de32; const PxU32* indices = (const PxU32*)triangleMesh->getTriangles(); for ( unsigned int i=0; i<triangleMesh->getNbTriangles(); ++i ) { de32->push_back( indices[3 * i + 0] ); de32->push_back( indices[3 * i + 1] ); de32->push_back( indices[3 * i + 2] ); } } geode->addDrawable( createGeometry(va.get(), NULL, NULL, de.get()) ); } } break; case PxGeometryType::eHEIGHTFIELD: { PxHeightFieldGeometry hfGeom; shape->getHeightFieldGeometry( hfGeom ); // TODO: consider hfGeom.*scale PxHeightField* heightField = hfGeom.heightField; if ( heightField ) { // TODO } } break; } } return transform.release(); }
PxMat44 toPhysicsMatrix( const osg::Matrix& matrix ) { PxReal d[16]; for ( int i=0; i<16; ++i ) d[i] = *(matrix.ptr() + i); return PxMat44(d); }
void JF::Component::ColisionBox::Reset() { // 1) auto* pTransform = GetOwner()->GetComponent<JF::Component::Transform>(); XMFLOAT4X4& rMatrix = pTransform->GetTransformMatrix(); assert(pTransform != nullptr); // 2) m_pShape = m_pPXDevice->GetPhysics()->createShape( PxBoxGeometry(m_Size.x, m_Size.y, m_Size.z), *m_pPXDevice->GetMaterial()); // 3) PxTransform localTm( PxMat44( PxVec4(rMatrix._11, rMatrix._12, rMatrix._13, rMatrix._14), PxVec4(rMatrix._21, rMatrix._22, rMatrix._23, rMatrix._24), PxVec4(rMatrix._31, rMatrix._32, rMatrix._33, rMatrix._34), PxVec4(rMatrix._41, rMatrix._42, rMatrix._43, rMatrix._44) )); // 4) m_pRigidBody = m_pPXDevice->GetPhysics()->createRigidDynamic(localTm); m_pRigidBody->attachShape(*m_pShape); PxRigidBodyExt::updateMassAndInertia(*m_pRigidBody, 10.0f); m_pRigidBody->setLinearVelocity(PxVec3(0, 10, 2.0f)); m_pPXDevice->GetScene()->addActor(*m_pRigidBody); // 5) float cX = 0.0f; float cY = 0.0f; float cZ = 0.0f; float eX = 1.0f; float eY = 1.0f; float eZ = 1.0f; // 정점 JF::Vertex::PosColor vertices[] = { // 1, 1, 1 (0) { XMFLOAT3(cX + eX, cY + eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // 1, 1, -1 (1) { XMFLOAT3(cX + eX, cY + eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // -1, 1, 1 (2) { XMFLOAT3(cX + -eX, cY + eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // -1, 1, -1 (3) { XMFLOAT3(cX + -eX, cY + eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // 1, -1, 1 (4) { XMFLOAT3(cX + eX, cY + -eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // 1, -1, -1 (5) { XMFLOAT3(cX + eX, cY + -eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // -1, -1, 1 (6) { XMFLOAT3(cX + -eX, cY + -eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // -1, -1, -1 (7) { XMFLOAT3(cX + -eX, cY + -eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) } }; m_VertexSize = ARRAYSIZE(vertices); // 인덱스 UINT indices[] = { // Top 0,1, 1,3, 3,2, 0,2, // Bottom 4,5, 5,7, 7,6, 6,4, // Top - Bottom 0,4, 1,5, 2,6, 3,7 }; m_indexCnt = ARRAYSIZE(indices); // Create gRENDERER->CreateVertexBuffer(&vertices[0], sizeof(JF::Vertex::PosColor) * m_VertexSize, &m_VertBuff); gRENDERER->CreateIndexBuffer(&indices[0], sizeof(UINT) * m_indexCnt, &m_IndexBuff); }
PxMat44 PhysicsTransform::getMatrix() const { return PxMat44(m_actor->getGlobalPose()); }
PxMat44 Camera3D::getView() const { return m_projection * PxMat44(getTransform().getTransformedRot().getConjugate()); }