Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
Frustum.cpp
Go to the documentation of this file.
3 
4 namespace Engine
5 {
6  Frustum::Frustum(Camera& camera, glm::vec3 right, glm::vec3 up)
7  {
8  float fov = camera.GetFoV();
9  float aspectRatio = camera.GetAspectRatio();
10  glm::vec3 cameraPosition = camera.GetPosition();
11  glm::vec3 cameraDirection = camera.GetRotation();
12  glm::vec2 clippingPlanes = camera.GetClippingPlanes();
13 
14  float nearPlaneHeight = 2 * tan(fov / 2) * clippingPlanes.x;
15  float nearPlaneWidth = nearPlaneHeight * aspectRatio;
16 
17  float farPlaneHeight = 2 * tan(fov / 2) * clippingPlanes.y;
18  float farPlaneWidth = farPlaneHeight * aspectRatio;
19 
20  glm::vec3 farCenter = cameraPosition + cameraDirection * clippingPlanes.y;
21 
22  glm::vec3 ftl = farCenter + (up * farPlaneHeight / 2.0f) - (right * farPlaneWidth / 2.0f); // far top left
23  glm::vec3 ftr = farCenter + (up * farPlaneHeight / 2.0f) + (right * farPlaneWidth / 2.0f); // far top right
24  glm::vec3 fbl = farCenter - (up * farPlaneHeight / 2.0f) - (right * farPlaneWidth / 2.0f); // far bottom left
25  glm::vec3 fbr = farCenter - (up * farPlaneHeight / 2.0f) + (right * farPlaneWidth / 2.0f); // far bottom right
26 
27  glm::vec3 nearCenter = cameraPosition + cameraDirection * clippingPlanes.x;
28 
29  glm::vec3 ntl = nearCenter + (up * nearPlaneHeight / 2.0f) - (right * nearPlaneWidth / 2.0f); // near top left
30  glm::vec3 ntr = nearCenter + (up * nearPlaneHeight / 2.0f) + (right * nearPlaneWidth / 2.0f); // near top right
31  glm::vec3 nbl = nearCenter - (up * nearPlaneHeight / 2.0f) - (right * nearPlaneWidth / 2.0f); // near bottom left
32  glm::vec3 nbr = nearCenter - (up * nearPlaneHeight / 2.0f) + (right * nearPlaneWidth / 2.0f); // near bottom right
33 
34  topFace = Face(ntr, ntl, ftl);
35  bottomFace = Face(nbl, nbr, fbl);
36  leftFace = Face(ntl, nbl, fbl);
37  rightFace = Face(nbr, ntr, fbr);
38  frontFace = Face(ntl, ntr, nbr);
39  backFace = Face(ftr, ftl, fbl);
40  }
41 
42  Frustum::Face::Face(glm::vec3 p0, glm::vec3 p1, glm::vec3 p2)
43  {
44  position0 = p0;
45  position1 = p1;
46  position2 = p2;
47 
48  v = p1 - p0;
49  u = p2 - p0;
50  normal = v * u;
51  glm::normalize(normal);
52 
53  d = glm::dot(-normal, p0);
54  }
55 
56  Frustum::Face Frustum::GetFrontFace() const
57  {
58  return frontFace;
59  }
60 
61  Frustum::Face Frustum::GetBackFace() const
62  {
63  return backFace;
64  }
65 
66  Frustum::Face Frustum::GetTopFace() const
67  {
68  return topFace;
69  }
70 
71  Frustum::Face Frustum::GetRightFace() const
72  {
73  return rightFace;
74  }
75 
76  Frustum::Face Frustum::GetBottomFace() const
77  {
78  return bottomFace;
79  }
80 
81  Frustum::Face Frustum::GetLeftFace() const
82  {
83  return leftFace;
84  }
85 } // namespace Engine
Face GetBottomFace() const
Definition: Frustum.cpp:76
Face GetBackFace() const
Definition: Frustum.cpp:61
Face GetFrontFace() const
Definition: Frustum.cpp:56
Face GetTopFace() const
Definition: Frustum.cpp:66
Face GetRightFace() const
Definition: Frustum.cpp:71
Face GetLeftFace() const
Definition: Frustum.cpp:81