ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

frustum.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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     // Based on "Fast Extraction of Viewing Frustum Planes from the
00032     // World-View-Projection matrix" by Gil Gribb and Klaus Hartmann.
00033     // http://www.cs.otago.ac.nz/postgrads/alexis/planeExtraction.pdf
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 }


Generated on Sat Mar 23 2019 05:01:38 for ResidualVM by doxygen 1.7.1
curved edge   curved edge