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?

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.


Here’s my source code:


I’m still modifying the Selection() function.

Hello, what library are you using on your program ?
You can check This NeHe Lesson 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 :slight_smile:

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?

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.

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


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


void glDisable2D(GLvoid)

Sample of my drawing code is

//draw code

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

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.