tags:

views:

39

answers:

1

Im using gluUnProject() to get the screen 2d coordinate in 3d world coordinate. I take 4 positions from each corner of the screen to get the area of visible objects.

How to check which points are inside that "rectangle" ?, i have no idea about the terms or anything. The image below shows what that "rectangle" looks like:

alt text

A: 

Are you trying to find which 3D point are visible by a camera? If so, you might find some interesting informations on this website: http://www.lighthouse3d.com/opengl/viewfrustum/.

In the following image, we can see the view frustum and your selection frustum (in red). Applying frustum visibility checks to your selection frustum should the same algorithm as the one used for frustum culling.

alt text

If you want a quick and non optimized solution:

GLdouble model_view[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model_view);

GLdouble projection[16];
glGetDoublev(GL_PROJECTION_MATRIX, projection);

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

for(unsigned i=0; i<points.size(); ++i){
  GLdouble winX, winY, winZ;
  gluProject(points[i].x, points[i].y, points[i].z, model_view, projection, viewport, &winX, &winY, &winZ);
  if(selectionMinX <= winX && winX <= selectionMaxX && selectionMinY <= winY && winY <= selectionMaxY && winZ>=0 && winZ<=1){
    /// point is selected
  }
}
tibur
nope, i just want to select the objects with mouse. Although i may use it for that purpose as well. But the point is i dont care if some polygon is partly visible, because i want to check only the point positions.
Newbie
But your question: find the points that are on my selection rectangle is the same as find all object inside my view frustum, with the view frustum being a *selection* frustum.
tibur
yeah, i cant get much out of that example since its using classes which i dont know anything about. isnt there a simple function anywhere that returns 1 if its inside and 0 if its not? i really dont care about the speeed or how high tech programming methods are being used. simpler = the better
Newbie
Ah, nice trick on the non-optimized solution, i had not thought of it that way! But i am thinking it might become too slow, ill try, thanks!
Newbie
How many points do you have ?
tibur
If it is too slow, you might take the code of `gluProject`, and put it inside your loop. Then you move everything that is not dependant of the points (there must be some matrix multiplication and inversion) outside of the loop. You should finish having only one matrix, point multiplication, a conversion from homogenous coordinates to 3D and that big `if` which test if point is inside. That will do the trick quicker.
tibur