View Full Version : Cube Maps and the Projection Matrix

02-01-2002, 12:19 AM
I'm using a cubemap for rendering reflections in the scene, and have problems transforming the Texture matrix stack to get the reflection to render correctly.

I've gone through several demos (nvidia's bubble demo and Nutty's tron demo to name a few), and have checked through lots of posts on this board, and the only info I've come up with is 'the Texture matrix stack must be the inverse of the Modelview matrix stack'

Now, the question I would like to ask is, how does the Projection matrix stack come in then? If I'm using the Projection matrix to transform my 'camera' or eye-coordinates, does the Texture matrix have to be an inverse of Projection x Modelview?

I've tried this and it still doesn't look right.

Thanks in advance.

02-01-2002, 02:36 AM
You shouldn't use the projection matrix for moving the viewpoint. Doing so will screw up some things, and cause other problems. You are now having one of those problems.

02-01-2002, 04:02 AM
Darn. So I guess I have to move my camera/viewpoint transform over to the Modelview matrix.

On a similar topic, I read somewhere that it is very slow to glGetFloatv(GL_PROJECTION_MATRIX). Is it then recommended to inversely transform the Texture Matrix step-by-step rather than to read the Projection Matrix, invert it, and multiply the Texture Matrix with it?

02-01-2002, 04:58 AM

Fetching the modelview matrix is not really very expensive. Go ahead and do it.
Note that you really just want the texture matrix to be the inverse of the view portion of the modelview matrix. So you could do:

// each frame
camera_lookat(/* view params */);
object_lookat(/* SAME view params */);

Thanks -

[This message has been edited by cass (edited 02-01-2002).]

02-03-2002, 02:49 AM
Ok, I decided to go with reading the Modelview matrix, inverting it, and multiplying the Texture matrix with the inverted matrix. This works fine for Orthographic views, but I'm still having a problem with the Perspective view.

The problem is, my camera/perspective view implementation is quite complicated (but it's user-friendly =) ). I built it as a target-centric camera, meaning you don't manipulate the camera directly, you manipulate its orientation relative to its target.

Here's the implementation of the camera:
(x, y, z) as the coordinates of the camera target
'distance' as the distance of the camera from the target
'rotX' and 'rotY' as the rotation angles of the camera around the target.

gluPerspective( blah blah );
glTranslatef(0, 0, -distance);
glRotatef(rotX, -1, 0, 0);
glRotatef(rotY, 0, -1, 0);
glTranslatef(-x, -y, -z);

Now, if I invert the matrix generated from these calls, my cubemapped object becomes a solid colour (from one of the cubemap faces). I'm guessing it's from the glTranslatef calls, because if I generate an inverted matrix without taking into account the glTranslatef calls, I get a more visible (but still incorrect) reflection.

The cubemap tutorials out there have quite simple view transforms (for one thing, those I've gone through all have the view centered on the cubemapped object), so I can't find a reference there.

02-03-2002, 05:52 AM
Looks like you still need to separate your projection from your viewing transforms.

02-03-2002, 06:01 AM
Originally posted by cass:

Looks like you still need to separate your projection from your viewing transforms.

Real sorry cass, but I think I'm a bit confused here. As of now, my Projection Matrix is the identity, and the bottom of my Modelview matrix stack is the matrix formed from those 5 opengl commands up there. I know you mentioned earlier that the Texture matrix is the inversion of only the view transform and not the modelling transforms, but since those 5 lines are actually modifying my view, don't they classify as view transforms instead of modelling transforms?

Or do you mean something else entirely? Real sorry to trouble you, but I've been stuck with this problem for the past 3 days. =(

02-04-2002, 12:12 AM
Originally posted by cass:

Note that you really just want the texture matrix to be the inverse of the view portion of the modelview matrix

ahhhh! I got confused here. I thought you meant the view portion as opposed to the modelling transform, but you actually meant the view portion as opposed to the projection transform, right? I got it now, and it works great. Thanks a lot, Bob n Cass.