determine if object is in view

Started by
11 comments, last by Trienco 18 years, 4 months ago
Greetings, I'm looking for a quick (can be crude) way to determine if an object (a point in space) is in my current view projection. I'm sure there's some nifty process involving the current projection matrix, but I'm not aware of it. In other words, I don't want to render an object if it's behind the camera. Any help is greatly appreciated.
Advertisement
Use a frustum (6 planes surrounding your view frustum) and test them against spheres or axis aligned boxes surrounding your objects.
I wrote a Wolf3D style game years ago that had a 90 degree view (-45 to +45). After translating all position of the objects, I first made sure that their position on the Z-axis was greater than my clipping point. Then, to see if they were on-screen, I simply checked that their X position was less than their Z position and greater than the negative of their Z position. I allowed a little more slack than that so objects could be partially off-screen but that's the jist of it.

Hey, you wanted crude =b
Quit screwin' around! - Brock Samson
Jan-Lieuwe, is there a simple way of creating the 6 sides of my frustum? Would I have to create the frustum (6 quads), and then apply the current MODELVIEW_MATRIX to these quads, then test against this translated frustum, or is there a simpler way?

Quote:Original post by slikrik
Jan-Lieuwe, is there a simple way of creating the 6 sides of my frustum? Would I have to create the frustum (6 quads), and then apply the current MODELVIEW_MATRIX to these quads, then test against this translated frustum, or is there a simpler way?
This article describes a straightforward method for extracting the frustum planes from the modelview and projection matrices. (It does require a little matrix math though...)
If you just want to know wether an object is behind your camera, just one plane of the frustum will do (the near clipping plane).
If you are really talking about points (or don't mind it being a bit crude), then it's usually enough to know the width/height of your frustum for some z (usually you should know it for the near plane, as that's what you set the projection up with.. unless using some helper function).

Then it comes down to transforming the point to camera space (can be done coordinate by coordinate), compare z to the near/far plane and x (y) to width (height)*z. Or do it with the six planes, as you will probably find countless tutorials about that ,-)
f@dzhttp://festini.device-zero.de
Trienco:

To use your method, wouldn't I need to transform the point in question by the *inverse* of the projection/modelview matrix to obtain the Z coordinate relative to the camera?
If you have front, right and up for your camera, you can dot the vector from camera to object center with those three. If depth is negative (more than radius) your object is behind camera. Else take the absolute of right and up dot products; subtract objet radius; divide each by depth dot product; if this projects less than your field of view, the object is likely visible. The "field of view" is the atan value for your sideways and upways half-viewing-angles.

This is generally quicker than doing the six planes test (assuming you pre-calculate your atan and camera values once each frame), and about as accurate.
enum Bool { True, False, FileNotFound };
hplus0603: could you please clarify the steps you're referring to? By "front, right and up" do you mean "near, right, and top" planes? You then refer to "depth" but I'm not sure how you're calculating depth. Your "else" portion then doesn't make sense-- I'm sorry if I am missing something obvious. I can't tell exactly what you're referring to with the right and top dot products...

Thanks!

This topic is closed to new replies.

Advertisement