00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "math/frustum.h"
00024
00025 namespace Math {
00026
00027 Frustum::Frustum() {
00028 }
00029
00030 void Frustum::setup(const Math::Matrix4 &matrix) {
00031
00032
00033
00034
00035 _planes[0]._normal.x() = matrix.getValue(3, 0) + matrix.getValue(0, 0);
00036 _planes[0]._normal.y() = matrix.getValue(3, 1) + matrix.getValue(0, 1);
00037 _planes[0]._normal.z() = matrix.getValue(3, 2) + matrix.getValue(0, 2);
00038 _planes[0]._d = matrix.getValue(3, 3) + matrix.getValue(0, 3);
00039
00040 _planes[1]._normal.x() = matrix.getValue(3, 0) - matrix.getValue(0, 0);
00041 _planes[1]._normal.y() = matrix.getValue(3, 1) - matrix.getValue(0, 1);
00042 _planes[1]._normal.z() = matrix.getValue(3, 2) - matrix.getValue(0, 2);
00043 _planes[1]._d = matrix.getValue(3, 3) - matrix.getValue(0, 3);
00044
00045 _planes[2]._normal.x() = matrix.getValue(3, 0) - matrix.getValue(1, 0);
00046 _planes[2]._normal.y() = matrix.getValue(3, 1) - matrix.getValue(1, 1);
00047 _planes[2]._normal.z() = matrix.getValue(3, 2) - matrix.getValue(1, 2);
00048 _planes[2]._d = matrix.getValue(3, 3) - matrix.getValue(1, 3);
00049
00050 _planes[3]._normal.x() = matrix.getValue(3, 0) + matrix.getValue(1, 0);
00051 _planes[3]._normal.y() = matrix.getValue(3, 1) + matrix.getValue(1, 1);
00052 _planes[3]._normal.z() = matrix.getValue(3, 2) + matrix.getValue(1, 2);
00053 _planes[3]._d = matrix.getValue(3, 3) + matrix.getValue(1, 3);
00054
00055 _planes[4]._normal.x() = matrix.getValue(3, 0) + matrix.getValue(2, 0);
00056 _planes[4]._normal.y() = matrix.getValue(3, 1) + matrix.getValue(2, 1);
00057 _planes[4]._normal.z() = matrix.getValue(3, 2) + matrix.getValue(2, 2);
00058 _planes[4]._d = matrix.getValue(3, 3) + matrix.getValue(2, 3);
00059
00060 _planes[5]._normal.x() = matrix.getValue(3, 0) - matrix.getValue(2, 0);
00061 _planes[5]._normal.y() = matrix.getValue(3, 1) - matrix.getValue(2, 1);
00062 _planes[5]._normal.z() = matrix.getValue(3, 2) - matrix.getValue(2, 2);
00063 _planes[5]._d = matrix.getValue(3, 3) - matrix.getValue(2, 3);
00064
00065 for (int i = 0; i < 6; ++i) {
00066 _planes[i].normalize();
00067 }
00068 }
00069
00070 bool Frustum::isInside(const Math::AABB &aabb) const {
00071 Math::Vector3d min = aabb.getMin();
00072 Math::Vector3d max = aabb.getMax();
00073
00074 for (int i = 0; i < 6; ++i) {
00075 const Plane &plane = _planes[i];
00076 Math::Vector3d positive = min;
00077
00078 if (plane._normal.x() >= 0.0f)
00079 positive.x() = max.x();
00080 if (plane._normal.y() >= 0.0f)
00081 positive.y() = max.y();
00082 if (plane._normal.z() >= 0.0f)
00083 positive.z() = max.z();
00084
00085 float dist = _planes[i].getSignedDistance(positive);
00086 if (dist < 0.0f)
00087 return false;
00088 }
00089
00090 return true;
00091 }
00092
00093 }