Added spotlight

This commit is contained in:
Robert 2021-01-29 20:36:17 +01:00
parent 6981510aaf
commit b185429475
9 changed files with 139 additions and 11 deletions

View file

@ -234,6 +234,10 @@ int main(int argc, char** argv)
oglu::PointLight pointLight;
pointLight.LinkPositionToTransformable(lightSource);
oglu::SpotLight flashlight;
flashlight.linear = 0.09f;
flashlight.quadratic = 0.032f;
// Create a shader
oglu::Shader shader, lightSourceShader;
try
@ -270,15 +274,31 @@ int main(int argc, char** argv)
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
flashlight.SetPosition(camera.GetPosition());
flashlight.direction = camera.GetFront();
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->SetUniform3fv("fl.position", 1, glm::value_ptr(camera.GetPosition()));
shader->SetUniform3fv("fl.direction", 1, glm::value_ptr(flashlight.direction));
shader->SetUniform("fl.angle", glm::cos(glm::radians(flashlight.angle)));
shader->SetUniform("fl.outerAngle", glm::cos(glm::radians(flashlight.outerAngle)));
shader->SetUniform("fl.diffuse", flashlight.diffusionColor, true);
shader->SetUniform("fl.specular", flashlight.specularColor, true);
shader->SetUniform("fl.constant", flashlight.constant);
shader->SetUniform("fl.linear", flashlight.linear);
shader->SetUniform("fl.quadratic", flashlight.quadratic);
shader->SetUniform3fv("viewPos", 1, glm::value_ptr(camera.GetPosition()));
@ -297,12 +317,14 @@ 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");
@ -328,7 +350,7 @@ int main(int argc, char** argv)
ImGui::Separator();
}
ImGui::SetNextItemOpen(true, ImGuiCond_Once);
ImGui::SetNextItemOpen(false, ImGuiCond_Once);
if (ImGui::TreeNode("Point"))
{
ImGui::ColorEdit3("Diffusion", &pointLight.diffusionColor.r);
@ -347,6 +369,27 @@ int main(int argc, char** argv)
ImGui::TreePop();
}
ImGui::SetNextItemOpen(true, ImGuiCond_Once);
if (ImGui::TreeNode("Flashlight"))
{
ImGui::ColorEdit3("Diffusion", &flashlight.diffusionColor.r);
ImGui::ColorEdit3("Specular", &flashlight.specularColor.r);
ImGui::SliderFloat("Angle", &flashlight.angle, 1.0f, flashlight.outerAngle - 1.0f);
ImGui::SliderFloat("Outer angle", &flashlight.outerAngle, flashlight.angle + 1.0f, 60.0f);
ImGui::SetNextItemOpen(true, ImGuiCond_Once);
if (ImGui::TreeNode("Attenuation"))
{
ImGui::SliderFloat("Constant", &flashlight.constant, 1.0f, 4.0f);
ImGui::SliderFloat("Linear", &flashlight.linear, 0.0014f, 0.7f);
ImGui::SliderFloat("Quadratic", &flashlight.quadratic, 0.00007f, 1.8f);
ImGui::TreePop();
}
ImGui::TreePop();
}
}
ImGui::SetNextItemOpen(true, ImGuiCond_Once);

View file

@ -15,6 +15,15 @@ struct Light
float constant, linear, quadratic;
};
struct Flashlight
{
vec3 position, direction;
float angle, outerAngle;
vec3 diffuse, specular;
float constant, linear, quadratic;
};
in vec2 oUV;
in vec3 oNormal;
in vec3 oFragPos;
@ -25,28 +34,33 @@ uniform vec3 viewPos;
uniform Material material;
uniform Light light;
uniform Flashlight fl;
void main()
{
// Ambient light
vec3 ambient = light.ambient * light.ambientStrength * vec3(texture(material.diffuse, oUV));
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
vec3 lightDir = normalize(light.position - oFragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, oUV));
vec3 diffuse = fl.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 = light.specular * spec * vec3(texture(material.specular, oUV));
vec3 specular = fl.specular * spec * vec3(texture(material.specular, oUV));
float dist = length(light.position - oFragPos);
float attenuation = 1.0 / (light.constant + light.linear * dist + light.quadratic * (dist * dist));
float dist = length(fl.position - oFragPos);
float attenuation = 1.0 / (fl.constant + fl.linear * dist + fl.quadratic * (dist * dist));
FragColor = vec4((ambient + diffuse + specular) * attenuation, 1.0);
// Ambient light
vec3 ambient = light.ambient * light.ambientStrength * vec3(texture(material.diffuse, oUV));
FragColor = vec4(ambient + ((diffuse + specular) * attenuation * intensity), 1.0);
}