Hello everyone,
I am rendering a sphere using phong shading in OpenGL/GLSL. The problem I have is that the underlying edges of the object are visible with phong shading. So I see the specular reflection and, on top of that, I can see the triangles that make up the object. Does anyone know how this is possible? I assume the error is not in my shader code, but more likely to be in the geometry? However I just directly load the object into my application (generated by 3ds max and exported into .obj format).
Here is the image I have:
Underlying edges visible with Phong shading
What you have is perfectly normal. It is called "per-vertex lighting". What you want to have is "per-pixel lighting". Move your light calculations into the fragment shader instead.
[font=arial, verdana, tahoma, sans-serif][size=2]Can you post your shader code? This looks more like per-vertex lighting instead of per fragment lighting.[/font]
Yes, it looks af if it is per vertex shading. However, I thought my shaders are supposed to give per pixel-shading. The shader code is below:
Vertex shader:
Fragment shader:
Vertex shader:
#version 130
uniform vec4 DiffuseColor;
uniform vec4 AmbientColor;
uniform vec4 SpecularColor;
uniform float Shininess;
in vec3 NormalIn;
in vec4 VertexIn;
smooth out vec3 Normal;
smooth out vec3 WorldPos;
void main()
{
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * VertexIn;
WorldPos = vec3(gl_ModelViewMatrix * VertexIn);
Normal = normalize(vec3(mat3(gl_ModelViewMatrix) * NormalIn));
}
Fragment shader:
#version 130
uniform vec4 DiffuseColor;
uniform vec4 AmbientColor;
uniform vec4 SpecularColor;
uniform float Shininess;
smooth in vec3 Normal;
smooth in vec3 WorldPos;
void main ()
{
vec3 LightPosition = vec3(20.0, 3.0, 0.0); //for simplicity
vec3 lightDir = normalize(LightPosition - WorldPos);
vec3 eyeDir = normalize(-WorldPos);
vec3 halfway = normalize(eyeDir + lightDir);
float diffIntensity = max( dot(lightDir, Normal), 0.0f);
float specIntensity = pow( max( dot(Normal, halfway), 0.0f ), Shininess );
gl_FragColor =
diffIntensity * DiffuseColor
+ specIntensity * SpecularColor;
+ 0.2 * AmbientColor;
}
Um, I'm not into shaders, but I can't find where you actually interpolate between the normals in your fragment shader.
I figured out that the normals arriving at the fragment shader need to be normalized again (ofcourse).
Thanks for the insight
Thanks for the insight
If you are normalizing in the fragment shader then you should simply pass the normal to the fragment shader without normalizing it first in the vertex shader. This is an extra calculation for nothing since you are not using the normalized result for any further vertex calculations. The code should run faster now.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement