open source
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
@file JCBoundingBox.cpp
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_10_31
|
||||
*/
|
||||
|
||||
#include "JCBoundingBox.h"
|
||||
|
||||
namespace laya
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
JCBoundingBox::JCBoundingBox(const Vector3& min, const Vector3& max)
|
||||
{
|
||||
m_kMin = min;
|
||||
m_kMax = max;
|
||||
}
|
||||
void JCBoundingBox::getCorners(Vector3* kOutCorners)
|
||||
{
|
||||
float minX = m_kMin.x;
|
||||
float minY = m_kMin.y;
|
||||
float minZ = m_kMin.z;
|
||||
float maxX = m_kMax.x;
|
||||
float maxY = m_kMax.y;
|
||||
float maxZ = m_kMax.z;
|
||||
kOutCorners[0] = Vector3(minX, maxY, maxZ);
|
||||
kOutCorners[1] = Vector3(maxX, maxY, maxZ);
|
||||
kOutCorners[2] = Vector3(maxX, minY, maxZ);
|
||||
kOutCorners[3] = Vector3(minX, minY, maxZ);
|
||||
kOutCorners[4] = Vector3(minX, maxY, minZ);
|
||||
kOutCorners[5] = Vector3(maxX, maxY, minZ);
|
||||
kOutCorners[6] = Vector3(maxX, minY, minZ);
|
||||
kOutCorners[7] = Vector3(minX, minY, minZ);
|
||||
}
|
||||
void JCBoundingBox::getDebugLinePoint(Vector3* kOutCorners)
|
||||
{
|
||||
float minX = m_kMin.x;
|
||||
float minY = m_kMin.y;
|
||||
float minZ = m_kMin.z;
|
||||
float maxX = m_kMax.x;
|
||||
float maxY = m_kMax.y;
|
||||
float maxZ = m_kMax.z;
|
||||
Vector3 v0 =Vector3(minX, maxY, maxZ);
|
||||
Vector3 v1 =Vector3(maxX, maxY, maxZ);
|
||||
Vector3 v2 =Vector3(maxX, minY, maxZ);
|
||||
Vector3 v3 =Vector3(minX, minY, maxZ);
|
||||
Vector3 v4 =Vector3(minX, maxY, minZ);
|
||||
Vector3 v5 =Vector3(maxX, maxY, minZ);
|
||||
Vector3 v6 =Vector3(maxX, minY, minZ);
|
||||
Vector3 v7 =Vector3(minX, minY, minZ);
|
||||
kOutCorners[0] = v0;
|
||||
kOutCorners[1] = v1;
|
||||
kOutCorners[2] = v1;
|
||||
kOutCorners[3] = v2;
|
||||
kOutCorners[4] = v2;
|
||||
kOutCorners[5] = v3;
|
||||
kOutCorners[6] = v3;
|
||||
kOutCorners[7] = v0;
|
||||
kOutCorners[8] = v4;
|
||||
kOutCorners[9] = v5;
|
||||
kOutCorners[10] = v5;
|
||||
kOutCorners[11] = v6;
|
||||
kOutCorners[12] = v6;
|
||||
kOutCorners[13] = v7;
|
||||
kOutCorners[14] = v7;
|
||||
kOutCorners[15] = v4;
|
||||
kOutCorners[16] = v4;
|
||||
kOutCorners[17] = v0;
|
||||
kOutCorners[18] = v7;
|
||||
kOutCorners[19] = v3;
|
||||
kOutCorners[20] = v5;
|
||||
kOutCorners[21] = v1;
|
||||
kOutCorners[22] = v6;
|
||||
kOutCorners[23] = v2;
|
||||
}
|
||||
JCBoundingBox::~JCBoundingBox()
|
||||
{
|
||||
|
||||
}
|
||||
void JCBoundingBox::setValues(float* value)
|
||||
{
|
||||
m_kMin.x = value[0];
|
||||
m_kMin.y = value[1];
|
||||
m_kMin.z = value[2];
|
||||
m_kMax.x = value[3];
|
||||
m_kMax.y = value[4];
|
||||
m_kMax.z = value[5];
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
@file JCBoundingBox.h
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_10_31
|
||||
*/
|
||||
|
||||
#ifndef __JCBoundingBox_H__
|
||||
#define __JCBoundingBox_H__
|
||||
|
||||
#include <vector>
|
||||
#include "Vector3.h"
|
||||
|
||||
namespace laya
|
||||
{
|
||||
enum CONTAINMENT_TYPE
|
||||
{
|
||||
CT_DISJOINT = 0, //不相交
|
||||
CT_CONTAINS = 1, //全部包含
|
||||
CT_INTERSECTS = 2, //相交
|
||||
};
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
class JCBoundingBox
|
||||
{
|
||||
public:
|
||||
|
||||
/** @brief构造函数
|
||||
*/
|
||||
JCBoundingBox(const Vector3& min = { -0.5f,-0.5f,-0.5f }, const Vector3& max = { 0.5f,0.5f,0.5f });
|
||||
|
||||
|
||||
/** @brief 返回八个点
|
||||
* @param[out] 返回vector每个点
|
||||
* @return
|
||||
*/
|
||||
void getCorners(Vector3* kOutCorners);
|
||||
|
||||
|
||||
/** @brief 返回绘制线框的点数 24个点
|
||||
* @param[out] 返回24个点
|
||||
* @return
|
||||
*/
|
||||
void getDebugLinePoint(Vector3* kOutCorners);
|
||||
|
||||
/** @brief析构函数
|
||||
*/
|
||||
~JCBoundingBox();
|
||||
|
||||
/** @brief 两个盒子是否相交
|
||||
* @param[in] 其他盒子
|
||||
* @return
|
||||
*/
|
||||
bool intersectsWithBox(JCBoundingBox& pOther)
|
||||
{
|
||||
return (m_kMin <= pOther.m_kMax) && (m_kMax >= pOther.m_kMin);
|
||||
}
|
||||
|
||||
/** @brief 是否完全在包含其中
|
||||
* @param[in] 其他盒子
|
||||
* @return
|
||||
*/
|
||||
bool isFullInside(JCBoundingBox& pOther)
|
||||
{
|
||||
return m_kMin >= pOther.m_kMin && m_kMax <= pOther.m_kMax;
|
||||
}
|
||||
|
||||
bool isContain(JCBoundingBox& pOther)
|
||||
{
|
||||
return pOther.m_kMin >= m_kMin && pOther.m_kMax <= m_kMax;
|
||||
}
|
||||
|
||||
Vector3 getCenter() const
|
||||
{
|
||||
return (m_kMin + m_kMax) * 0.5f;
|
||||
}
|
||||
|
||||
void setCenter( const Vector3& kCenter )
|
||||
{
|
||||
Vector3 kMove = kCenter - getCenter();
|
||||
m_kMin += kMove;
|
||||
m_kMax += kMove;
|
||||
}
|
||||
|
||||
/** @brief 获得size
|
||||
* @return
|
||||
*/
|
||||
Vector3 getSize()
|
||||
{
|
||||
return m_kMax - m_kMin;
|
||||
}
|
||||
|
||||
Vector3 getHalfSize()
|
||||
{
|
||||
return (m_kMax - m_kMin)*0.5;
|
||||
}
|
||||
|
||||
void setValues(float* value);
|
||||
|
||||
public:
|
||||
|
||||
Vector3 m_kMin;
|
||||
Vector3 m_kMax;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif //__JCBoundingBox_H__
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,278 @@
|
||||
/**
|
||||
@file JCBoundingFrustum.cpp
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_10_31
|
||||
*/
|
||||
|
||||
#include "JCBoundingFrustum.h"
|
||||
#include <stddef.h>
|
||||
|
||||
namespace laya
|
||||
{
|
||||
JCBoundingFrustum::JCBoundingFrustum()
|
||||
{
|
||||
}
|
||||
JCBoundingFrustum::~JCBoundingFrustum()
|
||||
{
|
||||
}
|
||||
JCPlane* JCBoundingFrustum::getPlane(int index)
|
||||
{
|
||||
switch (index) {
|
||||
|
||||
case 0:
|
||||
return &m_kNear;
|
||||
case 1:
|
||||
return &m_kFar;
|
||||
case 2:
|
||||
return &m_kLeft;
|
||||
case 3:
|
||||
return &m_kRight;
|
||||
case 4:
|
||||
return &m_kTop;
|
||||
case 5:
|
||||
return &m_kBottom;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
Vector3 JCBoundingFrustum::get3PlaneInterPoint(JCPlane* p1, JCPlane* p2, JCPlane* p3)
|
||||
{
|
||||
Vector3& p1Nor = p1->m_kNormal;
|
||||
Vector3& p2Nor = p2->m_kNormal;
|
||||
Vector3& p3Nor = p3->m_kNormal;
|
||||
|
||||
Vector3 temp1 = p2Nor.cross(p3Nor);
|
||||
Vector3 temp2 = p3Nor.cross(p1Nor);
|
||||
Vector3 temp3 = p1Nor.cross(p2Nor);
|
||||
|
||||
float a = p1Nor.dot(temp1);
|
||||
float b = p2Nor.dot(temp2);
|
||||
float c = p3Nor.dot(temp3);
|
||||
|
||||
temp1 *= -p1->m_nDistance / a;
|
||||
temp2 *= -p2->m_nDistance / b;
|
||||
temp3 *= -p3->m_nDistance / c;
|
||||
|
||||
return temp1 + temp2 + temp3;
|
||||
}
|
||||
void JCBoundingFrustum::getCorners(std::vector<Vector3>& pOutCorners)
|
||||
{
|
||||
pOutCorners.resize(8);
|
||||
pOutCorners[0] = get3PlaneInterPoint(&m_kNear, &m_kBottom, &m_kRight);
|
||||
pOutCorners[1] = get3PlaneInterPoint(&m_kNear, &m_kTop, &m_kRight);
|
||||
pOutCorners[2] = get3PlaneInterPoint(&m_kNear, &m_kTop, &m_kLeft);
|
||||
pOutCorners[3] = get3PlaneInterPoint(&m_kNear, &m_kBottom, &m_kLeft);
|
||||
pOutCorners[4] = get3PlaneInterPoint(&m_kFar, &m_kBottom, &m_kRight);
|
||||
pOutCorners[5] = get3PlaneInterPoint(&m_kFar, &m_kTop, &m_kRight);
|
||||
pOutCorners[6] = get3PlaneInterPoint(&m_kFar, &m_kTop, &m_kLeft);
|
||||
pOutCorners[7] = get3PlaneInterPoint(&m_kFar, &m_kBottom, &m_kLeft);
|
||||
}
|
||||
CONTAINMENT_TYPE JCBoundingFrustum::containsAxisAlignedBouningBox(JCBoundingBox& box)
|
||||
{
|
||||
static const int NUM_PLANES = 6;
|
||||
const JCPlane* pPlanes[NUM_PLANES] = { &m_kNear, &m_kLeft, &m_kRight, &m_kBottom, &m_kTop, &m_kFar };
|
||||
|
||||
Vector3 p, n;
|
||||
Vector3 boxMin = box.m_kMin;
|
||||
Vector3 boxMax = box.m_kMax;
|
||||
|
||||
for (int i = 0; i < NUM_PLANES; i++)
|
||||
{
|
||||
JCPlane* pPlane = (JCPlane*)(pPlanes[i]);
|
||||
|
||||
const Vector3& planeNor = pPlane->m_kNormal;
|
||||
|
||||
if (planeNor.x >= 0) {
|
||||
p.x = boxMax.x;
|
||||
n.x = boxMin.x;
|
||||
}
|
||||
else {
|
||||
p.x = boxMin.x;
|
||||
n.x = boxMax.x;
|
||||
}
|
||||
if (planeNor.y >= 0) {
|
||||
p.y = boxMax.y;
|
||||
n.y = boxMin.y;
|
||||
}
|
||||
else {
|
||||
p.y = boxMin.y;
|
||||
n.y = boxMax.y;
|
||||
}
|
||||
if (planeNor.z >= 0) {
|
||||
p.z = boxMax.z;
|
||||
n.z = boxMin.z;
|
||||
}
|
||||
else {
|
||||
p.z = boxMin.z;
|
||||
n.z = boxMax.z;
|
||||
}
|
||||
|
||||
if (pPlane->intersectsPoint(p) == PIT_BACK)
|
||||
return CT_DISJOINT;
|
||||
|
||||
if (pPlane->intersectsPoint(n) == PIT_BACK)
|
||||
return CT_INTERSECTS;
|
||||
}
|
||||
|
||||
return CT_CONTAINS;
|
||||
}
|
||||
CONTAINMENT_TYPE JCBoundingFrustum::containsBoundingBox(JCBoundingBox& box)
|
||||
{
|
||||
static const int NUM_PLANES = 6;
|
||||
const JCPlane* pPlanes[NUM_PLANES] = { &m_kNear, &m_kLeft, &m_kRight, &m_kBottom, &m_kTop, &m_kFar };
|
||||
Vector3 kBoxCorners[8];
|
||||
box.getCorners(kBoxCorners);
|
||||
int nTotalIn = 0;
|
||||
for (int p = 0; p < NUM_PLANES; p++)
|
||||
{
|
||||
int nInCount = 8;
|
||||
int nPointIn = 1;
|
||||
JCPlane* pPlane = (JCPlane*)(pPlanes[p]);
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
if (pPlane->intersectsPoint(kBoxCorners[i]) == PIT_BACK)
|
||||
{
|
||||
nPointIn=0;
|
||||
nInCount--;
|
||||
}
|
||||
}
|
||||
if(nInCount == 0)
|
||||
return CT_DISJOINT;
|
||||
nTotalIn += nPointIn;
|
||||
}
|
||||
if (nTotalIn == 6)
|
||||
return CT_CONTAINS;
|
||||
return CT_INTERSECTS;
|
||||
}
|
||||
CONTAINMENT_TYPE JCBoundingFrustum::containsBoundingSphere(JCBoundingSphere& sphere)
|
||||
{
|
||||
PLANE_INTERSECTION_TYPE result = PIT_FRONT;
|
||||
PLANE_INTERSECTION_TYPE planeResult = PIT_FRONT;
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
planeResult = m_kNear.intersectsSphere(sphere);
|
||||
break;
|
||||
case 1:
|
||||
planeResult = m_kFar.intersectsSphere(sphere);
|
||||
break;
|
||||
case 2:
|
||||
planeResult = m_kLeft.intersectsSphere(sphere);
|
||||
break;
|
||||
case 3:
|
||||
planeResult = m_kRight.intersectsSphere(sphere);
|
||||
break;
|
||||
case 4:
|
||||
planeResult = m_kTop.intersectsSphere(sphere);
|
||||
break;
|
||||
case 5:
|
||||
planeResult = m_kBottom.intersectsSphere(sphere);
|
||||
break;
|
||||
}
|
||||
switch (planeResult)
|
||||
{
|
||||
case PIT_BACK:
|
||||
return CT_DISJOINT;
|
||||
case PIT_INTERSECTING:
|
||||
result = PIT_INTERSECTING;
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch (result) {
|
||||
case PIT_INTERSECTING:
|
||||
return CT_INTERSECTS;
|
||||
default:
|
||||
return CT_CONTAINS;
|
||||
}
|
||||
}
|
||||
|
||||
void JCBoundingFrustum::getDebugLinePoint(Vector3* kOutPoints)
|
||||
{
|
||||
std::vector<Vector3> kOutCorners;
|
||||
getCorners(kOutCorners);
|
||||
kOutPoints[0] = kOutCorners[0];
|
||||
kOutPoints[1] = kOutCorners[1];
|
||||
kOutPoints[2] = kOutCorners[1];
|
||||
kOutPoints[3] = kOutCorners[2];
|
||||
kOutPoints[4] = kOutCorners[2];
|
||||
kOutPoints[5] = kOutCorners[3];
|
||||
kOutPoints[6] = kOutCorners[3];
|
||||
kOutPoints[7] = kOutCorners[0];
|
||||
kOutPoints[8] = kOutCorners[4];
|
||||
kOutPoints[9] = kOutCorners[5];
|
||||
kOutPoints[10] = kOutCorners[5];
|
||||
kOutPoints[11] = kOutCorners[6];
|
||||
kOutPoints[12] = kOutCorners[6];
|
||||
kOutPoints[13] = kOutCorners[7];
|
||||
kOutPoints[14] = kOutCorners[7];
|
||||
kOutPoints[15] = kOutCorners[4];
|
||||
kOutPoints[16] = kOutCorners[4];
|
||||
kOutPoints[17] = kOutCorners[0];
|
||||
kOutPoints[18] = kOutCorners[7];
|
||||
kOutPoints[19] = kOutCorners[3];
|
||||
kOutPoints[20] = kOutCorners[5];
|
||||
kOutPoints[21] = kOutCorners[1];
|
||||
kOutPoints[22] = kOutCorners[6];
|
||||
kOutPoints[23] = kOutCorners[2];
|
||||
}
|
||||
void JCBoundingFrustum::setValues(float* value)
|
||||
{
|
||||
Vector3* pNormal = &m_kNear.m_kNormal;
|
||||
pNormal->x = value[0]; pNormal->y = value[1]; pNormal->z = value[2]; m_kNear.m_nDistance = value[3];
|
||||
pNormal = &m_kFar.m_kNormal;
|
||||
pNormal->x = value[4]; pNormal->y = value[5]; pNormal->z = value[6]; m_kFar.m_nDistance = value[7];
|
||||
pNormal = &m_kLeft.m_kNormal;
|
||||
pNormal->x = value[8]; pNormal->y = value[9]; pNormal->z = value[10]; m_kLeft.m_nDistance = value[11];
|
||||
pNormal = &m_kRight.m_kNormal;
|
||||
pNormal->x = value[12]; pNormal->y = value[13]; pNormal->z = value[14]; m_kRight.m_nDistance = value[15];
|
||||
pNormal = &m_kTop.m_kNormal;
|
||||
pNormal->x = value[16]; pNormal->y = value[17]; pNormal->z = value[18]; m_kTop.m_nDistance = value[19];
|
||||
pNormal = &m_kBottom.m_kNormal;
|
||||
pNormal->x = value[20]; pNormal->y = value[21]; pNormal->z = value[22]; m_kBottom.m_nDistance = value[23];
|
||||
}
|
||||
bool JCBoundingFrustum::intersects(const JCBoundingBox& box)
|
||||
{
|
||||
const Vector3& min = box.m_kMin;
|
||||
const Vector3& max = box.m_kMax;
|
||||
float minX = min.x;
|
||||
float minY = min.y;
|
||||
float minZ = min.z;
|
||||
float maxX = max.x;
|
||||
float maxY = max.y;
|
||||
float maxZ = max.z;
|
||||
|
||||
Vector3 nearNormal = this->m_kNear.m_kNormal;
|
||||
if (this->m_kNear.m_nDistance + (nearNormal.x * (nearNormal.x < 0 ? minX : maxX)) + (nearNormal.y * (nearNormal.y < 0 ? minY : maxY)) + (nearNormal.z * (nearNormal.z < 0 ? minZ : maxZ)) < 0)
|
||||
return false;
|
||||
|
||||
Vector3 leftNormal = this->m_kLeft.m_kNormal;
|
||||
if (this->m_kLeft.m_nDistance + (leftNormal.x * (leftNormal.x < 0 ? minX : maxX)) + (leftNormal.y * (leftNormal.y < 0 ? minY : maxY)) + (leftNormal.z * (leftNormal.z < 0 ? minZ : maxZ)) < 0)
|
||||
return false;
|
||||
|
||||
Vector3 rightNormal = this->m_kRight.m_kNormal;
|
||||
if (this->m_kRight.m_nDistance + (rightNormal.x * (rightNormal.x < 0 ? minX : maxX)) + (rightNormal.y * (rightNormal.y < 0 ? minY : maxY)) + (rightNormal.z * (rightNormal.z < 0 ? minZ : maxZ)) < 0)
|
||||
return false;
|
||||
|
||||
Vector3 bottomNormal = this->m_kBottom.m_kNormal;
|
||||
if (this->m_kBottom.m_nDistance + (bottomNormal.x * (bottomNormal.x < 0 ? minX : maxX)) + (bottomNormal.y * (bottomNormal.y < 0 ? minY : maxY)) + (bottomNormal.z * (bottomNormal.z < 0 ? minZ : maxZ)) < 0)
|
||||
return false;
|
||||
|
||||
Vector3 topNormal = this->m_kTop.m_kNormal;
|
||||
if (this->m_kTop.m_nDistance + (topNormal.x * (topNormal.x < 0 ? minX : maxX)) + (topNormal.y * (topNormal.y < 0 ? minY : maxY)) + (topNormal.z * (topNormal.z < 0 ? minZ : maxZ)) < 0)
|
||||
return false;
|
||||
|
||||
// Can ignore far plane when distant object culling is handled by another mechanism
|
||||
Vector3 farNormal = this->m_kFar.m_kNormal;
|
||||
if (this->m_kFar.m_nDistance + (farNormal.x * (farNormal.x < 0 ? minX : maxX)) + (farNormal.y * (farNormal.y < 0 ? minY : maxY)) + (farNormal.z * (farNormal.z < 0 ? minZ : maxZ)) < 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,67 @@
|
||||
|
||||
/**
|
||||
@file JCBoundingFrustum.h
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_10_31
|
||||
*/
|
||||
|
||||
#ifndef __JCBoundingFrustum_H__
|
||||
#define __JCBoundingFrustum_H__
|
||||
|
||||
#include "JCBoundingBox.h"
|
||||
#include "JCBoundingSphere.h"
|
||||
#include "JCPlane.h"
|
||||
#include "Vector3.h"
|
||||
|
||||
namespace laya
|
||||
{
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
class JCBoundingFrustum
|
||||
{
|
||||
public:
|
||||
|
||||
/** @brief构造函数
|
||||
*/
|
||||
JCBoundingFrustum();
|
||||
|
||||
/** @brief析构函数
|
||||
*/
|
||||
~JCBoundingFrustum();
|
||||
|
||||
JCPlane* getPlane(int index);
|
||||
|
||||
Vector3 get3PlaneInterPoint( JCPlane* p1, JCPlane* p2, JCPlane* p3 );
|
||||
|
||||
void getCorners(std::vector<Vector3>& pOutCorners);
|
||||
|
||||
CONTAINMENT_TYPE containsAxisAlignedBouningBox(JCBoundingBox& box);
|
||||
|
||||
CONTAINMENT_TYPE containsBoundingBox(JCBoundingBox& box );
|
||||
|
||||
CONTAINMENT_TYPE containsBoundingSphere(JCBoundingSphere& sphere);
|
||||
|
||||
void getDebugLinePoint(Vector3* kOutPoints);
|
||||
|
||||
void setValues(float* value);
|
||||
|
||||
bool intersects(const JCBoundingBox& box);
|
||||
|
||||
public:
|
||||
|
||||
JCPlane m_kNear;
|
||||
JCPlane m_kFar;
|
||||
JCPlane m_kLeft;
|
||||
JCPlane m_kRight;
|
||||
JCPlane m_kTop;
|
||||
JCPlane m_kBottom;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif //__JCBoundingFrustum_H__
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
@file JCBoundingSphere.cpp
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_10_31
|
||||
*/
|
||||
|
||||
#include "JCBoundingSphere.h"
|
||||
|
||||
namespace laya
|
||||
{
|
||||
JCBoundingSphere::JCBoundingSphere()
|
||||
{
|
||||
m_nRadius = 0;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
JCBoundingSphere::JCBoundingSphere(const Vector3& center, float nRadius)
|
||||
{
|
||||
m_kCenter = center;
|
||||
m_nRadius = nRadius;
|
||||
}
|
||||
void JCBoundingSphere::setValues(float* value)
|
||||
{
|
||||
m_kCenter.x = value[0];
|
||||
m_kCenter.y = value[1];
|
||||
m_kCenter.z = value[2];
|
||||
m_nRadius = value[3];
|
||||
}
|
||||
JCBoundingSphere::~JCBoundingSphere()
|
||||
{
|
||||
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
@file JCBoundingSphere.h
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_10_31
|
||||
*/
|
||||
|
||||
#ifndef __JCBoundingSphere_H__
|
||||
#define __JCBoundingSphere_H__
|
||||
|
||||
#include <vector>
|
||||
#include "Vector3.h"
|
||||
|
||||
namespace laya
|
||||
{
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
class JCBoundingSphere
|
||||
{
|
||||
public:
|
||||
|
||||
JCBoundingSphere();
|
||||
|
||||
/** @brief构造函数
|
||||
*/
|
||||
JCBoundingSphere( const Vector3& center,float nRadius );
|
||||
|
||||
/** @brief析构函数
|
||||
*/
|
||||
~JCBoundingSphere();
|
||||
|
||||
void setValues(float* value);
|
||||
|
||||
public:
|
||||
|
||||
Vector3 m_kCenter;
|
||||
float m_nRadius;
|
||||
|
||||
};
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#endif //__JCBoundingSphere_H__
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
@file JCPlane.cpp
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_10_31
|
||||
*/
|
||||
|
||||
#include "JCPlane.h"
|
||||
#include "JCBoundingSphere.h"
|
||||
#include <math.h>
|
||||
|
||||
namespace laya
|
||||
{
|
||||
JCPlane::JCPlane()
|
||||
{
|
||||
m_nDistance = 0;
|
||||
}
|
||||
JCPlane::JCPlane(const Vector3& normal, float nDistance)
|
||||
{
|
||||
m_kNormal = normal;
|
||||
m_nDistance = nDistance;
|
||||
}
|
||||
JCPlane::JCPlane(const Vector3& point1, const Vector3& point2, const Vector3& point3)
|
||||
{
|
||||
float x1 = point2.x - point1.x;
|
||||
float y1 = point2.y - point1.y;
|
||||
float z1 = point2.z - point1.z;
|
||||
float x2 = point3.x - point1.x;
|
||||
float y2 = point3.y - point1.y;
|
||||
float z2 = point3.z - point1.z;
|
||||
float yz = (y1 * z2) - (z1 * y2);
|
||||
float xz = (z1 * x2) - (x1 * z2);
|
||||
float xy = (x1 * y2) - (y1 * x2);
|
||||
float invPyth = 1 / (sqrtf((yz * yz) + (xz * xz) + (xy * xy)));
|
||||
float x = yz * invPyth;
|
||||
float y = xz * invPyth;
|
||||
float z = xy * invPyth;
|
||||
m_kNormal.x = x;
|
||||
m_kNormal.y = y;
|
||||
m_kNormal.z = z;
|
||||
m_nDistance = -((x * point1.x) + (y * point1.y) + (z * point1.z));
|
||||
}
|
||||
JCPlane::~JCPlane()
|
||||
{
|
||||
|
||||
}
|
||||
void JCPlane::normalize()
|
||||
{
|
||||
float fMagnitude = 1 / sqrtf(m_kNormal.x * m_kNormal.x + m_kNormal.y * m_kNormal.y + m_kNormal.z * m_kNormal.z);
|
||||
m_kNormal.x *= fMagnitude;
|
||||
m_kNormal.y *= fMagnitude;
|
||||
m_kNormal.z *= fMagnitude;
|
||||
m_nDistance *= fMagnitude;
|
||||
}
|
||||
PLANE_INTERSECTION_TYPE JCPlane::intersectsPoint(const Vector3& point)
|
||||
{
|
||||
float distance = m_kNormal.dot(point) + m_nDistance;
|
||||
if (distance > 0)
|
||||
return PIT_FRONT;
|
||||
else if (distance < 0)
|
||||
return PIT_BACK;
|
||||
else
|
||||
return PIT_INTERSECTING;
|
||||
}
|
||||
PLANE_INTERSECTION_TYPE JCPlane::intersectsSphere( const JCBoundingSphere& sphere)
|
||||
{
|
||||
float sphereR = sphere.m_nRadius;
|
||||
float distance = m_kNormal.dot(sphere.m_kCenter) + m_nDistance;
|
||||
if (distance > sphereR)
|
||||
return PIT_FRONT;
|
||||
else if (distance < -sphereR)
|
||||
return PIT_BACK;
|
||||
else
|
||||
return PIT_INTERSECTING;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,75 @@
|
||||
|
||||
|
||||
/**
|
||||
@file JCPlane.h
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_10_31
|
||||
*/
|
||||
|
||||
#ifndef __JCPlane_H__
|
||||
#define __JCPlane_H__
|
||||
|
||||
#include "JCBoundingSphere.h"
|
||||
#include "Vector3.h"
|
||||
|
||||
namespace laya
|
||||
{
|
||||
enum PLANE_INTERSECTION_TYPE
|
||||
{
|
||||
PIT_BACK = 0,
|
||||
PIT_FRONT,
|
||||
PIT_INTERSECTING,
|
||||
};
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
class JCPlane
|
||||
{
|
||||
public:
|
||||
|
||||
/** @brief构造函数
|
||||
*/
|
||||
JCPlane();
|
||||
|
||||
/** @brief构造函数
|
||||
*/
|
||||
JCPlane( const Vector3& normal,float nDistance);
|
||||
|
||||
/** @brief构造函数
|
||||
*/
|
||||
JCPlane(const Vector3& point1, const Vector3& point2, const Vector3& point3);
|
||||
|
||||
/** @brief析构函数
|
||||
*/
|
||||
~JCPlane();
|
||||
|
||||
|
||||
/** @brief normallize
|
||||
* @return
|
||||
*/
|
||||
void normalize();
|
||||
|
||||
/** @brief 点和平面的关系
|
||||
* @param[in] 点的信息
|
||||
* @return
|
||||
*/
|
||||
PLANE_INTERSECTION_TYPE intersectsPoint( const Vector3& point );
|
||||
|
||||
/** @brief 点和包围球的关系
|
||||
* @param[in] 包围球
|
||||
* @return
|
||||
*/
|
||||
PLANE_INTERSECTION_TYPE intersectsSphere(const JCBoundingSphere& sphere);
|
||||
|
||||
public:
|
||||
Vector3 m_kNormal;
|
||||
float m_nDistance;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif //__JCPlane_H__
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,91 @@
|
||||
#ifndef __Vector3__H__
|
||||
#define __Vector3__H__
|
||||
|
||||
class Vector3
|
||||
{
|
||||
public:
|
||||
|
||||
Vector3() : x(0.0f),y(0.0f),z(0.0f)
|
||||
{}
|
||||
|
||||
Vector3(float x, float y, float z) : x(x), y(y),z(z)
|
||||
{}
|
||||
|
||||
friend Vector3 operator*(Vector3 const & v, float scalar)
|
||||
{
|
||||
return Vector3(v.x * scalar, v.y * scalar, v.z * scalar);
|
||||
}
|
||||
|
||||
friend Vector3 operator*(float scalar, Vector3 const & v)
|
||||
{
|
||||
return Vector3(scalar * v.x, scalar * v.y,scalar * v.z);
|
||||
}
|
||||
|
||||
Vector3& operator*=(float scalar)
|
||||
{
|
||||
this->x *= scalar;
|
||||
this->y *= scalar;
|
||||
this->z *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3& operator+=(const Vector3& b)
|
||||
{
|
||||
x += b.x;
|
||||
y += b.y;
|
||||
z += b.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3& operator-=(const Vector3& b)
|
||||
{
|
||||
x -= b.x;
|
||||
y -= b.y;
|
||||
z -= b.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 operator+(const Vector3& b) const
|
||||
{
|
||||
Vector3 ret = *this;
|
||||
ret += b;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Vector3 operator-(const Vector3& b) const
|
||||
{
|
||||
Vector3 ret = *this;
|
||||
ret -= b;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool operator<=(Vector3 const & v)
|
||||
{
|
||||
return x <= v.x && y <= v.y && z <= v.z;
|
||||
}
|
||||
|
||||
bool operator>=(Vector3 const & v)
|
||||
{
|
||||
return x >= v.x && y >= v.y && z >= v.z;
|
||||
}
|
||||
|
||||
Vector3 cross(const Vector3& rkVector) const
|
||||
{
|
||||
return Vector3(
|
||||
y * rkVector.z - z * rkVector.y,
|
||||
z * rkVector.x - x * rkVector.z,
|
||||
x * rkVector.y - y * rkVector.x);
|
||||
}
|
||||
|
||||
float dot(const Vector3& vec) const
|
||||
{
|
||||
return x * vec.x + y * vec.y + z * vec.z;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user