diff --git a/NothinFancy/src/Gamestate.cpp b/NothinFancy/src/Gamestate.cpp index 91b2b27..c35e552 100644 --- a/NothinFancy/src/Gamestate.cpp +++ b/NothinFancy/src/Gamestate.cpp @@ -39,15 +39,15 @@ namespace nf { } void Gamestate::onEnter() { - + Error("Gamestate has no overridden onEnter function!"); } void Gamestate::update(float deltaTime) { - + Error("Gamestate has no overridden update function!"); } void Gamestate::render(Renderer& renderer) { - + Error("Gamestate has no overridden render function!"); } Camera* Gamestate::getCamera() { @@ -63,7 +63,7 @@ namespace nf { } void Gamestate::onExit() { - + Error("Gamestate has no overridden onExit function!"); } void Gamestate::stop() { diff --git a/NothinFancy/src/NFObject/Entity.cpp b/NothinFancy/src/NFObject/Entity.cpp index 33d2a1a..5128554 100644 --- a/NothinFancy/src/NFObject/Entity.cpp +++ b/NothinFancy/src/NFObject/Entity.cpp @@ -34,10 +34,12 @@ namespace nf { m_model = model->loadedModel; } else { - bool physics = false; - if (m_type != Entity::Type::DETAIL) - physics = true; - m_model = new Model(model, physics); + bool convex = false, triangle = false; + if (m_type == Entity::Type::STATIC || m_type == Entity::Type::DYNAMIC) + convex = true; + else if (m_type == Entity::Type::ENVIRONMENT) + triangle = true; + m_model = new Model(model, convex, triangle); model->alreadyLoaded = true; model->loadedModel = m_model; } diff --git a/NothinFancy/src/PhysicsEngine.cpp b/NothinFancy/src/PhysicsEngine.cpp index e91667d..6a7a7c5 100644 --- a/NothinFancy/src/PhysicsEngine.cpp +++ b/NothinFancy/src/PhysicsEngine.cpp @@ -24,6 +24,7 @@ namespace nf { m_err(nullptr), m_foundation(nullptr), m_pvd(nullptr), + m_transport(nullptr), m_phy(nullptr), m_cooking(nullptr), m_dispacher(nullptr), @@ -83,7 +84,7 @@ namespace nf { m_scene->setGravity(PxVec3(grav.x, grav.y, grav.z)); unsigned int count = m_scene->getNbActors(PxActorTypeFlag::eRIGID_DYNAMIC); - PxActor** actors = new PxActor * [count]; + PxActor** actors = new PxActor*[count]; m_scene->getActors(PxActorTypeFlag::eRIGID_DYNAMIC, actors, count); for (unsigned int i = 0; i < count; i++) { @@ -96,7 +97,7 @@ namespace nf { void PhysicsEngine::setActorVelocity(Entity* ent, const Vec3& vel) { unsigned int count = m_scene->getNbActors(PxActorTypeFlag::eRIGID_DYNAMIC); - PxActor** actors = new PxActor * [count]; + PxActor** actors = new PxActor*[count]; m_scene->getActors(PxActorTypeFlag::eRIGID_DYNAMIC, actors, count); for (unsigned int i = 0; i < count; i++) { @@ -111,7 +112,7 @@ namespace nf { void PhysicsEngine::setActorMass(Entity* ent, float mass) { unsigned int count = m_scene->getNbActors(PxActorTypeFlag::eRIGID_DYNAMIC); - PxActor** actors = new PxActor * [count]; + PxActor** actors = new PxActor*[count]; m_scene->getActors(PxActorTypeFlag::eRIGID_DYNAMIC, actors, count); for (unsigned int i = 0; i < count; i++) { @@ -148,9 +149,8 @@ namespace nf { if (currEnt->getType() == Entity::Type::DYNAMIC) { updateEnt(actors[i], t, scale); } - else if (currEnt->getType() == Entity::Type::STATIC) { + else updateEnt(actors[i], t, scale); - } } delete[] actors; @@ -171,7 +171,7 @@ namespace nf { } } - void PhysicsEngine::addMesh(Model* model, std::vector& vertices) { + void PhysicsEngine::addConvexMesh(Model* model, std::vector& vertices) { PxConvexMeshDesc desc; desc.points.count = (unsigned int)vertices.size() / 3; desc.points.stride = 3 * sizeof(float); @@ -184,7 +184,25 @@ namespace nf { PxDefaultMemoryInputData in(buf.getData(), buf.getSize()); PxConvexMesh* mesh = m_phy->createConvexMesh(in); - m_meshes[model] = mesh; + m_convexMeshes[model] = mesh; + } + + void PhysicsEngine::addTriangleMesh(Model* model, std::vector& vertices, std::vector& indices) { + PxTriangleMeshDesc desc; + desc.points.count = (unsigned int)vertices.size() / 3; + desc.points.stride = sizeof(float) * 3; + desc.points.data = &vertices[0]; + desc.triangles.count = (unsigned int)indices.size() / 3; + desc.triangles.stride = sizeof(unsigned int) * 3; + desc.triangles.data = &indices[0]; + + PxDefaultMemoryOutputStream buf; + if (!m_cooking->cookTriangleMesh(desc, buf)) + Error("Could not create triangle mesh!"); + + PxDefaultMemoryInputData in(buf.getData(), buf.getSize()); + PxTriangleMesh* mesh = m_phy->createTriangleMesh(in); + m_triangleMeshes[model] = mesh; } void PhysicsEngine::addActor(Entity* entity) { @@ -196,18 +214,28 @@ namespace nf { float density = 100.0f; //Get mesh - if (m_meshes.find(entity->getModel()) == m_meshes.end()) + PxConvexMesh* convexMesh = nullptr; + PxTriangleMesh* triangleMesh = nullptr; + if (m_convexMeshes.find(entity->getModel()) != m_convexMeshes.end()) + convexMesh = m_convexMeshes[entity->getModel()]; + else if (m_triangleMeshes.find(entity->getModel()) != m_triangleMeshes.end()) + triangleMesh = m_triangleMeshes[entity->getModel()]; + else Error("No physics mesh found for this entity!"); - PxConvexMesh* mesh = m_meshes[entity->getModel()]; //Dynamic or static if (type == Entity::Type::DYNAMIC) { - PxRigidDynamic* act = PxCreateDynamic(*m_phy, PxTransform(PxIdentity), PxConvexMeshGeometry(mesh), *mat, density); + PxRigidDynamic* act = PxCreateDynamic(*m_phy, PxTransform(PxIdentity), PxConvexMeshGeometry(convexMesh), *mat, density); act->userData = entity; m_scene->addActor(*act); } else if (type == Entity::Type::STATIC) { - PxRigidStatic* act = PxCreateStatic(*m_phy, PxTransform(PxIdentity), PxConvexMeshGeometry(mesh), *mat); + PxRigidStatic* act = PxCreateStatic(*m_phy, PxTransform(PxIdentity), PxConvexMeshGeometry(convexMesh), *mat); + act->userData = entity; + m_scene->addActor(*act); + } + else if (type == Entity::Type::ENVIRONMENT) { + PxRigidStatic* act = PxCreateStatic(*m_phy, PxTransform(PxIdentity), PxTriangleMeshGeometry(triangleMesh), *mat); act->userData = entity; m_scene->addActor(*act); } @@ -216,10 +244,18 @@ namespace nf { void PhysicsEngine::closeScene() { if (m_scene) { //Does this actually release all of them? Only if the respective shapes have been released? - for (auto it = m_meshes.begin(); it != m_meshes.end();) { + for (auto it = m_convexMeshes.begin(); it != m_convexMeshes.end();) { if (!it->first->isBaseAsset()) { it->second->release(); - it = m_meshes.erase(it); + it = m_convexMeshes.erase(it); + } + else + it++; + } + for (auto it = m_triangleMeshes.begin(); it != m_triangleMeshes.end();) { + if (!it->first->isBaseAsset()) { + it->second->release(); + it = m_triangleMeshes.erase(it); } else it++; diff --git a/NothinFancy/src/Renderer/Model.cpp b/NothinFancy/src/Renderer/Model.cpp index 86667ca..2c9222a 100644 --- a/NothinFancy/src/Renderer/Model.cpp +++ b/NothinFancy/src/Renderer/Model.cpp @@ -13,7 +13,7 @@ #include "Utility.h" namespace nf { - Model::Model(AModel* model, bool physicsExport) : + Model::Model(AModel* model, bool physicsConvex, bool physicsTriangle) : m_base(model->isBaseAsset), m_newMtl("newmtl"), m_newLine("\n") @@ -251,8 +251,10 @@ namespace nf { m_vao->finishBufferLayout(); m_ib = new IndexBuffer(&vboIndices[0], vboIndices.size()); - if (physicsExport) - Application::getApp()->getPhysicsEngine()->addMesh(this, vboPositions); + if (physicsConvex) + Application::getApp()->getPhysicsEngine()->addConvexMesh(this, vboPositions); + else if(physicsTriangle) + Application::getApp()->getPhysicsEngine()->addTriangleMesh(this, vboPositions, vboIndices); } void Model::parseMaterials(std::unordered_map& mats, std::vector& mtl) { diff --git a/NothinFancy/src/include/Entity.h b/NothinFancy/src/include/Entity.h index ad7adfd..bff5311 100644 --- a/NothinFancy/src/include/Entity.h +++ b/NothinFancy/src/include/Entity.h @@ -15,6 +15,7 @@ namespace nf { enum class Type { STATIC, DYNAMIC, + ENVIRONMENT, DETAIL }; diff --git a/NothinFancy/src/include/Model.h b/NothinFancy/src/include/Model.h index ca8394f..462f6db 100644 --- a/NothinFancy/src/include/Model.h +++ b/NothinFancy/src/include/Model.h @@ -10,7 +10,7 @@ namespace nf { class Model : public Drawable { public: - Model(AModel* model, bool physicsExport); + Model(AModel* model, bool physicsConvex, bool physicsTriangle); void render(Shader* shader, bool onlyDepth, unsigned int count); void bindMaterials(Shader* shader); diff --git a/NothinFancy/src/include/PhysicsEngine.h b/NothinFancy/src/include/PhysicsEngine.h index 6670315..6d623bf 100644 --- a/NothinFancy/src/include/PhysicsEngine.h +++ b/NothinFancy/src/include/PhysicsEngine.h @@ -21,7 +21,8 @@ namespace nf { void setActorVelocity(Entity* ent, const Vec3& vel); void setActorMass(Entity* ent, float mass); void update(float dt); - void addMesh(Model* model, std::vector& vertices); + void addConvexMesh(Model* model, std::vector& vertices); + void addTriangleMesh(Model* model, std::vector& vertices, std::vector& indices); void addActor(Entity* entity); void closeScene(); @@ -41,7 +42,8 @@ namespace nf { PxDefaultCpuDispatcher* m_dispacher; PxMaterial* m_defaultMat; PxScene* m_scene; - std::unordered_map m_meshes; + std::unordered_map m_convexMeshes; + std::unordered_map m_triangleMeshes; const float m_stepSize; float m_accumulator;