View Full Version : Picking in 2D
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?
12-06-2004, 10:05 PM
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.
Here's my source code:
I'm still modifying the Selection() function.
12-06-2004, 10: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 :)
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?
12-07-2004, 05: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.
12-07-2004, 06: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:
glOrtho(0, vPort, 0, vPort, -1, 1);
glTranslatef(0.375, 0.375, 0.);
}Sample of my drawing code is
glDisable2D();Is there anything wrong with my pickmatrix?
gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport-mouse_y), 1.0f, 1.0f, viewport);
gluPerspective(45.0f, (GLfloat) (viewport-viewport)/(GLfloat) (viewport-viewport), 0.1f, 100.0f);
12-08-2004, 09:26 AM
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.