4

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?

Jorayen
  • 1,737
  • 2
  • 21
  • 52
  • I think this is a bad tutorial to follow, using cube geometry and a VAO is an extreme overkill for a screen-space effect, see for example https://webglfundamentals.org/webgl/lessons/webgl-skybox.html. Also, drawing the skybox first instead of last allows you to skip the framebuffer's color and depth clear, which I would profile heavily before calling one of both options a performance improvement. – mOfl Nov 04 '19 at 17:57

1 Answers1

5

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Oh I see, so if it was `GL_LESS` than it wouldn't be enough to pass the test. So If I were to set the depth value of the skybox to `0.999f` it would be good to use `GL_LESS` I suppose? – Jorayen Nov 04 '19 at 14:50