Simulant  21.12-1292
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
frustum.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef FRUSTUM_H
20 #define FRUSTUM_H
21 
22 #include <cstdint>
23 #include <vector>
24 
25 #include "types.h"
26 
27 namespace smlt {
28 
29 enum FrustumCorner {
30  FRUSTUM_CORNER_BOTTOM_LEFT = 0,
31  FRUSTUM_CORNER_BOTTOM_RIGHT,
32  FRUSTUM_CORNER_TOP_RIGHT,
33  FRUSTUM_CORNER_TOP_LEFT,
34  FRUSTUM_CORNER_MAX
35 };
36 
37 enum FrustumClassification {
38  FRUSTUM_CONTAINS_NONE = 0,
39  FRUSTUM_CONTAINS_PARTIAL,
40  FRUSTUM_CONTAINS_ALL
41 };
42 
43 class Frustum {
44 public:
45  Frustum();
46 
47  void build(const Mat4* modelview_projection);
48 
49  std::vector<Vec3> near_corners() const;
50  std::vector<Vec3> far_corners() const;
51 
52  bool contains_point(const Vec3& point) const;
53 
54  bool intersects_sphere(const Vec3& pos, const float diameter);
55  bool intersects_aabb(const AABB &box) const;
56  bool intersects_cube(const Vec3& center, float size) const;
57 
58  bool initialized() const { return initialized_; }
59 
60  float near_height() const {
61  assert(initialized_);
62  return (near_corners_[FRUSTUM_CORNER_BOTTOM_LEFT] - near_corners_[FRUSTUM_CORNER_TOP_LEFT]).length();
63  }
64 
65  float far_height() const {
66  assert(initialized_);
67  return (far_corners_[FRUSTUM_CORNER_BOTTOM_LEFT] - far_corners_[FRUSTUM_CORNER_TOP_LEFT]).length();
68  }
69 
70  float near_width() const {
71  assert(initialized_);
72  return (near_corners_[FRUSTUM_CORNER_BOTTOM_LEFT] - near_corners_[FRUSTUM_CORNER_BOTTOM_RIGHT]).length();
73  }
74 
75  float far_width() const {
76  assert(initialized_);
77  return (far_corners_[FRUSTUM_CORNER_BOTTOM_LEFT] - far_corners_[FRUSTUM_CORNER_BOTTOM_RIGHT]).length();
78  }
79 
80  float depth() const {
81  assert(initialized_);
82 
83  /*
84  * We need to find the central points of both the near and far corners
85  * and return the length between them
86  */
87  Vec3 near_avg, far_avg;
88 
89  for(uint32_t i = 0; i < FRUSTUM_CORNER_MAX; ++i) {
90  near_avg += near_corners_[i];
91  far_avg += far_corners_[i];
92  }
93 
94  near_avg /= FRUSTUM_CORNER_MAX;
95  far_avg /= FRUSTUM_CORNER_MAX;
96 
97  return (far_avg - near_avg).length();
98  }
99 
100  Plane plane(FrustumPlane p) const {
101  return planes_[p];
102  }
103 
104  Vec3 direction() const;
105  Vec3 up() const;
106  Vec3 right() const;
107 
108  float width_at_distance(float distance) const;
109  float height_at_distance(float distance) const;
110  Degrees field_of_view() const;
111  float aspect_ratio() const;
112 private:
113  bool initialized_;
114 
115  std::vector<Vec3> near_corners_;
116  std::vector<Vec3> far_corners_;
117  std::vector<Plane> planes_;
118 };
119 
120 }
121 
122 #endif // FRUSTUM_H
smlt::Frustum
Definition: frustum.h:43
smlt::Mat4
Definition: mat4.h:35
smlt::Vec3
Definition: vec3.h:25
smlt
Definition: animation.cpp:25
smlt::Frustum::near_corners
std::vector< Vec3 > near_corners() const
Returns the near 4 corners of the frustum.
Definition: frustum.cpp:196
smlt::Degrees
Definition: degrees.h:13
smlt::AABB
Definition: aabb.h:22
smlt::Frustum::far_corners
std::vector< Vec3 > far_corners() const
Returns the far 4 corners of the frustum.
Definition: frustum.cpp:200
smlt::Frustum::contains_point
bool contains_point(const Vec3 &point) const
Returns true if the frustum contains point.
Definition: frustum.cpp:204
smlt::Plane
Definition: plane.h:18