54 lines
2.5 KiB
C++
54 lines
2.5 KiB
C++
//
|
|
// Created by dawsh on 1/25/24.
|
|
//
|
|
#pragma once
|
|
|
|
#include "Plane.h"
|
|
#include <J3ML/LinearAlgebra/CoordinateFrame.h>
|
|
|
|
namespace Geometry
|
|
{
|
|
|
|
enum class FrustumType
|
|
{
|
|
Invalid,
|
|
/// Set the Frustum type to this value to define the orthographic projection formula. In orthographic projection,
|
|
/// 3D images are projected onto a 2D plane essentially by flattening the object along one direction (the plane normal).
|
|
/// The size of the projected images appear the same independent of their distance to the camera, and distant objects will
|
|
/// not appear smaller. The shape of the Frustum is identical to an oriented bounding box (OBB).
|
|
Orthographic,
|
|
/// Set the Frustum type to this value to use the perspective projection formula. With perspective projection, the 2D
|
|
/// image is formed by projecting 3D points towards a single point (the eye point/tip) of the Frustum, and computing the
|
|
/// point of intersection of the line of the projection and the near plane of the Frustum.
|
|
/// This corresponds to the optics in the real-world, and objects become smaller as they move to the distance.
|
|
/// The shape of the Frustum is a rectangular pyramid capped from the tip.
|
|
Perspective
|
|
};
|
|
|
|
class Frustum {
|
|
public:
|
|
Plane TopFace;
|
|
Plane BottomFace;
|
|
Plane RightFace;
|
|
Plane LeftFace;
|
|
Plane FarFace;
|
|
Plane NearFace;
|
|
static Frustum CreateFrustumFromCamera(const CoordinateFrame& cam, float aspect, float fovY, float zNear, float zFar);
|
|
};
|
|
|
|
Frustum Frustum::CreateFrustumFromCamera(const CoordinateFrame &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;
|
|
}
|
|
} |