PDA

View Full Version : Picking in 2D



12-06-2004, 10:43 PM
I'm having trouble picking 2D surfaces. I've used glOrtho to draw 2D.

Now I need help on how to determine if I clicked on a certain surface. e.g. a button?

mikael_aronsson
12-06-2004, 11:05 PM
Hi !

What is the problem ? there is no difference between 2D & 3D picking, just remember that you can get multiple hits for one point and in that case you might want to give your geometry different depth values in that case to make it easier to figure out what you clicked on.

Mikael

12-06-2004, 11:18 PM
Here's my source code:

http://www.geocities.com/christenlanger/source.zip

I'm still modifying the Selection() function.

Silkut
12-06-2004, 11:42 PM
Hello, what library are you using on your program ?
You can check This NeHe Lesson (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=32) which teach picking.

You can check the code to have an idea of the procedure.

[C++, OpenGL and Win32 are used in this lesson (all NeHe's lesson)]

hope that helps :)

12-07-2004, 03:05 AM
I found out a few bugs in my source but here's the catch.

I need to know how I will select the objects when they are at the same depth. Can I do it like that or, do I need another method?

Aeluned
12-07-2004, 06:30 AM
You can do it using normal OpenGL selection.

After you render the scene in selection mode your selection buffer will contain the total number of hits as well as a list of names.

the names should be associated with each primitive and you can select any one of the primitives whose name is in the selection buffer. Since you can't select via depth in the case of primitives at the same depth you can rely on their names.

you can either return the first name on the stack or you can iterate through 'hits' names searching for the particular primitive you want to select.

the names will be on the name stack in the order that the primitives were rendered.

you can use a key combination to flag selection of the next primitive in the list.

for example if the user is pressing ALT as they left click the selection code can iterate through the names list until it finds the currently selected primitive and then return the next one in the list, this way the user has the ability to cycle through the primitives.

Christen
12-07-2004, 07:29 AM
ok, let's say this is the situation...

I've set up the objects in 2D (same depth) and they are not overlapping in anyway.

Whenever I click, I always get hits equivalent to the number of rendered objects. Is this normal? It also happens when I click outside the objects.

Here are parts of my code

Functions I used to set up objects in 2D:

void glEnable2D(GLvoid)
{
int vPort[4];

glGetIntegerv(GL_VIEWPORT, vPort);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.375, 0.375, 0.);

glPushAttrib(GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
}

void glDisable2D(GLvoid)
{
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}Sample of my drawing code is

glEnable2D();
//draw code
glDisable2D();Is there anything wrong with my pickmatrix?

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);

mikael_aronsson
12-08-2004, 10:26 AM
Hi !

No, the gluPickMatrix call look's ok, even though 1.0f might be a little to small as a hit area, you have to hit the exact pixel, but that is up to you.

If you want to be picky you should use (height-1)-mouse_y_pos, but that should not have much impact on your code.

Are you putting the call to gluPickMatrix in the correct place ? (projection matrix, before the normal projection setup)

And make sure you call glInitNames() before you start to render the objects and then make the correct calls to setup the name stack when you render the objects.

Mikael