View Full Version : Help with Face Selection

09-03-2011, 12:15 AM
I've been trying to implement face selecting in a Minecraft-esque project. I found an example of triangle selection here (bottom of page) (http://www.opengl.org/resources/code/samples/glut_examples/examples/examples.html), 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);


gluPickMatrix(x, windH - y, 4, 4, vp);
gluOrtho2D(-175, 175, -175, 175);

glClearColor(0.0, 0.0, 0.0, 0.0);

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



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 (http://jmj.ath.cx/Goblin/opengl/chunk.h)
chunk.cpp (http://jmj.ath.cx/Goblin/opengl/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...

09-09-2011, 05:53 AM
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).


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 (http://www.google.com/url?sa=t&amp;source=web&amp;cd=8&amp;sqi=2&amp;ved=0CGEQFjAH&amp;url=h ttp%3A%2F%2Fwww.cs.virginia.edu%2F~gfx%2FCourses%2 F2003%2FImageSynthesis%2Fscribed_notes%2F03_accele ration.pdf&amp;rct=j&amp;q=uniform%20grid%20acceleration%2 0structure&amp;ei=LP1pTtjAHMPYiALyg6HFDg&amp;usg=AFQjCNGHV DrDU5r3X3k4wO2sn8t76WiJIQ&amp;sig2=pr50RvR3n6wBSLbAPn8 Isw)