PDA

View Full Version : Lighting Confusion



ToddAtWSU
05-13-2006, 09:20 PM
I am having problems with using lighting. I am drawing a simple 3x3 image that is 3-dimensional. I have successfully drawn it as a wireframe object but when I try to draw it as GL_POLYGON I get a bright white object. I have a sample program that my friend gave me that successfully lights an icosahedron in another program. He said I could use his lighting method in my program to draw the image. Unfortunately, his lighting produces this bright white object for me. Here is what I do to do my lighting:

In an init function:

mat_specular[0] = 1.0;
mat_specular[1] = 1.0;
mat_specular[2] = 1.0;
mat_specular[3] = 0.15;
mat_shininess[0] = 50.0;
mat_solid[0] = 0.75;
mat_solid[1] = 0.75;
mat_solid[2] = 0.0;
mat_solid[3] = 1.0;
/* These are properties of light. */
position[0] = 10.0;
position[1] = 10.0;
position[2] = 5.0;
position[3] = 0.0;
light_diffuse[0] = 1.0;
light_diffuse[1] = 1.0;
light_diffuse[2] = 1.0;
light_diffuse[3] = 1.0;
light_specular[0] = 1.0;
light_specular[1] = 1.0;
light_specular[2] = 1.0;
light_specular[3] = 1.0;

glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_solid );
glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );

glLightfv( GL_LIGHT0, GL_POSITION, position );
glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse );
glLightfv( GL_LIGHT0, GL_SPECULAR, light_specular );

glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_DEPTH_TEST );
glEnable( GL_NORMALIZE );
//glEnable( GL_CULL_FACE );

glFrontFace(GL_CW); When I draw my polygons, I do this:

calcNormal( v, normal );
glBegin( GL_POLYGON );
glNormal3fv( normal );
glVertex3fv( v[0] );
glVertex3fv( v[1] );
glVertex3fv( v[2] );
glEnd( ); This code is in a loop to draw multiple polygons. Why am I getting bright white objects all the time and not a shaded yellow-ish object like in my sample program. The sample program did not color the polygons at any time. Thanks for your help!!!!

Mars_999
05-13-2006, 09:44 PM
Well start with
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_solid );

and see if that works? If so you are rendering the back side of your polygons. Just a thought off the top of my head....

ToddAtWSU
05-13-2006, 10:20 PM
That didn't help any. It is still just a bright white object. Does it appear something is being done incorrectly? Thanks!

Komat
05-14-2006, 09:06 AM
What happens if you set specular contribution of light to zero? Do you use textures?

ToddAtWSU
05-14-2006, 10:21 AM
Actually, my problem seems to have come that I initialized my lighting before creating my window. Now that I create my window first and then initialize everything, it appears to be lighting properly. Any idea why this would be that way though? Thanks!

Komat
05-14-2006, 02:41 PM
The OGL functions needs valid OGL context. Otherwise they do not work.