Home > Back-end >  Issues with infinite grid in OpenGL 4.5 with GLSL
Issues with infinite grid in OpenGL 4.5 with GLSL

Time:06-29

I've been toying around with an infinite grid using shaders in OpenGL 4.5, following this tutorial enter image description here See that the cube is sliced in half. If I move the camera down, the rest of the cube will slowly appear. Conversely, if I move the camera up, the cube eventually disappears completely.

enter image description here This is the underside of the grid. I should be able to see the cube through the grid, but the grid behaves like a solid object.

Does anyone have any ideas of what I'm doing wrong? Should I be enabling something in the application? For reference, I currently have this enabled:

glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

CodePudding user response:

Blending only works when the Depth Test is disabled or the objects are drawn from back to front. When the depth test is enabled (with its default function GL_LESS) closer objects win against more distant objects. Even if a fragment's alpha channel is 0, the fragment affects the depth buffer and the depth test. Thus, a more distant fragment is discarded if a closer, transparent fragment was previously drawn.
You only have one object with transparent fragments, the grid. To solve your problem, just draw the grid after the cube:

  • enable depth test
  • draw solid objects (cube)
  • enable blending
  • draw grid
  • Related