Problems Getting Smooth Lighting

Started by ThorPrime, January 03, 2006, 10:11:11

Previous topic - Next topic

ThorPrime

I am playing around with Lighting and materials but I can't get smooth shading to work.

Here is a code sample of how openGL is initilized.
void initGL(){
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		GL11.glClearDepth(1.0); 
		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glDepthFunc(GL11.GL_LEQUAL);
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity(); 
		GLU.gluPerspective(
						   45.0f,
						   800f / 600f,
						   0.1f,
						   100.0f);
		GL11.glMatrixMode(GL11.GL_MODELVIEW); 
		GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_FASTEST);

		GL11.glShadeModel(GL11.GL_SMOOTH);
		GL11.glEnable(GL11.GL_LIGHTING);
		GL11.glEnable(GL11.GL_LIGHT0);


		FloatBuffer ambientLight = BufferUtils.createFloatBuffer(4);
		ambientLight.put(new float[] { 0.2f, 0.2f, 0.2f, 1.0f });
		ambientLight.rewind();
		
		FloatBuffer diffuseLight = BufferUtils.createFloatBuffer(4);
		diffuseLight.put( new float[] { 0.8f, 0.8f, 0.8f, 1.0f });
		diffuseLight.rewind();
		
		FloatBuffer specularLight = BufferUtils.createFloatBuffer(4); 
		specularLight.put(new float[] { 0.5f, 0.5f, 0.5f, 1.0f });
		specularLight.rewind();
		
		FloatBuffer position = BufferUtils.createFloatBuffer(4);
		position.put(new float[] { -5f, 0.0f, -3.0f, 1.0f });
		position.rewind();
		
		// Assign created components to GL_LIGHT0
		GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, ambientLight);
		GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, diffuseLight);
		GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, specularLight);
		GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, position);
		
	}


Is this a side effect of not enough polygons (I don't think I should have to use 35,000 polygons in a sphere to make it smooth) or am I missing something.

tomb

You need to set the vertex normals.

CaseyB

Those are just the normalized average of the for adjacent face normals, right?

Fool Running

QuoteYou need to set the vertex normals.
If he wasn't setting the normals, wouldn't he get no lighting at all? I think he needs to set more than 1 normal per poly (i.e. a normal at every vertex).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

elias4444

What are you using to draw the sphere? If you use the GLU library sphere element, it'll smooth the normals for you automatically. If you're running your own sphere creation routine, you need to use vertex normals rather than regular normals (vertex normals set the lighting on a per vertex basis, but regular normals are only one per polygon). Try using the GLU library to see if that fixes your problem.
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

ThorPrime

I am using a VRML model loader to draw the sphere, Switching to using the GLU library wouldn't do me any good as I need to be able to load arbitrary models.

Ok, I diddn't relize you have to set a normal for each vertex as opposed to a polygon. Got it fixed.