Adding a relationship component

The relationship component enables us to define a hierarchy through the ECS. This creates a pathway towards Inverse Kinematics
This commit is contained in:
Nigel Barink 2022-12-31 02:42:27 +01:00
parent 3722e63495
commit fe7e168e21
6 changed files with 84 additions and 45 deletions

View File

@ -199,18 +199,6 @@ void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene ) {
// Create a level and load it as the current level // Create a level and load it as the current level
auto importer = ModelImporter(); auto importer = ModelImporter();
// Create a cube
auto model = importer.Import("build/Debug/Models/Cube.obj");
auto cube = scene.AddEntity("Cube");
auto& render3DComponent = cube.AddComponent<Render3DComponent>();
render3DComponent.mesh = *(model->renderable->mesh);
cube.GetComponent<TransformComponent>().Position = glm::vec3(1.0f, 0.0f, 5.0f);
auto cube2 = scene.AddEntity("Cube1");
auto& rendercube2 = cube2.AddComponent<Render3DComponent>();
rendercube2.mesh = *(model->renderable->mesh);
// create an ambient light source // create an ambient light source
auto light = scene.AddEntity("Light"); auto light = scene.AddEntity("Light");
@ -218,5 +206,22 @@ void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene ) {
lightComponent.Color = glm::vec3(1.0f); lightComponent.Color = glm::vec3(1.0f);
// Create a cube
auto model = importer.Import("build/Debug/Models/Cube.obj");
auto cube = scene.AddEntity("Cube");
auto& render3DComponent = cube.AddComponent<Render3DComponent>();
render3DComponent.mesh = *(model->renderable->mesh);
cube.GetComponent<TransformComponent>().Position = glm::vec3(1.0f, 0.0f, 5.0f);
auto cube2 = scene.AddEntity("Cube2");
auto& rendercube2 = cube2.AddComponent<Render3DComponent>();
rendercube2.mesh = *(model->renderable->mesh);
auto relationcube = cube.AddComponent<RelationComponent>(cube2);
} }

View File

@ -199,15 +199,8 @@ void Renderer::GeometryPass() {
glBindVertexArray(command.VAO_identifier); glBindVertexArray(command.VAO_identifier);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, command.IBO_identifier); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, command.IBO_identifier);
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), command.transform.Rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
rotation *= glm::rotate(glm::mat4(1.0f), command.transform.Rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
rotation *= glm::rotate(glm::mat4(1.0f), command.transform.Rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), command.transform.Position) * glm::scale(glm::mat4(1.0f), command.transform.Scale) * rotation;
gBufferShader.setUniformVec3("Color", command.color); gBufferShader.setUniformVec3("Color", command.color);
gBufferShader.setUniformMat4("Model", modelMatrix); gBufferShader.setUniformMat4("Model", command.transform.LocalTransform);
gBufferShader.setUniformMat4("View", cam.ViewMatrix); gBufferShader.setUniformMat4("View", cam.ViewMatrix);
gBufferShader.setUniformMat4("Projection", cam.ProjectionMatrix); gBufferShader.setUniformMat4("Projection", cam.ProjectionMatrix);
@ -330,16 +323,8 @@ void Renderer::Render(Scene& scene)
command.shader.Use(); command.shader.Use();
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), command.transform.Rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
rotation *= glm::rotate(glm::mat4(1.0f), command.transform.Rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
rotation *= glm::rotate(glm::mat4(1.0f), command.transform.Rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), command.transform.Position) * glm::scale(glm::mat4(1.0f), command.transform.Scale) * rotation;
command.shader.setUniformVec3("Color", command.color); command.shader.setUniformVec3("Color", command.color);
command.shader.setUniformMat4("M", modelMatrix); command.shader.setUniformMat4("M", command.transform.LocalTransform);
command.shader.setUniformMat4("V", cam.ViewMatrix); command.shader.setUniformMat4("V", cam.ViewMatrix);
command.shader.setUniformMat4("P", cam.ProjectionMatrix); command.shader.setUniformMat4("P", cam.ProjectionMatrix);

View File

@ -1,29 +1,31 @@
#pragma once #pragma once
#include "Entity.h"
namespace YoggieEngine { namespace YoggieEngine {
struct IdentifierComponent { struct IdentifierComponent {
std::string name; std::string name;
}; };
struct TransformComponent { struct TransformComponent {
glm::vec3 Position = glm::vec3(0.0f); glm::vec3 Position = glm::vec3(0.0f);
glm::vec3 Rotation = glm::vec3(0.0f); glm::vec3 Rotation = glm::vec3(0.0f);
glm::vec3 Scale = glm::vec3(1.0f); glm::vec3 Scale = glm::vec3(1.0f);
glm::mat4 LocalTransform = glm::mat4(1.0f);
glm::mat4 GetTransformMatrix() { };
glm::mat4 result = glm::mat4(1.0f);
glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), Rotation.x, glm::vec3(1.f, 0.f, 0.f));
rotate *= glm::rotate(glm::mat4(1.0f), Rotation.y, glm::vec3(0.f, 1.f, 0.f));
rotate *= glm::rotate(glm::mat4(1.0f), Rotation.z, glm::vec3(0.f, 0.f, 1.f));
struct RelationComponent {
Entity Parent;
result = rotate * glm::scale(glm::mat4(1.0f), Scale) * glm::translate(glm::mat4(1.0f), Position) ; RelationComponent(Entity& entity)
{
return result; Parent = entity;
} }
}; };
struct LightComponent { struct LightComponent {
glm::vec3 Color = glm::vec3(1.0f, .5f, .5f); glm::vec3 Color = glm::vec3(1.0f, .5f, .5f);
}; };

View File

@ -8,9 +8,9 @@ namespace YoggieEngine {
Entity(entt::entity e, Scene* scene) ; Entity(entt::entity e, Scene* scene) ;
Entity(const Entity& other) = default; Entity(const Entity& other) = default;
template<class T > template<class T, class... argumentType >
T& AddComponent() { T& AddComponent(argumentType... args) {
return m_scene->m_registry.emplace<T>(m_entity); return m_scene->m_registry.emplace<T>(m_entity, args...);
} }
template<class T> template<class T>
@ -24,8 +24,6 @@ namespace YoggieEngine {
return m_scene->getReg().all_of<T>(m_entity); return m_scene->getReg().all_of<T>(m_entity);
} }
// NOTE: Not Scene context aware!! // NOTE: Not Scene context aware!!
bool operator== (Entity& other) { bool operator== (Entity& other) {
return m_entity == other.m_entity; return m_entity == other.m_entity;

View File

@ -1,7 +1,7 @@
#include <YoggieEngine.h> #include <YoggieEngine.h>
#include "Scene.h" #include "Scene.h"
#include "Entity.h" #include "Entity.h"
#include "Components.h" using namespace entt;
namespace YoggieEngine{ namespace YoggieEngine{
Scene::Scene() Scene::Scene()
@ -32,6 +32,54 @@ namespace YoggieEngine{
{ {
// Execute Update functions in scripts etc.... // Execute Update functions in scripts etc....
// Update transforms
auto& transforms = m_registry.view<TransformComponent>();
transforms.each([&](auto ent, TransformComponent& transform) {
glm::mat4 rotationX =
glm::rotate(
glm::mat4(1.0f),
glm::radians(transform.Rotation.x),
glm::vec3(1.f, 0.f, 0.0f)
);
glm::mat4 rotationY =
glm::rotate(
glm::mat4(1.0f),
glm::radians(transform.Rotation.y),
glm::vec3(0.f, 1.f, 0.0f)
);
glm::mat4 rotationZ =
glm::rotate(
glm::mat4(1.0f),
transform.Rotation.z,
glm::vec3(0.f, 0.f, 1.0f)
);
glm::mat4 rotationMatrix = rotationY * rotationX * rotationZ;
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), transform.Position);
glm::mat4 ScaleMatrix = glm::scale(glm::mat4(1.0f), transform.Scale);
Entity entity( ent, this );
if (entity.HasComponent<RelationComponent>())
{
auto& entityRelation = entity.GetComponent<RelationComponent>();
Entity parent = entityRelation.Parent;
TransformComponent parentTransform = parent.GetComponent<TransformComponent>();
glm::mat4 Model = translationMatrix * rotationMatrix * ScaleMatrix;
transform.LocalTransform = parentTransform.LocalTransform * Model;
}
else {
transform.LocalTransform = translationMatrix * rotationMatrix * ScaleMatrix;
}
});
} }
void Scene::FixedUpdate() void Scene::FixedUpdate()

View File

@ -45,6 +45,7 @@ extern "C"
#include "EventSystem/EventEmitter.h" #include "EventSystem/EventEmitter.h"
#include "EventSystem/EventListener.h" #include "EventSystem/EventListener.h"
#include "Scene/Entity.h"
#include "Scene/Scene.h" #include "Scene/Scene.h"
#include "Scene/Components.h"
#include "Application.h" #include "Application.h"