开发者

OpenGL : How can I put the skybox in the infinity

I need to know how can I make the skybox appears as it's in the in开发者_运维技巧finity?? I know that it's something related to depth, but I don't know the exact thing to disable or to enable??


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.


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();


The only problem with drawing the sky box is first is that your pixel shader will execute for every pixel in the sky box. Just to be overwritten by other object in your world later on. Your best bet is to render all opaque object first then render your sky box. That way the pixel shader for the sky box only gets executed for the pixel who pass the z buffer test.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜