Generalize directional light, spot light, point light

This commit is contained in:
4VRDriver
2020-09-07 00:24:15 +02:00
parent 4cba3da667
commit 4b7e9eeee8
2 changed files with 141 additions and 49 deletions

View File

@@ -15,60 +15,154 @@ struct Material {
};
uniform Material u_material;
struct Light {
vec3 position;
struct DirectionalLight {
vec3 direction;
float innerCutOff;
float outerCutOff;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform DirectionalLight u_directionalLight;
struct PointLight {
vec3 position;
float K_c;
float K_l;
float K_q;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
#define NR_POINT_LIGHTS 1
uniform PointLight u_pointLight[NR_POINT_LIGHTS];
struct SpotLight {
vec3 position;
vec3 direction;
float innerCutOff;
float outerCutOff;
float K_c;
float K_l;
float K_q;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform Light u_light;
uniform SpotLight u_spotLight;
uniform mat3 u_normalMatrix;
uniform vec3 u_viewPosition;
vec3 directionalLightContribution(DirectionalLight light, vec3 normal, vec3 viewDir);
vec3 pointLightContribution(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
vec3 spotLightContribution(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
void main() {
vec3 vecLightToFragment = u_light.position - v_fragmentPosition;
vec3 lightDir = normalize(vecLightToFragment);
//vec3 lightDir = normalize(-u_light.direction);
vec3 fragmentColor = vec3(0.0f);
// Diffuse lighting
vec3 diffuseColor0 = vec3(texture(u_material.texture_diffuse0, v_texCoord));
vec3 normal = normalize(u_normalMatrix * v_normal);
float diff = max(dot(normal, lightDir), 0.0f);
vec3 diffuse = u_light.diffuse * diff * diffuseColor0;
// Ambient lighting
vec3 ambient = u_light.ambient * diffuseColor0;
// Specular lighting
vec3 normal = normalize(v_normal);
vec3 viewDir = normalize(u_viewPosition - v_fragmentPosition);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0f), u_material.shininess);
vec3 specular = u_light.specular * spec * vec3(texture(u_material.texture_specular0, v_texCoord));
float distanceLightFragment = length(vecLightToFragment);
float attenuation = 1.0f / (u_light.K_c + u_light.K_l * distanceLightFragment + u_light.K_q * distanceLightFragment * distanceLightFragment);
fragmentColor += directionalLightContribution(u_directionalLight, normal, viewDir);
for(int i = 0; i < NR_POINT_LIGHTS; i++) {
fragmentColor += pointLightContribution(u_pointLight[i], normal, v_fragmentPosition, viewDir);
}
//fragmentColor += spotLightContribution(u_spotLight, normal, v_fragmentPosition, viewDir);
f_color = vec4(fragmentColor, 1.0f);
}
vec3 directionalLightContribution(DirectionalLight light, vec3 normal, vec3 viewDir) {
vec3 lightDir = normalize(-light.direction);
// Diffuse shading
float diffuseShading = max(dot(normal, lightDir), 0.0f);
// Specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float specularShading = pow(max(dot(viewDir, reflectDir), 0.0f), u_material.shininess);
vec3 diffuseTex = vec3(texture(u_material.texture_diffuse0, v_texCoord));
vec3 specularTex = vec3(texture(u_material.texture_specular0, v_texCoord));
vec3 ambient = light.ambient * diffuseTex;
vec3 diffuse = light.diffuse * diffuseShading * diffuseTex;
vec3 specular = light.specular * specularShading * specularTex;
return (ambient + diffuse + specular);
}
vec3 pointLightContribution(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 lightDir = normalize(light.position - fragPos);
// Diffuse shading
float diffuseShading = max(dot(normal, lightDir), 0.0f);
// Specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float specularShading = pow(max(dot(viewDir, reflectDir), 0.0f), u_material.shininess);
// Attenuation
float distanceLightFragment = length(light.position - fragPos);
float attenuation = 1.0f / (light.K_c + light.K_l * distanceLightFragment + light.K_q * distanceLightFragment * distanceLightFragment);
vec3 diffuseTex = vec3(texture(u_material.texture_diffuse0, v_texCoord));
vec3 specularTex = vec3(texture(u_material.texture_specular0, v_texCoord));
vec3 ambient = light.ambient * diffuseTex;
vec3 diffuse = light.diffuse * diffuseShading * diffuseTex;
vec3 specular = light.specular * specularShading * specularTex;
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
float theta = dot(lightDir, normalize(-u_light.direction));
float epsilon = u_light.innerCutOff - u_light.outerCutOff;
float intensity = clamp((theta - u_light.outerCutOff) / epsilon, 0.0f, 1.0f);
return (ambient + diffuse + specular);
}
vec3 spotLightContribution(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 lightDir = normalize(light.position - fragPos);
// Diffuse shading
float diffuseShading = max(dot(normal, lightDir), 0.0f);
// Specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float specularShading = pow(max(dot(viewDir, reflectDir), 0.0f), u_material.shininess);
// Attenuation
float distanceLightFragment = length(light.position - fragPos);
float attenuation = 1.0f / (light.K_c + light.K_l * distanceLightFragment + light.K_q * distanceLightFragment * distanceLightFragment);
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.innerCutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0f, 1.0f);
vec3 diffuseTex = vec3(texture(u_material.texture_diffuse0, v_texCoord));
vec3 specularTex = vec3(texture(u_material.texture_specular0, v_texCoord));
vec3 ambient = light.ambient * diffuseTex;
vec3 diffuse = light.diffuse * diffuseShading * diffuseTex;
vec3 specular = light.specular * specularShading * specularTex;
diffuse *= intensity;
specular *= intensity;
f_color = vec4(ambient + diffuse + specular, 1.0f);
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}

View File

@@ -50,41 +50,45 @@ void Controller::run() {
std::vector<Entity> scene;
//Model model_backpack("res/models/backpack.obj");
Model model_container("res/models/container.obj");
Model model_plant("res/models/plant.obj");
//Model model_container("res/models/container.obj");
Model model_cube("res/models/cube.obj");
Model model_sphere("res/models/sphere.obj");
//Model model_sphere("res/models/sphere.obj");
//Entity backpack1(&model_backpack, &shaderProgram);
Entity sphere(&model_sphere, &shaderProgram);
Entity cube(&model_container, &shaderProgram);
//Entity sphere(&model_sphere, &shaderProgram);
//Entity cube(&model_container, &shaderProgram);
Entity plant(&model_plant, &shaderProgram);
Entity lightSource(&model_cube, &lightProgram);
sphere.translate(glm::vec3(3.0f, 0.0f, 2.0f));
lightSource.translate(glm::vec3(-5.0f, 1.0f, 0.0f));
lightSource.scale(0.2f);
plant.scale(5.0f);
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 diffuseColor = lightColor * glm::vec3(1.0f);
glm::vec3 ambientColor = diffuseColor * glm::vec3(0.1f);
glm::vec3 specularColor = glm::vec3(1.0f);
shaderProgram.bind();
shaderProgram.setUniform("u_light.position", lightSource.getPosition());
//shaderProgram.setUniform("u_light.direction", glm::vec3(-0.2f, -1.0f, -0.3f));
shaderProgram.setUniform("u_light.ambient", ambientColor);
shaderProgram.setUniform("u_light.diffuse", diffuseColor);
shaderProgram.setUniform("u_light.specular", glm::vec3(1.0f, 1.0f, 1.0f));
shaderProgram.setUniform("u_directionalLight.direction", glm::vec3(-0.2f, -1.0f, -0.3f));
shaderProgram.setUniform("u_directionalLight.ambient", ambientColor * 0.25f);
shaderProgram.setUniform("u_directionalLight.diffuse", diffuseColor * 0.25f);
shaderProgram.setUniform("u_directionalLight.specular", specularColor * 0.25f);
shaderProgram.setUniform("u_light.K_c", 1.0f);
shaderProgram.setUniform("u_light.K_l", 0.09f);
shaderProgram.setUniform("u_light.K_q", 0.032f);
shaderProgram.setUniform("u_pointLight[0].position", lightSource.getPosition());
shaderProgram.setUniform("u_pointLight[0].ambient", ambientColor);
shaderProgram.setUniform("u_pointLight[0].diffuse", diffuseColor);
shaderProgram.setUniform("u_pointLight[0].specular", specularColor);
shaderProgram.setUniform("u_pointLight[0].K_c", 1.0f);
shaderProgram.setUniform("u_pointLight[0].K_l", 0.09f);
shaderProgram.setUniform("u_pointLight[0].K_q", 0.032f);
shaderProgram.setUniform("u_material.shininess", 32.0f);
shaderProgram.unbind();
scene.push_back(cube);
scene.push_back(plant);
scene.push_back(lightSource);
scene.push_back(sphere);
camera->translate(glm::vec3(0.0f, 0.0f, 7.5f));
@@ -103,12 +107,6 @@ void Controller::run() {
// Update game
// ...
shaderProgram.bind();
shaderProgram.setUniform("u_light.position", camera->getPosition());
shaderProgram.setUniform("u_light.direction", camera->getDirection());
shaderProgram.setUniform("u_light.innerCutOff", glm::cos(glm::radians(12.5f)));
shaderProgram.setUniform("u_light.outerCutOff", glm::cos(glm::radians(25.0f)));
shaderProgram.unbind();
// Render and buffer swap
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);