Jump to content
  • Advertisement
Sign in to follow this  
jay8ee

OpenGL GLSL pixel shader looks flat?

This topic is 3132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!