From b51c5a75578364e14dc0ca1a4e345aea7865fd56 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 26 Jan 2021 16:03:41 +0100 Subject: [PATCH] Added basis for point light --- examples/movement/main.cpp | 18 +++++++++++--- include/lighting/point.hpp | 34 +++++++++++++++++++++++++++ include/openglu.hpp | 1 + include/transformable.hpp | 6 ++++- src/lighting/point.cpp | 48 ++++++++++++++++++++++++++++++++++++++ src/transformable.cpp | 4 ++-- 6 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 include/lighting/point.hpp create mode 100644 src/lighting/point.cpp diff --git a/examples/movement/main.cpp b/examples/movement/main.cpp index 84ce3d0..c4549d6 100644 --- a/examples/movement/main.cpp +++ b/examples/movement/main.cpp @@ -221,6 +221,9 @@ int main(int argc, char** argv) oglu::Object lightSource(cubeDefault); lightSource.SetScale(glm::vec3(0.1f)); + oglu::PointLight pointLight; + pointLight.LinkPositionToTransformable(lightSource); + // Create a shader oglu::Shader shader, lightSourceShader; try @@ -266,7 +269,7 @@ int main(int argc, char** argv) shader->SetUniform("ambientColor", "ambientStrength", ambient); shader->SetUniform3fv("lightPos", 1, glm::value_ptr(lightSource.GetPosition())); - shader->SetUniform("lightColor", oglu::Color::White, true); + shader->SetUniform("lightColor", pointLight.color, true); shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix())); shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection())); @@ -279,10 +282,10 @@ int main(int argc, char** argv) } lightSourceShader->Use(); - lightSourceShader->SetUniform("model", lightSource); + lightSourceShader->SetUniformMatrix4fv("model", 1, GL_FALSE, glm::value_ptr(lightSource.GetMatrix(true))); lightSourceShader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix())); lightSourceShader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection())); - lightSourceShader->SetUniform("color", oglu::Color::White, true); + lightSourceShader->SetUniform("color", pointLight.color, true); lightSource.Render(); ImGui::Begin("Test"); @@ -305,6 +308,15 @@ int main(int argc, char** argv) ImGui::TreePop(); ImGui::Separator(); } + + if (ImGui::TreeNode("Point")) + { + ImGui::ColorEdit3("Color", &pointLight.color.r); + ImGui::SliderFloat3("Position", pointLight.GetPositionPointer(), -5.0f, 5.0f); + + ImGui::TreePop(); + ImGui::Separator(); + } } ImGui::End(); diff --git a/include/lighting/point.hpp b/include/lighting/point.hpp new file mode 100644 index 0000000..da1c280 --- /dev/null +++ b/include/lighting/point.hpp @@ -0,0 +1,34 @@ +#ifndef POINT_HPP +#define POINT_HPP + +#include +#include +#include + +namespace oglu +{ + class Transformable; + + class OGLU_API PointLight + { + public: + PointLight(); + PointLight(const glm::vec3& position, const Color& color); + PointLight(const PointLight& other); + + ~PointLight(); + + void LinkPositionToTransformable(Transformable& link); + void UnlinkPositionFromTransformable(); + float* GetPositionPointer(); + + public: + Color color; + + private: + glm::vec3* position; + bool isLinked; + }; +} + +#endif \ No newline at end of file diff --git a/include/openglu.hpp b/include/openglu.hpp index af5add8..e9ad54a 100644 --- a/include/openglu.hpp +++ b/include/openglu.hpp @@ -16,6 +16,7 @@ #include #include +#include namespace oglu { diff --git a/include/transformable.hpp b/include/transformable.hpp index 74c6179..3dabb50 100644 --- a/include/transformable.hpp +++ b/include/transformable.hpp @@ -15,6 +15,7 @@ namespace oglu { + class PointLight; /** * @brief Defines position, rotation and scale. * @@ -23,6 +24,9 @@ namespace oglu */ class OGLU_API Transformable { + private: + friend class PointLight; + public: /** * @brief Create new identity transformation. @@ -349,7 +353,7 @@ namespace oglu * * @return An array of 16 floats representing the transformation matrix */ - virtual const glm::mat4& GetMatrix(); + virtual const glm::mat4& GetMatrix(bool forceRecalc = false); /** * @brief Returns a normal matrix. diff --git a/src/lighting/point.cpp b/src/lighting/point.cpp new file mode 100644 index 0000000..69bdd36 --- /dev/null +++ b/src/lighting/point.cpp @@ -0,0 +1,48 @@ +#include "lighting/point.hpp" + +#include + +namespace oglu +{ + PointLight::PointLight() : + position(new glm::vec3(0.0f)), color(oglu::Color::White) + { + } + + PointLight::PointLight(const glm::vec3& position, const Color& color) : + position(new glm::vec3(0.0f)), color(color) + { + memcpy(this->position, &position, sizeof(glm::vec3)); + } + + PointLight::PointLight(const PointLight& other) : + position(new glm::vec3(0.0f)), color(other.color) + { + memcpy(this->position, position, sizeof(glm::vec3)); + } + + PointLight::~PointLight() + { + if(!isLinked) + delete position; + } + + void PointLight::LinkPositionToTransformable(Transformable& link) + { + if (!isLinked) + delete position; + + position = &link.translation; + } + + void PointLight::UnlinkPositionFromTransformable() + { + if (isLinked) + position = new glm::vec3(0.0f); + } + + float* PointLight::GetPositionPointer() + { + return &((*position)[0]); + } +} diff --git a/src/transformable.cpp b/src/transformable.cpp index 5502638..d683c67 100644 --- a/src/transformable.cpp +++ b/src/transformable.cpp @@ -183,9 +183,9 @@ namespace oglu recalculateMatrix = true; } - const glm::mat4& Transformable::GetMatrix() + const glm::mat4& Transformable::GetMatrix(bool forceRecalc) { - if (recalculateMatrix) + if (recalculateMatrix || forceRecalc) { transformation = glm::mat4(1.0f); transformation = glm::translate(transformation, translation);