diff --git a/examples/movement/main.cpp b/examples/movement/main.cpp index d8dc5d3..7c42978 100644 --- a/examples/movement/main.cpp +++ b/examples/movement/main.cpp @@ -160,21 +160,6 @@ int main(int argc, char** argv) -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f }; - //unsigned int indices[] = { - // 0, 1, 3, // front - // 1, 2, 3, - // 7, 4, 0, // top - // 7, 0, 3, - // 0, 4, 5, // right - // 0, 5, 1, - // 7, 3, 2, // right - // 7, 2, 6, - // 2, 1, 6, // bottom - // 1, 6, 5, - // 4, 7, 5, // back - // 7, 6, 5 - //}; - oglu::VertexAttribute topology[] = { { 0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0 }, { 1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)) }, @@ -279,16 +264,13 @@ int main(int argc, char** argv) shader->Use(); - - 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->SetUniform("pointLight.ambient", "pointLight.ambientStrength", ambient); + shader->SetUniform3fv("pointLight.position", 1, glm::value_ptr(lightSource.GetPosition())); + shader->SetUniform("pointLight.diffuse", pointLight.diffusionColor, true); + shader->SetUniform("pointLight.specular", pointLight.specularColor, true); + shader->SetUniform("pointLight.constant", pointLight.constant); + shader->SetUniform("pointLight.linear", pointLight.linear); + shader->SetUniform("pointLight.quadratic", pointLight.quadratic); shader->SetUniform3fv("fl.position", 1, glm::value_ptr(camera.GetPosition())); shader->SetUniform3fv("fl.direction", 1, glm::value_ptr(flashlight.direction)); @@ -317,14 +299,12 @@ int main(int argc, char** argv) cube.Render(); } - /* lightSourceShader->Use(); 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.diffusionColor, true); lightSource.Render(); - */ ImGui::Begin("Controls"); @@ -350,7 +330,7 @@ int main(int argc, char** argv) ImGui::Separator(); } - ImGui::SetNextItemOpen(false, ImGuiCond_Once); + ImGui::SetNextItemOpen(true, ImGuiCond_Once); if (ImGui::TreeNode("Point")) { ImGui::ColorEdit3("Diffusion", &pointLight.diffusionColor.r); diff --git a/examples/movement/shaders/fragmentShader.frag b/examples/movement/shaders/fragmentShader.frag index 8621997..fdf760d 100644 --- a/examples/movement/shaders/fragmentShader.frag +++ b/examples/movement/shaders/fragmentShader.frag @@ -33,34 +33,60 @@ out vec4 FragColor; uniform vec3 viewPos; uniform Material material; -uniform Light light; +uniform Light pointLight; uniform Flashlight fl; +vec3 CalcPointLight(Light light, vec3 normal, vec3 fragPos, vec3 viewDir); +vec3 CalcSpotLight(Flashlight light, vec3 normal, vec3 fragPos, vec3 viewDir); + void main() { - vec3 lightDir = normalize(fl.position - oFragPos); - float theta = dot(lightDir, normalize(-fl.direction)); - float epsilon = fl.angle - fl.outerAngle; - float intensity = clamp((theta - fl.outerAngle) / epsilon, 0.0, 1.0); - - vec3 norm = normalize(oNormal); - // Diffuse light - - float diff = max(dot(norm, lightDir), 0.0); - vec3 diffuse = fl.diffuse * diff * vec3(texture(material.diffuse, oUV)); - - // Specular light vec3 viewDir = normalize(viewPos - oFragPos); - vec3 reflectDir = reflect(-lightDir, norm); + vec3 finalColor = vec3(0.0); + finalColor += CalcPointLight(pointLight, oNormal, oFragPos, viewDir); + finalColor += CalcSpotLight(fl, oNormal, oFragPos, viewDir); + + FragColor = vec4(finalColor, 1.0); +} + +vec3 CalcPointLight(Light light, vec3 normal, vec3 fragPos, vec3 viewDir) +{ + vec3 lightDir = normalize(light.position - fragPos); + + float diff = max(dot(normal, lightDir), 0.0); + + vec3 reflectDir = reflect(-lightDir, normal); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); - vec3 specular = fl.specular * spec * vec3(texture(material.specular, oUV)); - float dist = length(fl.position - oFragPos); - float attenuation = 1.0 / (fl.constant + fl.linear * dist + fl.quadratic * (dist * dist)); + float dist = length(light.position - fragPos); + float attenuation = 1.0 / (light.constant + light.linear * dist + light.quadratic * dist * dist); - // Ambient light vec3 ambient = light.ambient * light.ambientStrength * vec3(texture(material.diffuse, oUV)); + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, oUV)); + vec3 specular = light.specular * spec * vec3(texture(material.specular, oUV)); - FragColor = vec4(ambient + ((diffuse + specular) * attenuation * intensity), 1.0); + return (ambient + (diffuse + specular) * attenuation); +} + +vec3 CalcSpotLight(Flashlight light, vec3 normal, vec3 fragPos, vec3 viewDir) +{ + vec3 lightDir = normalize(light.position - fragPos); + + float theta = dot(lightDir, -normalize(light.direction)); + float epsilon = light.angle - light.outerAngle; + float intensity = clamp((theta - light.outerAngle) / epsilon, 0.0, 1.0); + + float diff = max(dot(normal, lightDir), 0.0); + + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + + float dist = length(light.position - fragPos); + float attenuation = 1.0 / (light.constant + light.linear * dist + light.quadratic * dist * dist); + + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, oUV)); + vec3 specular = light.specular * spec * vec3(texture(material.specular, oUV)); + + return ((diffuse + specular) * attenuation * intensity); } \ No newline at end of file