volume rendering light problem

Firstly I must say thanks to Klaus and jide for their kind help in my topic "some questions about a volume rendering
project ". :slight_smile:
But the problem that light moves with the object haven’t been solved.So I raise this new topic to describe
the problem in detail ,and look forward to any help.

Problem decription:
In my volume rendering project, I manually set the light position and pass it into the fragment shader. But the light
position seems to move the object,so the object have one face which is always lighted.
If I want to make the light still in one position and shine different part of object when I rotate the object, how can I achieve that?

relative codes snip:

 void CEndoscopeWnd::RenderScene()
 //multiply the matrix for translation and rotation...	
 glBindTexture(GL_TEXTURE_3D , m_idTex3d);		
 //set light position.	
 //draw 100 slices	
 for(int i=0; i<100; i++)	
  //my vertex shader
  varying vec3 L;
  uniform vec3 LightPosition;
  void main(void)
  gl_TexCoord[0] = gl_MultiTexCoord0;		
  vec3 volPos = vec3 (gl_ModelViewMatrix * gl_Vertex);        
  L = LightPosition - volPos;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;   
 //my fragment shader
 uniform sampler3D my3DTexture;
 varying vec3 L;
 void main (void)
 vec4 texVolume = texture3D(my3DTexture , gl_TexCoord[0]);
texVolume.xyz -= 0.5;//map [0,1] to [-1,1]	 
texVolume.xyz *= 2;	
const float specularExp = 128.0;		
vec3 NN = normalize(gl_NormalMatrix * texVolume.xyz); //<<<----transform normal vector
vec3 NL = normalize(L);	vec3 lightCol; 

//light color	
lightCol = vec3(1.0 , 1.0 , 1.0); //white light	
gl_FragColor = vec4(0.0);
//half vector		
vec3 NH = normalize(NL + vec3(0.0 , 0.0 , 1.0));
gl_FragColor.rgb += texVolume.aaa * lightCol * max(0.0,dot(NN,NL));	

gl_FragColor.rgb += lightCol * pow(max(0.0,dot(NN,NH)),specularExp);	
gl_FragColor.a = texVolume.a;


afair Klaus told you EVERYTHING you should pay attention to. You should try to READ and UNDERSTAND what he told you :smiley: Your problem is to find the RIGHT transformations for your problem. If you rotate your volume position while the light vector is constant, everything seems ok. But have a closer look at the normal vectors you read from your 3d-texture. In your example they are not dependent on the modelview matrix, so they are not transformed! Only the volume postition is transformed, not the vectors read from the texture. Now think about that! If you have understood everything, then go on reading: If you want to find an efficient solution, think about transforming the light vector instead of the normal vectors.


mlb,Please forgive me,I didn’t mean to misunderstand Klaus’s words. :slight_smile: In fact,I read Klaus’s words for
many times,and experimented as he said, but the result wasn’t satisfying.

I made an experiment just now: simply draw a cube and calculate the phong-light in the shader with the
same algorithm, and the light was right !!! So I conclude that the normal used in the shader must be
something wrong! The light position is probably Ok while the normal is the devil on the behind.

Now back to my headlight problem,
You are right ,mlb, the normal vector are not dependent on the modelview matrix.But problem comes:
Please look throught my fragment shader,I USED ‘gl_NormalMatrix’ to transforms normals from model to
eye space (just as Klaus told me)
, why the result remains wrong?

many thanks ,mlb and Klaus.

please excuse my poor Engligh. :stuck_out_tongue: Yesterday I even understand the word “headlight” as ‘hint’,it is
not until today when I read the opengl FAQ that I know the right meaning for the word “headlight”. :stuck_out_tongue:


I’m unable to tell you, what about some additional information. Are your transformations correct now?
Or is it a problem in the lighting computations.

First, you should directly display the normal vectors, e.g. gl_FragColor.rgb = NN (post a pic here if possible). If you rotate the volume the colors (== normals) should “rotate” too. Then, if the normals are correct, test the shading. First try the diffuse shading and display it excluding all other computations. Then display the specular only, to see if that is the source of your problem. In short, DEBUG your code – if you don’t know where an error is located, it could be anywhere, so you have to check EVERYTHING. Your shader isn’t extremely complex, so you can easily test every single line to find out where the problem could be :wink:


Problem still exist
Four pictures printed from my project to illustrate the problem.
The shader is the same as the one above , except some small code-change decribed in the picture.

Pic Pair One:

  1. If I use the light direction as Klaus said, the render result will be like this:

    2.After rotation , it will be like this:

Pic Pair Two:
3.As jide said, I directly display the normal vectors , this one is the result .The next pic
is what it is like after rotation.
Display the normal vectors:

4.Display the normal vectors: (after the roation)

The problem must be the normals! But I have no idea how to solve it.Please give me some clues,
many heartful thank in advance. I’m not experienced in the volume rendering field.


your pictures are invisible :stuck_out_tongue:

hi , mlb.
I have send a message to you ,which contains my MSN. If possible, would you please
reply with your MSN or emain address , so that I can send these four pictures to you.

This forum’s private message function will not remind the user when a new msg received.
It’s so unconvenient!The private messages are listed in the profile.

First pic:
second one:

Klaus, I’m now fully understand what you said about the model to eye coordinate.
But all my try to achieve a headlight seems to be useless:(

I even these three kind of light transformation to convert the light into model coordinate:
1.vec3 NL = vec3(0,0,-1) * gl_NormalMatrix ;
When use this block of code, there is almost a headlight effect when rotating horizontally

2.vec3 NL = gl_NormalMatrix * vec3(0,0,-1);
When use this block of code, there is almost a headlight effect when rotating vertically

3.vec3 NL = vec3(-gl_NormalMatrix[0][2] , -gl_NormalMatrix[1][2] , -gl_NormalMatrix[2][2]);
this generate no-headlight effect.

why? :frowning:

Hi med3d,

The links to your pics don’t work. The error message is

You don’t have permission to access /daymist/32308664/859399961.jpg on this server.”


You seem to confuse everything. Just a hint:

There is a difference between Mv and vM!

Only one of them can be right. M[0] is the first column of M. v*M is the same as


whereas M*v is the same as



YOu are right , I got confused with the using * and dot.
I agree that “vec3 NL = vec3(-gl_NormalMatrix[0][2] , -gl_NormalMatrix[1][2] , -gl_NormalMatrix[2][2])” can do the right transformation, but why it doesn’t work??

sorry,I find no ther way to post the pictures here:)
My Msn is: daymist@hotmail.com, please kindly help me.
thanks in advance:)