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
\$\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\$
  • \$\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\$ – shun-sato93 Aug 18 '18 at 23:03
  • \$\begingroup\$ glDepthRange(1,1) is my generally preferred approach to this. \$\endgroup\$ – Maximus Minimus Aug 19 '18 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.