Skip to content

Commit

Permalink
load primitive box + fix crash on delete entity
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Jan 3, 2024
1 parent 77144ba commit 1aa118b
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 28 deletions.
14 changes: 14 additions & 0 deletions editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ void EditorApp::gui()
if (ImGui::Button("Change"))
{
mesh.path = std::string(meshPath);
if (oldMeshPath == "box")
{
mesh.primitive = std::monostate{};
}
goop::rm->loadMesh(mesh, oldMeshPath.c_str());
oldMeshPath = "";
std::memset(meshPath, 0, 256);
ImGui::CloseCurrentPopup();
changeMeshPopupOpen = false;
}
if (ImGui::Button("Box"))
{
mesh.primitive = goop::Box{};
mesh.path = "box";
goop::rm->loadMesh(mesh, oldMeshPath.c_str());
oldMeshPath = "";
std::memset(meshPath, 0, 256);
Expand Down
8 changes: 7 additions & 1 deletion goop/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstring>
#include <glm/glm.hpp>
#include <goop/Primitives.h>
#include <string>

namespace goop
Expand All @@ -27,14 +28,19 @@ struct TagComponent

struct MeshComponent
{
goop::Primitive primitive;
std::string path;
uint32_t id;
std::string texturePath;
uint32_t textureId;
MeshComponent(const std::string& path, const std::string& texturePath)
: path(path), texturePath(texturePath)
{
}
MeshComponent(goop::Primitive primitive, const std::string& texturePath,
const std::string& path)
: primitive(primitive), texturePath(texturePath), path(path)
{
}
};

struct RigidbodyComponent
Expand Down
25 changes: 25 additions & 0 deletions goop/Primitives.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <variant>
namespace goop
{
struct Box
{
float x;
float y;
float z;

Box(float x, float y, float z) : x(x), y(y), z(z) {}
Box() : x(1), y(1), z(1) {}
};

struct Sphere
{
float radius;
float resolution;
};

#define PRIMITIVES Box, Sphere

using Primitive = std::variant<std::monostate, PRIMITIVES>;
} // namespace goop
15 changes: 14 additions & 1 deletion goop/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ void Scene::loadScene(nlohmann::json& startScene)
}
else if (component["type"] == "mesh")
{
e.addComponent<goop::MeshComponent>(component["path"], component["texturePath"]);
if (component["primitive"].is_null())
{
e.addComponent<goop::MeshComponent>(component["path"],
component["texturePath"]);
}
else if (component["primitive"] == "box")
{
e.addComponent<goop::MeshComponent>(goop::Box(), component["texturePath"],
component["path"]);
}
}
else if (component["type"] == "rigidBody")
{
Expand Down Expand Up @@ -114,6 +123,10 @@ nlohmann::json Scene::saveScene()
auto mc = e.getComponent<MeshComponent>();
meshJson["path"] = mc.path;
meshJson["texturePath"] = mc.texturePath;
if (std::holds_alternative<goop::Box>(mc.primitive))
{
meshJson["primitive"] = "box";
}
eJson["components"].push_back(meshJson);
}
if (e.hasComponent<RigidbodyComponent>())
Expand Down
10 changes: 1 addition & 9 deletions goop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ int main(int argc, char** argv)
{
goop::Core core(argc, argv);

try
{
core.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
core.run();

return EXIT_SUCCESS;
}
73 changes: 73 additions & 0 deletions goop/sys/MeshLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,76 @@ int MeshLoader::initialize()
return 0;
}

int MeshLoader::loadPrimitive(Primitive primitive)
{
int index = data->size();
if (unloadedSlots.size() > 0)
{
index = unloadedSlots.back();
unloadedSlots.pop_back();
}
MeshImportData* mid = &data->emplace(index, MeshImportData{}).first->second;

if (std::holds_alternative<Box>(primitive))
{
Box box = std::get<Box>(primitive);
mid->vertices.reserve(8);
mid->indices.reserve(36);

std::vector<float> cubeVertices = {
// Front face
box.x, box.y, box.z, // Vertex 0
box.x, -box.y, box.z, // Vertex 1
-box.x, -box.y, box.z, // Vertex 2
-box.x, box.y, box.z, // Vertex 3
// Back face
box.x, box.y, -box.z, // Vertex 4
box.x, -box.y, -box.z, // Vertex 5
-box.x, -box.y, -box.z, // Vertex 6
-box.x, box.y, -box.z // Vertex 7
};

std::vector<unsigned int> cubeIndices = {// Front face
2, 1, 0, 0, 3, 2,
// Right face
1, 5, 4, 4, 0, 1,
// Back face
5, 6, 7, 7, 4, 5,
// Left face
6, 2, 3, 3, 7, 6,
// Bottom face
6, 5, 1, 1, 2, 6,
// Top face
3, 0, 4, 4, 7, 3};

std::vector<float> cubeTextureCoords = {
0.0f, 1.0f, // Front bottom-left
1.0f, 1.0f, // Front bottom-right
1.0f, 0.0f, // Front top-right
0.0f, 0.0f, // Front top-left

1.0f, 1.0f, // Back bottom-left
0.0f, 1.0f, // Back bottom-right
0.0f, 0.0f, // Back top-right
1.0f, 0.0f // Back top-left
};

int texCoordIndex = 0;
for (int i = 0; i < cubeVertices.size(); i += 3)
{
Vertex vertex{};
vertex.pos = glm::vec3(cubeVertices[i], cubeVertices[i + 1], cubeVertices[i + 2]);
vertex.texCoord =
glm::vec2(cubeTextureCoords[texCoordIndex], cubeTextureCoords[texCoordIndex + 1]);
vertex.color = glm::vec3(1.0f, 1.0f, 1.0f);
mid->vertices.push_back(vertex);
texCoordIndex += 2;
}

for (int i = 0; i < cubeIndices.size(); i++)
{
mid->indices.push_back(cubeIndices[i]);
}
}
return index;
}
4 changes: 4 additions & 0 deletions goop/sys/MeshLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <map>
#include <memory>
#include <vector>
#include <goop/Primitives.h>

namespace goop::sys
{
Expand All @@ -23,8 +24,11 @@ class MeshLoader : public ResourceSubsystem
public:
virtual int initialize() override;
const std::map<uint32_t, MeshImportData>* getData() const { return data.get(); }
int loadPrimitive(Primitive primitive);

protected:
std::unique_ptr<std::map<uint32_t, MeshImportData>> data;
std::map<std::string, uint32_t> loadedMeshes;
std::vector<int> unloadedSlots;
};
} // namespace goop::sys
30 changes: 23 additions & 7 deletions goop/sys/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,31 @@ bool ResourceManager::loadMesh(MeshComponent& mesh, const char* oldPath)
}
}

if (numLoadedMeshes[mesh.path] > 0)
// if primitive is not set - load from path provided
if (std::holds_alternative<std::monostate>(mesh.primitive))
{
std::cout << "Mesh " << mesh.path << " already loaded" << std::endl;
mesh.id = loadedMeshes[mesh.path];
numLoadedMeshes[mesh.path]++;
std::cout << "Loaded meshid: " << mesh.id << std::endl;
return true;
if (numLoadedMeshes[mesh.path] > 0)
{
std::cout << "Mesh " << mesh.path << " already loaded" << std::endl;
mesh.id = loadedMeshes[mesh.path];
numLoadedMeshes[mesh.path]++;
std::cout << "Loaded meshid: " << mesh.id << std::endl;
return true;
}
mesh.id = meshLoader->load(mesh.path);
}
else // load primitive
{
if (numLoadedMeshes[mesh.path] > 0)
{
std::cout << "Mesh " << mesh.path << " already loaded" << std::endl;
mesh.id = loadedMeshes[mesh.path];
numLoadedMeshes[mesh.path]++;
std::cout << "Loaded meshid: " << mesh.id << std::endl;
return true;
}
mesh.id = meshLoader->loadPrimitive(mesh.primitive);
}
mesh.id = meshLoader->load(mesh.path);
loadedMeshes[mesh.path] = mesh.id;
numLoadedMeshes[mesh.path]++;
std::cout << "Loaded mesh " << mesh.path << " with id " << mesh.id << std::endl;
Expand Down
2 changes: 0 additions & 2 deletions goop/sys/platform/assimp/MeshLoader_Assimp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ class MeshLoader_Assimp : public MeshLoader

private:
Assimp::Importer importer;
std::map<std::string, uint32_t> loadedMeshes;
std::vector<int> unloadedSlots;
};
} // namespace goop::sys::platform::assimp
20 changes: 12 additions & 8 deletions goop/sys/platform/bullet/Physics_Bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ void Physics_Bullet::simulate(float dt)

void Physics_Bullet::addRigidBody(RigidbodyComponent* rbc, TransformComponent* tc)
{
btCollisionShape* shape = new btBoxShape(btVector3(rbc->box[0], rbc->box[1], rbc->box[2]));
btCollisionShape* shape =
new btBoxShape(btVector3(rbc->box[0] / 2.f, rbc->box[1] / 2.f, rbc->box[2] / 2.f));
collisionShapes[rbc] = shape;

btTransform transform;
Expand Down Expand Up @@ -91,13 +92,16 @@ void Physics_Bullet::addRigidBody(RigidbodyComponent* rbc, TransformComponent* t

void Physics_Bullet::removeRigidBody(RigidbodyComponent* rbc)
{
dynamicsWorld->removeRigidBody(rigidBodies[rbc]);
delete rigidBodies[rbc]->getMotionState();
delete rigidBodies[rbc];
delete collisionShapes[rbc];
rigidBodies.erase(rbc);
collisionShapes.erase(rbc);
transforms.erase(rbc);
if (bIsInitialized)
{
dynamicsWorld->removeRigidBody(rigidBodies[rbc]);
delete rigidBodies[rbc]->getMotionState();
delete rigidBodies[rbc];
delete collisionShapes[rbc];
rigidBodies.erase(rbc);
collisionShapes.erase(rbc);
transforms.erase(rbc);
}
}

Physics_Bullet::~Physics_Bullet() { destroy(); }

0 comments on commit 1aa118b

Please sign in to comment.