diff --git a/src/Components.h b/src/Components.h index ab610e5..7e7858a 100644 --- a/src/Components.h +++ b/src/Components.h @@ -20,8 +20,10 @@ namespace Components { }; struct Relationship { - entt::entity entity; - std::vector children; + entt::entity parent {entt::null}; + entt::entity first_child {entt::null}; + entt::entity prev_sibling {entt::null}; + entt::entity next_sibling {entt::null}; }; struct Drawable { diff --git a/src/ModelManager.cpp b/src/ModelManager.cpp index fbf6a60..dde2e26 100644 --- a/src/ModelManager.cpp +++ b/src/ModelManager.cpp @@ -61,7 +61,6 @@ std::shared_ptr ModelManager::load_from_file(std::string_view _path, bool model->materials.push_back(std::make_shared(mat)); } - model->meshes.resize(scene->mNumMeshes); for (unsigned int i = 0; i < scene->mNumMeshes; i++) { aiMesh* aiMesh = scene->mMeshes[i]; Mesh mesh{}; @@ -80,22 +79,22 @@ std::shared_ptr ModelManager::load_from_file(std::string_view _path, bool return model; } -void process_ai_node(aiNode* aiNode, const aiScene* scene, glm::mat4 transform, ModelNode* node) +void process_ai_node(aiNode* aiNode, const aiScene* scene, glm::mat4 transform, ModelNode& node) { const auto t = aiNode->mTransformation; transform = transform * glm::mat4(t.a1, t.a2, t.a3, t.a4, t.b1, t.b2, t.b3, t.b4, t.c1, t.c2, t.c3, t.c4, t.d1, t.d2, t.d3, t.d4); - node->transform = transform; + node.transform = transform; for (unsigned int i = 0; i < aiNode->mNumMeshes; i++) { - node->meshIndices.push_back(aiNode->mMeshes[i]); + node.meshIndices.push_back(aiNode->mMeshes[i]); } for (unsigned int i = 0; i < aiNode->mNumChildren; i++) { ModelNode newNode{}; process_ai_node(aiNode->mChildren[i], scene, transform, newNode); - node->children.push_back(newNode); + node.children.push_back(newNode); } } diff --git a/src/Scene.cpp b/src/Scene.cpp index 882fd59..13fd1d2 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -4,7 +4,61 @@ #include "Scene.h" -entt::entity Scene::create_game_obejct() { - const entt::entity entity = _registry.create(); +#include "Components.h" + +Scene::Scene() { + root = _registry.create(); + attach_component(root); + attach_component(root); +} + +entt::entity Scene::create_game_object() { + return create_game_object(root); +} + +entt::entity Scene::create_game_object(entt::entity parent) { + const entt::entity entity = _registry.create(); + attach_component(entity); + attach_component(entity); + + add_child(parent, entity); + return entity; } + +template +T& Scene::attach_component(entt::entity e) { + return _registry.emplace(e); +} + +template +T &Scene::attach_component(entt::entity e, T data) { + return _registry.emplace(e, data); +} + +template +T& Scene::fetch_component(entt::entity e) { + return _registry.get(e); +} + +void Scene::add_child(entt::entity parent, entt::entity child) { + auto& parentRelationship = fetch_component(parent); + + if (parentRelationship.first_child == entt::null) { + parentRelationship.first_child = child; + return; + } + + entt::entity lastSibling = parentRelationship.first_child; + auto& lastSiblingRelationship = fetch_component(lastSibling); + while (lastSiblingRelationship.next_sibling != entt::null) { + lastSibling = lastSiblingRelationship.next_sibling; + lastSiblingRelationship = fetch_component(lastSibling); + } + lastSiblingRelationship.next_sibling = child; + + auto& childRelationship = fetch_component(child); + childRelationship.prev_sibling = lastSibling; + childRelationship.next_sibling = entt::null; + childRelationship.parent = parent; +} diff --git a/src/Scene.h b/src/Scene.h index 6dae0a2..edf7f25 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -7,14 +7,33 @@ #include "entt/entt.hpp" +struct SceneObject { + entt::entity e; + std::vector children; +}; + class Scene { public: - Scene() = default; + Scene(); ~Scene() = default; - entt::entity create_game_obejct(); + entt::entity create_game_object(); + entt::entity create_game_object(entt::entity parent); + + template + T& attach_component(entt::entity e); + template + T& attach_component(entt::entity e, T data); + + template + T& fetch_component(entt::entity e); + + private: entt::registry _registry{}; + entt::entity root; + + void add_child(entt::entity parent, entt::entity child); }; #endif //B_ENGINE_SCENE_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9e0e5a0..de09f2b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -250,20 +250,17 @@ void loop() { // draw the stuff if (shader && activeModel) { shader->bind(); - shader->setMat4("projection", gCamera.projection()); shader->setMat4("view", gCamera.view()); shader->setMat4("model", model); - shader->setVec3("viewPosition", gCamera.position()); - shader->setVec3("lightPosition", glm::vec3{-2.f, 0, 2.0f}); shader->setVec3("lightDirection", glm::vec3(1, -1, 1)); shader->setVec3("lightAmbient", glm::vec3(0.3f, 0.3f, 0.3f)); shader->setVec3("lightDiffuse", glm::vec3(0.5f, 0.5f, 0.5f)); shader->setVec3("lightSpecular", glm::vec3(1.0f, 1.0f, 1.0f)); - for (const auto& mesh: activeModel->meshes) { + /*for (const auto& mesh: activeModel->meshes) { unsigned int materialId = 0; if (materialId >= activeModel->materials.size()) { materialId = 0; @@ -294,7 +291,7 @@ void loop() { glBindVertexArray(mesh.get()->vao); glDrawElements(GL_TRIANGLES, mesh->numIndices, GL_UNSIGNED_INT, 0); - } + }*/ glBindVertexArray(0); ShaderProgram::unbind();