View Full Version : Find faces in camera frustum?

01 January 2009, 03:57 AM
Im looking to find all the faces that are in a cameras view frustum.

So some pseudo code.

1. get my cameras skewed matrix
2. use the skewed matrix and apply it to a copy of my mesh(s) to skew it
3. run triangle box intersection test on a 1unit cube over the mesh
4. save my list of faces and delete the mesh

And im stuck on point 1, how do i generate a Projection matrix from a camera, i have a near clip plane and a far clip plane.

01 January 2009, 05:54 AM
ok i think i need to write my own function to multiply matrix4's

Edit, making my own matrix 4 struct, is there a way to customize the way it prints out data so if i got "Print mMatrix4" it will format it the same as a matrix3 would?

01 January 2009, 09:37 AM
Hi Bronson,
I admit I cannot completely undertand what you're trying to do, but maybe my article Practical Space Mapping for interaction ( can be of help.

- Enrico

01 January 2009, 03:24 PM
I don't have time to look for an answer right now, maybe later,
but in the meanwhile I can suggest what seems to me a simpler solution.
If I understood you correctly, you can model a skewed box that covers the area of the camera fov and use volume select modifier on the object to select the faces outside that skewed box and delete them using delete mesh modifier.

02 February 2009, 09:51 PM
I am making a similar script to perform Culling and clipping accordly to the camera frustum.
I use "meshop.slice" to create the new vertices at the intersection of the frustum and the objects.
This way i hope i can get an accurate Face Count to simulate geometry budget in a realtime game context directly in 3dsmax.

02 February 2009, 11:53 PM
You can just use the 'how to write a vertex renderer' script in the help file as a guide (as per Bobo's reply to a similar question in ).

e.g. (completely taken from the help file - this can be optimized for faster processing)

fn isPointInFrustrum pointPos = (
thePos = pointPos * viewport.getTM()
screen_origin = mapScreenToView [0,0] (thePos.z) [renderWidth,renderHeight]
end_screen = mapScreenToView [renderWidth,renderHeight] thePos.z [renderWidth,renderHeight]
world_size = screen_origin-end_screen
x_aspect = renderWidth/(abs world_size.x)
y_aspect = renderHeight/(abs world_size.y)
screen_coords = point2 (x_aspect*(thePos.x-screen_origin.x)) (-(y_aspect*(thePos.y-screen_origin.y)))
(((screen_coords.x >= 0) AND (screen_coords.x < renderWidth)) AND ((screen_coords.y >= 0) AND (screen_coords.y < renderHeight)))

Select the vertices on the currently selected editable mesh which are within the current Perspective viewport's view frustrum:

$.selectedVerts = for i = 1 to $.numVerts collect (
if (isPointInFrustrum (getVert $ i)) then ( i ) else ( dontcollect )

With that basic function you can easily check if all 3 vertices of an object's face lay within the view frustrum - if they do, then that face is within the view frustrum. If zero vertices are within the view frustrum, it is outside. Otherwise, it's partially inside and partially outside.

Two notes:
- make sure you view the results with 'Show Safe Frames' enabled as otherwise the different viewport aspect from the actual (camera) view aspect may throw you off
- if any face -spans- the view frustrum or larger, it won't be counted. It will still be in view but because all of its -vertices- lay outside of the frustrum, it would be determined to be outside of the view frustrum. Rare in general, maybe not so rare in computer games (/me walks up to a wall) but probably not a huge issue.

CGTalk Moderation
02 February 2009, 11:53 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.