Most efficient method for on-screen check?

Started by
4 comments, last by SeiryuEnder 15 years, 4 months ago
Ok, I use openGL, and have several objects being rendered in a scene (several = up to around 100). The camera's position, including rotation, is regularly changing. I can check each object's screen position each frame by using gluProject(), but it's rather expensive on the framerate just to determine if the object is on the screen or not (to decide whether or not to render it). I know this is a VERY common issue with openGL programming, and so I'm guessing that it's been solved time and time again. Would someone please share the secret? I searched the forums, but couldn't find any solution other than using gluProject().
Advertisement
Sphere-Frustum intersections are usually implemented as a set of 5-6 sphere-plane tests, which are very straightforward. more info. For greater performance, consider the use of a spatial culling structure.
A faster method might be to check to see if the object's bounding box (or part of it) is in the view volume. Check out "occlusion culling" or "View frustum culling".
You are looking at the problem upside down.

Instead of checking if the an object is where you clicked by projecting every object onto the screen manually:

You need to get the position of the click, calculate the ray (with an inverse matrix of projection/modelview) and test this ray intersection with bounding box/sphere of your objects.
The hint about searching for View Frustum Culling was what I needed. After searching on that, I found several tutorials that worked quite well.

Thanks to all who offered help!
Just a heads up, there are a few ways you can do View Frustum culling.

The simple and fast method is to create AABBs for each object in the world, then do half-space tests on each of the planes. AABBs are typically faster, since (if properly done) they contain only positional checks. Spheres are just a tad slower. If you are only ever going to use spheres in your world, it's probably faster to use spheres as bounding volumes for accuracy (such that non-visible spheres will never be rendered). However, it typically goes such that spheres are more desirable for objects whose orientation may change, and AABBs are more desirable for objects who retain their orientation.

A nice tutorial on view frustum culling: http://www.lighthouse3d.com/opengl/viewfrustum/

The more complex but very fast method is to create a spacial partitioning system and couple it with the frustum culling. If you're not familiar with spacial partitioning, I would suggest looking up some tutorials. I will go as far as to say that any major game these days has SOME form of spatial partitioning.

A few different spatial partitioning systems are: K-DOPS, KD-Trees, Octrees, Quadtrees, and Binary Spatial Partitioning (BSP Tree).

Also, if you really want to speed things up, you can implement occlusion culling on the objects that are still on the screen.

If you still want to optimize, let me know and I'll see what I can cook up! Have a nice day! :)

This topic is closed to new replies.

Advertisement