PDA

View Full Version : mouse picking problem



homer_simpson0
02-09-2009, 10:33 PM
Here's the code where i render stuff:

void renderscene()
{
globaltime = glutGet(GLUT_ELAPSED_TIME);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (mode == SELECT) {
startPicking();
}

glLoadIdentity();
fpsframe++;
if (globaltime - fpstimebase > 1000) {
fps = (fpsframe*1000.0)/(globaltime-fpstimebase);
fpstimebase = globaltime;
fpsframe = 0;
}

float time = (float)timeGetTime();


Vec3 p(Camera.x,Camera.y,Camera.z);
Vec3 l(Camera.x+(Camera.dir.vx),Camera.y+(Camera.dir.vy ),Camera.z+(Camera.dir.vz));
Vec3 u(Camera.dir.arbxx,Camera.dir.arbxy,Camera.dir.arb xz);

//glLoadIdentity();
gluLookAt(p.x,p.y,p.z,
l.x,l.y,l.z,
u.x,u.y,u.z);//
frustum.setCamDef(p,l,u);

glEnable(GL_TEXTURE_2D);



model[0].movemodel(time);
model[1].movemodel(time);
glPushMatrix();
glPushName(9);
glTranslatef(-3,0,-3);
//glRotatef(45,0,1,0);
glScalef(.1,.1,.1);

model[0].drawmodel();
glPopName();
glPopMatrix();



glPushMatrix();
glPushName(3);
//glTranslatef(x1,_terrain->getHeight((int)x1,(int)z1)+2,z1);
//glRotatef(45,0,1,0);
glScalef(.1,.1,.1);
model[1].drawmodel();
glPopName();
glPopMatrix();//*/

if (mode == SELECT)
stopPicking();
else
glutSwapBuffers();
}


if (mode == SELECT)
stopPicking();
else
glutSwapBuffers();
here's the code for object drawing


void CMd2Model::drawmodel()
{
glBindTexture(GL_TEXTURE_2D, texid);
glBegin(GL_TRIANGLES);

for(int i = 0, k = 0; i < md2Header.numTriangles; i++, k += 3)
{
// Draw the triangle.
glTexCoord2f(texCoords[TriangleList[i].texCoordIndex[0]].tu,
texCoords[TriangleList[i].texCoordIndex[0]].tv);
glNormal3f(normal[i].x, normal[i].y, normal[i].z);
glVertex3f(animatedMesh[k].x, animatedMesh[k].y,
animatedMesh[k].z);

glTexCoord2f(texCoords[TriangleList[i].texCoordIndex[1]].tu,
texCoords[TriangleList[i].texCoordIndex[1]].tv);
glNormal3f(normal[i].x, normal[i].y, normal[i].z);
glVertex3f(animatedMesh[k + 1].x, animatedMesh[k + 1].y,
animatedMesh[k + 1].z);

glTexCoord2f(texCoords[TriangleList[i].texCoordIndex[2]].tu,
texCoords[TriangleList[i].texCoordIndex[2]].tv);
glNormal3f(normal[i].x, normal[i].y, normal[i].z);
glVertex3f(animatedMesh[k + 2].x, animatedMesh[k + 2].y,
animatedMesh[k + 2].z);
}

glEnd();
}


the problem is that only the first object that i push trough the name stack gets hits when it's clicked on, the second object returns no hits. i tried this with drawing other models and it keeps doing the same thing.
any idea what might be causing this?

also this method of mouse picking seems very inefficient, any ideas on where i could find some tutorials on other methods of doing this?
thanx in advance

homer_simpson0
02-09-2009, 10:38 PM
never mind it was a stupid mistake on my behalf i was pushing name 9 before name 3...

i wouldn't mind finding other methods of doing this tho any help would be appreciated.

scratt
02-09-2009, 10:47 PM
If you use gluUnProject twice with it set once for the close z depth and again for the far z depth, then subtract the near result from the far you will get a projection ray into the screen.

You can then iterate through your geometry on the CPU side using that direction vector to do a ray / sphere intersection on objects.

Overall it's more efficient IMO than relying on OpenGL to do picking.