1
0
forked from josh/j3ml

Implement CreateFrustumFromCamera

This commit is contained in:
2024-01-10 14:46:12 -05:00
parent cc9ff95daa
commit dea5735c87
7 changed files with 179 additions and 14 deletions

View File

@@ -3,12 +3,13 @@
#pragma once
namespace Geometry {
using Point2D = LinearAlgebra::Vector2;
using Vector2 = LinearAlgebra::Vector2;
using Vector3 = LinearAlgebra::Vector3;
class LineSegment2D
{
Point2D A;
Point2D B;
Vector2 A;
Vector2 B;
};
class Rectangle; //AABB2D;
@@ -29,7 +30,7 @@ namespace Geometry {
using Point3D = LinearAlgebra::Vector3;
// A 3D axis-aligned bounding box
// This data structure can be used to represent coarse bounds of objects, in situations where detailed triangle-level
@@ -44,18 +45,59 @@ namespace Geometry {
class Line;
class LineSegment
{
Point3D A;
Point3D B;
Vector3 A;
Vector3 B;
};
class Ray
{
Point3D Origin;
Point3D Direction;
Vector3 Origin;
Vector3 Direction;
};
class OBB;
class Frustum;
class Plane;
class Plane
{
public:
Vector3 Position;
Vector3 Normal;
float distance = 0.f;
};
class Frustum {
public:
Plane TopFace;
Plane BottomFace;
Plane RightFace;
Plane LeftFace;
Plane FarFace;
Plane NearFace;
};
class Camera {
public:
Vector3 Position;
Vector3 Front;
Vector3 Right;
Vector3 Up;
};
static Frustum CreateFrustumFromCamera(const Camera& cam, float aspect, float fovY, float zNear, float zFar)
{
Frustum frustum;
const float halfVSide = zFar * tanf(fovY * 0.5f);
const float halfHSide = halfVSide * aspect;
const Vector3 frontMultFar = cam.Front * zFar;
frustum.NearFace = Plane{cam.Position + cam.Front * zNear, cam.Front};
frustum.FarFace = Plane{cam.Position + frontMultFar, -cam.Front};
frustum.RightFace = Plane{cam.Position, Vector3::Cross(frontMultFar - cam.Right * halfHSide, cam.Up)};
frustum.LeftFace = Plane{cam.Position, Vector3::Cross(cam.Up, frontMultFar+cam.Right*halfHSide)};
frustum.TopFace = Plane{cam.Position, Vector3::Cross(cam.Right, frontMultFar - cam.Up * halfVSide)};
frustum.BottomFace = Plane{cam.Position, Vector3::Cross(frontMultFar + cam.Up * halfVSide, cam.Right)};
return frustum;
}
class Polygon;
class Polyhedron;
class QuadTree;