Elevate Engine 1
Loading...
Searching...
No Matches
Frustum.cpp
Go to the documentation of this file.
1#include "eepch.h"
2#include "Frustum.h"
3
6
7namespace Elevate
8{
10 {
11 float zFar = cam.GetFar();
12 float zNear = cam.GetNear();
13 float aspectRatio = cam.GetAspectRatio();
14 float fov = cam.GetFOV();
15
16 glm::vec3 front = cam.GetFrontVec();
17 glm::vec3 right = cam.GetRightVec();
18 glm::vec3 up = cam.GetUpVec();
19 glm::vec3 position = cam.gameObject->GetPosition();
20
21 float tanFovHalf = tan(fov / 2.0f);
22
23 float nearHeight = 2.0f * tanFovHalf * zNear;
24 float nearWidth = nearHeight * aspectRatio;
25 float farHeight = 2.0f * tanFovHalf * zFar;
26 float farWidth = farHeight * aspectRatio;
27
28 glm::vec3 nearCenter = position + front * zNear;
29 glm::vec3 farCenter = position + front * zFar;
30
31 const glm::vec3 nearTopLeft = nearCenter + up * (nearHeight * 0.5f) - right * (nearWidth * 0.5f);
32 const glm::vec3 nearTopRight = nearCenter + up * (nearHeight * 0.5f) + right * (nearWidth * 0.5f);
33 const glm::vec3 nearBottomLeft = nearCenter - up * (nearHeight * 0.5f) - right * (nearWidth * 0.5f);
34 const glm::vec3 nearBottomRight = nearCenter - up * (nearHeight * 0.5f) + right * (nearWidth * 0.5f);
35
36 const glm::vec3 farTopLeft = farCenter + up * (farHeight * 0.5f) - right * (farWidth * 0.5f);
37 const glm::vec3 farTopRight = farCenter + up * (farHeight * 0.5f) + right * (farWidth * 0.5f);
38 const glm::vec3 farBottomLeft = farCenter - up * (farHeight * 0.5f) - right * (farWidth * 0.5f);
39 const glm::vec3 farBottomRight = farCenter - up * (farHeight * 0.5f) + right * (farWidth * 0.5f);
40
41 planes[FrustumPlane::NEAR_PLANE].Set(front, nearCenter);
42 planes[FrustumPlane::FAR_PLANE].Set(-front, farCenter);
43 planes[FrustumPlane::TOP_PLANE].Set(glm::cross(nearTopRight - position, farTopLeft - position), nearTopLeft);
44 planes[FrustumPlane::BOTTOM_PLANE].Set(glm::cross(farBottomLeft - position, nearBottomRight - position), nearBottomLeft);
45 planes[FrustumPlane::LEFT_PLANE].Set(glm::cross(nearBottomLeft - position, farTopLeft - position), nearBottomLeft);
46 planes[FrustumPlane::RIGHT_PLANE].Set(glm::cross(farTopRight - position, nearBottomRight - position), nearBottomRight);
47 }
48}
const float GetNear() const
Definition Camera.h:36
const float GetAspectRatio() const
Definition Camera.h:42
const glm::vec3 & GetRightVec() const
Definition Camera.h:29
const glm::vec3 & GetUpVec() const
Definition Camera.h:30
const float GetFOV() const
Definition Camera.h:32
const float GetFar() const
Definition Camera.h:39
const glm::vec3 & GetFrontVec() const
Definition Camera.h:28
GameObject * gameObject
Definition Component.h:73
@ RIGHT_PLANE
Definition Frustum.h:30
@ TOP_PLANE
Definition Frustum.h:28
@ FAR_PLANE
Definition Frustum.h:27
@ BOTTOM_PLANE
Definition Frustum.h:29
@ NEAR_PLANE
Definition Frustum.h:26
@ LEFT_PLANE
Definition Frustum.h:31
Frustum()=default
Plane planes[6]
Definition Frustum.h:39
void Set(const glm::vec3 &normal, const glm::vec3 &point)
Definition Frustum.h:12