specular reflections won't turn on + slightly strange light position

I can’t seem to get specular lighting to turn on in my little
OGL program. Well, it looks off to me. Take a look here: http://users.bestweb.net/~jandl/snapshot1.png

Note (in the above pic):
red == (+) x-axis
green == (+) y-axis
blue == (+) z-axis
The axes are about 10 units long.

Also, the direction of the lighting (as seen in the pic) seems
a bit different than what I’ve specified in the code – not
sure if this is related. Here’s the relevant code:

glClearColor( 0.0 , 0.0 , 0.0 , 0.0 );
glShadeModel( GL_SMOOTH ); // Yes, this is redundant.
glDepthFunc( GL_LESS ); // Yes, this is redundant.

glPolygonMode( GL_FRONT , GL_FILL ); // Yes, this is redundant.
glPolygonMode( GL_BACK , GL_LINE );

// The material:
// Bright specular reflections.
float mat_specular[] = { 1.0 , 1.0 , 1.0 , 1.0 };
int mat_shininess[] = { 100 } ; // Very shiny.

// Material is mostly green, that is to say, the ambient and
// diffuse light reflected off of the material is mostly green.
float mat_amb_and_diffuse[] = { 0.0 , 0.8 , 0.2 , 1.0 };

glMaterialfv( GL_FRONT_AND_BACK , GL_SPECULAR , mat_specular );
glMaterialiv( GL_FRONT_AND_BACK , GL_SHININESS , mat_shininess );
mat_amb_and_diffuse );

// The light:
float light_ambient[] = { 0.2 , 0.2 , 0.2 , 1.0 };
float white_light[] = { 1.0 , 1.0 , 1.0 , 1.0 };

glLightfv( GL_LIGHT0 , GL_AMBIENT , light_ambient ); // redundant
glLightfv( GL_LIGHT0 , GL_DIFFUSE , white_light ); // redundant
glLightfv( GL_LIGHT0 , GL_SPECULAR , white_light ); // redundant

// This one’s funny… The light doesn’t seem to be shining in the
// -1,-1,-1 direction but rather the -1,-1,0 dir… ******
float light_dir[] = { -1.0 , -1.0 , -1.0 , 0.0 }; // Directional.
glLightfv( GL_LIGHT0 , GL_POSITION , light_dir );


// And finally, enable everything.
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_DEPTH_TEST );

Also, in my reshape callback I’ve got:

glViewport( 0 , 0 , static_cast<GLsizei>( w ) ,
static_cast<GLsizei>( h ) );
glMatrixMode( GL_PROJECTION );

// args: fovy, aspect, near, far
gluPerspective( 40.0 , 1.0 , 5.0 , 45.0 );

glMatrixMode( GL_MODELVIEW );

gluLookAt( 20.0 , 20.0 , 20.0 , // camera location
0.0 , 0.0 , 0.0 , // looking at
0.0 , 1.0 , 0.0 ); // which way up is


[This message has been edited by jmg (edited 04-24-2002).]

Ok, 1 down, one to go.

Turned out that I needed to make the call to glLight*( …GL_POSITION…) after the call to gluLookAt() in my reshape callback. This now makes the position of the light correct so the “slightly strange light position” in the subject of this thread is taken care of. The reson is that light positions specified by glLight*() get transformed by the modelview matrix just like any other vertices.

I still can’t get the specular highlights to show up though. I’ve tried
[ul][li]varying mat_shininess,[]fiddling with making GL_LIGHT_MODEL_LOCAL_VIEWER and GL_LIGHT_MODEL_TWO_SIDE GL_FALSE,[/ul][/li]and also grasped at some straws, like
[ul][li]culling the back faces,[
]making glLightfv( GL_LIGHT0, GL_DIFFUSE, more_dull ),[]and even telling the calls to glMaterial() to use GL_FRONT rather than GL_FRONT_AND_BACK.[/ul][/li]
What’s very curious is that, now, when I set the light to directional, it points in the correct direction. When I set it to positional, it’s -x, -y, & -x of where it should be…

If anyone could throw a suggestion my way, I’d appreciate it.



Based on my experiences with specular lighting, using a directional light source is not going to work. You need to make it positional. Also, from the look of the picture, you need to specify vertex normals to get a nice specular effect. My sugestion is to make a spotlight. Specify the position and direction like this:

GLfloat lightpos[] = { 0.0f, 6.0f, 0.0f, 1.0f }; // spotlight postion (1.0f for positional light source)
GLfloat spotdir[] = { 0.0f, -1.0f, 0.0f }; // spotlight direction


Then with


you can make your positional lightsource shine in all directions. Use a value of 90 or less to simulate a spotlight. This method works great for me. Hope this helps.

Old GLman

Well son-of-a-gun Old GL Man, your comment

you need to specify vertex normals

made me double-take. I had specified normals! I mean, take a look at the pic and it’s obvious, no?

Well, apparently not. I had goofed up my normals and had them all pointing the wrong way.

I changed the light to positional, fixed the normals, and voila:



[This message has been edited by jmg (edited 04-24-2002).]

Hey lookin much better! You probably know this but I presume you are specifying surface normals there and not vertex normals. With vertex normals you can achieve a smooth appearance rather than the boxy look. Glad I could help.

Old GLman

Computing per-vertex normals is my next task, thanks.

By the way, it works fine with the directional as well as the positional lighting (for my simple case anyhow).

My trouble was getting fooled because I was having OGL draw fronts and backs of the polys – somewhere it was reversing normals for me.

Yeah its definately easy to miss stuff when your programs get really complex. Happens to me all the time hehe. Yeah you are right about the positional/directional thing. What I meant to say was because directional lights have the appearence of coming from an infinite distance away, it may have been hard to tell whether you had any specular effects or not. Atleast I have had that problem.

Old GLman