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…