Children transforms relative to parents using TransformSystem::AbsolutePosition and TransformSystem::AbsoluteOrientation

This commit is contained in:
2014-03-13 23:44:32 +01:00
parent ebd54f0833
commit cbb8eac917
15 changed files with 199 additions and 66 deletions
+3 -1
View File
@@ -11,8 +11,10 @@ namespace Components
struct Model : Component
{
const char* ModelFile;
Model() : Visible(true) { }
std::string ModelFile;
Color Color;
bool Visible;
};
}
+2
View File
@@ -110,6 +110,7 @@
<ClCompile Include="Renderer.cpp" />
<ClCompile Include="ShaderProgram.cpp" />
<ClCompile Include="Systems\CollisionSystem.cpp" />
<ClCompile Include="Systems\TransformSystem.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="TextFrame.cpp" />
<ClCompile Include="World.cpp" />
@@ -161,6 +162,7 @@
<ClInclude Include="Systems\PlayerSystem.h" />
<ClInclude Include="Systems\RenderSystem.h" />
<ClInclude Include="Systems\SoundSystem.h" />
<ClInclude Include="Systems\TransformSystem.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="TextFrame.h" />
<ClInclude Include="World.h" />
@@ -47,6 +47,9 @@
<ClCompile Include="OBJ.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="GameWorld.cpp" />
<ClCompile Include="Systems\TransformSystem.cpp">
<Filter>Systems</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Component.h" />
@@ -148,6 +151,9 @@
<ClInclude Include="Camera.h" />
<ClInclude Include="OBJ.h" />
<ClInclude Include="OpenGL.h" />
<ClInclude Include="Systems\TransformSystem.h">
<Filter>Systems</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="Shaders\Fragment.glsl">
+52 -26
View File
@@ -5,14 +5,6 @@ void GameWorld::Initialize()
{
World::Initialize();
AddSystem("LevelGenerationSystem");
AddSystem("InputSystem");
AddSystem("CollisionSystem");
//AddSystem("ParticleSystem");
AddSystem("PlayerSystem");
AddSystem("SoundSystem");
AddSystem("RenderSystem");
std::shared_ptr<Components::Transform> transform;
std::shared_ptr<Components::Model> model;
std::shared_ptr<Components::PointLight> pointLight;
@@ -21,11 +13,8 @@ void GameWorld::Initialize()
std::shared_ptr<Components::Collision> collision;
EntityID ent;
// Fucking lights
ent = CreateEntity();
/*ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(0.f, 7.f, 0.f);
pointLight = AddComponent<Components::PointLight>(ent, "PointLight");
@@ -60,7 +49,7 @@ void GameWorld::Initialize()
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
model = AddComponent<Components::Model>(ent, "Model");
model->ModelFile = "Models/sphere.obj";
model->ModelFile = "Models/sphere.obj";*/
//ground
ent = CreateEntity();
@@ -91,13 +80,6 @@ void GameWorld::Initialize()
transform->Scale = glm::vec3(1.0f);
model = AddComponent<Components::Model>(m_Player, "Model");
model->ModelFile = "Models/ship.obj";
pointLight = AddComponent<Components::PointLight>(m_Player, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(0.4, 0.4, 1.0);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
AddComponent<Components::Input>(m_Player, "Input");
collision = AddComponent<Components::Collision>(m_Player, "Collision");
collision->Phantom = false;
@@ -105,12 +87,43 @@ void GameWorld::Initialize()
bounds = AddComponent<Components::Bounds>(m_Player, "Bounds");
bounds->Origin = glm::vec3(0, 0, 2.f);
bounds->VolumeVector = glm::vec3(4.f, 0.7f, 1);
// Player light
auto playerLight = CreateEntity(m_Player);
transform = AddComponent<Components::Transform>(playerLight, "Transform");
transform->Position = glm::vec3(1.5f, 0.25f, 2.5f);
pointLight = AddComponent<Components::PointLight>(playerLight, "PointLight");
pointLight->Specular = glm::vec3(0.1f, 0.1f, 0.1f);
pointLight->Diffuse = glm::vec3(0.45f, 0.65f, 1.f);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 0.9f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
bounds = AddComponent<Components::Bounds>(playerLight, "Bounds");
bounds->VolumeVector = glm::vec3(0.5f);
playerLight = CreateEntity(m_Player);
transform = AddComponent<Components::Transform>(playerLight, "Transform");
transform->Position = glm::vec3(-1.5f, 0.25f, 2.5f);
pointLight = AddComponent<Components::PointLight>(playerLight, "PointLight");
pointLight->Specular = glm::vec3(0.1f, 0.1f, 0.1f);
pointLight->Diffuse = glm::vec3(0.45f, 0.65f, 1.f);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 0.9f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
bounds = AddComponent<Components::Bounds>(playerLight, "Bounds");
bounds->VolumeVector = glm::vec3(0.5f);
playerLight = CreateEntity(m_Player);
transform = AddComponent<Components::Transform>(playerLight, "Transform");
transform->Position = glm::vec3(0.0f, 2.0f, 2.5f);
pointLight = AddComponent<Components::PointLight>(playerLight, "PointLight");
pointLight->Specular = glm::vec3(0.1f, 0.1f, 0.1f);
pointLight->Diffuse = glm::vec3(0.45f, 0.65f, 1.f);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.0f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
bounds = AddComponent<Components::Bounds>(playerLight, "Bounds");
bounds->VolumeVector = glm::vec3(0.5f);
/*ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
@@ -155,5 +168,18 @@ void GameWorld::RegisterSystems()
//m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); });
m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); });
m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); });
m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this); });
m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); });
}
void GameWorld::AddSystems()
{
AddSystem("TransformSystem");
AddSystem("LevelGenerationSystem");
AddSystem("InputSystem");
AddSystem("CollisionSystem");
//AddSystem("ParticleSystem");
AddSystem("PlayerSystem");
AddSystem("SoundSystem");
AddSystem("RenderSystem");
}
+3 -1
View File
@@ -4,6 +4,7 @@
#include "World.h"
#include "Renderer.h"
#include "Systems/TransformSystem.h"
#include "Systems/CollisionSystem.h"
#include "Systems/InputSystem.h"
#include "Systems/LevelGenerationSystem.h"
@@ -36,9 +37,10 @@ public:
void Initialize();
void RegisterSystems() override;
void AddSystems() override;
void RegisterComponents() override;
void Update(double dt) ;
void Update(double dt);
private:
std::shared_ptr<Renderer> m_Renderer;
+24 -19
View File
@@ -14,7 +14,7 @@ Renderer::Renderer()
#endif
m_ShadowMapRes = 2048*2;
m_SunPosition = glm::vec3(0, 0.3f, 10);
m_SunPosition = glm::vec3(0, 1.5f, 10);
m_SunTarget = glm::vec3(0, 0, 0);
m_SunProjection = glm::ortho<float>(-200, 200, -10, 100, -800, 800);
Lights = 0;
@@ -216,7 +216,11 @@ void Renderer::DrawScene()
{
Model* model;
glm::mat4 modelMatrix;
std::tie(model, modelMatrix) = tuple;
bool visible;
std::tie(model, modelMatrix, visible) = tuple;
if (!visible)
continue;
MVP = cameraMatrix * modelMatrix;
depthMVP = depthCameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
@@ -267,7 +271,8 @@ void Renderer::DrawShadowMap()
{
Model* model;
glm::mat4 modelMatrix;
std::tie(model, modelMatrix) = tuple;
bool visible;
std::tie(model, modelMatrix, visible) = tuple;
MVP = depthCamera * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramShadows.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
@@ -296,26 +301,26 @@ void Renderer::DrawDebugShadowMap()
void Renderer::DrawModels(ShaderProgram &shader)
{
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix();
/*glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix();
glm::mat4 MVP;
for (auto tuple : ModelsToRender)
{
Model* model;
glm::mat4 modelMatrix;
std::tie(model, modelMatrix) = tuple;
Model* model;
glm::mat4 modelMatrix;
std::tie(model, modelMatrix) = tuple;
MVP = cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix()));
glBindVertexArray(model->VAO);
for (auto texGroup : model->TextureGroups) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texGroup.Texture->texture);
glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1);
}
MVP = cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix()));
glBindVertexArray(model->VAO);
for (auto texGroup : model->TextureGroups) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texGroup.Texture->texture);
glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1);
}
}*/
}
void Renderer::DrawText()
@@ -328,11 +333,11 @@ void Renderer::AddTextToDraw()
//Add to draw shit vector
}
void Renderer::AddModelToDraw(std::shared_ptr<Model> model, glm::vec3 position, glm::quat orientation, glm::vec3 scale)
void Renderer::AddModelToDraw(std::shared_ptr<Model> model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible)
{
glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale);
// You can now use ModelMatrix to build the MVP matrix
ModelsToRender.push_back(std::make_tuple(model.get(), modelMatrix));
ModelsToRender.push_back(std::make_tuple(model.get(), modelMatrix, visible));
}
void Renderer::AddPointLightToDraw(
+2 -2
View File
@@ -30,7 +30,7 @@ public:
int HEIGHT, WIDTH;
std::list<std::tuple<Model*, glm::mat4>> ModelsToRender;
std::list<std::tuple<Model*, glm::mat4, bool>> ModelsToRender;
int Lights;
std::vector<float> Light_position;
std::vector<float> Light_specular;
@@ -47,7 +47,7 @@ public:
void Draw(double dt);
void DrawText();
void AddModelToDraw(std::shared_ptr<Model> model, glm::vec3 position, glm::quat orientation, glm::vec3 scale);
void AddModelToDraw(std::shared_ptr<Model> model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible);
void AddTextToDraw();
void AddPointLightToDraw(
glm::vec3 _position,
+2
View File
@@ -13,6 +13,8 @@ public:
System(World* world) : m_World(world) { }
virtual ~System() { }
virtual void Initialize() { }
// Called once per system every tick
virtual void Update(double dt) { }
// Called once for every entity in the world every tick
@@ -93,6 +93,7 @@ void Systems::LevelGenerationSystem::Update( double dt )
for(auto ent : obstacles)
{
auto transform = m_World->GetComponent<Components::Transform>(ent, "Transform");
auto model = m_World->GetComponent<Components::Model>(ent, "Model");
transform->Velocity.z = velocity;
transform->Position += transform->Velocity * (float)dt;
@@ -103,7 +104,12 @@ void Systems::LevelGenerationSystem::Update( double dt )
transform->Velocity.y = 0;
}
if(transform->Position.z > 800)
if (transform->Position.z > 10 && model->Visible)
{
model->Visible = false;
}
if(transform->Position.z > 2000)
{
removethis.push_back(ent);
}
+13 -3
View File
@@ -25,7 +25,9 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p
}
auto model = m_CachedModels[modelComponent->ModelFile];
m_Renderer->AddModelToDraw(model, transformComponent->Position, transformComponent->Orientation, transformComponent->Scale);
glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity);
m_Renderer->AddModelToDraw(model, position, orientation, transformComponent->Scale, modelComponent->Visible);
}
// Debug draw bounds
@@ -33,7 +35,7 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p
auto collision = m_World->GetComponent<Components::Collision>(entity, "Collision");
auto bounds = m_World->GetComponent<Components::Bounds>(entity, "Bounds");
if (bounds != nullptr) {
glm::vec3 origin = transformComponent->Position + (transformComponent->Scale * bounds->Origin);
glm::vec3 origin = m_TransformSystem->AbsolutePosition(entity) + (transformComponent->Scale * bounds->Origin);
glm::vec3 volumeVector = transformComponent->Scale * bounds->VolumeVector;
m_Renderer->AddAABBToDraw(origin, volumeVector, (collision != nullptr && collision->CollidingEntities.size() > 0));
}
@@ -42,8 +44,9 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p
auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity, "PointLight");
if (pointLightComponent != nullptr)
{
glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
m_Renderer->AddPointLightToDraw(
transformComponent->Position,
position,
pointLightComponent->Specular,
pointLightComponent->Diffuse,
pointLightComponent->constantAttenuation,
@@ -51,6 +54,7 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p
pointLightComponent->quadraticAttenuation,
pointLightComponent->spotExponent);
}
auto cameraComponent = m_World->GetComponent<Components::Camera>(entity, "Camera");
if (cameraComponent != nullptr)
{
@@ -63,6 +67,12 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p
}
}
void Systems::RenderSystem::Initialize()
{
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>("TransformSystem");
}
+4 -1
View File
@@ -4,6 +4,7 @@
#include <unordered_map>
#include "System.h"
#include "Systems/TransformSystem.h"
#include "Model.h"
#include "Texture.h"
#include "Components/Model.h"
@@ -22,6 +23,8 @@ namespace Systems
RenderSystem(World* world, std::shared_ptr<Renderer> renderer)
: System(world), m_Renderer(renderer){ }
void Initialize() override;
std::unordered_map<std::string, std::shared_ptr<Model>> m_CachedModels;
void OnComponentCreated(std::string type, std:: shared_ptr<Component> component) override;
@@ -29,7 +32,7 @@ namespace Systems
private:
std::shared_ptr<Renderer> m_Renderer;
std::shared_ptr<Systems::TransformSystem> m_TransformSystem;
};
@@ -0,0 +1,47 @@
#include "TransformSystem.h"
#include "World.h"
//void Systems::TransformSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
//{
// if (parent == 0)
// return;
//
// auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
// auto parentTransform = m_World->GetComponent<Components::Transform>(parent, "Transform");
//
// transform->Position = parentTransform->Position + transform->RelativePosition;
//}
glm::vec3 Systems::TransformSystem::AbsolutePosition(EntityID entity)
{
glm::vec3 absPosition;
glm::quat accumulativeOrientation;
do
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
//absPosition += transform->Position;
entity = m_World->GetEntityParent(entity);
auto transform2 = m_World->GetComponent<Components::Transform>(entity, "Transform");
if (entity != 0)
absPosition += transform2->Orientation * transform->Position;
else
absPosition += transform->Position;
} while (entity != 0);
return absPosition * accumulativeOrientation;
}
glm::quat Systems::TransformSystem::AbsoluteOrientation(EntityID entity)
{
glm::quat absOrientation;
do
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
absOrientation *= transform->Orientation;
entity = m_World->GetEntityParent(entity);
} while (entity != 0);
return absOrientation;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef TransformSystem_h__
#define TransformSystem_h__
#include "System.h"
#include "Components/Transform.h"
namespace Systems
{
class TransformSystem : public System
{
public:
TransformSystem(World* world)
: System(world) { }
//void Update(double dt) override;
//void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
glm::vec3 AbsolutePosition(EntityID entity);
glm::quat AbsoluteOrientation(EntityID entity);
};
}
#endif // TransformSystem_h__
+5 -10
View File
@@ -107,19 +107,14 @@ World::World()
void World::Initialize()
{
RegisterSystems();
AddSystems();
for (auto system : m_Systems) {
system.second->Initialize();
}
RegisterComponents();
}
void World::RegisterSystems()
{
}
void World::RegisterComponents()
{
}
std::shared_ptr<Component> World::AddComponent(EntityID entity, std::string componentType)
{
return AddComponent<Component>(entity, componentType);
+4 -2
View File
@@ -25,8 +25,10 @@ public:
virtual void Initialize();
virtual void RegisterComponents();
virtual void RegisterSystems();
virtual void RegisterSystems() = 0;
virtual void AddSystems() = 0;
virtual void RegisterComponents() = 0;
void AddSystem(std::string systemType);
template <class T>