PDA

View Full Version : specular reflections won't turn on + slightly strange light position



jmg
04-24-2002, 11:57 AM
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 );
glMaterialfv( GL_FRONT_AND_BACK , GL_AMBIENT_AND_DIFFUSE ,
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 );

glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER , GL_TRUE );
glLightModeli( GL_LIGHT_MODEL_TWO_SIDE , GL_TRUE );

// 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 );
glLoadIdentity();

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

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

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


Thanks,
---j


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

jmg
04-24-2002, 04:31 PM
Ok, 1 down, one to go. http://www.opengl.org/discussion_boards/ubb/biggrin.gif

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
varying mat_shininess, fiddling with making GL_LIGHT_MODEL_LOCAL_VIEWER and GL_LIGHT_MODEL_TWO_SIDE GL_FALSE,
and also grasped at some straws, like
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.

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. http://www.opengl.org/discussion_boards/ubb/smile.gif

---j

Old GLman
04-24-2002, 05:57 PM
Hi,

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

glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotdir);

Then with



glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0f);

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

jmg
04-24-2002, 06:31 PM
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:
http://users.bestweb.net/~jandl/snapshot4.png

Thanks!

---john


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

Old GLman
04-24-2002, 06:41 PM
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

jmg
04-24-2002, 06:53 PM
Computing per-vertex normals is my next task, thanks. http://www.opengl.org/discussion_boards/ubb/smile.gif

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.

Old GLman
04-24-2002, 07:11 PM
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