📄 frustum.cpp
字号:
{
// The distance was greater than the radius so the sphere is outside of the frustum
return false;
}
}
// The sphere was inside of the frustum!
return true;
}
///////////////////////////////// CUBE IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This determines if a cube is in or around our frustum by it's center and 1/2 it's length
/////
///////////////////////////////// CUBE IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
bool CFrustum::CubeInFrustum( float x, float y, float z, float size )
{
// Basically, what is going on is, that we are given the center of the cube,
// and half the length. Think of it like a radius. Then we checking each point
// in the cube and seeing if it is inside the frustum. If a point is found in front
// of a side, then we skip to the next side. If we get to a plane that does NOT have
// a point in front of it, then it will return false.
// *Note* - This will sometimes say that a cube is inside the frustum when it isn't.
// This happens when all the corners of the bounding box are not behind any one plane.
// This is rare and shouldn't effect the overall rendering speed.
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;
// If we get here, it isn't in the frustum
return false;
}
return true;
}
///////////////////////////////// BOX IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This determines if a BOX is in or around our frustum by it's min and max points
/////
///////////////////////////////// BOX IN FRUSTUM \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
bool CFrustum::BoxInFrustum( float x, float y, float z, float x2, float y2, float z2)
{
// Go through all of the corners of the box and check then again each plane
// in the frustum. If all of them are behind one of the planes, then it most
// like is not in the frustum.
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;
// If we get here, it isn't in the frustum
return false;
}
// Return a true for the box being inside of the frustum
return true;
}
///////////////////////////////// RENDER DEBUG LINES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This goes through all of the lines that we stored in our list and draws them
/////
///////////////////////////////// RENDER DEBUG LINES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CDebug::RenderDebugLines() // This renders all of the lines
{
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES); // Start rendering lines
glColor3ub(255, 255, 0); // Turn the lines yellow
// Go through the whole list of lines stored in the vector m_vLines.
for(unsigned int i = 0; i < m_vLines.size(); i++)
{
// Pass in the current point to be rendered as part of a line
glVertex3f(m_vLines[i].x, m_vLines[i].y, m_vLines[i].z);
}
glEnd();
}
///////////////////////////////// ADD DEBUG LINE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This adds a debug LINE to the stack of lines
/////
///////////////////////////////// ADD DEBUG LINE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CDebug::AddDebugLine(CVector3 vPoint1, CVector3 vPoint2)
{
// Add the 2 points that make up the line into our line list.
m_vLines.push_back(vPoint1);
m_vLines.push_back(vPoint2);
}
///////////////////////////////// ADD DEBUG BOX \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This adds a debug BOX to the stack of lines
/////
///////////////////////////////// ADD DEBUG BOX\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CDebug::AddDebugBox(CVector3 vCenter, float width, float height, float depth)
{
// So we can work with the code better, we divide the dimensions in half.
// That way we can create the box from the center outwards.
width /= 2.0f; height /= 2.0f; depth /= 2.0f;
// Below we create all the 8 points so it will be easier to input the lines
// of the box. With the dimensions we calculate the points.
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);
////////// TOP LINES //////////
// Store the top front line of the box
m_vLines.push_back(vTopLeftFront); m_vLines.push_back(vTopRightFront);
// Store the top back line of the box
m_vLines.push_back(vTopLeftBack); m_vLines.push_back(vTopRightBack);
// Store the top left line of the box
m_vLines.push_back(vTopLeftFront); m_vLines.push_back(vTopLeftBack);
// Store the top right line of the box
m_vLines.push_back(vTopRightFront); m_vLines.push_back(vTopRightBack);
////////// BOTTOM LINES //////////
// Store the bottom front line of the box
m_vLines.push_back(vBottom_LeftFront); m_vLines.push_back(vBottomRightFront);
// Store the bottom back line of the box
m_vLines.push_back(vBottom_LeftBack); m_vLines.push_back(vBottomRightBack);
// Store the bottom left line of the box
m_vLines.push_back(vBottom_LeftFront); m_vLines.push_back(vBottom_LeftBack);
// Store the bottom right line of the box
m_vLines.push_back(vBottomRightFront); m_vLines.push_back(vBottomRightBack);
////////// SIDE LINES //////////
// Store the bottom front line of the box
m_vLines.push_back(vTopLeftFront); m_vLines.push_back(vBottom_LeftFront);
// Store the back left line of the box
m_vLines.push_back(vTopLeftBack); m_vLines.push_back(vBottom_LeftBack);
// Store the front right line of the box
m_vLines.push_back(vTopRightBack); m_vLines.push_back(vBottomRightBack);
// Store the front left line of the box
m_vLines.push_back(vTopRightFront); m_vLines.push_back(vBottomRightFront);
}
/////////////////////////////////////////////////////////////////////////////////
//
// * QUICK NOTES *
//
// This code was taken directly from the frustum tutorial located at www.GameTutorials.com.
// Most of the large block of comments were taken out. If you want to learn more about
// this frustum code, visit our site. We added BoxInFrustum() to this file so that we
// could check against bounding boxes.
//
//
// Ben Humphrey
// Game Programmer
// DigiBen@GameTutorials.com
// www.GameTutorials.com
//
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -