Jump to content
  • Advertisement
Sign in to follow this  

Transforming light position in a vertex program?

This topic is 4971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying some simple vertex and fragment programming, but I'm somewhat stuck. When using the fixed-function pipeline, it's no problem to transform a lightsource with the modelview matrix by just setting the position inside the correct "scope". With vertex programs, I'm somewhat stuck. No matter what I try, the lightsource doesn't seem to move properly or does something weird. If I manually calculate the light's position and position it with the identity transform, the behaviour is essentally reversed (with some minor strageness), meaning that now the light is almost properly moving with the vertex programs but not moving when using fixed-function shading. The relevant code when positioning the light is:
      // build 3x3 matrix from GL data (transform result is correct)
      GLfloat mv[16];
      glGetFloatv( GL_MODELVIEW_MATRIX, mv );
      ThreeD::Matrix3 cmv;
      cmv.fromGLMatrix( mv );

      // Vector object containig light position
      ThreeD::Vector3 clp( config::light_pos[0], config::light_pos[1], config::light_pos[2] );

      // Transform light position according to GL matrix stack
      clp = cmv*clp;
      clp.dump( "  clp" ); // dumps vector to console

      // position the light
      glLightfv( GL_LIGHT1, GL_POSITION, clp.data );

The vertex shader computing the L vector is:

ATTRIB in = vertex.position;
ATTRIB nrm = vertex.normal;
ATTRIB tex = vertex.texcoord[0];

PARAM mvp[4] = { state.matrix.mvp };
PARAM mv[4] = { state.matrix.modelview };
PARAM light = state.light[1].position;
PARAM const = {0.0, 0.0, 0.0, 0.2};

OUTPUT out = result.position;
OUTPUT oTex = result.texcoord[0];
OUTPUT oNrm = result.texcoord[1];
OUTPUT lVec = result.texcoord[2];
OUTPUT vDir = result.texcoord[3];

TEMP tmp;
TEMP tmp2;

#transform the positions
DP4 tmp2.x, in, mvp[0];
DP4 tmp2.y, in, mvp[1];
DP4 tmp2.z, in, mvp[2];
DP4 tmp2.w, in, mvp[3];
MOV out, tmp2;

#transform the position to eye-space
DP4 tmp.x, in, mv[0];
DP4 tmp.y, in, mv[1];
DP4 tmp.z, in, mv[2];
DP4 tmp.w, in, mv[3];

#view direction
MOV vDir, -tmp;

#transform the normal to eye-space
DP3 oNrm.x, mv[0], nrm;
DP3 oNrm.y, mv[1], nrm;
DP3 oNrm.z, mv[2], nrm;

#calculate L
SUB tmp, tmp, light;

#normalize L
DP3 tmp2.w, tmp, tmp;
RSQ tmp2.w, tmp2.w;
#implicit MOV to output register
MUL lVec, tmp, tmp2.w;

#output the texcoords
MOV oTex, tex;

(The vertex program is an adapted one from a tutorial, I only tried to introduce proper light positioning from actual GL context data). Now, if anyone knows a working example or can explain what I'm doing wrong, I'd be eternally grateful [smile]

Share this post

Link to post
Share on other sites
I usually do that on the CPU and send it as a local or env data. Mainly because my shaders are already extremely packed with way too many other things to deal with those calculations.

Share this post

Link to post
Share on other sites

PARAM mv[4] = { state.matrix.modelview };


#transform the normal to eye-space

DP3 oNrm.x, mv[0], nrm;

DP3 oNrm.y, mv[1], nrm;

DP3 oNrm.z, mv[2], nrm;

Your mv should be state.matrix.modelview.inverse.. I don't still understand why you have to rotate the normal with the inverse matrix in GL, but you just do..

And on the fixed-function pipeline, you don't need to transform the light, like you do for vertices and stuff. Lights are automatically transformed by GL.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!