PDA

View Full Version : volume rendering light problem



med3d
02-17-2006, 07:26 PM
Firstly I must say thanks to Klaus and jide for their kind help in my topic "some questions about a volume rendering
project ". :)
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...
glMultMatrixf(mat);

glEnable(GL_TEXTURE_3D);
glActiveTextureARB(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D , m_idTex3d);
m_pShaderObject->begin();

m_pShaderObject->sendUniform1i("my3DTexture",0);
//set light position.
m_pShaderObject->sendUniform3f("LightPosition",0.0f,15.0f,0.0f);
//draw 100 slices
for(int i=0; i<100; i++)
{
RenderOneSlice(i);
}
m_pShaderObject->end();
....
}

//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));

//diffuse
gl_FragColor.rgb += texVolume.aaa * lightCol * max(0.0,dot(NN,NL));

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

michael.bauer
02-18-2006, 03:20 AM
med3d,

afair Klaus told you EVERYTHING you should pay attention to. You should try to READ and UNDERSTAND what he told you :D 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.

Michael

med3d
02-18-2006, 03:56 AM
mlb,Please forgive me,I didn't mean to misunderstand Klaus's words. :) 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.

med3d
02-18-2006, 04:01 AM
please excuse my poor Engligh. :p 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". :p

michael.bauer
02-18-2006, 02:56 PM
med3d,

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 ;)

Michael

med3d
02-18-2006, 11:29 PM
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:
http://img248.photo.163.com/daymist/32308664/859399961.jpg
2.After rotation , it will be like this:
http://img248.photo.163.com/daymist/32308664/859398962.jpg

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:
http://img248.photo.163.com/daymist/32308664/859404270.jpg

4.Display the normal vectors: (after the roation)
http://img248.photo.163.com/daymist/32308664/859401895.jpg

med3d
02-18-2006, 11:33 PM
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.

michael.bauer
02-19-2006, 08:13 AM
med3d,

your pictures are invisible :p

med3d
02-20-2006, 01:10 AM
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:
http://img248.photo.163.com/daymist/32308664/859399961.jpg
second one:
http://img248.photo.163.com/daymist/32308664/859398962.jpg
3rd
http://img248.photo.163.com/daymist/32308664/859404270.jpg
4th
http://img248.photo.163.com/daymist/32308664/859401895.jpg

med3d
02-20-2006, 04:27 AM
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? :(

michael.bauer
02-20-2006, 06:15 AM
Hi med3d,

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

"Forbidden
You don't have permission to access /daymist/32308664/859399961.jpg on this server."

michael.bauer
02-20-2006, 08:07 AM
med3d,

You seem to confuse everything. Just a hint:

There is a difference between M*v and v*M!

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

M[0].dot(v),
...,
M[2].dot(v)

whereas M*v is the same as

M[0].x*v.x+M[1].x*v.y+M[2].x*v.z.
...
M[0].z*v.x+M[1].z*v.y+M[2].z*v.z

med3d
02-20-2006, 03:10 PM
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:)