2013-12-18 10:12:15 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-11-27 10:34:30 +08:00
|
|
|
#ifndef __CC_FRUSTUM_H__
|
|
|
|
#define __CC_FRUSTUM_H__
|
|
|
|
|
|
|
|
#include "CCPlatformMacros.h"
|
2013-12-17 14:18:41 +08:00
|
|
|
#include "kazmath/kazmath.h"
|
2013-11-27 10:34:30 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class ViewTransform
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ViewTransform();
|
|
|
|
~ViewTransform();
|
|
|
|
void Init(const kmVec3& pos, const kmVec3& focus, const kmVec3& up);
|
|
|
|
|
|
|
|
const kmVec3& getPosition() const { return _position; }
|
|
|
|
const kmVec3& getFocus() const { return _focus; }
|
|
|
|
const kmVec3& getDirection() const;
|
|
|
|
const kmVec3& getRight() const;
|
|
|
|
const kmVec3& getUp() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void LazyAdjust() const;
|
|
|
|
private:
|
|
|
|
kmVec3 _position;
|
|
|
|
kmVec3 _focus;
|
|
|
|
kmVec3 _up;
|
|
|
|
|
|
|
|
mutable bool _dirty;
|
|
|
|
mutable kmMat4 _matrix;
|
|
|
|
mutable kmVec3 _adjustDir;
|
|
|
|
mutable kmVec3 _adjustRight;
|
|
|
|
mutable kmVec3 _adjustUp;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AABB
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AABB(const kmVec3& min, const kmVec3& max);
|
|
|
|
~AABB();
|
|
|
|
|
|
|
|
kmVec3 getCenter() const;
|
|
|
|
|
|
|
|
float getDimensionX() const;
|
|
|
|
float getDimensionY() const;
|
|
|
|
float getDimensionZ() const;
|
|
|
|
|
|
|
|
kmVec3 getPositivePoint(const kmVec3& direction) const;
|
|
|
|
kmVec3 getNegativePoint(const kmVec3& direction) const;
|
|
|
|
|
|
|
|
const AABB& expand(const kmVec3& point);
|
|
|
|
private:
|
|
|
|
kmVec3 _min;
|
|
|
|
kmVec3 _max;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Frustum
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class IntersectResult
|
|
|
|
{
|
|
|
|
OUTSIDE = 0,
|
|
|
|
INTERSECT = 1,
|
|
|
|
INSIDE = 2
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
Frustum();
|
|
|
|
~Frustum();
|
|
|
|
|
|
|
|
void setupProjectionOrthogonal(const ViewTransform& view, float width, float height, float near, float far);
|
|
|
|
void setupProjectionPerspective(const ViewTransform& view, float left, float right, float top, float bottom, float near, float far);
|
|
|
|
void setupProjectionPerspectiveFov(const ViewTransform& view, float fov, float ratio, float near, float far);
|
|
|
|
|
|
|
|
void setupFromMatrix(const kmMat4& view, const kmMat4& projection);
|
|
|
|
|
|
|
|
IntersectResult intersectPoint(const kmVec3& point) const;
|
|
|
|
IntersectResult intersectAABB(const AABB& aabb) const;
|
|
|
|
IntersectResult intersectSphere(const kmVec3& center, float radius) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum FrustumPlane
|
|
|
|
{
|
2013-12-18 14:36:49 +08:00
|
|
|
FRUSTUM_NEAR = 0,
|
|
|
|
FRUSTUM_FAR = 1,
|
|
|
|
FRUSTUM_BOTTOM = 2,
|
|
|
|
FRUSTUM_TOP = 3,
|
|
|
|
FRUSTUM_LEFT = 4,
|
|
|
|
FRUSTUM_RIGHT = 5,
|
|
|
|
FRUSTUM_NUMBER = 6
|
2013-11-27 10:34:30 +08:00
|
|
|
};
|
2013-12-18 14:36:49 +08:00
|
|
|
kmPlane _frustumPlanes[FrustumPlane::FRUSTUM_NUMBER];
|
2013-11-27 10:34:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif
|