title: How to draw dynamic 2D grid that adjusts according to camera zoom: OpenGL
body: <p>So my question is exactly like the title states. I'm not looking for a concrete code example, just some thoughts/guidelines on any preferred methods of doing this. </p>
  
  <p>In my 2D game engine, I like to have a grid that represents a certain amount of pixels (e.g 32x32 pixels). I specifically want to be able to define, the width and height of each grid in world space, have a consistent stroke width on all the lines, and be able to adjust the tile size according to camera zoom.</p>
  
  <p>I've tried several different methods in the past to draw this grid:</p>
  
  <ol>
  <li><p>The first thing I tried was using a custom shader to draw a grid. I found some code online that used some concepts I didn't understand to just draw the grid on a 2D plane. The problems with this approach are that I didn't know how to control the exact size in my game world, and didn't understand how it worked in general.</p></li>
  <li><p>My second approach was to use my debug drawing facilities. I coded a suite of functions that allows me to add lines of different stroke widths in world space, and I implemented that by just dynamically generating quads for each line every frame. I can then draw a bunch of lines which gives me a grid.</p></li>
  <li><p>My third approach was to draw a bunch of quads, and use a custom shader to give them an outline. Then I just draw a bunch of white quads with black outlines, giving the appearance of a grid.</p></li>
  </ol>
  
  <p>Does anybody have a better solution? In all my approaches I have never tried to adjust the grid dynamically according to the camera's zoom level either. So any thoughts on whether these approaches are the typical solutions, or if there is a better one would be much appreciated. Also, if you have any thoughts on how to extend these methods to 3D space that would also be nice, I can't seem to find a good way to get a uniform stroke width on lines drawn in world space.</p>
  
  <p>My engine is currently being coded using Opengl and C++ too, so I'm primarily concerned on how to implement this using modern opengl, not immediate mode.</p>
  
  <p>Here's an image of what I'm trying to achieve: 
  <a href="https://i.stack.imgur.com/s1czy.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s1czy.jpg" alt="grid example"></a></p>
  
  <p><strong>Edit:</strong>
  I am primarily concerned with rendering techniques. Do you use several quads? One plane and a complex shader? Or do you use a quad for each line? Or something else entirely?</p>
  
  <p>This is one of my attempts, for another reference of the look and feel I'm trying to achieve. I just can't help but think there has to be a better method then the ones I've tried.
  <a href="https://i.stack.imgur.com/8N42Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8N42Q.png" alt="enter image description here"></a></p>