optimizing grid draw

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:

glBegin(GL_QUAD_STRIP);
do {
glColor …
glVertex(x,y,z)
glColor …
glVertex(x2,y2,z2);
} while (box should be drawn)
glEnd();

I also draw grid lines on top.
I’d like to use a single grid call, particularly because Java is comparatively slow, and I’ll bet performance would go way up, but this would require faking OpenGL into not drawing the boxes that aren’t there.

So:

  1. Is there a value I can put in for color that will make the box disappear?

  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-left-corner)
glVertex(lower-right-corner) // draw bottom edge
glVertex(loewr-left-corner)
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?

thanks!

You could probably write a shader to do exactly what you want to do, and simply pass along the relevant box data either in a uniform 4x4 matrix, or as a texture, telling you which boxes will not be needed. Since each fragment shader is called once per pixel, you could easily discard the ones you don’t need to draw. Then, you’d meet all of your criteria:

// psuedocode
loadShader(drawBoxShader);
activateShader(drawBoxShader);

//...more code

loadBoxData(boxDataTexture);
// above includes your glTexImage2D and glBind functions

glBegin();
{
  glMultiTexCoord(upperLeftCorner);
  glVertex(upperLeftCorner);

  glMultiTexCoord(lowerLeftCorner);
  glVertex(lowerLeftCorner);

  glMultiTexCoord(lowerRightCorner);
  glVertex(lowerRightCorner);

  glMultiTexCoord(upperRightCorner);
  glVertex(upperRightCorner);
}
glEnd();

And that would be your rendering code! Now, this was psuedo code, so the bulk of your drawing would take place (a) in the shader and (b) in the preparation function loadBoxData.

I’m not at all familiar with shaders yet, but thanks.

First, at the very least I would have to keep the existing code in case shaders are not supported on a client OpenGL, right?

Second, each box has four values: x, y, z, v. Wouldn’t it make more sense to send these as vectors into the card?