Enable & Disable Lighting

I am working on a project where I need to be able to enable or disable lighting depending on the type of data (facet models vs. point cloud data).

The problem I am having is that if I open the facet model first with lighting enabled then the lighting is fine, but if I open a point cloud first (with lighting disalbed) and then open a facet model, the model is very dark.

The first time I enable lighting I use the following code:

GLfloat gray[] = {0.75f, 0.75f, 0.75f, 1.0f};
GLfloat lightPos[] = {-50.0f, 50.0f, 100.0f, 1.0f};
glEnable (GL_LIGHTING);
glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
glEnable (GL_LIGHT0);
glLightfv (GL_LIGHT0, GL_POSITION, lightPos);

Thereafter I just use

glEnable (GL_LIGHTING) and
glDisable (GL_LIGHTING)

to enable and disable the lighting. Can anyone tell me what I am doing wrong?

Thank you for your help.

What are you providing for vertex colors? IIRC, if you disable lighting, then (with the fixed-function pipeline) you end up just rendering vertex colors (well, plus fog/texturing/etc. of course, if enabled).

Try also mashing fog and texturing off to verify they’re not hosing your result.

I haven’t defined vertex colors, and I’m not using texture mapping or fog (or any other special effects). I just use straight lighting. The sample code I included above shows everything I do for lighting. The only difference is how I render the facet data and point cloud data.

For the facet models:

glEnable (GL_LIGHTING)
and then for each facet

glNormal3f (n1, n2, n3) and
glVertex3f (x,y,z)

For point cloud data:

glDisable (GL_LIGHTING)
and for each point

glColor3f (r,g,b)
glVertex3f (x,y,z)

Thank you.

What is the glColor setting by default in your code?

Because that would seem (from glancing at the code you have supplied) the only thing that changes…

Actually you do. When rendering using immediate mode, glColor*() sets the vertex color. Per your code, you set this for your vertices when lighting is DISABLED.

Also note that unless you have glDisable( GL_COLOR_MATERIAL ), then your last active vertex color will be used to set material attributes (which attributes is selected by glColorMaterial()).

Try adding glDisable( GL_COLOR_MATERIAL ) in your “lighting enabled” path. By default, COLOR_MATERIAL is enabled, and it is used to set the AMBIENT and DIFFUSE material properties. It sounds like this is not what you want, since you’re not providing any vertex colors for your lighting ENABLED path.

I found the problem. When you define the lighting position using glLightfv, the position is relative to the current model view. When I display the point cloud data I have to apply a large offset to center it within the display. Then when I switched to the facet model that offset was still active so it was applying it to the lighting position as well. I just modified the coded so that before I define lighting I do

glMatrixMode (GL_MODELVIEW);
glLoadIdentity();

and it fixed it.

Dark Photon, I need to try your suggestion with disabling the color material because if I change the lighting from GL_POSITION to GL_AMBIENT everything comes out cyan for some reason.

Thanks for your help.