efficient drawing of a grid

I am drawing a grid that has holes in it, ie some boxes I do not want to draw.

I am currently doing this in a very low-tech way, determining by if statement whether or not to draw the box, and drawing a sequence of connected boxes, like:

do {
glColor …
glColor …
} while (box should be drawn)

I also draw grid lines on top.
I’d like to use a single grid call, particularly because I’m calling this from Java and all the calls in and out of the engine are comparatively slow. Just reducing to a single draw call would drive performance, but this would require getting OpenGL to not draw the boxes that aren’t there.


  1. Is there a value I can put in for color that will make the box disappear, or is there a way to specify a boolean mask for quads I do not want to draw?

  2. Will this technique work the same way using VBO? I would like to put the (constant) x,y grid up on the video card and only transmit the z value and the colors.

  3. I am currently drawing the edges in a similarly ugly manner:

if (box exists) {
glVertex(lower-right-corner) // draw bottom edge
glVertex(upper-left-corner) // draw left edge

This isn’t even fully correct, as it leaves off edges. I’d rather not draw all four sides however, as that would be redundant for all the boxes that are adjacent to other boxes. Ideally, there is a single meshing call that can be used to draw the grid lines but still skip the ones I want to skip?


glEnable( GL_ALPHA_TEST );
glAlphaFunc( GL_GREATER, 0.0 );

draw boxes

set coloralpha to 0.0 glColor4f(x,x,x,Alpha) for the ones u dont wanna see + 1 otherwise

this could be all drawn with a single draw call

I would suggest using one static vertex array (VBO) and put all indices in index array (actually this would be just an array in system memory).
Now create second index array - every time you want to draw something, copy it’s indices from original index array to this one.
And then render everything with just one call to glDrawElements.
I use this approach for terrain rendering + occlusion culling - copying indices works faster than calling glDrawElements separately for every fragment of terrain.

By the way - do not crosspost

thanks for the good answers

(and sorry for the cross post) I was not getting answers so I thought I should switch to here, and then by the time I had deleted, someone did answer there).

I know I can do a vectorized call for a single strip of quads, but is there a call to do a grid, since at the end of each strip the quad cannot be connected to the next row? Or is the trick to make an invisible connection?

All things being equal, I presume it would be better not to draw things invisibly than to draw them, but compared to doing lots of separate calls, obviously it’s a huge win.

Also, I am not familiar with VBO, can you just give me a sequence of the commands to look up? Presumably I have to bind a variable, then use it. I believe I have written code to check in the past, and have had hardware or an OpenGL driver that did not support VBO. So another question that arises is how best to support performance both with and without features.

I’m using VBO in my game and it runs even on TNT2 (which has no HW T&L). I believe it’s also supported by old ATI GPU’s (worked on Radeon 9550, but I haven’t tried older cards).

i wouldnt worry about VBO just yet

also though its possible to draw the whole lot in one call to QUAD_STRIP/TRI_STRIP (with degenerants)
the performnce might suffer, in fact u will see most games eg doom3 will not use strips but just GL_TRIANGLES