specular lighting does nott wor


The following code intends to test specular lighting but creates a scene totally dark. If I specify some ambient amount for material, I can see the model. Do I do anything wrong? Your help is appreciated. Tony

GLfloat light_position0[] = { 0., 0., 100., 0.0 };
GLfloat light_ambientDiffuse[] = { 0., .02, 0., 1.0};
GLfloat light_specular[] = { 1., 1., 1., 1.0};

glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambientDiffuse);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_ambientDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);


GLfloat mat_ambientDiffuse[] = { 0.0, 0.0, 0.0, 1.0};
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 60.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_ambientDiffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

Since you specify a black color for ambient and diffuse the model will not be visible. Your specular setup looks fine as far as i can tell.
Whether specular highlights show up depends on your model and camera and light setup.
You specify a directional lightsource as w is 0 for light_position0. Maybe you can try could try -100 instead of 100 for you z coordinate in the light position and see if it changes anything (Don’t know whether for directional lights position specifies a direction vector towards the origin or away from it)

Did you by chance enable lighting by setting glEnable(GL_LIGHTING); ??

I intended to make ambient and diffuse inactive so that I can observe specular highlights independently. I also tried -100 as you said but nothing shows up. I tried to increase some amount to mat_ambientDiffuse and then the model showed up. It meant that the camera and light setup was ok, right?


Maybe you need to call

If you aren’t using texturing, that call doesn’t matter.

Also, remember that GL does per vertex lighting so it could be the reason why you won’t see the specular highlight. It’s best to make a highly detailed sphere in this case and it will be visible.

As V-man says, that exponent of 60 will give you a very small highlight which may miss a vertex.

You could also try lowering the exponent to give you a broader specular highlight.