Added basis for point light

This commit is contained in:
Robert 2021-01-26 16:03:41 +01:00
parent 862ef6d34b
commit b51c5a7557
6 changed files with 105 additions and 6 deletions

View file

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

View file

@ -0,0 +1,34 @@
#ifndef POINT_HPP
#define POINT_HPP
#include <core.hpp>
#include <color.hpp>
#include <glm/glm.hpp>
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

View file

@ -16,6 +16,7 @@
#include <camera.hpp>
#include <lighting/ambient.hpp>
#include <lighting/point.hpp>
namespace oglu
{

View file

@ -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.

48
src/lighting/point.cpp Normal file
View file

@ -0,0 +1,48 @@
#include "lighting/point.hpp"
#include <transformable.hpp>
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]);
}
}

View file

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