PDA

View Full Version : Triangle selection in perspective projection

devdept
11-11-2009, 03:09 AM
Hi All,

Here is today quiz.

Suppose you have a triangle in perspective projection and that you want to select it in two ways:

1) clicking inside of it
2) crossing one of its sides with a selection box

When you look to it far enough, you can easily project vertices on screen and do PointInTriangle with the mouse coords as 2D problem, the same applies for box crossing selection: project everything on screen and check for intersections between triangles sides and box sides.

Troubles arise when you zoom *very* close to one vertex the other two are probably behind the viewer point and the projection returns wild coordinates.

Two questions:
1) Why we are still able to see the triangle correctly on screen? I belived that if you get wild coordinates from gluProject even the display should be wild.
2) How can we skip the triangle in this case? Checking if the screen Z (depth) is Z < 0 or Z > 1 ?

Thanks,

Alberto

ZbuffeR
11-11-2009, 04:40 AM
1) projected geometry is clipped against the near plane, so no risk of "behind the viewer" problems
2) yes you may skip the triangle if projected depth is z<0, or do proper clipping like in 1).

devdept
11-11-2009, 05:22 AM
Hi ZbuffeR,

Therefore we could - for each triangle vertex - check if the projected depth is < 0, if true:

1) get the triangle side world coords
2) intersect with near plane to get a 3D point on the near plane
3) gluPorject this new point
4) go back to the point in triangle algorithm using this point

Right?

Thanks,

Alberto

devdept
11-11-2009, 06:09 AM
ZbuffeR,

We also get points with Z > 1, do you know why?

Thanks,

Alberto

ZbuffeR
11-11-2009, 06:40 AM
Sound good.
But don't forget When triangle is cut by a plane, it will result in quad if 1 vertex has depth < 0, or a triangle if 2 vertices are in this case.

EDIT: Same thing for depth > 1, but with far plane clip. And for top, left, right, bottom. All this is nicely detailed in the GL spec by the way.

devdept
11-11-2009, 08:25 AM
Wow, checking also top, left, right and bottom will become cumbersome...

Is there any more straigthforward approach?

Thanks,

Alberto

11-11-2009, 08:44 AM
There was something good in GLU: picking. The caveat is that GLU is officialy dead but you can implement something similar: Rasterize the scene to a tiny viewport in the back buffer big enough to encompass your picking region with a different RGB value for each object, I mean encoding a 32 bit identifier in RGB. Then you can glGetPixels to get the color and depth buffer to traverse the rasterized pixels and depth and know wich primitives where drawn, and which one is closer.

My 2 cts.

devdept
11-11-2009, 12:47 PM
Thanks DandyYuyo,

We know this approach but unfortunately does not allow selection of all the entities crossing the selection box, only the one closer to the viewer will be selected.

Alberto

devdept
11-12-2009, 12:38 AM
It looks so cumbersome to me that probably it is better to do everything in 3D. The 2D projection simplification is stright forward if you don't incur in wild coordinates, but when they arise it's probably better to go 3D. what do you think? What about intersecting a segment with a small furstum with the dimension of the selection box? :p

Thanks,

Alberto