# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/math3D.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #include <vlCore/math3D.hpp>
    00033 #include <vlCore/AABB.hpp>
    00034 #include <vlCore/Vector2.hpp>
    00035 #include <vlCore/Vector4.hpp>
    00036 #include <vlCore/Plane.hpp>
    00037 #include <vlCore/checks.hpp>
    00038 #include <cstdlib>
    00039
    00040 using namespace vl;
    00041
    00042 //-----------------------------------------------------------------------------
    00043 Real vl::randomMinMax(Real min, Real max)
    00044 {
    00045   Real t = rand() / (Real)RAND_MAX;
    00046   return min + (max-min)*t;
    00047 }
    00048 //-----------------------------------------------------------------------------
    00049 int vl::greaterEqualPow2(int n)
    00050 {
    00051   int pow2=2;
    00052   for(int i=0; i<20; ++i) {
    00053     if (pow2 >= n)
    00054       return pow2;
    00055     pow2 = pow2 * 2;
    00056   }
    00057   return pow2;
    00058 }
    00059 //-----------------------------------------------------------------------------
    00060 int vl::smallerEqualPow2(int n)
    00061 {
    00062   int pow2=2;
    00063   for(int i=0; i<20; ++i) {
    00064     if (pow2 > n)
    00065       return pow2/2;
    00066     pow2 = pow2 * 2;
    00067   }
    00068   return pow2;
    00069 }
    00070 //-----------------------------------------------------------------------------
    00071 void vl::extractPlanes( Plane* planes, const mat4& modelviewproj )
    00072 {
    00073   // see also http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
    00074   // see also http://zach.in.tu-clausthal.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
    00075   // the equation is a*x+b*y+x*z+d = 0
    00076   // where <a b c> is the normal of the plane
    00077   // the normals are pointing inside the viewfrustum
    00078
    00079   // 1) we want the planes to point outward so we reverse them
    00080   // 2) because of 1) "d" becomes the distance of the plane from the origin
    00081
    00082   vec3 n;
    00083   Real d;
    00084
    00085   // left clipping plane
    00086   n.x() = modelviewproj.e(3,0) + modelviewproj.e(0,0);
    00087   n.y() = modelviewproj.e(3,1) + modelviewproj.e(0,1);
    00088   n.z() = modelviewproj.e(3,2) + modelviewproj.e(0,2);
    00089   d = modelviewproj.e(3,3) + modelviewproj.e(0,3);
    00090   d /= n.length();
    00091   n.normalize();
    00092   planes[0] = Plane(d,-n);
    00093
    00094   // right clipping plane
    00095   n.x() = modelviewproj.e(3,0) - modelviewproj.e(0,0);
    00096   n.y() = modelviewproj.e(3,1) - modelviewproj.e(0,1);
    00097   n.z() = modelviewproj.e(3,2) - modelviewproj.e(0,2);
    00098   d = modelviewproj.e(3,3) - modelviewproj.e(0,3);
    00099   d /= n.length();
    00100   n.normalize();
    00101   planes[1] = Plane(d,-n);
    00102
    00103   // top clipping plane
    00104   n.x() = modelviewproj.e(3,0) - modelviewproj.e(1,0);
    00105   n.y() = modelviewproj.e(3,1) - modelviewproj.e(1,1);
    00106   n.z() = modelviewproj.e(3,2) - modelviewproj.e(1,2);
    00107   d = modelviewproj.e(3,3) - modelviewproj.e(1,3);
    00108   d /= n.length();
    00109   n.normalize();
    00110   planes[2] = Plane(d,-n);
    00111
    00112   // bottom clipping plane
    00113   n.x() = modelviewproj.e(3,0) + modelviewproj.e(1,0);
    00114   n.y() = modelviewproj.e(3,1) + modelviewproj.e(1,1);
    00115   n.z() = modelviewproj.e(3,2) + modelviewproj.e(1,2);
    00116   d = modelviewproj.e(3,3) + modelviewproj.e(1,3);
    00117   d /= n.length();
    00118   n.normalize();
    00119   planes[3] = Plane(d,-n);
    00120
    00121   // near clipping plane
    00122   n.x() = modelviewproj.e(3,0) + modelviewproj.e(2,0);
    00123   n.y() = modelviewproj.e(3,1) + modelviewproj.e(2,1);
    00124   n.z() = modelviewproj.e(3,2) + modelviewproj.e(2,2);
    00125   d = modelviewproj.e(3,3) + modelviewproj.e(2,3);
    00126   d /= n.length();
    00127   n.normalize();
    00128   planes[4] = Plane(d,-n);
    00129
    00130   // far clipping plane
    00131   n.x() = modelviewproj.e(3,0) - modelviewproj.e(2,0);
    00132   n.y() = modelviewproj.e(3,1) - modelviewproj.e(2,1);
    00133   n.z() = modelviewproj.e(3,2) - modelviewproj.e(2,2);
    00134   d = modelviewproj.e(3,3) - modelviewproj.e(2,3);
    00135   d /= n.length();
    00136   n.normalize();
    00137   planes[5] = Plane(d,-n);
    00138 }

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:21.
Permission is granted to use this page to write and publish articles regarding Visualization Library.