• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Does the OpenGL fixed function pipeline compute lighting in view-space?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1infact  Members

Posted 31 May 2012 - 05:04 AM

Does the OpenGL fixed function pipeline compute lighting in view-space?

If the answer is yes, then how does it cope with view transformations with non-uniform scale? Actually, how does it cope with view transformations incorporating any scale at all?

If this is true then scaling the view space will result in different light-to-vertex distances, meaning the lighting intensity for point-lights will change as the view matrix is scaled.

Lighting in world-space would make the computed point-light intensity independent of view space scaling, but would require:
• That an object-to-world matrix is supplied to the API (such as in DirectX, where the light positions are specified in world-space).
• That the API transform all geometry twice when drawing. Once by world*view*proj into clip space, and again by world, in order to compute lighting at the vertices in world space.

I also posted this to stackoverflow, perhaps you want to answer it there too: http://stackoverflow...g-in-view-space

Edited by infact, 31 May 2012 - 05:30 AM.

### #2V-man  Members

Posted 31 May 2012 - 05:14 AM

It sounds like you aren't a OpenGL user.
GL doesn't have a view matrix and it doesn't have a world matrix. GL only has a modelview matrix.
GL transforms vertices by the modelview matrix to get eye space vertices. It also transform normals by the inverse transpose of the modelview matrix to get eye space normal vectors. Lighting is computed in eye space. In order to deal with non uniform scale, you can glEnable(GL_NORMALIZE). If you only have a uniform scale, you can glEnable(GL_RESCALE) which is "faster" than GL_NORMALIZE.

Of course, all of the above is relevant to the year 2002 and before. It's time to move to the world of shaders.

Edited by V-man, 31 May 2012 - 07:38 AM.

Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

### #3infact  Members

Posted 31 May 2012 - 05:21 AM

My question is not about the effect that the transformation has on the normals.

From the documentation I can see that OpenGL has a MODELVIEW matrix, which is a concatenation of the MODEL and VIEW matrices, so from that point of view OpenGL can support separate model and view matrices.

Now consider the difference between: distance(vertex*model - world_light) and distance(vertex*modelview - view_light).

Notice how the distance between the vertex and the light can change, potentially depending on the non-uniform scaling in the matrix.

Edited by infact, 31 May 2012 - 05:52 AM.

### #4V-man  Members

Posted 31 May 2012 - 07:28 AM

From the documentation I can see that OpenGL has a MODELVIEW matrix, which is a concatenation of the MODEL and VIEW matrices, so from that point of view OpenGL can support separate model and view matrices.

From the point of view of OpenGL, there is only 1 matrix called the modelview matrix. Notice how "modelview" is composed of the words model and view. No, OpenGL doesn't support "separate model and view matrices". It is a single matrix.

Now consider the difference between: distance(vertex*model - world_light) and distance(vertex*modelview - view_light).

Notice how the distance between the vertex and the light can change, potentially depending on the non-uniform scaling in the matrix.

You don't apply the same matrix to all your objects and all your lights.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

### #5infact  Members

Posted 31 May 2012 - 09:40 AM

My point is that lighting in world space will be different to lighting in view space.

distance(vertex*view - light) != distance(vertex - light*view^-1)

this is clear if we let the light origin be 0,0,0, and consider a matrix with no translation, then we have that:

length(vertex*view) != length(vertex)

which is true

In other words, the transformation distorts the lighting

So I wonder why opengl does lighting in view space and not in world space.

Is it for the the sake of having a single modelview matrix instead of separate model and view matrices in the pipeline?

Edited by infact, 01 June 2012 - 02:50 AM.

### #6Yours3!f  Members

Posted 31 May 2012 - 11:56 AM

as far as I know lighting is space independent. It should be the same for world, view, and tangent space.
The implementation might compute lighting in any of these spaces, but it must provide you with the variables and functions that you see in the specs, and it has to act accordingly.

### #7Brother Bob  Moderators

Posted 31 May 2012 - 12:20 PM

My point is that lighting in world space will be different to lighting in view space.

distance(vertex*view - light) != distance(vertex - light*view^-1)

this is clear if we let the light origin be 0,0,0, and consider a matrix with no translation, then we have that:

length(vertex*view) != distance(vertex)

which is true

In other words, the transformation distorts the lighting

So I wonder why opengl does lighting in view space and not in world space.

Is it for the the sake of having a single modelview matrix instead of separate model and view matrices in the pipeline?

If view is a zero-translation matrix without scaling, then distance(vertex*view) == distance(vertex) and so lighting is the same in both spaces. If it incorporates scaling, then the distances will indeed be different, but it is only different from a conceptual quantity that doesn't exist in OpenGL. You can easily move the scaling from the view matrix into the model matrix instead to ensure that the view matrix is rotation only (assuming the given non-zero translation). You are basically comparing a result agains an intermetiate value that has no meaning in OpenGL.

### #8V-man  Members

Posted 31 May 2012 - 08:41 PM

My point is that lighting in world space will be different to lighting in view space.

distance(vertex*view - light) != distance(vertex - light*view^-1)

this is clear if we let the light origin be 0,0,0, and consider a matrix with no translation, then we have that:

length(vertex*view) != distance(vertex)

which is true

In other words, the transformation distorts the lighting

So I wonder why opengl does lighting in view space and not in world space.

Is it for the the sake of having a single modelview matrix instead of separate model and view matrices in the pipeline?

Ok, I see. I did a test and yes, there is a difference in distance depending on which space you are working in when there is scale applied.
It seems to be a problem for point lights and spot lights and when you need to use attenuation.

However, I don't see this as a problem for fixed function GL because fixed function GL doesn't do its lighting computation in object space.

But for someone who is using shaders, and let's say he wants to do bump mapping on certain objects and phong lighting on something else, the attenuation will appear different and he might wonder why. The bump mapped object would appear brighter.

Is the above correct? Can someone verify it?

Edited by V-man, 31 May 2012 - 08:43 PM.

Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Posted 06 June 2012 - 12:00 PM

yes lighting is not distorted. If you compute light intensity at several distances from light source and you place vertices at that distances from light source the intensity will be correct.

the only thing that is distorted is fog, and that's because fog is usually computed using Z-buffer values instead of distances of vertices from camera. NVIDIA made an extension for that (eye radial fog) but that's OT.

Maybe you should know that geometries are distorted by projection. Human eyes is a semisphere internally, while computer monitor is a flat surface (does anyone ever experimented stretched cubes near sides of frustum view? In the reality a cube will not be stretched even if at limits of vision range) so geometry projection is already a distortion from what you will see if you were for really in the game. But there is nothing you can do for that ^^.

That's why stereo-monitors aren't so good and some people complain about headhackes looking at 3D movies for more than several minutes.. There was also a study at Berkley sponsored by Samsung.

For shaders that's the same. If you want correct lighting you need to compute distance from camera in vertex shader and then feed fragment shader with the interpolated varying. you can't use Zbuffer value in shaders for that.

Most shaders based on Zbuffer values infact are doing some space correction (for example SSAO)

Edited by DemonRad, 06 June 2012 - 12:02 PM.

Peace and love, now I understand really what it means! Guardian Angels exist! Thanks!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.