Files
bluflame/demo09/src/demo09/Resources/Camera/Frustum.cpp
2026-04-18 22:31:51 +02:00

94 lines
1.6 KiB
C++

#include "stdafx.h"
#include "Frustum.h"
Frustum::Frustum()
{
}
Frustum::~Frustum()
{
}
void Frustum::SetCameraInternals(float angle, float ratio, float nearD, float farD)
{
this->ratio = ratio;
this->angle = angle;
this->nearD = nearD;
this->farD = farD;
tang = (float)tan(angle* ANG2RAD * 0.5);
nh = nearD * tang;
nw = nh * ratio;
fh = farD * tang;
fw = fh * ratio;
}
void Frustum::SetCameraDefinition(Vector &p, Vector &l, Vector &u)
{
Vector dir,nc,fc,X,Y,Z;
Z = p - l;
Z.normalize();
X = u * Z;
X.normalize();
Y = Z * X;
nc = p - Z * nearD;
fc = p - Z * farD;
ntl = nc + Y * nh - X * nw;
ntr = nc + Y * nh + X * nw;
nbl = nc - Y * nh - X * nw;
nbr = nc - Y * nh + X * nw;
ftl = fc + Y * fh - X * fw;
ftr = fc + Y * fh + X * fw;
fbl = fc - Y * fh - X * fw;
fbr = fc - Y * fh + X * fw;
planes[0] = Plane(ntr,ntl,ftl);
planes[1] = Plane(nbl,nbr,fbr);
planes[2] = Plane(ntl,nbl,fbl);
planes[3] = Plane(nbr,ntr,fbr);
planes[4] = Plane(ntl,ntr,nbr);
planes[5] = Plane(ftr,ftl,fbl);
}
ClipTestResult::Enumeration Frustum::TestPointInFrustum(Vector &p)
{
ClipTestResult::Enumeration result = ClipTestResult::Inside;
for(int i=0; i < 6; i++)
{
if (planes[i].GetDistanceToPoint(p) < 0)
return ClipTestResult::Outside;
}
return result;
}
ClipTestResult::Enumeration Frustum::TestSphereInFrustum(Vector &p, float raio)
{
ClipTestResult::Enumeration result = ClipTestResult::Inside;
float distance;
for(int i=0; i < 6; i++)
{
distance = planes[i].GetDistanceToPoint(p);
if (distance < -raio)
return ClipTestResult::Outside;
else if (distance < raio)
result = ClipTestResult::Intersect;
}
return result;
}