openglskyboxzbufferdepth-testing

OpenGL optimizing skybox rendering


I'm learning how to draw skybox using cubemaps from the following resource.

I've got to the part where he talks about how we can optimize the rendering of the skybox. I get that instead of rendering the skybox first, which will result in width*height of your viewport fragments to be calculated and then only to be overdrawn by other objects, it's best to draw it last and fake its depth value to be 1.0f by assigning the gl_Position of the skybox vertex shader to gl_Position = pos.xyww essentially making each gl_FragCoord.z equals to 1.0f due to perspective division.

Now after we get a skybox with each of its fragments have maximum depth value of 1.0f he changes the depth function to GL_LEQUAL instead of GL_LESS.

Here's where I got a little bit confused.
If we render the skybox last and have its depth values equal to 1.0f why do we need to change the depth function to GL_LEQUAL? Wouldn't it be sufficient to have it set to GL_LESS because if we render every other object in the scene it depth value will probably be less than 1.0f so it'll write its value to the z-buffer a value less than 1.0f. Now if we set the depth function for the skybox to GL_LESS it will then only pass the fragments with depth value of less than what is actually in the z-buffer which will probably only pass fragments that other objects are not covering, so why do we need the GL_LEQUAL?


Solution

  • When you initially cleared the framebuffer at the start of the frame, you probably did so to a value of 1.0f. So if you want to draw the skybox at all, you'll need to allow the skybox to draw in areas with a cleared depth value.