4
\$\begingroup\$

I'm making a minecraft-clone game in C++ using OpenGL.

I created a skybox using OpenGL's cube map but the camera can go outside of the skybox since it's an infinite world. Then I changed it to update the skybox's coordinate based on camera's coordinate like below but it didn't change anything.

How can I make a skybox stay at a relative position from the camera?

auto coord = camera.getCoords();
// positive x
skyboxMesh->addFace(
              coord.x + size, coord.y + size, coord.z - size,
              coord.x + size, coord.y + size, coord.z + size,
              coord.x + size, coord.y - size, coord.z - size,
              coord.x + size, coord.y - size, coord.z + size
              );
\$\endgroup\$
2

1 Answer 1

2
\$\begingroup\$

The traditional way to do that is to create a modelView matrix that contains only rotation information. It should contain the same rotations as the camera, but not the translation, so that it's always at the origin. Use that with the same projection matrix as you use for the rest of the scene. Draw your skybox centered at the origin. After you draw your skybox, clear the depth buffer if you're using one, and then draw the rest of your scene as you normally would.

What this does is draws the skybox and then treats it like it's infinitely far away so that all other things draw over it.

As @DMGregory points out, you can reduce the amount of overdraw by doing this:

A slight improvement is to draw all your opaque geometry first, then draw the skybox, setting its depth to the far plane in the vertex shader. This gives you a little less overdraw, since the skybox gets occluded anywhere ground was already written.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you so much user1118321 and HolyBlackCat! You guys saved my day. I just removed camera's translation from skybox's MVP matrix and it worked. \$\endgroup\$ Aug 18, 2018 at 23:03
  • \$\begingroup\$ glDepthRange(1,1) is my generally preferred approach to this. \$\endgroup\$ Aug 19, 2018 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .