Lighting Theory

Started by
7 comments, last by V-man 16 years, 11 months ago
ok Im really new to OpenGL and 3D programming as a whole. So Im reading along in this book "OpenGL Game Programming" (which I must add has alot of badly copied code in it but that actually helps cause when you can fix the bugs it shows you understand it) anyways, I got to lighting and I cant seem to comprehend why when you use the vector (0,0,1) to get directional light it shines into the screen. Last time I checked, the vector (0,0,1) is out of the screen so shouldn't it shine out of the screen? I'm only in Grade 12 and Im only doing Geometry this semester so I may have made a mistake.
Advertisement
If it doesn't match your expectations, it probably has to deal with different transformed spaces.
The light vector could be eye-space, world-space or tangent-space or whatever.

Previously "Krohm"

In opengl, by default, the view looks down the negative z axis. That is, the more negative the z component it, the further away the thing is. From helping other people, the confusion may have arisen when you tried to move the camera. In opengl, you can't move the camera as such. But you can move everything else, which if you take a moment to think about it is the same thing. So translate(0, 0, -10) or whatever you do to set up your view is actually moving your object/world 10 units along the negative z axis, away from the camera. Your (0, 0, 1) on the other hand points towards the camera.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
The vector is in eye coordinates as someone else hinted... I'll give you one guess as to the direction of the z axis of the eye coordinate system...
I know you cant move the camera, I dont have any trouble with transformations, but no where did it say that the lights were in eyespace and all we've done is modelview and projection so far so I assumed it was in the modelview
ok still not making sense

if I make

float lightPosition[] = { 0.0f, 5.0f, -5.0f, 0.0f };
....
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

if its in the eye co-ordinate then that should be 5 units behind the camera and 5 units above should it not?

Its not its 5 units infront of the camera, behind the rotating cube, and 5 units above it.

So why when I do this,


float lightPosition[] = { 0.0f, 0.0f, 1.0f, 1.0f };
....
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

the light comes FROM the positive z axis when it should come FROM the negative z axis?

This lighting is making my head hurt.
float lightPosition[] = { 0.0f, 0.0f, 1.0f, 1.0f };

creates a point light because w = 1.0

An object at {0, 0, 0} will get the light from the positive z axis.

For directional lights, w must be 0.0
The light vector should be

float lightPosition[] = { 0.0f, 0.0f, 1.0f, 0.0f };

The light vector should be in the opposite direction. It will be able to light the object at {0, 0, 0}

Although float lightPosition[] = { 0.0f, 0.0f, -1.0f, 0.0f };
seems like the logical choice, GL wants the vector to be in the other direction.
It's normal because that what the mathematics of light requires.


PS : you should normalize float lightPosition[] = { 0.0f, 5.0f, -5.0f, 0.0f };
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
err I knew it had to be 0.0 but I copied it when I was actually positioning it behind the cube and forgot to change that.

Thanks, but I dont know why it has to be backwards for math and I dont know why the book didnt say anything, either would have been easier but I thank all of you for putting up with me
Read about the mathematics of lighting.
For doing diffuse lighting, it's normal dotproduct lightvector and the lightvetor is from the vertex towards the light

The math goes like

diffuseColor = (lightvec DOT normal) *materialdiffuse * lightdiffuse;
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement