Permalink
Browse files

transforms

  • Loading branch information...
MattRyder committed Jul 26, 2018
1 parent 5f99ac0 commit bf7093796e5927cc904cd603164361e30335ec6e
@@ -81,10 +81,10 @@ namespace GibEditor
{
ImGui::Dummy(ImVec2(ImGui::GetWindowWidth(), 20));

glm::mat4 matrix = entity->GetLocalTransform();
glm::vec3 pos = glm::vec3(matrix[3][0], matrix[3][1], matrix[3][2]);
glm::mat4 matrix = entity->GetLocalTransform().GetTransformMatrix();
glm::vec3 pos = entity->GetLocalTransform().GetPosition();
glm::quat rot = glm::quat();//entity->GetRotation();
glm::vec3 scale = glm::vec3(matrix[0][0], matrix[1][1], matrix[2][2]);
glm::vec3 scale = entity->GetLocalTransform().GetScale();

if (ImGui::DragFloat3("Position", glm::value_ptr(pos), INCREMENT_SLOW, -1000.0f, 1000.0f))
{
@@ -160,6 +160,7 @@ set(ROOT_INCLUDES
include/Shader.h
include/Skybox.h
include/Texture.h
include/Transform.h
include/LightBase.h
include/PointLight.h)

@@ -9,6 +9,7 @@
#include <glm/vec3.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include "Transform.h"
#include "EnumFlags.h"
#include "IEntity.h"
#include "input/InputManager.h"
@@ -75,18 +76,15 @@ namespace GibEngine
const std::string& GetTypeName() const;

const std::string& GetNameKey() const;
const glm::mat4& GetWorldTransform() const;
const glm::mat4& GetLocalTransform() const;
Transform GetWorldTransform() const;
Transform GetLocalTransform() const;

const unsigned int GetChildCount() const;

void RecalculateWorldTransform();

static const std::string GetTypeString(Type type);

// Inherited from IEntity
virtual glm::vec3 GetPosition() const;

virtual void RegisterEvents(Event::EventManager* const eventManager) override;
virtual void OnTick(float deltaTime, Event::OnTickEvent& e);
virtual void OnKeyDown(float deltaTime, Event::KeyDownEvent& e);
@@ -107,14 +105,13 @@ namespace GibEngine
BaseEntity const* parent;
std::vector<std::shared_ptr<BaseEntity>> children;

//glm::vec3 position;
std::string name;
BaseEntity::Type type;

std::string nameKey;

glm::mat4 worldTransform;
glm::mat4 localTransform;
Transform worldTransform;
Transform localTransform;
glm::quat rotationQuaternion;

glm::vec3 upVector;
@@ -126,5 +123,9 @@ namespace GibEngine
static int _Id;

Direction activeDirections;

// Inherited from IEntity, unrequired
virtual glm::vec3 GetPosition() const;

};
}
@@ -0,0 +1,50 @@
#pragma once

#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>

namespace GibEngine
{
class Transform
{
glm::mat4 transform;

public:

Transform(const glm::mat4& transformMatrix) : transform(transformMatrix) { }

Transform() : Transform(glm::mat4(1.0)) { }

void SetTransform(const glm::mat4 transform)
{
this->transform = transform;
}

void Scale(const glm::vec3& scale)
{
this->SetTransform(glm::scale(transform, scale));
}

void SetPosition(const glm::vec3& position)
{
auto tmp = transform;
tmp[3] = glm::vec4(position, tmp[3][3]);
this->SetTransform(tmp);
}

glm::mat4 GetTransformMatrix() const
{
return transform;
}

glm::vec3 GetPosition() const
{
return glm::vec3(transform[3]);
}

glm::vec3 GetScale() const
{
return glm::vec3(transform[0][0], transform[1][1], transform[2][2]);
}
};
}
@@ -11,7 +11,7 @@ namespace GibEngine
{
class VisibleSet
{
typedef std::map<const std::shared_ptr<Mesh>, std::vector<glm::mat4>> MeshInstanceMap;
typedef std::map<const std::shared_ptr<Mesh>, std::vector<Transform>> MeshInstanceMap;

std::shared_ptr<BaseEntity> rootEntity;
std::shared_ptr<Skybox> skyboxNode;
@@ -76,12 +76,12 @@ GibEngine::BaseEntity::Type GibEngine::BaseEntity::GetType() const
return this->type;
}

const glm::mat4& GibEngine::BaseEntity::GetWorldTransform() const
GibEngine::Transform GibEngine::BaseEntity::GetWorldTransform() const
{
return worldTransform;
}

const glm::mat4& GibEngine::BaseEntity::GetLocalTransform() const
GibEngine::Transform GibEngine::BaseEntity::GetLocalTransform() const
{
return localTransform;
}
@@ -93,7 +93,7 @@ const unsigned int GibEngine::BaseEntity::GetChildCount() const

glm::vec3 GibEngine::BaseEntity::GetPosition() const
{
return glm::vec3(GetLocalTransform()[3].x, GetLocalTransform()[3].y, GetLocalTransform()[3].z);
return glm::vec3();
}

std::string GibEngine::BaseEntity::GetName() const
@@ -110,9 +110,8 @@ void GibEngine::BaseEntity::AddChild(const std::shared_ptr<BaseEntity> childEnti

void GibEngine::BaseEntity::SetPosition(const glm::vec3 position)
{
auto lt = localTransform;
lt[3] = glm::vec4(position, 1.0);
SetLocalTransform(lt);
localTransform.SetPosition(position);
RecalculateWorldTransform();
}

void GibEngine::BaseEntity::SetName(const std::string name)
@@ -122,20 +121,26 @@ void GibEngine::BaseEntity::SetName(const std::string name)

void GibEngine::BaseEntity::SetLocalTransform(const glm::mat4 localTransform)
{
this->localTransform = localTransform;
this->localTransform.SetTransform(localTransform);
RecalculateWorldTransform();
}

void GibEngine::BaseEntity::RecalculateWorldTransform()
{
const auto& rotationMatrix = glm::toMat4(rotationQuaternion);
const auto& localTransMatrix = localTransform.GetTransformMatrix();

if (parent != nullptr)
{
worldTransform = parent->GetWorldTransform() * glm::toMat4(rotationQuaternion) * localTransform;
const auto& parentWorldTransMatrix = parent->GetWorldTransform().GetTransformMatrix();
worldTransform.SetTransform(parentWorldTransMatrix * rotationMatrix * localTransMatrix);
//worldTransform = parentWorldTransMatrix * glm::toMat4(rotationQuaternion) * localTransform;
}
else
{
// Root node's word trans is the local trans:
worldTransform = glm::toMat4(rotationQuaternion) * localTransform;
worldTransform.SetTransform(rotationMatrix * localTransMatrix);
//worldTransform.SetTransform(glm::toMat4(rotationQuaternion) * localTransform;
}

for (auto child : children)
@@ -146,7 +151,6 @@ void GibEngine::BaseEntity::RecalculateWorldTransform()

void GibEngine::BaseEntity::Translate(const glm::vec3& translation)
{
SetLocalTransform(glm::translate(localTransform, translation));
}

void GibEngine::BaseEntity::Rotate(const float angle, const glm::vec3& axis)
@@ -172,7 +176,7 @@ void GibEngine::BaseEntity::RotateZ(const float angle)

void GibEngine::BaseEntity::Scale(const glm::vec3& scale)
{
SetLocalTransform(glm::scale(localTransform, scale));
localTransform.Scale(scale);
}

glm::vec3 GibEngine::BaseEntity::GetFront() const
@@ -183,7 +187,7 @@ glm::vec3 GibEngine::BaseEntity::GetFront() const
void GibEngine::BaseEntity::OnTick(float deltaTime, Event::OnTickEvent & e)
{
const float MOVEMENT_SPEED = 15.0f * deltaTime;
glm::vec3 position = GetPosition();
glm::vec3 position = GetWorldTransform().GetPosition();

bool positionChanged = false;
auto activeDir = static_cast<int>(activeDirections);
@@ -202,20 +206,20 @@ void GibEngine::BaseEntity::OnTick(float deltaTime, Event::OnTickEvent & e)
positionChanged = true;

// Rotate Left
Rotate(MOVEMENT_SPEED, upVector);
//Rotate(MOVEMENT_SPEED, upVector);

// Strafe Left
//position -= glm::normalize(glm::cross(frontVector, upVector)) * MOVEMENT_SPEED;
position -= glm::normalize(glm::cross(frontVector, upVector)) * MOVEMENT_SPEED;
}
if (activeDir & static_cast<int>(Direction::RIGHT))
{
positionChanged = true;

// Rotate Left
Rotate(-MOVEMENT_SPEED, upVector);
//Rotate(-MOVEMENT_SPEED, upVector);

// Strafe Left
//position += glm::normalize(glm::cross(frontVector, upVector)) * MOVEMENT_SPEED;
position += glm::normalize(glm::cross(frontVector, upVector)) * MOVEMENT_SPEED;
}

if (positionChanged)
@@ -24,6 +24,5 @@ const glm::mat4& GibEngine::CameraBase::GetViewMatrix() const

void GibEngine::CameraBase::LookAt(const glm::vec3& target)
{

this->viewMatrix = glm::mat4(glm::lookAt(GetPosition(), target, upVector));
this->viewMatrix = glm::mat4(glm::lookAt(GetLocalTransform().GetPosition(), target, upVector));
}
@@ -6,7 +6,7 @@ GibEngine::FreeCamera::FreeCamera(int cameraWidth, int cameraHeight, float nearP
: CameraBase(BaseEntity::Type::CAMERA, cameraWidth, cameraHeight, nearPlane, farPlane, fieldOfViewDegrees)
{
this->cameraYaw = -90.0f;
LookAt(GetPosition() + frontVector);
LookAt(GetLocalTransform().GetPosition() + frontVector);
}

void GibEngine::FreeCamera::RegisterEvents(Event::EventManager* const eventManager)
@@ -23,12 +23,12 @@ void GibEngine::FreeCamera::OnTick(float deltaTime, Event::OnTickEvent & e)
{
BaseEntity::OnTick(deltaTime, e);

auto back = parent->GetPosition();
auto back = parent->GetLocalTransform().GetPosition();
back -= parent->GetFront() * 20.0f;
back += upVector * 5.0f;

SetPosition(back);
this->viewMatrix = glm::lookAt(GetPosition(), parent->GetPosition(), upVector);
this->viewMatrix = glm::lookAt(GetLocalTransform().GetPosition(), parent->GetLocalTransform().GetPosition(), upVector);
}

void GibEngine::FreeCamera::OnMouseMove(float deltaTime, Event::MouseMoveEvent& e)
@@ -52,5 +52,5 @@ void GibEngine::FreeCamera::OnMouseMove(float deltaTime, Event::MouseMoveEvent&

frontVector = front;

LookAt(GetPosition() + frontVector);
LookAt(GetLocalTransform().GetPosition() + frontVector);
}
@@ -95,8 +95,7 @@ void GibEngine::Renderer::RenderPass::BindLights(const GibEngine::Scene::Visible

if (light->GetType() != BaseEntity::Type::DIRECTIONAL_LIGHT)
{
auto lightWorldTrans = light->GetWorldTransform();
auto lightPosition = glm::vec3(lightWorldTrans[3][0], lightWorldTrans[3][1], lightWorldTrans[3][2]);
auto lightPosition = light->GetWorldTransform().GetPosition();
BindLightUniform3f(position.c_str(), lightPosition);
}

@@ -21,7 +21,7 @@ void GibEngine::Renderer::SkyboxRenderPass::Render(const GibEngine::Scene::Visib
skyboxUniformLocation = graphicsApi->GetUniformLocation("skyboxModelMatrix");
}

glm::mat4 skyboxMatrix = skybox->GetWorldTransform();
glm::mat4 skyboxMatrix = skybox->GetWorldTransform().GetTransformMatrix();
glUniformMatrix4fv(skyboxUniformLocation, 1, GL_FALSE, glm::value_ptr(skyboxMatrix));

graphicsApi->BindTextureCubemap(0, skybox->GetTexture()->GetTextureId());
@@ -333,7 +333,7 @@ void GibEngine::Renderer::API::GL420::RegisterCamera(std::shared_ptr<CameraBase>

memcpy(&cameraData[PROJECTION_MATRIX_INDEX], glm::value_ptr(camera->GetProjectionMatrix()), sizeof(float) * 16);
memcpy(&cameraData[VIEW_MATRIX_INDEX], glm::value_ptr(camera->GetViewMatrix()), sizeof(float) * 16);
memcpy(&cameraData[POSITION_INDEX], glm::value_ptr(camera->GetPosition()), sizeof(float) * 3);
memcpy(&cameraData[POSITION_INDEX], glm::value_ptr(camera->GetLocalTransform().GetPosition()), sizeof(float) * 3);

cameraUBO->Update(cameraData);

@@ -418,7 +418,7 @@ bool GibEngine::Renderer::API::GL420::UpdateCamera(const CameraBase& camera)

memcpy(&cameraData[PROJECTION_MATRIX_INDEX], glm::value_ptr(camera.GetProjectionMatrix()), sizeof(float) * 16);
memcpy(&cameraData[VIEW_MATRIX_INDEX], glm::value_ptr(camera.GetViewMatrix()), sizeof(float) * 16);
memcpy(&cameraData[POSITION_INDEX], glm::value_ptr(camera.GetPosition()), sizeof(float) * 3);
memcpy(&cameraData[POSITION_INDEX], glm::value_ptr(camera.GetLocalTransform().GetPosition()), sizeof(float) * 3);

cameraUBO->Update(cameraData);

@@ -27,7 +27,7 @@ void GibEngine::Renderer::API::GLES3::BindCamera(GibEngine::CameraBase *camera)
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(camera->GetViewMatrix()));

int posLoc = glGetUniformLocation(currentShaderID, "camera.CameraPosition");
glUniform3fv(posLoc, 1, glm::value_ptr(camera->GetPosition()));
glUniform3fv(posLoc, 1, glm::value_ptr(camera->GetLocalTransform().GetPosition()));
}

void GibEngine::Renderer::API::GLES3::BindFramebuffer(const GibEngine::Renderer::Framebuffer& framebuffer)
@@ -20,7 +20,7 @@ void GibEngine::Scene::VisibleSet::AddMeshInstance(const std::shared_ptr<Mesh> m
}
else
{
meshInstances->insert(std::pair<const std::shared_ptr<Mesh>, std::vector<glm::mat4>>(meshNode, { meshNode->GetWorldTransform() }));
meshInstances->insert(std::pair<const std::shared_ptr<Mesh>, std::vector<Transform>>(meshNode, { meshNode->GetWorldTransform() }));
}
}

@@ -97,7 +97,13 @@ void GibEngine::Scene::VisibleSet::Parse(std::shared_ptr<Renderer::API::IGraphic
const auto mesh = iter->first;
if (mesh->GetMeshUploadTicket())
{
graphicsApi->UpdateMeshInstances(*mesh->GetMeshUploadTicket(), iter->second);
auto instanceMatrixList = std::vector<glm::mat4>();
for (auto i = 0; i < iter->second.size(); i++)
{
instanceMatrixList.push_back(iter->second.at(i).GetTransformMatrix());
}

graphicsApi->UpdateMeshInstances(*mesh->GetMeshUploadTicket(), instanceMatrixList);
}
}
}
@@ -29,14 +29,16 @@ add_executable(GibTest
include/LightBaseTest.h
# include/MeshServiceTest.h
include/AABBTest.h
include/TransformTest.h
src/EntityTest.cpp
src/WorldDatabaseTest.cpp
src/ControllerTest.cpp
src/InputManagerTest.cpp
src/FreeCameraTest.cpp
src/LightBaseTest.cpp
#src/MeshServiceTest.cpp
src/AABBTest.cpp)
src/AABBTest.cpp
src/TransformTest.cpp)

target_link_libraries(GibTest GibEngine ${GTEST_BOTH_LIBRARIES})

Oops, something went wrong.

0 comments on commit bf70937

Please sign in to comment.