PDA

View Full Version : Object Selection



Blacknight
10-24-2010, 08:30 PM
I am having an issue with selecting objects.

The following code only seems to work at certain camera angles. And is not very accurate if it does work.

Is there something that needs to be done differently if the camera is rotated?

Function that draws all the triangles:


void drawTerrain() {
glRotatef(ourPlayer.cameraX, 1.0f, 0.0f, 0.0f);
glRotatef((360.0f - ourPlayer.cameraY), 0.0f, 1.0f, 0.0f);

glTranslatef(-ourPlayer.x, -ourPlayer.y, -ourPlayer.z);
glBindTexture(GL_TEXTURE_2D, texture[0]);

for(int triangle = 0; triangle < currentMap.triangleCount; triangle++) {
glLoadName(triangle);
glBegin(GL_TRIANGLES);
glNormal3f(0.0f, 0.0f, 1.0f);
for(int vertex = 0; vertex < 3; vertex++) {
glTexCoord2f
(
currentMap.triangle[triangle].vertex[vertex].u,
currentMap.triangle[triangle].vertex[vertex].v
);

glVertex3f
(
currentMap.triangle[triangle].vertex[vertex].x,
currentMap.triangle[triangle].vertex[vertex].y,
currentMap.triangle[triangle].vertex[vertex].z
);
}
glEnd();
}
}


Function that is called when the mouse is clicked: (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=32)


void selectObject(int x, int y) {
GLuint buffer[512];
GLint viewPort[4];
GLint hits;

glGetIntegerv(GL_VIEWPORT, viewPort);

glSelectBuffer(512, buffer);

glRenderMode(GL_SELECT);

glInitNames();
glPushName(0);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble) x,
(GLdouble) (viewPort[3] - y),
1.0f,
1.0f,
viewPort);
gluPerspective(45.0f, (double) viewPort[2] / (double) viewPort[3], 0.1f, 200.0f);

glMatrixMode(GL_MODELVIEW);
drawTerrain();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glMatrixMode(GL_MODELVIEW);

hits = glRenderMode(GL_RENDER);

cout << "hits: " << hits << endl;

if(hits > 0) {
int choose = buffer[3];
int depth = buffer[1];

for (int loop = 1; loop < hits; loop++) {
if (buffer[loop*4+1] < GLuint(depth)) {
choose = buffer[loop * 4 + 3];
depth = buffer[loop * 4 + 1];
}
}
}
}

david_f_knight
10-25-2010, 09:01 PM
There's not a single comment in your code, nor have you given much of an explanation for what you're trying to do or why. If all you want to do is select objects, it is much easier to do color picking. Here's a previous (of many) discussion:

http://www.opengl.org/discussion_boards/...4748#Post284748 (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Main=55041&amp;Number=2847 48#Post284748)