[504] | 1 | #include "Frustum.h"
|
---|
| 2 | #include "VssRay.h"
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | void
|
---|
| 8 | Frustum::Construct(const AxisAlignedBox3 &box,
|
---|
| 9 | const AxisAlignedBox3 &dBox)
|
---|
| 10 | {
|
---|
| 11 | // the frustum is defined by set of negative halfspace described by mPlanes
|
---|
| 12 |
|
---|
| 13 | // construct
|
---|
| 14 | Vector3 center = box.Center();
|
---|
| 15 | Vector3 dCenter = dBox.Center();
|
---|
| 16 |
|
---|
| 17 | // "back plane"
|
---|
| 18 | mPlanes.push_back(Plane3(-VssRay::GetDirection(dCenter.x, dCenter.y), center));
|
---|
| 19 |
|
---|
| 20 | Vector3 directions[4];
|
---|
| 21 | directions[0] = VssRay::GetDirection(dBox.Min().x, dBox.Min().y);
|
---|
| 22 | directions[1] = VssRay::GetDirection(dBox.Max().x, dBox.Min().y);
|
---|
| 23 | directions[2] = VssRay::GetDirection(dBox.Max().x, dBox.Max().y);
|
---|
| 24 | directions[3] = VssRay::GetDirection(dBox.Min().x, dBox.Max().y);
|
---|
| 25 |
|
---|
| 26 | // side planes
|
---|
| 27 | mPlanes.push_back(Plane3(CrossProd(directions[0], directions[1]), center));
|
---|
| 28 | mPlanes.push_back(Plane3(CrossProd(directions[1], directions[2]), center));
|
---|
| 29 | mPlanes.push_back(Plane3(CrossProd(directions[2], directions[3]), center));
|
---|
| 30 | mPlanes.push_back(Plane3(CrossProd(directions[3], directions[0]), center));
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | // now make sure all planes contain the spatial box
|
---|
| 34 | int i, j;
|
---|
| 35 | for (i=0; i < mPlanes.size(); i++) {
|
---|
| 36 | for (j=0; j < 8; j++) {
|
---|
| 37 | float dist = mPlanes[i].Distance(box.GetVertex(j));
|
---|
| 38 | if (dist > 0)
|
---|
| 39 | mPlanes[i].mD -= dist;
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | // conservative frustum box intersection
|
---|
| 45 | // returns
|
---|
| 46 | // 0 - box intersects the frustum
|
---|
| 47 | // -1 - box intersects the frustum and is fully inside
|
---|
| 48 | // 1 - box does not intersect the frustum
|
---|
| 49 |
|
---|
| 50 | int
|
---|
| 51 | Frustum::ComputeIntersection(const AxisAlignedBox3 &box)
|
---|
| 52 | {
|
---|
| 53 | int i;
|
---|
| 54 | int result = -1;
|
---|
| 55 | for (i=0; i < mPlanes.size(); i++) {
|
---|
| 56 | int side = box.Side(mPlanes[i]);
|
---|
| 57 | if (side == 1)
|
---|
| 58 | return 1;
|
---|
| 59 | if (side > result)
|
---|
| 60 | result = side;
|
---|
| 61 | }
|
---|
| 62 | return result;
|
---|
| 63 | }
|
---|