wavepacket-lib/lib/geometry/frustum.h

Go to the documentation of this file.
00001 /*
00002  * frustum.h
00003  *
00004  * Copyright (C) 2009,2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * 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 THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * 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
00026  * ON 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  * Basic frustum object (mostly used for 3D rendering).
00032  */
00033 
00034 #ifndef WAVEPACKET_FRUSTUM_H__
00035 #define WAVEPACKET_FRUSTUM_H__
00036 
00037 // includes --------------------------------------------------------------------
00038 #include "plane.h"
00039 
00040 
00041 /// \ingroup geometry
00042 /*@{*/
00043 
00044 
00045 // common typedefs -------------------------------------------------------------
00046 
00047 
00048 /// Basic frustum object.  This is a truncated 4-sided pyramid, also called a
00049 ///   right square frustum.  See http://en.wikipedia.org/wiki/Frustum
00050 struct frustum_t {
00051         // public enums --------------------------------------------------------
00052 
00053         /// this enum defines planes as if someone was standing at the apex of
00054         ///  the frustum, and looking down it.
00055         enum ePlane {
00056                 eTop            = 0,    ///< index of top plane
00057                 eBottom         = 1,    ///< index of bottom plane
00058                 eLeft           = 2,    ///< index of left plane
00059                 eRight          = 3,    ///< index of right plane
00060                 eNear           = 4,    ///< index of near plane
00061                 eFar            = 5,    ///< index of far plane
00062 
00063                 // keep these last!
00064                 eEdgeCount      = 12,   ///< total number of edges
00065                 ePlaneCount     = 6     ///< total number of planes
00066         };
00067 
00068         // constructor, destructor ---------------------------------------------
00069         frustum_t(void) throw() {}
00070 
00071         // manipulators --------------------------------------------------------
00072         void clear(void) throw() {
00073                         for (int i = 0; i < ePlaneCount; ++i) {
00074                                 plane[i].clear();
00075                         }
00076                 }
00077 
00078         void dump(IN const char * msg) const throw() {
00079                         DPRINTF("%s (frustum):", msg);
00080                         plane[eTop].dump(   "    top   :");
00081                         plane[eBottom].dump("    bottom:");
00082                         plane[eLeft].dump(  "    left  :");
00083                         plane[eRight].dump( "    right :");
00084                         plane[eFar].dump(   "    far   :");
00085                         plane[eNear].dump(  "    near  :");
00086                 }
00087 
00088         /// does the frustum contain the given rectangle?
00089         eContains containsRect(IN const rect3d_t& r) const throw();
00090 
00091         /// does the frustum contain the given sphere?
00092         eContains containsSphere(IN const point3d_t& position,
00093                                 IN float radius) const throw();
00094 
00095         /// returns the endpoints of the specified edge
00096         ///     (0 <= index < eEdgeCount)
00097         void getEdge(IN int index,
00098                                 OUT point3d_t& p0,
00099                                 OUT point3d_t& p1) const throw();
00100 
00101         // data fields ---------------------------------------------------------
00102         plane_t         plane[ePlaneCount];  // a frustum is defined by 6 planes
00103 };
00104 
00105 
00106 
00107 /// create a view frustum given basic geometric information.
00108 /// Keep in mind, this uses standard OpenGL right-handed coordinates, with Y
00109 ///   being the usual up direction, and x and z in the plane of the ground.
00110 bool createViewFrustum(IN const point3d_t& position,
00111                                 IN const point3d_t& facing,
00112                                 IN const point3d_t& up,
00113                                 IN float fovyRadians,   ///< field of view, Y
00114                                 IN float zNear,
00115                                 IN float zFar,
00116                                 IN float aspect,
00117                                 OUT frustum_t& frustum) throw();
00118 
00119 
00120 
00121 #endif  // WAVEPACKET_FRUSTUM_H__
00122