Hello Guest

Problems Getting Smooth Lighting

  • 5 Replies
  • 9787 Views
Problems Getting Smooth Lighting
« on: January 03, 2006, 10:11:11 »
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.
Code: [Select]

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.

*

Offline tomb

  • ***
  • 148
Problems Getting Smooth Lighting
« Reply #1 on: January 03, 2006, 13:46:01 »
You need to set the vertex normals.

*

Offline CaseyB

  • ***
  • 118
Problems Getting Smooth Lighting
« Reply #2 on: January 03, 2006, 16:25:15 »
Those are just the normalized average of the for adjacent face normals, right?

hmmmmmm...
« Reply #3 on: January 03, 2006, 19:06:02 »
Quote
You 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

Problems Getting Smooth Lighting
« Reply #4 on: January 03, 2006, 20:11:01 »
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

Problems Getting Smooth Lighting
« Reply #5 on: January 03, 2006, 23:05:25 »
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.