title: Issues with infinite grid in OpenGL 4.5 with GLSL
body: <p>I've been toying around with an infinite grid using shaders in OpenGL 4.5, following this tutorial <a href="https://asliceofrendering.com/scene%20helper/2020/01/05/InfiniteGrid/" rel="nofollow noreferrer">here</a>. Since the tutorial was written for Vulkan and a higher version of GLSL (I'm using 450 core), I had to move the vertices out of the vertex shader and into the application code. I'm rendering the quad using an element buffer, so my vertices ended up looking like this:</p>
  <pre><code>std::vector&lt;glm::vec3&gt; points{glm::vec3{-1, -1, 0},
                                glm::vec3{1, -1, 0},
                                glm::vec3{1, 1, 0},
                                glm::vec3{-1, 1, 0}};
  
  std::vector&lt;GLuint&gt; indices{0, 1, 2, 2, 3, 0};
  
  // Render like this:
  glBindVertexArray(m_vao);
  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  
  </code></pre>
  <p>I've managed to get the grid fully working, but it has two issues that I can't figure out:</p>
  <ol>
  <li>Objects that are drawn on top of the grid are getting clipped as the horizon moves up. This feels like z-fighting, but I'm not 100% sure why this would happen, seeing how the cube is being rendered after the grid.</li>
  <li>The underside of the grid is completely solid. This one also confuses me, because based on the fragment shader, the spaces in between the grid lines should be fully transparent... and yet they appear to be completely solid.</li>
  </ol>
  <p>Here are some sample images of what is happening:
  <a href="https://i.stack.imgur.com/TFar0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TFar0.png" alt="enter image description here" /></a> 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.</p>
  <p><a href="https://i.stack.imgur.com/kXaQ8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kXaQ8.png" alt="enter image description here" /></a> 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.</p>
  <p>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:</p>
  <pre><code>glEnable(GL_MULTISAMPLE);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  </code></pre>