Added the environment entity type for use as maps

This commit is contained in:
Grayson Riffe (Laptop) 2021-11-03 23:45:18 -05:00
parent bae89e65a1
commit a1fe7c3c1a
7 changed files with 70 additions and 27 deletions

View File

@ -39,15 +39,15 @@ namespace nf {
} }
void Gamestate::onEnter() { void Gamestate::onEnter() {
Error("Gamestate has no overridden onEnter function!");
} }
void Gamestate::update(float deltaTime) { void Gamestate::update(float deltaTime) {
Error("Gamestate has no overridden update function!");
} }
void Gamestate::render(Renderer& renderer) { void Gamestate::render(Renderer& renderer) {
Error("Gamestate has no overridden render function!");
} }
Camera* Gamestate::getCamera() { Camera* Gamestate::getCamera() {
@ -63,7 +63,7 @@ namespace nf {
} }
void Gamestate::onExit() { void Gamestate::onExit() {
Error("Gamestate has no overridden onExit function!");
} }
void Gamestate::stop() { void Gamestate::stop() {

View File

@ -34,10 +34,12 @@ namespace nf {
m_model = model->loadedModel; m_model = model->loadedModel;
} }
else { else {
bool physics = false; bool convex = false, triangle = false;
if (m_type != Entity::Type::DETAIL) if (m_type == Entity::Type::STATIC || m_type == Entity::Type::DYNAMIC)
physics = true; convex = true;
m_model = new Model(model, physics); else if (m_type == Entity::Type::ENVIRONMENT)
triangle = true;
m_model = new Model(model, convex, triangle);
model->alreadyLoaded = true; model->alreadyLoaded = true;
model->loadedModel = m_model; model->loadedModel = m_model;
} }

View File

@ -24,6 +24,7 @@ namespace nf {
m_err(nullptr), m_err(nullptr),
m_foundation(nullptr), m_foundation(nullptr),
m_pvd(nullptr), m_pvd(nullptr),
m_transport(nullptr),
m_phy(nullptr), m_phy(nullptr),
m_cooking(nullptr), m_cooking(nullptr),
m_dispacher(nullptr), m_dispacher(nullptr),
@ -148,10 +149,9 @@ namespace nf {
if (currEnt->getType() == Entity::Type::DYNAMIC) { if (currEnt->getType() == Entity::Type::DYNAMIC) {
updateEnt<PxRigidDynamic>(actors[i], t, scale); updateEnt<PxRigidDynamic>(actors[i], t, scale);
} }
else if (currEnt->getType() == Entity::Type::STATIC) { else
updateEnt<PxRigidStatic>(actors[i], t, scale); updateEnt<PxRigidStatic>(actors[i], t, scale);
} }
}
delete[] actors; delete[] actors;
m_accumulator += dt; m_accumulator += dt;
@ -171,7 +171,7 @@ namespace nf {
} }
} }
void PhysicsEngine::addMesh(Model* model, std::vector<float>& vertices) { void PhysicsEngine::addConvexMesh(Model* model, std::vector<float>& vertices) {
PxConvexMeshDesc desc; PxConvexMeshDesc desc;
desc.points.count = (unsigned int)vertices.size() / 3; desc.points.count = (unsigned int)vertices.size() / 3;
desc.points.stride = 3 * sizeof(float); desc.points.stride = 3 * sizeof(float);
@ -184,7 +184,25 @@ namespace nf {
PxDefaultMemoryInputData in(buf.getData(), buf.getSize()); PxDefaultMemoryInputData in(buf.getData(), buf.getSize());
PxConvexMesh* mesh = m_phy->createConvexMesh(in); PxConvexMesh* mesh = m_phy->createConvexMesh(in);
m_meshes[model] = mesh; m_convexMeshes[model] = mesh;
}
void PhysicsEngine::addTriangleMesh(Model* model, std::vector<float>& vertices, std::vector<unsigned int>& 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) { void PhysicsEngine::addActor(Entity* entity) {
@ -196,18 +214,28 @@ namespace nf {
float density = 100.0f; float density = 100.0f;
//Get mesh //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!"); Error("No physics mesh found for this entity!");
PxConvexMesh* mesh = m_meshes[entity->getModel()];
//Dynamic or static //Dynamic or static
if (type == Entity::Type::DYNAMIC) { 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; act->userData = entity;
m_scene->addActor(*act); m_scene->addActor(*act);
} }
else if (type == Entity::Type::STATIC) { 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; act->userData = entity;
m_scene->addActor(*act); m_scene->addActor(*act);
} }
@ -216,10 +244,18 @@ namespace nf {
void PhysicsEngine::closeScene() { void PhysicsEngine::closeScene() {
if (m_scene) { if (m_scene) {
//Does this actually release all of them? Only if the respective shapes have been released? //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()) { if (!it->first->isBaseAsset()) {
it->second->release(); 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 else
it++; it++;

View File

@ -13,7 +13,7 @@
#include "Utility.h" #include "Utility.h"
namespace nf { namespace nf {
Model::Model(AModel* model, bool physicsExport) : Model::Model(AModel* model, bool physicsConvex, bool physicsTriangle) :
m_base(model->isBaseAsset), m_base(model->isBaseAsset),
m_newMtl("newmtl"), m_newMtl("newmtl"),
m_newLine("\n") m_newLine("\n")
@ -251,8 +251,10 @@ namespace nf {
m_vao->finishBufferLayout(); m_vao->finishBufferLayout();
m_ib = new IndexBuffer(&vboIndices[0], vboIndices.size()); m_ib = new IndexBuffer(&vboIndices[0], vboIndices.size());
if (physicsExport) if (physicsConvex)
Application::getApp()->getPhysicsEngine()->addMesh(this, vboPositions); Application::getApp()->getPhysicsEngine()->addConvexMesh(this, vboPositions);
else if(physicsTriangle)
Application::getApp()->getPhysicsEngine()->addTriangleMesh(this, vboPositions, vboIndices);
} }
void Model::parseMaterials(std::unordered_map<std::string, TempMaterial*>& mats, std::vector<char>& mtl) { void Model::parseMaterials(std::unordered_map<std::string, TempMaterial*>& mats, std::vector<char>& mtl) {

View File

@ -15,6 +15,7 @@ namespace nf {
enum class Type { enum class Type {
STATIC, STATIC,
DYNAMIC, DYNAMIC,
ENVIRONMENT,
DETAIL DETAIL
}; };

View File

@ -10,7 +10,7 @@ namespace nf {
class Model : public Drawable { class Model : public Drawable {
public: public:
Model(AModel* model, bool physicsExport); Model(AModel* model, bool physicsConvex, bool physicsTriangle);
void render(Shader* shader, bool onlyDepth, unsigned int count); void render(Shader* shader, bool onlyDepth, unsigned int count);
void bindMaterials(Shader* shader); void bindMaterials(Shader* shader);

View File

@ -21,7 +21,8 @@ namespace nf {
void setActorVelocity(Entity* ent, const Vec3& vel); void setActorVelocity(Entity* ent, const Vec3& vel);
void setActorMass(Entity* ent, float mass); void setActorMass(Entity* ent, float mass);
void update(float dt); void update(float dt);
void addMesh(Model* model, std::vector<float>& vertices); void addConvexMesh(Model* model, std::vector<float>& vertices);
void addTriangleMesh(Model* model, std::vector<float>& vertices, std::vector<unsigned int>& indices);
void addActor(Entity* entity); void addActor(Entity* entity);
void closeScene(); void closeScene();
@ -41,7 +42,8 @@ namespace nf {
PxDefaultCpuDispatcher* m_dispacher; PxDefaultCpuDispatcher* m_dispacher;
PxMaterial* m_defaultMat; PxMaterial* m_defaultMat;
PxScene* m_scene; PxScene* m_scene;
std::unordered_map<Model*, PxConvexMesh*> m_meshes; std::unordered_map<Model*, PxConvexMesh*> m_convexMeshes;
std::unordered_map<Model*, PxTriangleMesh*> m_triangleMeshes;
const float m_stepSize; const float m_stepSize;
float m_accumulator; float m_accumulator;