# OpenGL Lighting: Object Space vs Eye Space

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

## Recommended Posts

I'm playing about with lighting through Cg. I have setup a basic lighting algorithm which calculates diffuse,specular, global ambient etc. I also have some nice looking distance attenuation going on. It is using the same basic model as OpenGL. I am lighting all my objects in their local object-space and it's been working great... until they move. Then I found that the lighting still think they're in their original location and they end being completely unlit. Not a good thing. How can I solve this?? Is this a limit with lighting in object space? Do I need to perform my lighting in eye-space instead?? Sre there any tutorials on lighting in eye space?? How do you do this because I am unable to get the same visual effect like I had in my object-space calculations. The lighting keeps flying off everytime I rotate the camera and it only lights the scenery properly when I'm looking directly down the negative Z axis. Looking down the positive Z axis causes the scene to go pitch black. This may be linked to the light flying off when the camera is rotated. Anyone got any advice?? :) Cheers, Steve

##### Share on other sites
just multiply with current modelview ?!

##### Share on other sites
Like Eitsch said, you need to either transform the lights into object space, or transform the objects into view space. Obviously, your lighting calculations should be the same in either space, so I'm curious as to what's wrong with your view space lighting calculations. Perhaps you can paste the relevant snippet of your lighting calculation?

##### Share on other sites
hi, i have the same problem (http://www.gamedev.net/community/forums/topic.asp?topic_id=395799 == wrong explanation ;-( ).

The mathematical solution is simple and easy, but i have trouble with implementation.

Math:

Let l be the light position. Now do some transformation's with our object A. The resulting matrix is R. So the origin o = (0, 0, 0) for object A has coordinates Ro. Simple math. So the new light coordinates for A in A's space are l' = l - Ro. Isn't it so?

Implementation:

Here is the problem:

//the transformation
glTranslatef(10, 10, 10);
glRotatef(rotate, 0,0,1);

//computing the new coordinates of the light
GLfloat m[16];
glGetFloatv(GL_MODELVIEW_MATRIX, &(m[0]));

float r = rotate*(3.14159265/180);

float l[3];
l[0] = light.position[0] - m[12];
l[1] = light.position[1] - m[13];
l[2] = light.position[2] - m[14];

cgGLSetParameter3fv(cgGetNamedParameter(*vertexProgram, "lightVec"), l);

End of problem.

The problem is, the result is completly wrong. If i compute it by hand it is correct, but in games i would not be able to compute it by hand on the fly ;-D.

The problem is that the computation of Ro is wrong. I dont know how to aquire the R matrix. Well with the modelview matrix it does not work. One solution is to keep track of the transformation's done. So we would need another matrix multiplycation. But that is weird. I want to know how to get the R matrix from openGL.

Can anybody help?

XorMultiPleXus

##### Share on other sites
Well, there's a few solutions that should work. You can specify your light in view space and transform it to object space with the inverse modelview matrix. If you're specifying your light in world space, you can pass in R_inverse (given that R is he matrix that transforms from object to world space); just compute R_inverse in your application and pass it in as an uniform and transform your light by it in your vertex program.

Quote:
 The problem is that the computation of Ro is wrong. I dont know how to aquire the R matrix. Well with the modelview matrix it does not work. One solution is to keep track of the transformation's done. So we would need another matrix multiplycation. But that is weird. I want to know how to get the R matrix from openGL.

Unfortunately, there is no way to retrieve the model matrix from the modelview matrix unless you're carrying around an extra copy of the (inverse) view matrix.

##### Share on other sites
Here is the code that I'm using. Mhamlin: If you look at the code you will see the lines which perform the modelview inverse calculations. They are commented out cos of the problems I've been having. I think I may have to send the camera view vector to the cg function as well as the eye position. Maybe??

Output VertexShader(Input IN, Lighting LIGHT,                    Surfaces MATERIAL,                    uniform float4 eyePosition,                    uniform float3x3 modelView,                    uniform float4x4 modelViewInverse,                    uniform float3x3 modelViewInvTrans,                    uniform float4x4 modelViewProj){	Output OUT;	// Here we convert the output vertex from object-space to clip-space	OUT.position = mul(modelViewProj, IN.position);	// Set the material ambient property to 100% by default (temporary??)	MATERIAL.Ka = (1.0, 1.0, 1.0);	// *** NEW EYE-SPACE TRANSFORMATIONS ***	float3 P = IN.position.xyz;	float3 N = IN.normal.xyz;	// Convert the vertex position from object-space to eye-space/*	float3 P = mul(modelView, IN.position.xyz);	normalize(P);	// Convert the vertex normal using the inverse transpose of the modelview matrix	float3 N = mul(modelViewInvTrans, IN.normal);	normalize(N);*/		// Multiply the light position by the inverse modelview matrix//	float3 L = mul(modelViewInverse, LIGHT.position - P);	float3 L = normalize(LIGHT.position.xyz - P);	// *** NEW EYE-SPACE TRANSFORMATIONS ***	// Calculate the attenuation	float attenuation = Attenuation(P, LIGHT);	// Compute the emmisive term	float3 emmisive = MATERIAL.Ke;	// Compute the ambient term	float3 ambient = MATERIAL.Ka * LIGHT.ambient.xyz;	// Compute the diffuse term	float diffuseLight = max(dot(N, L), 0);	float3 diffuse = MATERIAL.Kd * LIGHT.colour.xyz * diffuseLight * attenuation;	// Compute the specular term	float3 V = eyePosition.xyz;	float3 H = normalize(L + V);	float specularLight = pow(max(dot(N, H), 0), MATERIAL.shininess);	if(diffuseLight <= 0) specularLight = 0;	float3 specular = MATERIAL.Ks * LIGHT.colour.xyz * specularLight * attenuation;	// Compute the final vertex colour	float3 lighting = (emmisive + ambient + diffuse + specular);	// Pass the texture in Texture Unit 0 to the fragment program	OUT.texCoord = IN.texCoord;	// Pass the current colour to the fragment program	OUT.colour.xyz = IN.colour.xyz * lighting;	OUT.colour.w = IN.colour.w;		return OUT;}

##### Share on other sites
I just quickly look over the code, so I apologize in advance if I misunderstood anything.

float3 P = mul(modelView, IN.position.xyz);normalize(P);

Firstly, I'm guessing the Cg compiler rejects this bit? Firstly, you can't multiply a 3-vector by a 4x4 matrix, you need to just pass in IN.position. Secondly, you shouldn't normalize the result. P is not a vector, it's a point--normalizing it will change its translation, which is disastrous.

float3 L = mul(modelViewInverse, LIGHT.position - P);

So you're specifying your light position in object space? By the time you are performing the vector subtraction, P is in view space and LIGHT.position is (presumably) still in object space. You need to transform the light into view space, then calculate the vector from P to the light.

The view space transformation should probably look more like this:
float3 view_space_vertex = mul(modelView, IN.position).xyz;float3 view_space_normal = normalize(mul(modelViewInvTrans, IN.normal).xyz);float3 view_space_light = mul(modelView, LIGHT.position).xyz; /* Assuming LIGHT.position is the light's position in object space. */float3 vertex_to_light = normalize(view_space_light - view_space_vertex);...

##### Share on other sites
I have create a small library for keeping track of the model matrix. Feel free to use it. It is here http://xormultiplexus.googlepages.com/opengl

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015658
×