View Full Version : "Skybox"-style cube map?

12-11-2004, 09:33 PM
I get nice metal refractions using a cube map with glGenTexCoords/GL_REFLECTION_MAP, but how do you get a "sky" cube map? These are useful if you want to show a skybox, but only want it appearing in one little place in the ceiling, instead of being drawn behind the whole scene and causing little specks of white in the seams in the map.

12-14-2004, 11:06 AM
If you want to view an environment in a cubemap, then all you need is an identity mapping for the texture coordinates. For texgen, you could load the standard axes into the object linear planes.

... little specks of white in the seams in the map.Hmmm, sounds like you have a nasty case of t-junctions.

12-14-2004, 03:53 PM
...all you need is an identity mapping for the texture coordinates.How do I do this?

12-14-2004, 04:03 PM
What I mean is an identity mapping from the vertex position to the texture coordinate. That is, texture coordinate == vertex coordinate.

In code, it might go something like this:

float X[4] = { 1,0,0,0 };
float Y[4] = { 0,1,0,0 };
float Z[4] = { 0,0,1,0 };
glTexGenfv( GL_S, GL_OBJECT_PLANE, X );
glTexGenfv( GL_T, GL_OBJECT_PLANE, Y );
glTexGenfv( GL_R, GL_OBJECT_PLANE, Z );
glEnable( GL_TEXTURE_GEN_S );
glEnable( GL_TEXTURE_GEN_T );
glEnable( GL_TEXTURE_GEN_R ); Of course, this assumes you plan to render your cube at the origin, not translated off into the blue somewhere. Were that the case, though, you could shift the object planes accordingly.

02-26-2005, 05:08 PM
Have you actually tried this in real life? Or did you pull this out of your ass? Explanations with a lot of "mights" and "tries" sound kind of like BS.

02-26-2005, 06:29 PM
Easy there, Napoleon Dynamite, THAT's not proper etiquette! Graham is helping you out and he certainly does know what he's talking about.

02-27-2005, 08:35 PM
Have you actually tried this in real life? Or did you pull this out of your ass? Explanations with a lot of "mights" and "tries" sound kind of like BS.Am I to take it that you're having some difficulty, Halo? If so, I'd be more than happy to answer your questions.

02-27-2005, 09:03 PM
Haha, yeah you could say that.

Are you actually talking about cube mapping, or just rendering a cube? See my "Cube map reflections - texture matrix" thread for a more detailed explanation of what I am after, and a demo(!).

02-27-2005, 09:08 PM
I see. So you are after plain old-fashioned reflection mapping then? I was under the impression that you wanted to view a skybox.

02-27-2005, 09:17 PM
Well actually, I want to do both.

Blitz3D has two cube map modes. One makes it look like you are viewing a skybox through a "window". The other is a reflection-map distorted thing. I updated the demo, you can see what I mean:

02-27-2005, 09:28 PM
All you have here is 2 different texture coordinate generation modes. For viewing the skybox, you could use the object texgen given above. For reflecting the same skybox in world geometry, use the reflection texgen, along with the inverse (transpose) of the camera rotation on the texture matrix stack.

That is, if you want mirror-like reflections on the geometry. The distorted stuff you mentioned is probably a result of using sphere-mapping.

02-27-2005, 09:30 PM
No, wait, the cube map does not move based on camera rotation. Check the demo and see.

02-27-2005, 09:35 PM
What do you mean? For the fixed-function pipe it's perfectly acceptable and convenient to do it this way. For the programmable pipe, there's no need to dance around the bush like this.

02-27-2005, 09:37 PM
If you rotate the camera, in that demo I posted, the cube map does not move. The cube map only moves when you move the position of the camera.

02-27-2005, 09:40 PM
That's why you push the INVERSE of the camera rotation: It cancels the effect of any camera rotation. Try it, you'll like it :)

02-27-2005, 09:49 PM
Ohhhh...now we are getting somewhere.

I assume I have to use glMultMatrix here? Or are you saying to just rotate the texture matrix the opposite of the camera rotation?

I tried rotating the texture matrix the opposite of the camera rotation, and it looks like the cube map is sort of staying in front of me, but this isn't the same behavior as the demo I posted.

I'll make a movie of it right now.

02-27-2005, 10:00 PM
A simple way to do this is to grab the current modelview matrix with

glGetFloatv(GL_MODELVIEW_MATRIX, mat);

transpose mat, then load that onto the texture matrix stack with


Everything else is business as usual. Initialize your texgen stuff, and so on.

You might want to keep track of the modelview matrix yourself, but that's up to you.

Note that it's important to grab the modelview right after the camera transformations, since that's all we want.

02-27-2005, 10:19 PM
I will try what you said. Meanwhile, here is a movie showing the cube behavior if I just rotate the texture matrix by the opposite of the camera rotation:
http://www.leadwerks.com/post/cubemovie.zip (http://www.leadwerks.com/post/cubemovie.rar) (like 5 mb)

The effect this shows is not what I want. I showed how it rotates with the camera, and doesn't move when I move the camera. I don't know if that is the same thing you are describing.

Won't be replying any more tonight...need to sleep. Thanks for helping with this.

02-27-2005, 10:28 PM
Halo, what I have described here is nothing new. If you set this up right, it will work. I've used this technique to great effect, as have countless others. It's important to understand the principals at work here. Even in the programmable pipeline, there's no escaping the math. This is all just a bunch of math in disguise. The API is good at hidding that fact.

If you have trouble, feel free to ask.

02-28-2005, 07:55 AM
Is the effect you're describing what is seen in the video I posted? Can you please watch it and confirm one way or another?

This morning, I was able to get something similar to the demo I posted by translating the texture matrix the opposite of the camera position. The only caveat is I had to divide the camera position components by about 1024. (My texture is 128x128 each face, so I have no idea why it has to be divided by ~1100):
glTranslatef -x/1024.0,-y/1024.0,-z/1024.0

However, it still isn't behaving quite right.

02-28-2005, 10:36 AM
Here's a thread that came up ealier that might interest you:

Give the 2.0 spec a good looking over (page 51). The rub on texture coordinate generation is in there. Once you understand how it works, the rest is straightforward math. The trick is to take the reflections--which, by design and default are in eye-space--and move them into world-space, where your cubemap is.

If you're having trouble with the math, here's a free book on linear algebra--well worth the investment of time.