00001 /* 00002 Copyright (c) 2011, The Cinder Project, All rights reserved. 00003 This code is intended for use with the Cinder C++ library: http://libcinder.org 00004 00005 Portions of this code (C) Paul Houx 00006 All rights reserved. 00007 00008 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00009 the following conditions are met: 00010 00011 * Redistributions of source code must retain the above copyright notice, this list of conditions and 00012 the following disclaimer. 00013 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00014 the following disclaimer in the documentation and/or other materials provided with the distribution. 00015 00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00017 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00018 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00019 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00020 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00021 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00022 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00023 POSSIBILITY OF SUCH DAMAGE. 00024 */ 00025 00026 #include "cinder/AxisAlignedBox.h" 00027 #include "cinder/Camera.h" 00028 #include "cinder/Plane.h" 00029 #include "cinder/Sphere.h" 00030 00031 #pragma once 00032 00033 #if defined( CINDER_MSW ) 00034 #pragma push_macro( "NEAR" ) 00035 #undef NEAR 00036 #pragma push_macro( "FAR" ) 00037 #undef FAR 00038 #endif 00039 00040 namespace cinder { 00041 00042 template<typename T> 00043 class Frustum 00044 { 00045 public: 00046 enum { NEAR, FAR, LEFT, RIGHT, TOP, BOTTOM }; 00047 00048 public: 00049 Frustum() {} 00050 Frustum( const Camera &cam ); 00051 00053 void set( const Camera &cam ); 00055 void set( const Camera &cam, const Vec3<T> &ntl, const Vec3<T> &ntr, const Vec3<T> &nbl, const Vec3<T> &nbr ); 00056 00058 bool contains( const Vec3<T> &loc ) const; 00060 bool contains( const Sphere &sphere ) const { return contains(sphere.getCenter(), sphere.getRadius()); }; 00062 bool contains( const Vec3<T> ¢er, T radius ) const; 00064 bool contains( const AxisAlignedBox3f &box ) const; 00066 bool contains( const Vec3<T> ¢er, const Vec3<T> &size ) const { 00067 AxisAlignedBox3f box(center-0.5f*size, center+0.5f*size); 00068 return contains(box); 00069 }; 00070 00072 bool intersects( const Vec3<T> &loc ) const { return contains(loc); }; 00074 bool intersects( const Sphere &sphere ) const { return intersects(sphere.getCenter(), sphere.getRadius()); }; 00076 bool intersects( const Vec3<T> ¢er, T radius ) const; 00078 bool intersects( const AxisAlignedBox3f &box ) const; 00080 bool intersects( const Vec3<T> ¢er, const Vec3<T> &size ) const { 00081 AxisAlignedBox3f box(center-0.5f*size, center+0.5f*size); 00082 return intersects(box); 00083 }; 00084 00085 protected: 00086 Plane<T> mFrustumPlanes[6]; 00087 }; 00088 00089 typedef Frustum<float> Frustumf; 00090 typedef Frustum<double> Frustumd; 00091 00092 #if defined( CINDER_MSW ) 00093 #pragma pop_macro( "FAR" ) 00094 #pragma pop_macro( "NEAR" ) 00095 #endif 00096 00097 } // namespace cinder