Sign in to follow this  
LargeJ

OpenGL Underlying edges visible with Phong shading

Recommended Posts

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:

[img]http://img17.imageshack.us/img17/2607/geosphere.jpg[/img]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.[/size][/font]

Share this post


Link to post
Share on other sites
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:

[b]Vertex shader:[/b]
[code]#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));
}[/code]

[b]Fragment shader:[/b]
[code]#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;
}[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this