Help with Face Selection

I’ve been trying to implement face selecting in a Minecraft-esque project. I found an example of triangle selection here (bottom of page), but I’m having a hard time understanding it. Here is the selection code from the example above:

static GLint DoSelect(GLint x, GLint y)
{
  GLint hits;

  glSelectBuffer(MAXSELECT, selectBuf);
  glRenderMode(GL_SELECT);
  glInitNames();
  glPushName(~0);

  glPushMatrix();

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPickMatrix(x, windH - y, 4, 4, vp);
  gluOrtho2D(-175, 175, -175, 175);
  glMatrixMode(GL_MODELVIEW);

  glClearColor(0.0, 0.0, 0.0, 0.0);
  glClear(GL_COLOR_BUFFER_BIT);

  glScalef(zoom, zoom, zoom);
  glRotatef(zRotation, 0, 0, 1);

  Render(GL_SELECT);

  glPopMatrix();

  hits = glRenderMode(GL_RENDER);
  if (hits <= 0) {
    return -1;
  }
  return selectBuf[(hits - 1) * 4 + 3];
}

The code looks relatively simple, and I’ve been looking up all the commands in the OpenGL documentation, but I’m still not 100% sure how this works. Things like the variable “vp” aren’t commented, in fact there aren’t any comments in the whole source (except for a license). Can anyone shine some light on this? Or perhaps put in some comments in the source?

On a side note, I’m not even quite sure if this would be the best implementation for this project. All my blocks are just a three-dimensional vector of unsigned ints. And to get the “faces” I check all the blocks around each empty block (block==0), then draw each face accordingly. Each face is put into a member vector, then I draw all blocks with one call to a global member function (Chunk::draw();). Does anyone know of a different way of selecting the faces, other than going through all the quads in the draw buffer?

I tried using some trig to find the block the mouse is looking at, but it isn’t as precise as I would like. I’ve been trying to perfect this, but I have been unsuccessful. Here is my chunk source if anyone is interested:
chunk.h
chunk.cpp

Someone may notice a bunch of bools in the ChunkFace class, this is because I thought a bool was smaller than an int…which turned out to not be the case and I haven’t gotten around to fixing that part as of yet…

The code doesn’t really need commenting as it’s mostly just OpenGL functions, which are already documented for you. “vp” is typically the viewport dimensions (x,y,width,height).

http://www.opengl.org/sdk/docs/man/xhtml/gluPickMatrix.xml

If you look at this function, for example, you’ll see it’s expecting a viewport. It says it’s actually the same array you get by querying OpenGL for it’s viewport using glGetIntegerv.

I’d imagine for this project you’ll want to do your picking on the CPU. A uniform grid comes to mind since that’s basically what you have already. Except instead of hitting a cell and intersecting against its contents, you intersect against the cell itself (if there’s a block there).

Ray Tracing Techniques