Created
January 15, 2012 03:34
-
-
Save paulhoux/1614175 to your computer and use it in GitHub Desktop.
Additions to Cinder to facilitate frustum culling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2012, Paul Houx | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that | |
the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, this list of conditions and | |
the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and | |
the following disclaimer in the documentation and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | |
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#include "cinder/Frustum.h" | |
#include "cinder/gl/gl.h" | |
namespace cinder { | |
Frustum::Frustum(void) | |
{ | |
// set planes using default perspective camera | |
set( CameraPersp() ); | |
} | |
Frustum::Frustum(const Camera &cam) | |
{ | |
// set planes using camera | |
set( cam ); | |
} | |
Frustum::~Frustum(void) | |
{ | |
} | |
void Frustum::set(const Camera &cam) | |
{ | |
Vec3f ntl, ntr, nbl, nbr; | |
cam.getNearClipCoordinates( &ntl, &ntr, &nbl, &nbr ); | |
Vec3f ftl, ftr, fbl, fbr; | |
cam.getFarClipCoordinates( &ftl, &ftr, &fbl, &fbr ); | |
mFrustumPlanes[TOPPLANE].set(ntr, ntl, ftl); | |
mFrustumPlanes[BOTTOMPLANE].set(nbl, nbr, fbr); | |
mFrustumPlanes[LEFTPLANE].set(ntl, nbl, fbl); | |
mFrustumPlanes[RIGHTPLANE].set(nbr, ntr, fbr); | |
mFrustumPlanes[NEARPLANE].set(ntl, ntr, nbr); | |
mFrustumPlanes[FARPLANE].set(ftr, ftl, fbl); | |
} | |
void Frustum::set(const Camera &cam, const Vec3f &ntl, const Vec3f &ntr, const Vec3f &nbl, const Vec3f &nbr) | |
{ | |
Vec3f eye = cam.getEyePoint(); | |
float farClip = cam.getFarClip(); | |
Vec3f ftl = (ntl - eye).normalized() * farClip; | |
Vec3f ftr = (ntr - eye).normalized() * farClip; | |
Vec3f fbl = (nbl - eye).normalized() * farClip; | |
Vec3f fbr = (nbr - eye).normalized() * farClip; | |
mFrustumPlanes[TOPPLANE].set(ntr, ntl, ftl); | |
mFrustumPlanes[BOTTOMPLANE].set(nbl, nbr, fbr); | |
mFrustumPlanes[LEFTPLANE].set(ntl, nbl, fbl); | |
mFrustumPlanes[RIGHTPLANE].set(nbr, ntr, fbr); | |
mFrustumPlanes[NEARPLANE].set(ntl, ntr, nbr); | |
mFrustumPlanes[FARPLANE].set(ftr, ftl, fbl); | |
} | |
bool Frustum::contains(const Vec3f &loc) | |
{ | |
for(size_t i=0; i<6; ++i) { | |
if (mFrustumPlanes[i].distance(loc) < 0) | |
return false; | |
} | |
return true; | |
} | |
bool Frustum::contains(const Vec3f ¢er, float radius) | |
{ | |
float distance; | |
for(size_t i=0; i<6; ++i) { | |
distance = mFrustumPlanes[i].distance(center); | |
if (distance < -radius) | |
return false; | |
else if (distance < radius) | |
return false; | |
} | |
return true; | |
} | |
bool Frustum::intersects(const Vec3f ¢er, float radius) | |
{ | |
float distance; | |
for(size_t i=0; i<6; ++i) { | |
distance = mFrustumPlanes[i].distance(center); | |
if (distance < -radius) | |
return false; | |
} | |
return true; | |
} | |
bool Frustum::contains(AxisAlignedBox3f &box) | |
{ | |
for(size_t i=0; i<6; ++i) { | |
if (mFrustumPlanes[i].distance(box.getPositive(mFrustumPlanes[i].normal())) < 0) | |
return false; | |
else if (mFrustumPlanes[i].distance(box.getNegative(mFrustumPlanes[i].normal())) < 0) | |
return false; | |
} | |
return true; | |
} | |
bool Frustum::intersects(AxisAlignedBox3f &box) | |
{ | |
for(size_t i=0; i<6; ++i) { | |
if (mFrustumPlanes[i].distance(box.getPositive(mFrustumPlanes[i].normal())) < 0) | |
return false; | |
} | |
return true; | |
} | |
} // namespace cinder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2012, Paul Houx | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that | |
the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, this list of conditions and | |
the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and | |
the following disclaimer in the documentation and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | |
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#include "cinder/AxisAlignedBox.h" | |
#include "cinder/Camera.h" | |
#include "cinder/Plane.h" | |
#include "cinder/Sphere.h" | |
#pragma once | |
namespace cinder { | |
class Frustum | |
{ | |
public: | |
static enum { NEARPLANE, FARPLANE, LEFTPLANE, RIGHTPLANE, TOPPLANE, BOTTOMPLANE }; | |
public: | |
Frustum(void); | |
Frustum( const Camera &cam ); | |
virtual ~Frustum(void); | |
//! Creates a frustum based on the camera's parameters. | |
void set( const Camera &cam ); | |
//! Creates a frustum based on the camera's parameters and four corners of a portal. | |
void set( const Camera &cam, const Vec3f &ntl, const Vec3f &ntr, const Vec3f &nbl, const Vec3f &nbr ); | |
//! Returns TRUE if point is within frustum. | |
bool contains( const Vec3f &loc ); | |
//! Returns TRUE if the sphere is fully contained within frustum. See also 'intersects'. | |
bool contains( const Sphere &sphere ){ return contains(sphere.getCenter(), sphere.getRadius()); }; | |
//! Returns TRUE if the sphere is fully contained within frustum. See also 'intersects'. | |
bool contains( const Vec3f ¢er, float radius ); | |
//! Returns TRUE if the box is fully contained within frustum. See also 'intersects'. | |
bool contains( AxisAlignedBox3f &box ); | |
//! Returns TRUE if the box is fully contained within frustum. See also 'intersects'. | |
bool contains( const Vec3f ¢er, const Vec3f &size ){ return contains(AxisAlignedBox3f(center-0.5f*size, center+0.5f*size)); }; | |
//! Returns TRUE if point is within frustum. | |
bool intersects( const Vec3f &loc ){ return contains(loc); }; | |
//! Returns TRUE if the sphere is partialy contained within frustum. See also 'contains'. | |
bool intersects( const Sphere &sphere ){ return intersects(sphere.getCenter(), sphere.getRadius()); }; | |
//! Returns TRUE if the sphere is partialy contained within frustum. See also 'contains'. | |
bool intersects( const Vec3f ¢er, float radius ); | |
//! Returns TRUE if the box is partialy contained within frustum. See also 'contains'. | |
bool intersects( AxisAlignedBox3f &box ); | |
//! Returns TRUE if the box is fully contained within frustum. See also 'intersects'. | |
bool intersects( const Vec3f ¢er, const Vec3f &size ){ return intersects(AxisAlignedBox3f(center-0.5f*size, center+0.5f*size)); }; | |
protected: | |
Plane mFrustumPlanes[6];; | |
}; | |
} // namespace cinder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2012, Paul Houx | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that | |
the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, this list of conditions and | |
the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and | |
the following disclaimer in the documentation and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | |
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#include "cinder/Plane.h" | |
namespace cinder { | |
Plane::Plane(void) | |
: mDistance(0.0f) | |
{ | |
// set default plane | |
set( Vec3f::yAxis(), Vec3f::zero() ); | |
} | |
Plane::Plane(const Vec3f &v1, const Vec3f &v2, const Vec3f &v3) | |
{ | |
set(v1,v2,v3); | |
} | |
Plane::Plane(const Vec3f &normal, const Vec3f &point) | |
{ | |
set(normal, point); | |
} | |
Plane::Plane(float a, float b, float c, float d) | |
{ | |
set(a, b, c, d); | |
} | |
Plane::~Plane(void) | |
{ | |
} | |
void Plane::set(const Vec3f &v1, const Vec3f &v2, const Vec3f &v3) | |
{ | |
Vec3f aux1(v1 - v2); | |
Vec3f aux2(v3 - v2); | |
Vec3f normal = aux2.cross(aux1); | |
if(normal.lengthSquared() == 0.0f) { | |
// error! invalid parameters | |
throw PlaneExc(); | |
return; | |
} | |
mNormal.set(normal); | |
mNormal.normalize(); | |
mPoint.set(v2); | |
mDistance = -(mNormal.dot(mPoint)); | |
} | |
void Plane::set(const Vec3f &normal, const Vec3f &point) | |
{ | |
if(normal.lengthSquared() == 0.0f) { | |
// error! invalid parameters | |
throw PlaneExc(); | |
return; | |
} | |
mNormal.set(normal); | |
mNormal.normalize(); | |
mPoint.set(point); | |
mDistance = -(mNormal.dot(mPoint)); | |
} | |
void Plane::set(float a, float b, float c, float d) | |
{ | |
Vec3f normal(a, b, c); | |
float length = normal.length(); | |
if(length == 0.0f) { | |
// error! invalid parameters | |
throw PlaneExc(); | |
return; | |
} | |
mNormal.set(normal); | |
mNormal.normalize(); | |
mDistance = d / length; | |
} | |
} // namespace cinder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2012, Paul Houx | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that | |
the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, this list of conditions and | |
the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and | |
the following disclaimer in the documentation and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | |
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#pragma once | |
#include "cinder/Vector.h" | |
namespace cinder { | |
class Plane | |
{ | |
public: | |
Plane(void); | |
Plane::Plane( const Vec3f &v1, const Vec3f &v2, const Vec3f &v3 ); | |
Plane::Plane( const Vec3f &normal, const Vec3f &point ); | |
Plane::Plane( float a, float b, float c, float d ); | |
virtual ~Plane(void); | |
//! Defines a plane using 3 points. | |
void set( const Vec3f &v1, const Vec3f &v2, const Vec3f &v3 ); | |
//! Defines a plane using a normal vector and a point. | |
void set( const Vec3f &normal, const Vec3f &point ); | |
//! Defines a plane using 4 coefficients. | |
void set( float a, float b, float c, float d ); | |
const Vec3f& point() const { return mPoint; }; | |
const Vec3f& normal() const { return mNormal; }; | |
float distance( const Vec3f &p ){ return (mDistance + mNormal.dot(p)); }; | |
protected: | |
Vec3f mNormal; | |
Vec3f mPoint; | |
float mDistance; | |
}; | |
class PlaneExc : public std::exception { | |
public: | |
virtual const char* what() const throw() { return "Invalid parameters specified"; } | |
}; | |
} // namespace cinder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment