picking

Started by
4 comments, last by Bob III 17 years, 10 months ago
am using the NeHe tutorial with some modifactions, with it i got picking too work, but then i need to be able to place objects after i picked them up, so for that i needed to use glOrtho(0,640,480,0,-1,1); so that i could use the mouse coords to place the object. So now i have decided that i may as well just draw everything with glOrtho(0,640,480,0,-1,1); but now picking does not work correctly, no matter where i click on the screen it detects a collision with my object, here is my selection code: void Selection(void) { GLuint buffer[512]; GLint hits; GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(512, buffer); (void) glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y), 1.0f, 1.0f, viewport); gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); DrawTargets(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); hits=glRenderMode(GL_RENDER); if (hits > 0) { int choose = buffer[3]; x+=0.02f; pickedObject = choose; if (!object[choose].hit) { object[choose].x += 0.5; } } }
Advertisement
Why are you using a perspective transform for selection when you're drawing everything in ortho?

I'm tired and can't rule out that there's a reason (why am I checking the forum then? ehh)
"That''s to do with the day I finally realized the world had gone totally mad and built the Asylum to put it in, poor thing, and hoped it would get better."-Wonko the SaneSo Long, and Thanks for All the Fish
Yes, i know what i have there is not correct, that is just how i was doing picking before, I am learning openGL as i go so i sterted by drawing everything in a way it would work with this method of picking. I am just not sure exactly how to change the code to work with ortho, I have tried a few things that i thought would work, but i still have the same problem of always regestering a hit.
To correct this problem you would just replace gluPerspective with glOrtho, I'm pretty sure.

Is the modelview matrix in the right state when you DrawTargets?

If you arn't, check glGetError() - maybe you missed a matrix op somewhere.
"That''s to do with the day I finally realized the world had gone totally mad and built the Asylum to put it in, poor thing, and hoped it would get better."-Wonko the SaneSo Long, and Thanks for All the Fish
hmm, well i had already tried just replacing gluPerspective with glOrtho, but i still get the same problem. Here is my code for DrawTargets:

void DrawTargets(void)
{
glLoadIdentity();
for (int loop=0; loop<1; loop++)
{
glLoadName(loop);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,640,480,0,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(object[loop].x,object[loop].y,object[loop].distance);
Object(30,30,0);
glPopMatrix();
}
}

I googled the problem, and it looks liek there are a few other people out there with the same issue, where it always registers a hit, but i have not yet found a good solution short of completly re-coding my program. I suspect i am missing some small detail, but i have not been able to figure it out yet. Thanks for any help you can offer.
You overwrote the selection matrix you computed in the projection matrix setup by loading identity and making another ortho transform.

And you glPushMatrix two times more than you pop.
"That''s to do with the day I finally realized the world had gone totally mad and built the Asylum to put it in, poor thing, and hoped it would get better."-Wonko the SaneSo Long, and Thanks for All the Fish

This topic is closed to new replies.

Advertisement