#version 330 core layout (location = 0) in vec3 invPosition; layout (location = 1) in…
- User
#version 330 core
layout (location = 0) in vec3 in_vPosition; layout (location = 1) in vec3 in_vNormal; layout (location = 2) in vec3 in_vColor; layout (location = 3) in vec2 in_vUV;
out vec3 in_fPositionWorld; out vec3 in_fNormal; out vec3 in_fColor; out vec2 in_fUV;
uniform mat3 uModelMat; uniform mat3 uNormalMat; uniform mat4 uMVPMat;
void main() { gl_Position = uMVPMat * vec4(in_vPosition, 1.0f); in_fPositionWorld = uModelMat * in_vPosition; in_fNormal = normalize(uNormalMat * in_vNormal); in_fColor = in_vColor; in_fUV = in_vUV; }
#version 330 core
in vec3 in_fPositionWorld; in vec3 in_fNormal; in vec3 in_fColor; in vec2 in_fUV;
out vec4 out_fColor;
struct PointLight { vec3 position; vec3 ambientColor; vec3 diffuseColor; vec3 specularColor; float shininess; float constant; float linear; float quadratic; };
uniform PointLight uPointLights[1]; uniform int uPointLightCount; uniform sampler2D uTextureSampler; uniform vec3 uViewPosition;
vec3 calcPointLight(PointLight light) {}
void main() {}
Can you fill in calcPointLight and main? These are GLSL shaders, I want them to compute lighting according to the Phong illumination model.
- Assistant
- User
vec3 calcPointLight(PointLight light) { float lightDistance = length(light.position - in_fPositionWorld); float attenuation = 1.0 / (light.constant + light.linear * lightDistance + light.quadratic * (lightDistance * lightDistance));
vec3 lightDirection = normalize(in_fPositionWorld - light.position); float diffuseIntensity = max(dot(lightDirection, in_fNormal), 0.0f); vec3 viewDirection = normalize(uViewPosition - in_fPositionWorld); vec3 reflectionDirection = reflect(-lightDirection, in_fNormal); float specularIntensity = pow(max(dot(viewDirection, reflectionDirection), 0.0f), light.shininess); return vec3(specularIntensity); return light.ambientColor + diffuseIntensity * light.diffuseColor * attenuation + specularIntensity * light.specularColor * attenuation;} Your code works correctly... why isn't mine correctly working too? Can you do a comparison
- Assistant
Certainly! Let's compare your code with the corrected version:
vec3 calcPointLight(PointLight light) { float lightDistance = length(light.position - in_fPositionWorld); float attenuation = 1.0 / (light.constant + light.linear * lightDistance + light.quadratic * (lightDistance * lightDistance)); vec3 lightDirection = normalize(light.position - in_fPositionWorld); float diffuseIntensity = max(dot(lightDirection, in_fNormal), 0.0f); vec3 viewDirection = normalize(uViewPosition - in_fPositionWorld); vec3 reflectionDirection = reflect(-lightDirection, in_fNormal); float specularIntensity = pow(max(dot(viewDirection, reflectionDirection), 0.0f), light.shininess); return light.ambientColor + diffuseIntensity * light.diffuseColor * attenuation + specularIntensity * light.specularColor * attenuation; }The issue in your code is with the
lightDirection. In the corrected code, we calculatelightDirectionasnormalize(light.position - in_fPositionWorld), whereas in your code, it is calculated as . - User
That might not be the only thing wrong - this is my main function: void main() { vec3 lightColor = vec3(0.0f); for (int i = 0; i < uPointLightCount; ++i) { lightColor += calcPointLight(uPointLights[i]); } out_fColor = vec4(lightlightColor * in_fColor, 1.0f) * texture(uTextureSampler, in_fUV); }
- Assistant
I see the issue in your main function. There's a typo in the line where you accumulate the
lightColor. You havelightlightColorinstead oflightColor.Here's the corrected code for the main function:
void main() { vec3 lightColor = vec3(0.0f); for (int i = 0; i < uPointLightCount; ++i) { lightColor += calcPointLight(uPointLights[i]); } out_fColor = vec4(lightColor * in_fColor, 1.0f) * texture(uTextureSampler, in_fUV); }Now, the
calcPointLightfunction is correctly called in the loop to accumulate the light color contribution for each point light. The resultinglightColoris multiplied with the combination ofin_fColorand the texture color, and assigned toout_fColoras the final output of the shader.With this correction, the shader should correctly compute lighting according to the Phong illumination model.
- User
Are you sure that your code is correct? Particularly the specular highlight, when it's not on the side of the face (past an edge) I get this brighter light in the edge/corner. Is this normal? And also, for my cube with hard vertex normals, is it okay for the specular highlight to not always be on top of the diffuse highlight, especially on steep angles?
- Assistant
I apologize for the confusion. You are correct that the code provided may not produce the desired results for specular highlights, especially in cases where the surface is not smooth or when the surface has hard vertex normals.
To address the issue, we will need to modify the code in the
calcPointLightfunction, specifically the calculation of the specular intensity. We will use a modified Blinn-Phong shading model instead of the traditional Phong model, as it handles non-smooth surfaces better. - User
No no, I'm not looking for a physically accurate implementation. I'm looking for a correct Phong implementation, that's what I'm trying to say. Are the cases I've described before alright in Phong? Is the code accurate?
- Assistant
Apologies for the confusion earlier. If you're specifically looking for a Phong implementation, which does not take into account the halfway vector, the code you provided in your
calcPointLightfunction can work for the Phong illumination model.Regarding your additional questions:
-
Specular highlight on corners/edges: In Phong shading, the specular highlight is typically calculated based on the angle between the reflection vector and the view direction. When you have a sharp edge or corner, the normal vectors at those points may change abruptly, leading to an inconsistent orientation of the reflection vector. This can cause the specular highlight to appear brighter or distorted in those areas. This behavior is a characteristic of the Phong model and not considered physically accurate.
-
Specular highlight alignment with diffuse highlight: In Phong shading with hard vertex normals, it is expected that the specular highlight may not always align perfectly with the diffuse highlight, especially on steep angles or sharp edges. This discrepancy arises from the interpolation of the vertex normals across the surface. The sharp transition in normals can cause the specular highlight to deviate from the direction of the diffuse highlight. This behavior is also inherent to the Phong shading model and is not unusual.
-
- User
I'd like the specular highlight to stop working for faces that aren't facing it, I'm getting a highlight on all 6 of them which isn't right.
- Assistant
I apologize for the confusion earlier. To make the specular highlight only appear on faces that are facing the light source, we can modify the
calcPointLightfunction to include a check based on the angle between the surface normal and the light direction.Here's an updated version of the
calcPointLightfunction that incorporates this check: