volume rendering light problem

Some days ago, I tried to achieve a headlight effect for my volume rendering.But the light’s position is always wrong after rotating the object. Thanks for guys in this forum, but the problem still exists. :frowning:

I send the fixed light position into the shader then calculate phong light in the shader.But the light position seems to change when rotating the object. So I try to transform the normal to eye space or transform the light position into model space. BUT all of them doesn’t work in my project!

I have some comments , please tell me what’s wrong in it.

Method 1. I use vec3 NN = normalize(gl_NormalMatrix * myNormalTexure.xyz); to transform normals.
These codes does work when rotating along x and y axis, but it goes wrong when rotating
along z-axis.
Method 2. I use vec3 lightVec = vec3(-gl_NormalMatrix[0][2],-gl_NormalMatrix[1][2],-gl_NormalMatrix[2][2]);
to transform light position.But the light stay fixed relative to my scene. I don’t know why it doesn’t work.

Any suggestion will be warmly welcomed.
thanks in advance :slight_smile:

Are u using the glLightfv command to set the light pos? if u are specifying the light position after u set the rotation matrix the state.light[n].position will contain the transformed light position ,… am not sure what method u are using

gl_coder
thank you for your reply
I’m not using glLightfv() actually. Here is my codes snippet outside the shader.

When initialize the window ,I use OpenGL tranformation as this:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0 , 0 , -2);

Then in my Redraw() function, my code goes like this:
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrixf(m_AB.matTranslation.m);
glMultMatrixf(m_AB.matRotation.m);
glMultMatrixf(m_AB.matScale.m);
shader->begin();
shader->sendUniform3f(...);  //send light pos
DrawTextureSlices();    
shader->end();    
glPopMatrix();
glPopClientAttrib();
/*-------m_AR is an object for my arcball class which is used to calculate the translation and rotation and scale ------------*/

I had used arcball method some time ago , it gave me problems with per vertex lighting … the scene used to go black at certain angles , the model view matrix was getting some scale factor … but guess my arcball code was wrong ,i dumped it for a diff method. . y dont u try the following just to check if the Z-rotation u said is causing the problem …

comment out the arc_ball transformation.
Call glRotate to rotate it about the z-axis … and see whether the light still moves with the scene …
also try using simple per vertex lighting to just check out if the arc works fine ,