From 3f6e2c2b6b8a1d830515dc4541ce314a947e8edd Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 26 Jan 2021 14:44:41 +0100 Subject: [PATCH] Added ambient lighting --- examples/movement/main.cpp | 28 ++++++++++++++++++++++------ include/shader.hpp | 23 +++++++++++++++++++++++ src/shader.cpp | 16 ++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/examples/movement/main.cpp b/examples/movement/main.cpp index f903d77..2f4a090 100644 --- a/examples/movement/main.cpp +++ b/examples/movement/main.cpp @@ -225,8 +225,7 @@ int main(int argc, char** argv) shader->Use(); shader->SetUniformTexture("texture1", crate, 0); shader->SetUniformTexture("texture2", opengl, 1); - shader->SetUniform("ambientStrength", ambient.intensity); - shader->SetUniform("ambientColor", ambient.color, true); + shader->SetUniform("ambientColor", "ambientStrength", ambient); shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix())); shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection())); @@ -237,10 +236,27 @@ int main(int argc, char** argv) } ImGui::Begin("Test"); - ImGui::ColorEdit3("Background color", &bgColor.r); - ImGui::SliderFloat("FOV", &camera.fov, 30.0f, 100.0f); - ImGui::SliderFloat("zNear", &camera.zNear, 0.01, 1.0f); - ImGui::SliderFloat("zFar", &camera.zFar, 2.0f, 100.0f); + + if(ImGui::CollapsingHeader("Scene")); + { + ImGui::ColorEdit3("Background color", &bgColor.r); + ImGui::SliderFloat("FOV", &camera.fov, 30.0f, 100.0f); + ImGui::SliderFloat("zNear", &camera.zNear, 0.01, 1.0f); + ImGui::SliderFloat("zFar", &camera.zFar, 2.0f, 100.0f); + } + + if(ImGui::CollapsingHeader("Lighting")) + { + if (ImGui::TreeNode("Ambient")) + { + ImGui::ColorEdit3("Color", &ambient.color.r); + ImGui::SliderFloat("Intensity", &ambient.intensity, 0.0f, 1.0f); + + ImGui::TreePop(); + ImGui::Separator(); + } + } + ImGui::End(); ImGui::Render(); diff --git a/include/shader.hpp b/include/shader.hpp index 8325df5..cbe7bf8 100644 --- a/include/shader.hpp +++ b/include/shader.hpp @@ -16,6 +16,7 @@ namespace oglu class Color; class AbstractTexture; class Transformable; + class AmbientLight; typedef std::shared_ptr Texture; @@ -338,6 +339,28 @@ namespace oglu */ void SetUniform(GLint location, Transformable& v0, GLboolean transpose = GL_FALSE); + /** + * @brief Set the ambient lighting. + * + * This function sets two variables, the ambient color and the intensity + * + * @param[in] colorName The name of the uniform holding the color information (must be vec3) + * @param[in] intensityName The name of the uniform holding the intensity information (must be float) + * @param[in] v0 A reference to an AmbientLight + */ + void SetUniform(const GLchar* colorName, const GLchar* intensityName, const AmbientLight& v0); + + /** + * @brief Set the ambient lighting. + * + * This function sets two variables, the ambient color and the intensity + * + * @param[in] colorLocation The location of the uniform holding the color information (must be vec3) + * @param[in] intensityLocation The location of the uniform holding the intensity information (must be float) + * @param[in] v0 A reference to an AmbientLight + */ + void SetUniform(GLint colorLocation, GLint intensityLocation, const AmbientLight& v0); + /** * @brief Set array of uniform float. * diff --git a/src/shader.cpp b/src/shader.cpp index 5bfc247..9629bb0 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -259,6 +260,21 @@ namespace oglu glUniformMatrix4fv(location, 1, transpose, glm::value_ptr(v0.GetMatrix())); } + void AbstractShader::SetUniform(const GLchar* colorName, const GLchar* intensityName, const AmbientLight& v0) + { + SetUniform( + glGetUniformLocation(program, colorName), + glGetUniformLocation(program, intensityName), + v0 + ); + } + + void AbstractShader::SetUniform(GLint colorLocation, GLint intensityLocation, const AmbientLight& v0) + { + SetUniform(colorLocation, v0.color, true); + SetUniform(intensityLocation, v0.intensity); + } + void AbstractShader::SetUniform1fv(const GLchar* name, GLsizei count, const GLfloat* value) { glUniform1fv(glGetUniformLocation(program, name), count, value);