2024-01-21 22:22:03 +00:00
|
|
|
// FROM https://cgvr.cs.uni-bremen.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
|
|
|
|
|
|
|
|
#include "Frustum.h"
|
|
|
|
|
|
|
|
void Frustum::setCameraInternals(float focalLength, float ratio, float nearDistance, float farDistance) {
|
|
|
|
// store the information
|
2024-02-11 22:06:35 +00:00
|
|
|
this->focalLength = focalLength;
|
2024-01-21 22:22:03 +00:00
|
|
|
this->ratio = ratio;
|
|
|
|
this->nearDistance = nearDistance;
|
|
|
|
this->farDistance = farDistance;
|
|
|
|
|
|
|
|
// compute width and height of the near section
|
2024-02-11 18:56:46 +00:00
|
|
|
float fov = 2 * std::atan(1 / focalLength);
|
2024-01-21 22:22:03 +00:00
|
|
|
tang = (float) std::tan(fov * 0.5);
|
|
|
|
height = nearDistance * tang;
|
|
|
|
width = height * ratio;
|
|
|
|
}
|
|
|
|
|
2024-02-11 22:06:35 +00:00
|
|
|
void Frustum::clipToFrustum(Vec3 &p) {
|
2024-01-21 22:22:03 +00:00
|
|
|
float pcz, pcx, pcy, aux;
|
|
|
|
|
|
|
|
// compute and test the Z coordinate
|
2024-02-11 22:06:35 +00:00
|
|
|
pcz = p.z;
|
|
|
|
pcz = pcz < nearDistance ? nearDistance : (pcz > farDistance ? farDistance : pcz);
|
2024-01-21 22:22:03 +00:00
|
|
|
|
|
|
|
// compute and test the Y coordinate
|
2024-02-11 22:06:35 +00:00
|
|
|
pcy = p.y;
|
2024-02-11 18:56:46 +00:00
|
|
|
aux = std::abs(pcz * tang);
|
2024-02-11 22:06:35 +00:00
|
|
|
pcy = pcy < -aux ? -aux : (pcy > aux ? aux : pcy);
|
2024-01-21 22:22:03 +00:00
|
|
|
|
|
|
|
// compute and test the X coordinate
|
2024-02-11 22:06:35 +00:00
|
|
|
pcx = p.x;
|
2024-01-21 22:22:03 +00:00
|
|
|
aux = aux * ratio;
|
2024-02-11 22:06:35 +00:00
|
|
|
pcx = pcx < -aux ? -aux : (pcx > aux ? aux : pcx);
|
2024-01-21 22:22:03 +00:00
|
|
|
|
2024-02-11 22:06:35 +00:00
|
|
|
p = Vec3(pcx, pcy, pcz);
|
2024-01-21 22:22:03 +00:00
|
|
|
}
|