🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Simple (I think) Shader problem

Started by
1 comment, last by doublex_3 17 years, 6 months ago
Right I'll dive straight in here, basically I'm trying to write a simple diffuse lighting shader, using the 'dot product of incident beam and normal' method, but have been having some problems, so to try elimate them I've written a simple shader to show what my normals are doing: vertex shader:
	varying vec3 normal;
	void main()
	{	
		normal =  normalize(gl_Normal);
		gl_Position = ftransform();
	} 
fragment shader:
	varying vec3 normal;
	void main()
	{	
	vec3 n=normalize(normal);
	vec4 color = vec4(n.xyz,1.0);
	gl_FragColor = color;
	}
Basically the idea is to colour a polygon based on where its normal is pointing, which does work fine, but when I rotate the model, the colours do not change, indicating that these aren't the normals in 'world space' (sorry if I get terminology wrong, I'm new to the 3d scene really). This is causing the model's lighting to not depend on its orientation, which is obviously wrong. Just in case it helps, the code regarding the rotation of the model is:
void drawPlayer(){
	glPushMatrix();
		glTranslatef(player.x, player.y, player.z);
		glRotatef(-player.rotation/PIOVER180,0,1,0);
		glPushMatrix();
			glRotatef(180,0,1,0); //stands the model upright
			if(model.bLoaded==true){model.draw();}
		glPopMatrix();
	glPopMatrix();
Any help would be much appreciated, I'm completely stumped on this one. I've tried implementing many different shaders I've sourced from around the net, but nothing works so far. :(
Advertisement
try
normal = normalize(gl_NormalMatrix * gl_Normal);
instead of
normal = normalize(gl_Normal);

that should move the normals along with the rotation of the geometry
I've tried that, but it then makes the normals depend on what direction the geometry is being looked at, I think a consequence of gl_NormalMatrix transforming points into 'eye space'.

So with a multiplication by gl_NormalMatrix, the brightness of each polygon depends on the viewing angle, which is more akin to specular rather than diffuse lighting.

Thanks for thoughts though :)

Any more ideas?

This topic is closed to new replies.

Advertisement