osci-render/Source/obj/Frustum.cpp

37 wiersze
1.1 KiB
C++
Czysty Zwykły widok Historia

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
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
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;
}
void Frustum::clipToFrustum(Vec3 &p) {
2024-01-21 22:22:03 +00:00
float pcz, pcx, pcy, aux;
// compute and test the Z coordinate
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
pcy = p.y;
aux = std::abs(pcz * tang);
pcy = pcy < -aux ? -aux : (pcy > aux ? aux : pcy);
2024-01-21 22:22:03 +00:00
// compute and test the X coordinate
pcx = p.x;
2024-01-21 22:22:03 +00:00
aux = aux * ratio;
pcx = pcx < -aux ? -aux : (pcx > aux ? aux : pcx);
2024-01-21 22:22:03 +00:00
p = Vec3(pcx, pcy, pcz);
2024-01-21 22:22:03 +00:00
}