VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
frustum.hpp
Go to the documentation of this file.
1
6
7#pragma once
8#include <glm/glm.hpp>
9#include <array>
10
11namespace vex {
12
14struct Plane {
15 glm::vec3 normal;
16 float distance;
17
18 void normalize() {
19 float length = glm::length(normal);
20 normal /= length;
21 distance /= length;
22 }
23
25 float getSignedDistance(const glm::vec3& point) const {
26 return glm::dot(normal, point) + distance;
27 }
28};
29
31struct Frustum {
32 std::array<Plane, 6> planes;
33
36 void update(const glm::mat4& viewProj) {
37 glm::mat4 m = glm::transpose(viewProj);
38
39 // Left
40 planes[0].normal = glm::vec3(m[3] + m[0]);
41 planes[0].distance = m[3].w + m[0].w;
42 // Right
43 planes[1].normal = glm::vec3(m[3] - m[0]);
44 planes[1].distance = m[3].w - m[0].w;
45 // Bottom
46 planes[2].normal = glm::vec3(m[3] + m[1]);
47 planes[2].distance = m[3].w + m[1].w;
48 // Top
49 planes[3].normal = glm::vec3(m[3] - m[1]);
50 planes[3].distance = m[3].w - m[1].w;
51 // Near
52 planes[4].normal = glm::vec3(m[3] + m[2]);
53 planes[4].distance = m[3].w + m[2].w;
54 // Far
55 planes[5].normal = glm::vec3(m[3] - m[2]);
56 planes[5].distance = m[3].w - m[2].w;
57
58 for (auto& plane : planes) plane.normalize();
59 }
60
62 bool testSphere(const glm::vec3& center, float radius) const {
63 for (const auto& plane : planes) {
64 if (plane.getSignedDistance(center) < -radius) {
65 return false;
66 }
67 }
68 return true;
69 }
70};
71
72}
Represents a frustum in 3D space.
Definition frustum.hpp:31
void update(const glm::mat4 &viewProj)
Extract planes from View-Projection Matrix.
Definition frustum.hpp:36
bool testSphere(const glm::vec3 &center, float radius) const
Returns true if sphere is visible (or partially visible).
Definition frustum.hpp:62
Represents a plane in 3D space.
Definition frustum.hpp:14
float getSignedDistance(const glm::vec3 &point) const
Returns signed distance from plane to point.
Definition frustum.hpp:25