modified scene system

This commit is contained in:
2026-05-09 22:15:46 -04:00
parent 83be792420
commit c54ca2adc5
5 changed files with 87 additions and 16 deletions

View File

@@ -20,8 +20,10 @@ namespace Components {
};
struct Relationship {
entt::entity entity;
std::vector<entt::entity> 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 {

View File

@@ -61,7 +61,6 @@ std::shared_ptr<Model> ModelManager::load_from_file(std::string_view _path, bool
model->materials.push_back(std::make_shared<Material>(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<Model> 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);
}
}

View File

@@ -4,7 +4,61 @@
#include "Scene.h"
entt::entity Scene::create_game_obejct() {
#include "Components.h"
Scene::Scene() {
root = _registry.create();
attach_component<Components::Transform>(root);
attach_component<Components::Relationship>(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<Components::Transform>(entity);
attach_component<Components::Relationship>(entity);
add_child(parent, entity);
return entity;
}
template<typename T>
T& Scene::attach_component(entt::entity e) {
return _registry.emplace<T>(e);
}
template<typename T>
T &Scene::attach_component(entt::entity e, T data) {
return _registry.emplace<T>(e, data);
}
template<typename T>
T& Scene::fetch_component(entt::entity e) {
return _registry.get<T>(e);
}
void Scene::add_child(entt::entity parent, entt::entity child) {
auto& parentRelationship = fetch_component<Components::Relationship>(parent);
if (parentRelationship.first_child == entt::null) {
parentRelationship.first_child = child;
return;
}
entt::entity lastSibling = parentRelationship.first_child;
auto& lastSiblingRelationship = fetch_component<Components::Relationship>(lastSibling);
while (lastSiblingRelationship.next_sibling != entt::null) {
lastSibling = lastSiblingRelationship.next_sibling;
lastSiblingRelationship = fetch_component<Components::Relationship>(lastSibling);
}
lastSiblingRelationship.next_sibling = child;
auto& childRelationship = fetch_component<Components::Relationship>(child);
childRelationship.prev_sibling = lastSibling;
childRelationship.next_sibling = entt::null;
childRelationship.parent = parent;
}

View File

@@ -7,14 +7,33 @@
#include "entt/entt.hpp"
struct SceneObject {
entt::entity e;
std::vector<SceneObject*> 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<typename T>
T& attach_component(entt::entity e);
template<typename T>
T& attach_component(entt::entity e, T data);
template<typename T>
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

View File

@@ -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();