[osg-users] How to translate the light posiotion from world space to eye space in glsl

guilianzhang guilianzhang at 163.com
Mon Sep 8 20:27:15 PDT 2008


Hi,
To make phong shading using shaders(in GLSL), I have to pass the the light position to the vertex shader,  But the position of the vertex will be in eyespace (after multiplication with the ModelView matrix). So I need the light position in eye-space as well. Which is the best way of obtaining the correct light position? Either I need the vertex position to be in world space, I do in this way:

 
mat4 modelMatrix = osg_ViewMatrixInverse* gl_ModelViewMatrix ;
WorldPos=modelMatrix * gl_Vertex;
mat3 normalmatrix=mat3(modelMatrix);
WorldNrm=normalize(normalmatrix * gl_Normal);

though it works well to get the world space coordinate of each vertex, but to compute specular of object I have to know the eye position, since we are not in eye coordinates the eye postion is no longer (0,0,0), must the eye coordinate be passed by uniform?

Or I need the light position in eye space. I try to translate the light position into eye coordinate in this way:
 Pos_light0 =  osg_ViewMatrix *light0_pos  ; 
 or 
 Pos_light0 =  light0_pos * osg_ViewMatrix ; 

 but it seems not work.

What are the general strategy for this problem?


Thanks very much!


Julie
2008-09-09
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20080909/7c75ab2c/attachment-0002.htm>


More information about the osg-users mailing list