PDA

View Full Version : Confused about direction light problem. (cause it doesn't work)



darkrappey
11-23-2006, 01:02 AM
My intention is to use basic directional lighting.
I am implementing the light set up like so:



GLfloat lightDir[] = { 0.0, 1.0, 0.0, 0.0};

glLightfv(GL_LIGHT1, GL_POSITION, lightDir);



GLfloat ambientProperties[] = {0.0f, 0.0f, 0.0f, 1.0f};

GLfloat diffuseProperties[] = {1.0f, 1.0f, 1.0f, 1.0f};

GLfloat specularProperties[] = {0.0f,0.0f, 0.0f, 1.0f};



glLightfv( GL_LIGHT1, GL_AMBIENT, ambientProperties);

glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuseProperties);

glLightfv( GL_LIGHT1, GL_SPECULAR, specularProperties);





glEnable(GL_LIGHT1);

glEnable(GL_LIGHTING);
The problem is, the brightness of the polygons does not change based on their rotation(normal), but instead changes on the entire object's rotation (the last glRotatef call).

For example I have a small game where you fly a plane, The plane is dark all over until you perform a roll in the X axis. At which point the entire plane becomes white as if it was one big polygon.

The initialization code seems pretty straight forward so I don't see how it could be causing problems, this is why I am confused. Could this problem be deeper in the code?
If you would like any screen shots pleas notify me.

-thanks in advance

jide
11-23-2006, 02:31 AM
When you specify the light position before your modelview/camera matrix then the light will move with the viewpoint. Simply call the light position after gluLookAt if you use it.

Relic
11-23-2006, 02:36 AM
Two things:
1.) Does your plane have per vertex normals?
If not, OpenGL will use the normal in its current state for all vertices.
2.) The light's position and directions are transformed by the current modelview matrix.
If you want to have a static light direction you need to send it outside the transformation hierarchy, for example directly after glLoadIdentity() on the modelview matrix.

GL_LIGHTi enums start at GL_LIGHT0.

darkrappey
11-23-2006, 11:52 AM
Well, I'm not using gluLookAt, and it is already called after glLoadIdentity(of ModelView).

So hope it is your 1) suggestion, except, I'm not sure what you mean by by per-vertex normals. How could a single point have a normal? Aren't normals calculated by OpenGL automatically?

My geometry drawing loop only has calls to glTexCoord2f
glVertex3f.

Several of the objects in the scene are display lists and they behave the same way.

-thanks again in advance

jide
11-23-2006, 01:45 PM
No, normals aren't calculated automagically. Per vertex normals are just a mean for representing curve surfaces whereas per face normals won't able you to do so.

You'll need at least per face normals (so one call of glNormal for each face) if you don't have the per vertex ones. A cross product will give you that per face normal.

darkrappey
11-23-2006, 09:11 PM
Oh, I see. Okay.
I'll look for some docs on how to use glNormal.
Thanks!