Added basis for point light
This commit is contained in:
parent
862ef6d34b
commit
b51c5a7557
|
@ -221,6 +221,9 @@ int main(int argc, char** argv)
|
||||||
oglu::Object lightSource(cubeDefault);
|
oglu::Object lightSource(cubeDefault);
|
||||||
lightSource.SetScale(glm::vec3(0.1f));
|
lightSource.SetScale(glm::vec3(0.1f));
|
||||||
|
|
||||||
|
oglu::PointLight pointLight;
|
||||||
|
pointLight.LinkPositionToTransformable(lightSource);
|
||||||
|
|
||||||
// Create a shader
|
// Create a shader
|
||||||
oglu::Shader shader, lightSourceShader;
|
oglu::Shader shader, lightSourceShader;
|
||||||
try
|
try
|
||||||
|
@ -266,7 +269,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
shader->SetUniform("ambientColor", "ambientStrength", ambient);
|
shader->SetUniform("ambientColor", "ambientStrength", ambient);
|
||||||
shader->SetUniform3fv("lightPos", 1, glm::value_ptr(lightSource.GetPosition()));
|
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("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix()));
|
||||||
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection()));
|
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection()));
|
||||||
|
@ -279,10 +282,10 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
lightSourceShader->Use();
|
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("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix()));
|
||||||
lightSourceShader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection()));
|
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();
|
lightSource.Render();
|
||||||
|
|
||||||
ImGui::Begin("Test");
|
ImGui::Begin("Test");
|
||||||
|
@ -305,6 +308,15 @@ int main(int argc, char** argv)
|
||||||
ImGui::TreePop();
|
ImGui::TreePop();
|
||||||
ImGui::Separator();
|
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();
|
ImGui::End();
|
||||||
|
|
34
include/lighting/point.hpp
Normal file
34
include/lighting/point.hpp
Normal 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
|
|
@ -16,6 +16,7 @@
|
||||||
#include <camera.hpp>
|
#include <camera.hpp>
|
||||||
|
|
||||||
#include <lighting/ambient.hpp>
|
#include <lighting/ambient.hpp>
|
||||||
|
#include <lighting/point.hpp>
|
||||||
|
|
||||||
namespace oglu
|
namespace oglu
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
|
|
||||||
namespace oglu
|
namespace oglu
|
||||||
{
|
{
|
||||||
|
class PointLight;
|
||||||
/**
|
/**
|
||||||
* @brief Defines position, rotation and scale.
|
* @brief Defines position, rotation and scale.
|
||||||
*
|
*
|
||||||
|
@ -23,6 +24,9 @@ namespace oglu
|
||||||
*/
|
*/
|
||||||
class OGLU_API Transformable
|
class OGLU_API Transformable
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
friend class PointLight;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Create new identity transformation.
|
* @brief Create new identity transformation.
|
||||||
|
@ -349,7 +353,7 @@ namespace oglu
|
||||||
*
|
*
|
||||||
* @return An array of 16 floats representing the transformation matrix
|
* @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.
|
* @brief Returns a normal matrix.
|
||||||
|
|
48
src/lighting/point.cpp
Normal file
48
src/lighting/point.cpp
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -183,9 +183,9 @@ namespace oglu
|
||||||
recalculateMatrix = true;
|
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::mat4(1.0f);
|
||||||
transformation = glm::translate(transformation, translation);
|
transformation = glm::translate(transformation, translation);
|
||||||
|
|
Loading…
Reference in a new issue