diff --git a/examples/movement/assets/crate.jpg b/examples/movement/assets/crate.jpg deleted file mode 100644 index 98a3f1a..0000000 Binary files a/examples/movement/assets/crate.jpg and /dev/null differ diff --git a/examples/movement/assets/metalbox_AO.png b/examples/movement/assets/metalbox_AO.png new file mode 100644 index 0000000..c2869eb Binary files /dev/null and b/examples/movement/assets/metalbox_AO.png differ diff --git a/examples/movement/assets/metalbox_bump.png b/examples/movement/assets/metalbox_bump.png new file mode 100644 index 0000000..94c0c36 Binary files /dev/null and b/examples/movement/assets/metalbox_bump.png differ diff --git a/examples/movement/assets/metalbox_diffuse.png b/examples/movement/assets/metalbox_diffuse.png new file mode 100644 index 0000000..7afc2d7 Binary files /dev/null and b/examples/movement/assets/metalbox_diffuse.png differ diff --git a/examples/movement/assets/metalbox_normal.png b/examples/movement/assets/metalbox_normal.png new file mode 100644 index 0000000..efe124a Binary files /dev/null and b/examples/movement/assets/metalbox_normal.png differ diff --git a/examples/movement/assets/opengl.png b/examples/movement/assets/opengl.png deleted file mode 100644 index 5be3706..0000000 Binary files a/examples/movement/assets/opengl.png and /dev/null differ diff --git a/examples/movement/main.cpp b/examples/movement/main.cpp index cf75cb2..808a5d6 100644 --- a/examples/movement/main.cpp +++ b/examples/movement/main.cpp @@ -186,9 +186,9 @@ int main(int argc, char** argv) oglu::SharedMaterial cubeMaterial(new oglu::Material); //cubeMaterial->AddProperty("ambient", oglu::Color::White); - cubeMaterial->AddProperty("diffuse", oglu::Color::White); - cubeMaterial->AddProperty("specular", oglu::Color::White); cubeMaterial->AddProperty("shininess", 32.f); + cubeMaterial->AddProperty("diffuse", oglu::MakeTexture("assets/metalbox_diffuse.png")); + cubeMaterial->AddProperty("specular", oglu::MakeTexture("assets/metalbox_bump.png")); oglu::Object cubes[10] = { oglu::Object(cubeDefault), @@ -247,10 +247,6 @@ int main(int argc, char** argv) return -1; } - // Create a texture - oglu::Texture crate = oglu::MakeTexture("assets/crate.jpg"); - oglu::Texture opengl = oglu::MakeTexture("assets/opengl.png"); - oglu::AmbientLight ambient; ambient.intensity = 0.1f; @@ -275,28 +271,29 @@ int main(int argc, char** argv) ImGui::NewFrame(); shader->Use(); - shader->SetUniformTexture("texture1", crate, 0); - shader->SetUniformTexture("texture2", opengl, 1); 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->SetUniform("light.constant", pointLight.constant); + shader->SetUniform("light.linear", pointLight.linear); + shader->SetUniform("light.quadratic", pointLight.quadratic); shader->SetUniform3fv("viewPos", 1, glm::value_ptr(camera.GetPosition())); shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix())); shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection())); + shader->SetUniformTexture("material.diffuse", cubeMaterial->GetPropertyValue("diffuse"), 0); + shader->SetUniformTexture("material.specular", cubeMaterial->GetPropertyValue("specular"), 1); + shader->SetUniform("material.shininess", cubeMaterial->GetPropertyValue("shininess")); + for (oglu::Object& cube : cubes) { shader->SetUniform("model", cube); shader->SetUniformMatrix3fv("normal", 1, GL_FALSE, glm::value_ptr(cube.GetNormalMatrix())); - shader->SetUniform("material.ambient", cube.material->GetPropertyValue("ambient"), true); - shader->SetUniform("material.diffuse", cube.material->GetPropertyValue("diffuse"), true); - shader->SetUniform("material.specular", cube.material->GetPropertyValue("specular"), true); - shader->SetUniform("material.shininess", cube.material->GetPropertyValue("shininess")); cube.Render(); } @@ -338,6 +335,16 @@ int main(int argc, char** argv) ImGui::ColorEdit3("Specular", &pointLight.specularColor.r); ImGui::SliderFloat3("Position", pointLight.GetPositionPointer(), -5.0f, 5.0f); + ImGui::SetNextItemOpen(true, ImGuiCond_Once); + if (ImGui::TreeNode("Attenuation")) + { + ImGui::SliderFloat("Constant", &pointLight.constant, 1.0f, 4.0f); + ImGui::SliderFloat("Linear", &pointLight.linear, 0.0014f, 0.7f); + ImGui::SliderFloat("Quadratic", &pointLight.quadratic, 0.00007f, 1.8f); + + ImGui::TreePop(); + } + ImGui::TreePop(); } } @@ -345,9 +352,6 @@ int main(int argc, char** argv) ImGui::SetNextItemOpen(true, ImGuiCond_Once); if (ImGui::CollapsingHeader("Cube Material")) { - ImGui::ColorEdit3("Ambient", &(cubeMaterial->GetProperty("ambient")->r)); - ImGui::ColorEdit3("Diffuse", &(cubeMaterial->GetProperty("diffuse")->r)); - ImGui::ColorEdit3("Specular", &(cubeMaterial->GetProperty("specular")->r)); ImGui::SliderFloat("Shininess", cubeMaterial->GetProperty("shininess"), 1.0f, 256.0f); } diff --git a/examples/movement/shaders/fragmentShader.frag b/examples/movement/shaders/fragmentShader.frag index ba636c7..1abbdc0 100644 --- a/examples/movement/shaders/fragmentShader.frag +++ b/examples/movement/shaders/fragmentShader.frag @@ -1,7 +1,8 @@ #version 330 core struct Material { - vec3 ambient, diffuse, specular; + sampler2D diffuse; + sampler2D specular; float shininess; }; @@ -10,6 +11,8 @@ struct Light vec3 position; vec3 ambient, diffuse, specular; float ambientStrength; + + float constant, linear, quadratic; }; in vec2 oUV; @@ -18,9 +21,6 @@ in vec3 oFragPos; out vec4 FragColor; -uniform sampler2D texture1; -uniform sampler2D texture2; - uniform vec3 viewPos; uniform Material material; @@ -29,23 +29,24 @@ uniform Light light; void main() { // Ambient light - vec3 ambient = light.ambient * light.ambientStrength * material.ambient; + vec3 ambient = light.ambient * light.ambientStrength * vec3(texture(material.diffuse, oUV)); vec3 norm = normalize(oNormal); // Diffuse light vec3 lightDir = normalize(light.position - oFragPos); float diff = max(dot(norm, lightDir), 0.0); - vec3 diffuse = (diff * material.diffuse) * light.diffuse; + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, oUV)); // 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) * light.specular; + vec3 specular = light.specular * spec * vec3(texture(material.specular, oUV)); - vec4 objColor = mix(texture(texture1, oUV), texture(texture2, oUV), 0.2); + float dist = length(light.position - oFragPos); + float attenuation = 1.0 / (light.constant + light.linear * dist + light.quadratic * (dist * dist)); - FragColor = vec4(ambient + diffuse + specular, 1.0) * objColor; + FragColor = vec4((ambient + diffuse + specular) * attenuation, 1.0); } \ No newline at end of file diff --git a/include/lighting/point.hpp b/include/lighting/point.hpp index 9f69735..4a53a14 100644 --- a/include/lighting/point.hpp +++ b/include/lighting/point.hpp @@ -80,6 +80,9 @@ namespace oglu Color diffusionColor; ///< Diffusion color of the light Color specularColor; ///< Specular color of the light + float constant; ///< Constant value of the attenuation term + float linear; ///< Coefficient of the linear attenuation term + float quadratic; ///< Coefficient of the quadratic attenuation term private: glm::vec3* position; ///< Position of the light diff --git a/include/material.hpp b/include/material.hpp index de728e9..972b1f2 100644 --- a/include/material.hpp +++ b/include/material.hpp @@ -71,7 +71,7 @@ namespace oglu * @param name The name of the property * @returns The value of the stored property */ - template inline T GetPropertyValue(const std::string& name, std::ostream& errorStream = OGLU_ERROR_STREAM) + template inline T& GetPropertyValue(const std::string& name, std::ostream& errorStream = OGLU_ERROR_STREAM) { return *(GetProperty(name, errorStream)); } diff --git a/src/lighting/point.cpp b/src/lighting/point.cpp index 5fa456e..1c81182 100644 --- a/src/lighting/point.cpp +++ b/src/lighting/point.cpp @@ -5,18 +5,21 @@ namespace oglu { PointLight::PointLight() : - position(new glm::vec3(0.0f)), diffusionColor(oglu::Color::White), specularColor(oglu::Color::White) + position(new glm::vec3(0.0f)), diffusionColor(oglu::Color::White), specularColor(oglu::Color::White), + constant(1.0f), linear(0.09f), quadratic(0.032f), isLinked(false) { } PointLight::PointLight(const glm::vec3& position, const Color& diffusionColor, const Color& specularColor) : - position(new glm::vec3(0.0f)), diffusionColor(diffusionColor), specularColor(specularColor) + position(new glm::vec3(0.0f)), diffusionColor(diffusionColor), specularColor(specularColor), + constant(1.0f), linear(0.09f), quadratic(0.032f), isLinked(false) { memcpy(this->position, &position, sizeof(glm::vec3)); } PointLight::PointLight(const PointLight& other) : - position(new glm::vec3(0.0f)), diffusionColor(other.diffusionColor), specularColor(other.specularColor) + position(new glm::vec3(0.0f)), diffusionColor(other.diffusionColor), specularColor(other.specularColor), + constant(1.0f), linear(0.09f), quadratic(0.032f), isLinked(false) { memcpy(this->position, position, sizeof(glm::vec3)); } @@ -33,12 +36,15 @@ namespace oglu delete position; position = &link.translation; + isLinked = true; } void PointLight::UnlinkPositionFromTransformable() { if (isLinked) - position = new glm::vec3(0.0f); + position = new glm::vec3(*position); + + isLinked = false; } float* PointLight::GetPositionPointer()