tags:

views:

228

answers:

4

Hey all,

I need to know how can I make the skybox appears as it's in the infinity?? I know that it's something related to depth, but I don't know the exact thing to disable or to enable?? Any one can help??

+2  A: 

First, turn off depth writes/testing (you don't need to bother with turning off depth testing if you draw the skybox first and clear your depth buffer):

glDisable(GL_DEPTH_TEST);
glDepthMask(false);

Then, move the camera to the origin and rotate it the inverse of the modelview matrix:

// assume we're working with the modelview
glPushMatrix();

// inverseModelView is a 4x4 matrix with no translation and a transposed 
// upper 3x3 portion from the regular modelview
glLoadMatrix(&inverseModelView);

Now, draw your sky box and turn depth writes back on:

DrawSkybox();

glPopMatrix();
glDepthMask(true);
glEnable(GL_DEPTH_TEST);

You'll probably want to use glPush/PopAttrib() to ensure your other states get correctly set after you draw the skybox too (make sure to turn off things like lighting or blending if necessary).

You should do this before drawing anything so all color buffer writes happen on top of your sky box.

Ron Warholic
skybox as first thing rendered is the best way to waste a bunch of bandwidth.
Bahbar
True however I think that the OP isn't concerned about performance quite yet. Drawing the skybox last is more complicated as you have to make sure you've handled transparent and far objects correctly as to not clip into the skybox. Optimizing for the early-Z test should be done after you have a working implementation.
Ron Warholic
A: 

There is no infinity. A skybox is just a textured box, with normaly 0,0,0 in the middle. Here is a short tut: link text

InsertNickHere
A: 

The best approach I can think of is to draw it on a first pass(or layer), then clear only the depth buffer. After that just draw the rest of the scene in another pass. This way the skybox will always remain "behind" the scene. Just remember to use the same camera for both passes and somehow snap the skybox to the camera.

Sanctus2099
+1  A: 

First, Clear the buffer.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Then, save your current modelview matrix and load the identity.

glPushMatrix();
glLoadIdentity();

Then render your skybox.

Skybox.render();

Then, clear the depth buffer and continue normally with rendering

glClear(GL_DEPTH_BUFFER_BIT);

OtherStuff.render();

glutSwapBuffers();
Ned