I am using modern OpenGL to render a scene having obj(wavefront) meshes. I have 3 point lights, a directional light and a spot-light.
And it suffers from the problem of unwanted specular highlight in the bottom.
My vertex Shader :
#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoord;
uniform mat4 model; // model matrix
uniform mat4 view; // view matrix
uniform mat4 projection; // projection matrix
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoord;
void main()
{
FragPos = vec3(model * vec4(pos, 1.0f)); // vertex position in world space
Normal = mat3(transpose(inverse(model))) * normal; // normal direction in world space
TexCoord = texCoord;
gl_Position = projection * view * model * vec4(pos, 1.0f);
}
And here is the fragment shader :
#version 330 core
struct Material
{
vec3 ambient;
sampler2D diffuseMap;
vec3 specular;
float shininess;
};
struct DirectionalLight
{
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight
{
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
float exponent;
};
struct SpotLight
{
vec3 position;
vec3 direction;
float cosInnerCone;
float cosOuterCone;
vec3 ambient;
vec3 diffuse;
vec3 specular;
int on;
float constant;
float linear;
float exponent;
};
in vec2 TexCoord;
in vec3 FragPos;
in vec3 Normal;
#define MAX_POINT_LIGHTS 3
uniform DirectionalLight sunLight;
uniform PointLight pointLights[MAX_POINT_LIGHTS];
uniform SpotLight spotLight;
uniform Material material;
uniform vec3 viewPos;
out vec4 frag_color;
vec3 calcDirectionalLightColor(DirectionalLight light, vec3 normal, vec3 viewDir);
vec3 calcPointLightColor(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
vec3 calcSpotLightColor(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
void main()
{
vec3 normal = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);
vec3 ambient = spotLight.ambient * material.ambient * vec3(texture(material.diffuseMap, TexCoord)) ;
vec3 outColor = vec3(0.0f);
outColor += calcDirectionalLightColor(sunLight, normal, viewDir);
for(int i = 0; i < MAX_POINT_LIGHTS; i++)
outColor += calcPointLightColor(pointLights[i], normal, FragPos, viewDir);
// If the light isn't on then just return 0 for diffuse and specular colors
if (spotLight.on == 1)
outColor += calcSpotLightColor(spotLight, normal, FragPos, viewDir);
frag_color = vec4(ambient + outColor, 1.0f);
}
vec3 calcDirectionalLightColor(DirectionalLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction); // negate => Must be a direction from fragment towards the light
// Diffuse ------------------------------------------------------------------------- --------
float NdotL = max(dot(normal, lightDir), 0.0);
vec3 diffuse = light.diffuse * NdotL * vec3(texture(material.diffuseMap, TexCoord));
// Specular - Blinn-Phong ------------------------------------------------------------------
vec3 halfDir = normalize(lightDir + viewDir);
float NDotH = max(dot(normal, halfDir), 0.0f);
vec3 specular = light.specular * material.specular * pow(NDotH, material.shininess);
return (diffuse + specular);
}
vec3 calcPointLightColor(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
// Diffuse ----------------------------------------------------------------------------------
float NdotL = max(dot(normal, lightDir), 0.0);
vec3 diffuse = light.diffuse * NdotL * vec3(texture(material.diffuseMap, TexCoord));
// Specular - Blinn-Phong ------------------------------------------------------------------
vec3 halfDir = normalize(lightDir + viewDir);
float NDotH = max(dot(normal, halfDir), 0.0f);
vec3 specular = light.specular * material.specular * pow(NDotH, material.shininess);
// Attenuation using Kc, Kl, Kq -------------------------------------------------------------
float d = length(light.position - FragPos);
float attenuation = 1.0f / (light.constant + light.linear * d + light.exponent * (d * d));
diffuse *= attenuation;
specular *= attenuation;
return (diffuse + specular);
}
vec3 calcSpotLightColor(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
vec3 spotDir = normalize(light.direction);
float cosDir = dot(-lightDir, spotDir); // angle between the lights direction vector and spotlights direction vector
float spotIntensity = smoothstep(light.cosOuterCone, light.cosInnerCone, cosDir);
// Diffuse ----------------------------------------------------------------------------------
float NdotL = max(dot(normal, lightDir), 0.0);
vec3 diffuse = spotLight.diffuse * NdotL * vec3(texture(material.diffuseMap, TexCoord));
// Specular - Blinn-Phong ------------------------------------------------------------------
vec3 halfDir = normalize(lightDir + viewDir);
float NDotH = max(dot(normal, halfDir), 0.0f);
vec3 specular = light.specular * material.specular * pow(NDotH, material.shininess);
// Attenuation using Kc, Kl, Kq -------------------------------------------------------------
float d = length(light.position - FragPos);
float attenuation = 1.0f / (light.constant + light.linear * d + light.exponent * (d * d));
diffuse *= attenuation * spotIntensity;
specular *= attenuation * spotIntensity;
return (diffuse + specular);
}
Mesh_file.obj Here is the object file for the mesh :
The bottom of the mesh is expected to show only ambient light but a sharp specular highlight is seen. I am using Bling-Phong illumination with Phong shading for rendering.
I will share more info if needed to debug the issue but I feel only sharing the shader will suffice.
Aucun commentaire:
Enregistrer un commentaire