Reflection TexGen with Cube Maps

I’ve been working on figuring out cube maps and I’ve managed to bind, enable, and display them. Now I’m trying to get the Reflection TexGen functionality working. In the extension specification it says:

An application note: When using cube mapping with dynamic cube
maps (meaning the cube map texture is re-rendered every frame),
by keeping the cube map’s orientation pointing at the eye position,
the texgen-computed reflection or normal vector texture coordinates
can be always properly oriented for the cube map. However if the
cube map is static (meaning that when view changes, the cube map
texture is not updated), the texture matrix must be used to rotate
the texgen-computed reflection or normal vector texture coordinates
to match the orientation of the cube map. The rotation can be
computed based on two vectors: 1) the direction vector from the cube
map center to the eye position (both in world coordinates), and 2)
the cube map orientation in world coordinates. The axis of rotation
is the cross product of these two vectors; the angle of rotation is
the arcsin of the dot product of these two vectors.

What I think this means is that to get the reflection to look correct with a static cube map, you need to rotate the texture matrix. My code to do this is:

void setupCubeTextureMatrix(float cmo1, float cmo2, float cmo3, float camp1, float camp2, float camp3, float cmor1, float cmor2, float cmor3)
   float v1 = camp1 - cmo1;
   float v2 = camp2 - cmo2;
   float v3 = camp3 - cmo3;
   float rotaxisx = v2 * cmor3 - cmor2 * v3;
   float rotaxisy = cmor1 * v3 - cmor3 * v1;
   float rotaxisz = v1 * cmor2 - cmor1 * v2;
   float angle = asin(v1 * cmor1 + v2 * cmor2 + v3 * cmor3);
   glRotatef(angle, rotaxisx, rotaxisy, rotaxisz);

The cmo variables are the x, y, and z coordinates of the cube map orgin, the camp variables are the x, y, and z coordinates of the camera position, and the cmor variables are the x, y, and z coordinates for the cube map orientation.

In my actual drawing code, I do this

setupCubeTextureMatrix(0, 0, 0, camerax, cameray, cameraz, 0, 0, 1);
gluSphere(sphere, 0.5f, 64, 64);

I have enabled normal generation for the sphere, so that is not the reason it isn’t working.

The problem is that this does not actually rotate the texture properly. It always looks like I am looking at the sphere from the same viewpoint, even when I view the sphere from different directions.

Does anybody have any idea what I am doing wrong, or have any example code for doing this?

Thanks in advance.



I found some information on how to do it properly. It involves taking the inverse of the rotation part of the modelview matrix and loading that into the texture matrix.