PDA

View Full Version : Light Position in eye's cordinate?



ZwqXin
09-24-2009, 10:23 PM
Hi,glad that you can have a look at my problem.

You know, we can explictly define the position of a set of light, using opengl code glLightfv(GL_LIGHT_POSITION,...) or so.

And I think the position is defined as a world -coordination, right?

Then, if I want to pass the lightposition to my shader, I can well use gl_LightPosition[0] orso, Or I can explictly pass the data as vec3/vec4 , and use it.(Well , in opengl 2.x)

is there anything different of the two ways?

In some articles, lighthouse3D's glsl lesson as well, it's said
"OpenGL stores the lights direction in eye space coordinates"

so, if I use the latter means, should I multiply the incoming shader vec3 uniform, with a somewhat "ViewMatrix"? And is gl_LightPosition[0] really the eye-coordinate? it changes along with the camera changes?

To get the light vector, directly use
"lightvec = gl_LightPosition[0] - gl_ModelViewMatrix * gl_Vertex" ?

I am getting confused.....

Dark Photon
09-25-2009, 09:31 AM
You know, we can explictly define the position of a set of light, using opengl code glLightfv(GL_LIGHT_POSITION,...) or so.

And I think the position is defined as a world -coordination, right?
No, technically the light position you pass to the above call is in object coordinates, as (when the above call is made) it is automagically multiplied by the entire MODELVIEW matrix to convert it into eye coordinates.

Now if your current MODELVIEW matrix is only the VIEWING matrix (meaning the MODELING transform is the identity in this case), then of course object coordinates == world coordinates for this case, so you could consider the input position in object or world coordinates.


Then, if I want to pass the lightposition to my shader, I can well use gl_LightPosition[0] orso, Or I can explictly pass the data as vec3/vec4 , and use it.(Well , in opengl 2.x)

is there anything different of the two ways?
Yes, these are the same, so long as the lightposition == gl_LightPosition[0] (that is, lightposition in your shader was converted to eye space using the exact same MODELVIEW transform that gl_LightPosition[0] was).


In some articles, lighthouse3D's glsl lesson as well, it's said
"OpenGL stores the lights direction in eye space coordinates"
so, if I use the latter means, should I multiply the incoming shader vec3 uniform, with a somewhat "ViewMatrix"?
s/ViewMatrix/ModelViewMatrix/ aka MODELVIEW


And is gl_LightPosition[0] really the eye-coordinate? it changes along with the camera changes

To get the light vector, directly use
"lightvec = gl_LightPosition[0] - gl_ModelViewMatrix * gl_Vertex" ?

Almost. gl_ModelViewMatrix * gl_Vertex yields a vec4 and gl_LightPosition[0] is a vec4. Subtracting w components doesn't really makes sense, and the result is a vec4 -- you probably want a vec3.

So assuming your MODELVIEW only has the usual rotates and translates in it and your light source is a point light source (your math only makes sense if it is), then your w's are always 1. So this is probably what you want:

vec3 lightvec = gl_LightPosition[0].xyz - (gl_ModelViewMatrix * gl_Vertex).xyz" ?


I am getting confused.....
Sounds like you've just about got it.

ZwqXin
09-26-2009, 02:12 AM
Thank you
Really help me a lot!

some questions,though.
1.Every set of light has its own object coordination, and would be affected by the modelview, is it right?

2.If
glMatrixMode(GL_PROJECTION);...
glMatrixMode(GL_MODELVIEW);...
glRotate();
glTranslate();
glLightfv(GL_LIGHT0, GL_POSITION,..);
....
As your said, when call to glLightfv, the lightposition is transformed by the modelview.
Then will it be transformed again by the modelview again with the glRotate,glTranslate?
Or will it be omitted then, as it isn't "really" a defined point?


If,
{ //init of the program
glLightfv(GL_LIGHT0, GL_POSITION,..);
}
glMatrixMode(GL_PROJECTION);...
glMatrixMode(GL_MODELVIEW);...
glRotate();
glTranslate();

What kind of ModelView does it suffer? Or thre's no effect? or only after the program know the modelview then the lightposition would be transformed?
If the modelview matrix changes, does the light's position in eye-space changes?

3.Now I want to always place the light at POS(0,0,10,1) in the world-space,for example.
then,
glMatrixMode(GL_PROJECTION);...
glMatrixMode(GL_MODELVIEW);...
gluLookat()...//camera changable
glLightfv(GL_LIGHT0, GL_POSITION,&POS);
glRotate();
glTranslate();
DOes this right?

4.QUOTE: s/ViewMatrix/ModelViewMatrix/ aka MODELVIEW
I am soory but what does it mean?

thank you again, and anyone who offeres help as well.

Dark Photon
09-28-2009, 06:09 PM
1.Every set of light has its own object coordination, and would be affected by the modelview, is it right?
Yes. By the modelview that's active when you call glLightfv( ... GL_POSITION ... ).


2.If
glMatrixMode(GL_PROJECTION);...
glMatrixMode(GL_MODELVIEW);...
glRotate();
glTranslate();
glLightfv(GL_LIGHT0, GL_POSITION,..);
....
As your said, when call to glLightfv, the lightposition is transformed by the modelview.
Then will it be transformed again by the modelview again with the glRotate,glTranslate?
No. There's no "again". glRotate and glTranslate modify the MODELVIEW matrix. glLightfv then takes that then-active MODELVIEW matrix and multiplies it by the position you pass in at the time you call glLightfv( ... GL_POSITION ...). This transforms the light position you pass in to eye-space. The light position is then stored internally by OpenGL in eye-space.


If,
{ //init of the program
glLightfv(GL_LIGHT0, GL_POSITION,..);
}
glMatrixMode(GL_PROJECTION);...
glMatrixMode(GL_MODELVIEW);...
glRotate();
glTranslate();

What kind of ModelView does it suffer?

The one that was active when you called glLightfv( ... GL_POSITION ...).

So the glRotate and glTranslate in the above make no difference, since they're after your call to set the light position.


If the modelview matrix changes, does the light's position in eye-space changes?
No. There's no state tracking here. If you want to move the light, you need to change the modelview matrix and then update the light position again.


3.Now I want to always place the light at POS(0,0,10,1) in the world-space,for example.
then,
glMatrixMode(GL_PROJECTION);...
glMatrixMode(GL_MODELVIEW);...
gluLookat()...//camera changable
glLightfv(GL_LIGHT0, GL_POSITION,&POS);
glRotate();
glTranslate();
DOes this right?
Yes. Again, the glRotate and glTranslate here have absolutely no bearing on the MODELVIEW matrix used to transform the light position.