Get the Polygon Mouse Position

Hi Friends,

Using the gluUnProject, i’m get the object(Ex:A Simple Polygon) position while mouse click. Its perfect

My Problem is if any other object like sphere or cyliner or even a line is infront of the Polygon object means it returns the position of object which is infront of the quad object. But I need whatever object infront of the Polygon, it should return the position of the Polygon…Is it Possible…?

Actually gluUnproject just performs the matrix operation described here. It does not have any knowledge of what is currently drawn and just perform a matrix operation using the window coordinates (winX, winY, winZ) that you compute and specify yourself.

Now, I don’t know how you compute the window coordinates. I assume that WinX and WinY are computed from the mouse position and WinZ, reading depth value from the depth buffer.

In this case, if you want to ignore an object in the object picking code you may perform an additional depth pass removing this object from the rendering batch.

can u please explain me how to perform additional depth pass to removing a object from the rendering batch?


You do the same thing as the first pass but disable color writing (glColorMask) as it is not needed because you just want to sample the depth buffer then. Clear depth buffer, enable depth writing and don’t draw the objects you don’t want to pick.

Thnxs for ur reply…

I used the glColorMask before the glReadPixels and set false all the RGBA , but the result is same it detect sphere which is infront of the polygon…can u please the explain the step sequence…


When you disable color writing while you draw this sphere, you can’t prevent depth writing for this particular sphere. So either you know which objects don’t have to be detected and simply don’t write them in the depth pass. Or you have to select a particular depth layer (the question I can’t answer is which one?). Can you explain exactly what you are intending to do, for what purpose?

There is something that might interest you that is called depth peeling. Google it and read about it. This algorithm allows you to draw a particular depth layer but is not performance-wise.

i am trying to develop drawing software based on the plane. For example, if i draw a circle on the xy plane means, i create a dummy polygon on xy plane(to get the exact position of the mouse, otherwise it return the near/far value of the frustum. i dont know its correct way for get mouse position of exact xy plane…but i tried this way) and draw the circle…its work.

But the problem is …i drawn one sphere on xy plane and try to draw a line on xy from sphere center position to some point(Again i create the dummy polygon on xy plane to get the xy mouse poistion). I cant draw line from the sphere center postion, instead sphere center position it return the surface the spere which is not the exact xy plane…

I am sorry but I am still not sure to understand exactly your problem. Perhaps some screenshots would help.

If you are trying to find the position of a point below the mouse on one of (xy, xz, yz) 3d planes, why don’t you simply draw first your dummy polygon only then compute the 3d point position and finally draw the entire scene. I still don’t understand why you have to draw this sphere above the plane.


These are the screen shots.

I already draw a Cylinder on the center (0, 0, 0).Now i trying to draw a line on the selected plane(yellow is the selected plane, i draw a dummy plane on the selected plane to get the mouse position). X1 is the some point on the selected plane and x1 is the center (0, 0, 0) which also the cylinder center.

Now the line is drawn. X1 is on the selected plane… but the X2 is not on the selected plane…it’s on the surface of the Cylinder


What Dletozeun means is that you don’t need to draw anything other than your selection plane(s) in your selection pass. Then you can’t have the problem that some other object’s name is being returned because it ended up being closer to the camera than the selection plane itself (which seems to be the case judging by the screenshots you posted).

If you’re using a GL_SELECT-based picking method then it’s somewhat traditional to make a render function with 2 modes (one being render and one being select) which you can then call in select-mode when your app receives a mouseclick event. Nehe has a tutorial (I think) which demonstrates this technique.

You could look into the maths involved in unprojection of your mouse coordinates and then doing a ray/plane intersect or something; GL_SELECT can be a bit slow at times but for a drawing application it could do just fine.