? Saw Tooth Lighting ?

I’m having a problem with saw tooth lighting on a 3D gluSphere() that I just can’t seem to figure out or fix. I originally posted this problem inside another post related to drawing distance (http://www.java-gaming.org/forums/index.php?topic=18296.0) but this really should have been its own topic so I’m starting one here.

Here is a picture (sawtoothlight.jpg) showing the issue I’m having:

Here is the source code for this viewer so you can see my poor jogl code:
http://www.zaczek.com/jogl/SawToothLight.zip (main is in TexturedSphere.java)

 Keys that can be used:  
  • W - toggles wireframe on/off
  • S - toggles glShadeModel() between GL.GL_FLAT and GL.GL_SMOOTH
  • T - toggles sphere texture on/off
  • P - toggles light to be infinite or point light
  • COMMA / PERIOD - decrease / increase the tessellation of the 3D gluSphere()
  • PAGE UP Arrow / PAGE DOWN arrow - increase / decrease X coordinate of light position
  • RIGHT Arrow / LEFT arrow - increase / decrease Y coordinate of light position
  • UP Arrow / DOWN arrow - increase / decrease Z coordinate of light position
  • RIGHT BRACKET / LEFT BRACKET - Scale up / Scale down light position/distance…ie move light farther out or in
  • Mouse left button for rotation
  • Mouse middle button for zooming

As you can see from the image I get this saw tooth effect on my lighting even though I’m using GL_SMOOTH - note the sawtooth is best visible when the light position is not along the “equator” of the sphere but off at some angle…light position in this code is [-500, 500, 0] and it can be changed using the inputs above. I think it might be an issue with normals but I use the gluQuadricNormals(sphere, GLU.GLU_SMOOTH) and I thought that would be all I needed…I didn’t think I would need to go off an calculate the normals at each vertex of this sphere that I don’t even create directly but instead use the gluSphere() quadric. Note that removing the texture results in the same issue and increasing the tessellations of the sphere doesn’t help either…try out toggling the FLAT/SMOOTH using the ‘S’ key

I’ve been hacking around this code for days commenting in and out different parts and I still don’t understand what the cause is. I though this might be an issue with the gluSphere() in general so I even loaded a 3DS model of a sphere using joglutils and I also get the same sawtooth light pattern. I really need to have code that generates a smooth looking day/night transition that is distinguishable and not just a simply lit sphere…any thoughts on what I’m doing wrong here?

The lighting is correct. However, because the lighting equation is performed per vertex then interpolated across the fragments, your precieving this as sawtooth.

You have two options:

  1. Increase the tesselation of the sphere.
  2. Start using shaders to do your own per-pixel lighting

The first option will not get rid of the artifact, but it would make it less visisble and noticable. The second option would add a significat amount of complexity to your renderer to manage shaders and uniforms correctly and in a performant manner.

PS. You could also add some ambient to reduce the effect, but since your in space…there isn’t any ambient lighting! So experiement with it…

HTH, DP :slight_smile:

sigh…I was afraid of that. Increasing the tessellation ends up costing me frame rates and using shaders is currently not an option because I’m trying to make an app that I can’t guarantee the hardware it will be running on and I’m told that shaders are hardware specific so for now I’d like to concentrate on simple opengl solution which doesn’t appear to exist. Plus I don’t even know opengl that well much less shaders.

thx, darkprophet.

I forgot to add in my previous post a link to another forum with a user having the same issue (though in OpenGL not JOGL)…he also didn’t have a solution: http://idevgames.com/forum/showthread.php?t=14508

Also, this site mentions the issue could be with poorly defined ambient lighting http://www.ozone3d.net/tutorials/stencil_shadow_volumes.php

shaders are easy once you get used to the quirks.

The problem is two fold really, the first one is the pixel shaders have versions that are not regressable. I.e. PS3.0 will not run on hardware that doesn’t support it, meaning you can’t gain from its flexibility unless you use newer hardware. Which means you are forced to create a baseline for your game/application and create multiple pixel shaders that do the same thing.

The second problem is compatibility, Nvidia and ATI do things different and will have their own quirks when it comes to things, just experiement and you’ll soon find them.

HTH, DP :slight_smile:

Steady on there, I’m pretty sure that the lighting is not correct.
Before you go off and try shaders, make the simplest possible test of gluSphere rendering.
Take the Gears demo from jogl and replace the gear-rendering code with a call to gluSphere.

It looked like this when I did it

http://base.googlehosted.com/base_media?q=hand8449835297158586250&size=1

Thats with 10 stacks and slices for the spheres, so fewer than in your sample image. Code here

Hmmm…apologies, your right. Looks like normals are to blame. I initially thought it was something to do with the way he’s set his materials up, but looking at the image he’s posted, there doesnt seam to be any interpolation across the lit faces at all.

I would suggest you look at the gluSphere code and copy what it is doing for normals.

DP :slight_smile:

Thanks for that code…I’m wodering if it may be more an issue with the choice of color properties for the specular, diffuse, ambient that could lead me to a nice terminator (day/night transition) on the map. I suspect it is a combination of all of those factors and I just need to add some JSliders to my example code and start messing with the colors and properties to hit upon the right combination. I used the light setting from the Gears/Spheres code you provided and unfortunately that combination of lighting properties made it hard to even see my textured sphere, but when I removed the texture the sphere underneath did appear to have a nice terminator…I think I just need to do the color tweaking I suggested and see what I can come up with. Thank you

Looking through your code briefly I didn’t see any non-uniform scales, but it might still be worth it to test out glEnable(GL_NORMALIZE) or glEnable(GL_RESCALE_NORMAL)