From 5c51de37a636029a074544df1bc9e2861232d6cf Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 28 Jan 2021 01:07:19 +0100 Subject: [PATCH] improved phong lighting --- examples/movement/main.cpp | 41 +++++++++---------- examples/movement/shaders/fragmentShader.frag | 22 +++++----- include/lighting/point.hpp | 5 ++- src/lighting/point.cpp | 8 ++-- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/examples/movement/main.cpp b/examples/movement/main.cpp index a592e33..65d041d 100644 --- a/examples/movement/main.cpp +++ b/examples/movement/main.cpp @@ -272,9 +272,10 @@ int main(int argc, char** argv) shader->SetUniformTexture("texture1", crate, 0); shader->SetUniformTexture("texture2", opengl, 1); - shader->SetUniform("ambientColor", "ambientStrength", ambient); - shader->SetUniform3fv("lightPos", 1, glm::value_ptr(lightSource.GetPosition())); - shader->SetUniform("lightColor", pointLight.color, true); + shader->SetUniform("light.ambient", "light.ambientStrength", ambient); + shader->SetUniform3fv("light.position", 1, glm::value_ptr(lightSource.GetPosition())); + shader->SetUniform("light.diffuse", pointLight.diffusionColor, true); + shader->SetUniform("light.specular", pointLight.specularColor, true); shader->SetUniform3fv("viewPos", 1, glm::value_ptr(camera.GetPosition())); @@ -297,11 +298,12 @@ int main(int argc, char** argv) 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", pointLight.color, true); + lightSourceShader->SetUniform("color", pointLight.diffusionColor, true); lightSource.Render(); - ImGui::Begin("Test"); + ImGui::Begin("Controls"); + ImGui::SetNextItemOpen(true, ImGuiCond_Once); if(ImGui::CollapsingHeader("Scene")); { ImGui::ColorEdit3("Background color", &bgColor.r); @@ -310,10 +312,10 @@ int main(int argc, char** argv) ImGui::SliderFloat("zFar", &camera.zFar, 2.0f, 100.0f); } - ImGui::SetNextItemOpen(true); + ImGui::SetNextItemOpen(true, ImGuiCond_Once); if(ImGui::CollapsingHeader("Lighting")) { - ImGui::SetNextItemOpen(true); + ImGui::SetNextItemOpen(true, ImGuiCond_Once); if (ImGui::TreeNode("Ambient")) { ImGui::ColorEdit3("Color", &ambient.color.r); @@ -323,27 +325,24 @@ int main(int argc, char** argv) ImGui::Separator(); } - ImGui::SetNextItemOpen(true); + ImGui::SetNextItemOpen(true, ImGuiCond_Once); if (ImGui::TreeNode("Point")) { - ImGui::ColorEdit3("Color", &pointLight.color.r); + ImGui::ColorEdit3("Diffusion", &pointLight.diffusionColor.r); + ImGui::ColorEdit3("Specular", &pointLight.specularColor.r); ImGui::SliderFloat3("Position", pointLight.GetPositionPointer(), -5.0f, 5.0f); ImGui::TreePop(); - ImGui::Separator(); } + } - ImGui::SetNextItemOpen(true); - if (ImGui::TreeNode("Cube Material")) - { - ImGui::ColorEdit3("Ambient", &(cubeMaterial->ambient.r)); - ImGui::ColorEdit3("Diffuse", &(cubeMaterial->diffuse.r)); - ImGui::ColorEdit3("Specular", &(cubeMaterial->specular.r)); - ImGui::SliderFloat("Shininess", &(cubeMaterial->shininess), 1.0f, 256.0f); - - ImGui::TreePop(); - ImGui::Separator(); - } + ImGui::SetNextItemOpen(true, ImGuiCond_Once); + if (ImGui::CollapsingHeader("Cube Material")) + { + ImGui::ColorEdit3("Ambient", &(cubeMaterial->ambient.r)); + ImGui::ColorEdit3("Diffuse", &(cubeMaterial->diffuse.r)); + ImGui::ColorEdit3("Specular", &(cubeMaterial->specular.r)); + ImGui::SliderFloat("Shininess", &(cubeMaterial->shininess), 1.0f, 256.0f); } ImGui::End(); diff --git a/examples/movement/shaders/fragmentShader.frag b/examples/movement/shaders/fragmentShader.frag index 7ddd62d..ba636c7 100644 --- a/examples/movement/shaders/fragmentShader.frag +++ b/examples/movement/shaders/fragmentShader.frag @@ -5,6 +5,13 @@ struct Material float shininess; }; +struct Light +{ + vec3 position; + vec3 ambient, diffuse, specular; + float ambientStrength; +}; + in vec2 oUV; in vec3 oNormal; in vec3 oFragPos; @@ -14,34 +21,29 @@ out vec4 FragColor; uniform sampler2D texture1; uniform sampler2D texture2; -uniform float ambientStrength; -uniform vec3 ambientColor; - -uniform vec3 lightPos; -uniform vec3 lightColor; - uniform vec3 viewPos; uniform Material material; +uniform Light light; void main() { // Ambient light - vec3 ambient = ambientColor * ambientStrength * material.ambient; + vec3 ambient = light.ambient * light.ambientStrength * material.ambient; vec3 norm = normalize(oNormal); // Diffuse light - vec3 lightDir = normalize(lightPos - oFragPos); + vec3 lightDir = normalize(light.position - oFragPos); float diff = max(dot(norm, lightDir), 0.0); - vec3 diffuse = (diff * material.diffuse) * lightColor; + vec3 diffuse = (diff * material.diffuse) * light.diffuse; // Specular light vec3 viewDir = normalize(viewPos - oFragPos); vec3 reflectDir = reflect(-lightDir, norm); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); - vec3 specular = (material.specular * spec) * lightColor; + vec3 specular = (material.specular * spec) * light.specular; vec4 objColor = mix(texture(texture1, oUV), texture(texture2, oUV), 0.2); diff --git a/include/lighting/point.hpp b/include/lighting/point.hpp index 743a414..b43d2fa 100644 --- a/include/lighting/point.hpp +++ b/include/lighting/point.hpp @@ -13,7 +13,7 @@ namespace oglu { public: PointLight(); - PointLight(const glm::vec3& position, const Color& color); + PointLight(const glm::vec3& position, const Color& diffusionColor = Color::White, const Color& specularColor = Color::White); PointLight(const PointLight& other); ~PointLight(); @@ -23,7 +23,8 @@ namespace oglu float* GetPositionPointer(); public: - Color color; + Color diffusionColor; + Color specularColor; private: diff --git a/src/lighting/point.cpp b/src/lighting/point.cpp index 69bdd36..5fa456e 100644 --- a/src/lighting/point.cpp +++ b/src/lighting/point.cpp @@ -5,18 +5,18 @@ namespace oglu { PointLight::PointLight() : - position(new glm::vec3(0.0f)), color(oglu::Color::White) + position(new glm::vec3(0.0f)), diffusionColor(oglu::Color::White), specularColor(oglu::Color::White) { } - PointLight::PointLight(const glm::vec3& position, const Color& color) : - position(new glm::vec3(0.0f)), color(color) + PointLight::PointLight(const glm::vec3& position, const Color& diffusionColor, const Color& specularColor) : + position(new glm::vec3(0.0f)), diffusionColor(diffusionColor), specularColor(specularColor) { memcpy(this->position, &position, sizeof(glm::vec3)); } PointLight::PointLight(const PointLight& other) : - position(new glm::vec3(0.0f)), color(other.color) + position(new glm::vec3(0.0f)), diffusionColor(other.diffusionColor), specularColor(other.specularColor) { memcpy(this->position, position, sizeof(glm::vec3)); }