Frustum.cpp
资源名称:QuakeBsp.rar [点击查看]
上传用户:cxh8989
上传日期:2021-01-22
资源大小:2544k
文件大小:9k
源码类别:
射击游戏
开发平台:
Visual C++
- #include "Frustum.h"
- #define FIRST_OBJECT_ID 3
- enum FrustumSide
- {
- RIGHT = 0, // 右平面
- LEFT = 1, // 左平面
- BOTTOM = 2, // 底平面
- TOP = 3, // 顶平面
- BACK = 4, // 后平面
- FRONT = 5 // 前平面
- };
- enum PlaneData
- {
- A = 0, //(A,B,C)分别为平面法向量
- B = 1,
- C = 2,
- D = 3
- };
- void NormalizePlane(float frustum[6][4], int side)
- {
- float magnitude = (float)sqrt( frustum[side][A] * frustum[side][A] +
- frustum[side][B] * frustum[side][B] +
- frustum[side][C] * frustum[side][C] );
- frustum[side][A] /= magnitude;
- frustum[side][B] /= magnitude;
- frustum[side][C] /= magnitude;
- frustum[side][D] /= magnitude;
- }
- /** 从模型矩阵和投影矩阵计算平头体 */
- void CFrustum::CalculateFrustum()
- {
- float proj[16];
- float modl[16];
- float clip[16];
- /** 获得当前模型矩阵和投影矩阵 */
- glGetFloatv( GL_PROJECTION_MATRIX, proj );
- glGetFloatv( GL_MODELVIEW_MATRIX, modl );
- clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
- clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
- clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14];
- clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15];
- clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] + modl[ 6] * proj[ 8] + modl[ 7] * proj[12];
- clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + modl[ 6] * proj[ 9] + modl[ 7] * proj[13];
- clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] + modl[ 6] * proj[10] + modl[ 7] * proj[14];
- clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + modl[ 6] * proj[11] + modl[ 7] * proj[15];
- clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + modl[10] * proj[ 8] + modl[11] * proj[12];
- clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + modl[10] * proj[ 9] + modl[11] * proj[13];
- clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + modl[10] * proj[10] + modl[11] * proj[14];
- clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + modl[10] * proj[11] + modl[11] * proj[15];
- clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + modl[14] * proj[ 8] + modl[15] * proj[12];
- clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] + modl[14] * proj[ 9] + modl[15] * proj[13];
- clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + modl[14] * proj[10] + modl[15] * proj[14];
- clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + modl[14] * proj[11] + modl[15] * proj[15];
- m_Frustum[RIGHT][A] = clip[ 3] - clip[ 0];
- m_Frustum[RIGHT][B] = clip[ 7] - clip[ 4];
- m_Frustum[RIGHT][C] = clip[11] - clip[ 8];
- m_Frustum[RIGHT][D] = clip[15] - clip[12];
- NormalizePlane(m_Frustum, RIGHT);
- /** 左平面 */
- m_Frustum[LEFT][A] = clip[ 3] + clip[ 0];
- m_Frustum[LEFT][B] = clip[ 7] + clip[ 4];
- m_Frustum[LEFT][C] = clip[11] + clip[ 8];
- m_Frustum[LEFT][D] = clip[15] + clip[12];
- NormalizePlane(m_Frustum, LEFT);
- /** 底平面 */
- m_Frustum[BOTTOM][A] = clip[ 3] + clip[ 1];
- m_Frustum[BOTTOM][B] = clip[ 7] + clip[ 5];
- m_Frustum[BOTTOM][C] = clip[11] + clip[ 9];
- m_Frustum[BOTTOM][D] = clip[15] + clip[13];
- NormalizePlane(m_Frustum, BOTTOM);
- /** 顶平面 */
- m_Frustum[TOP][A] = clip[ 3] - clip[ 1];
- m_Frustum[TOP][B] = clip[ 7] - clip[ 5];
- m_Frustum[TOP][C] = clip[11] - clip[ 9];
- m_Frustum[TOP][D] = clip[15] - clip[13];
- NormalizePlane(m_Frustum, TOP);
- /** 后平面 */
- m_Frustum[BACK][A] = clip[ 3] - clip[ 2];
- m_Frustum[BACK][B] = clip[ 7] - clip[ 6];
- m_Frustum[BACK][C] = clip[11] - clip[10];
- m_Frustum[BACK][D] = clip[15] - clip[14];
- NormalizePlane(m_Frustum, BACK);
- /** 前平面 */
- m_Frustum[FRONT][A] = clip[ 3] + clip[ 2];
- m_Frustum[FRONT][B] = clip[ 7] + clip[ 6];
- m_Frustum[FRONT][C] = clip[11] + clip[10];
- m_Frustum[FRONT][D] = clip[15] + clip[14];
- NormalizePlane(m_Frustum, FRONT);
- }
- bool CFrustum::PointInFrustum( float x, float y, float z )
- {
- for(int i = 0; i < 6; i++ )
- {
- if(m_Frustum[i][A] * x + m_Frustum[i][B] * y + m_Frustum[i][C] * z + m_Frustum[i][D] <= 0)
- {
- return false;
- }
- }
- return true;
- }
- bool CFrustum::SphereInFrustum( float x, float y, float z, float radius )
- {
- for(int i = 0; i < 6; i++ )
- {
- if( m_Frustum[i][A] * x + m_Frustum[i][B] * y + m_Frustum[i][C] * z + m_Frustum[i][D] <= -radius )
- {
- return false;
- }
- }
- return true;
- }
- bool CFrustum::CubeInFrustum( float x, float y, float z, float size )
- {
- for(int i = 0; i < 6; i++ )
- {
- if(m_Frustum[i][A] * (x - size) + m_Frustum[i][B] * (y - size) + m_Frustum[i][C] * (z - size) + m_Frustum[i][D] > 0)
- continue;
- if(m_Frustum[i][A] * (x + size) + m_Frustum[i][B] * (y - size) + m_Frustum[i][C] * (z - size) + m_Frustum[i][D] > 0)
- continue;
- if(m_Frustum[i][A] * (x - size) + m_Frustum[i][B] * (y + size) + m_Frustum[i][C] * (z - size) + m_Frustum[i][D] > 0)
- continue;
- if(m_Frustum[i][A] * (x + size) + m_Frustum[i][B] * (y + size) + m_Frustum[i][C] * (z - size) + m_Frustum[i][D] > 0)
- continue;
- if(m_Frustum[i][A] * (x - size) + m_Frustum[i][B] * (y - size) + m_Frustum[i][C] * (z + size) + m_Frustum[i][D] > 0)
- continue;
- if(m_Frustum[i][A] * (x + size) + m_Frustum[i][B] * (y - size) + m_Frustum[i][C] * (z + size) + m_Frustum[i][D] > 0)
- continue;
- if(m_Frustum[i][A] * (x - size) + m_Frustum[i][B] * (y + size) + m_Frustum[i][C] * (z + size) + m_Frustum[i][D] > 0)
- continue;
- if(m_Frustum[i][A] * (x + size) + m_Frustum[i][B] * (y + size) + m_Frustum[i][C] * (z + size) + m_Frustum[i][D] > 0)
- continue;
- return false;
- }
- return true;
- }
- bool CFrustum::BoxInFrustum( float x, float y, float z, float x2, float y2, float z2)
- {
- for(int i = 0; i < 6; i++ )
- {
- if(m_Frustum[i][A] * x + m_Frustum[i][B] * y + m_Frustum[i][C] * z + m_Frustum[i][D] > 0) continue;
- if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y + m_Frustum[i][C] * z + m_Frustum[i][D] > 0) continue;
- if(m_Frustum[i][A] * x + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z + m_Frustum[i][D] > 0) continue;
- if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z + m_Frustum[i][D] > 0) continue;
- if(m_Frustum[i][A] * x + m_Frustum[i][B] * y + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0) continue;
- if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0) continue;
- if(m_Frustum[i][A] * x + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0) continue;
- if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0) continue;
- return false;
- }
- return true;
- }
- void CDebug::RenderDebugLines()
- {
- glDisable(GL_TEXTURE_2D);
- glBegin(GL_LINES);
- glColor3ub(255, 255, 0);
- for(unsigned int i = 0; i < m_vLines.size(); i++)
- {
- glVertex3f(m_vLines[i].x, m_vLines[i].y, m_vLines[i].z);
- }
- glEnd();
- }
- void CDebug::AddDebugLine(CVector3 vPoint1, CVector3 vPoint2)
- {
- m_vLines.push_back(vPoint1);
- m_vLines.push_back(vPoint2);
- }
- void CDebug::AddDebugBox(CVector3 vCenter, float width, float height, float depth)
- {
- width /= 2.0f; height /= 2.0f; depth /= 2.0f;
- CVector3 vTopLeftFront( vCenter.x - width, vCenter.y + height, vCenter.z + depth);
- CVector3 vTopLeftBack( vCenter.x - width, vCenter.y + height, vCenter.z - depth);
- CVector3 vTopRightBack( vCenter.x + width, vCenter.y + height, vCenter.z - depth);
- CVector3 vTopRightFront(vCenter.x + width, vCenter.y + height, vCenter.z + depth);
- CVector3 vBottom_LeftFront( vCenter.x - width, vCenter.y - height, vCenter.z + depth);
- CVector3 vBottom_LeftBack( vCenter.x - width, vCenter.y - height, vCenter.z - depth);
- CVector3 vBottomRightBack( vCenter.x + width, vCenter.y - height, vCenter.z - depth);
- CVector3 vBottomRightFront(vCenter.x + width, vCenter.y - height, vCenter.z + depth);
- m_vLines.push_back(vTopLeftFront); m_vLines.push_back(vTopRightFront);
- m_vLines.push_back(vTopLeftBack); m_vLines.push_back(vTopRightBack);
- m_vLines.push_back(vTopLeftFront); m_vLines.push_back(vTopLeftBack);
- m_vLines.push_back(vTopRightFront); m_vLines.push_back(vTopRightBack);
- m_vLines.push_back(vBottom_LeftFront); m_vLines.push_back(vBottomRightFront);
- m_vLines.push_back(vBottom_LeftBack); m_vLines.push_back(vBottomRightBack);
- m_vLines.push_back(vBottom_LeftFront); m_vLines.push_back(vBottom_LeftBack);
- m_vLines.push_back(vBottomRightFront); m_vLines.push_back(vBottomRightBack);
- m_vLines.push_back(vTopLeftFront); m_vLines.push_back(vBottom_LeftFront);
- m_vLines.push_back(vTopLeftBack); m_vLines.push_back(vBottom_LeftBack);
- m_vLines.push_back(vTopRightBack); m_vLines.push_back(vBottomRightBack);
- m_vLines.push_back(vTopRightFront); m_vLines.push_back(vBottomRightFront);
- }