GLSL pixel shader looks flat?

Started by
2 comments, last by jay8ee 14 years, 1 month ago
Hey guys. I am having a little trouble implementing per pixel lighting in OpenGL. The scene renders, however the geometry doesn't look smooth and looks more like GL_FLAT shiz form the fixed function pipe. A screenshot so you can take a look see. Here is the vert shader..

varying vec3 v_lightDir;
varying vec3 v_halfVec;
varying vec3 Normal;
varying vec4 Colour;

uniform int lightIndex;

void main()
{
	v_lightDir = normalize( gl_LightSource[lightIndex].position.xyz );
	v_halfVec = normalize( gl_LightSource[lightIndex].halfVector.xyz );

	Normal = gl_NormalMatrix * gl_Normal;
	Colour = gl_Color;
	gl_Position = ftransform();
}



and here is the frag shader..

varying vec3 v_lightDir;
varying vec3 v_halfVec;

varying vec3 Normal;
varying vec4 Colour;

uniform float shininess;
uniform int lightIndex;

void main()
{
	// Calculate Ambient
	vec4 kAmbient = gl_LightSource[lightIndex].ambient * Colour;

	// Process Light Direction and Normals
	float NdotL = max( dot(Normal.xyz, v_lightDir ), 0.0 );
	vec4 kColour = kAmbient;

	// Add Diffuse and Specular if NdotL is greater than zero
	if( NdotL > 0.0 )
	{
		// Calculate Diffuse
		vec4 kDiffuse = ( 1.0 - kAmbient ) * ( gl_LightSource[lightIndex].diffuse * Colour);
		vec4 kSpecular = gl_LightSource[lightIndex].specular;

		float NdotHV = max( dot(Normal.xyz, v_halfVec ), 0.0 );
		kColour += kDiffuse * NdotL;
		kColour += kSpecular * pow( NdotHV, shininess );
	}

	// Write Pixel Colour
	gl_FragColor = kColour;
}


It doesn't seem like the normal is getting interpolated across the surface to me. If you can give me any advice or spot something really stupid please let me know :)
Advertisement
I just took a quick look and noticed one thing. Correct me if I'm wrong but shouldn't v_lightDir = normalize(gl_LightSource[lightIndex].position.xyz - (gl_ModelViewMatrix * gl_Vertex));

edit: if you are not using Eye space then you should take out gl_ModelViewMatrix.
Try normalizing the normal in the pixel shader. Because the interpolation across the triangle is linear, the normal will change length.

How do you calculate the half-vector?
Quote:Original post by Promethium
Try normalizing the normal in the pixel shader. Because the interpolation across the triangle is linear, the normal will change length.

How do you calculate the half-vector?


That did it :). I didn't think about the normal changing length when interpolating. Looks just fine now. Really fast replies, this forum is great.

This topic is closed to new replies.

Advertisement