Calculate bounding box (axis-aligned (AABB) or oriented (OBB)) And check if it is visible. If it is not visible, don't draw the object.
Required knowledge:
- Plane equation (
ax + by + cz + d = 0
)
- Point to plane distance calculation.
- Construct plane (equation) using point and normal or using 3 points.
- Applying matrix transformation to a point/vector.
- Vector cross-product.
That's basic 3D stuff that can be found in plenty of tutorials on the web. Google "matrix faq", for example, gamasutra also had aabb/obb tutorials 7 years ago.
--edit--
Figuring out if a coordinate in 3D space is visible is the part chat escapes me. Well, back to notepad and a few tutorials
Plane can be defined by point (p
) and normal (n
). p
is a point on the plane, n
is a vector that is perpendicular to the plane.
Given point p1
, you can figure out whether this point lies above the plane, on the plane, or below it, by performing simple calculation.
dist = dot((p1 - p), n)
, where dot
is dot-product (dot(a, b) = a.x*b.x + a.y+b.y + a.z*b.z
) If n
is unit-length (dot(n, n) == 1
) dist will store distance from point to plane. Otherwise it's sign will indicate where p1
is located. If dist > 0
p1 is above the plane, if dist < 0
, it is below the plane, and if dist == 0
it is on the plane.
Frustrum is a "visible volume" which is defined by 4 planes that intersect at camera origin. Ever saw "camera cone" in 3d editor? That's what frustrum looks like if visualized.
Now, let's assume that for all those "frustrum" planes normals (plane normals, n
, remember?) point outside the frustrum.
Now, you have an object you want to test. If ALL points of this objects's bounding box are "above" (or "outside") even for one frustrum plane, object is invisible, because it does not itnersect the frustrum. That's based on separating axis theorem. This will also work even if you use object's points instead of bounding box.
That should be enough to get you started. You can google the rest. Google for tutorials and 3d related stuff - those topics were discussed to death in last 10 years (mostly by wannabe game developers and beginners), there should be sea of info on the subject.
gluProject
. However, that's not an efficient way for clipping objects. – SigTerm