1414
1515namespace openage ::renderer::camera {
1616
17- Frustum::Frustum () :
18- top_face_distance{0 .0f },
19- bottom_face_distance{0 .0f },
20- right_face_distance{0 .0f },
21- left_face_distance{0 .0f },
22- far_face_distance{0 .0f },
23- near_face_distance{0 .0f },
24- top_face_normal{Eigen::Vector3f (0 .0f , 0 .0f , 0 .0f )},
25- bottom_face_normal{Eigen::Vector3f (0 .0f , 0 .0f , 0 .0f )},
26- right_face_normal{Eigen::Vector3f (0 .0f , 0 .0f , 0 .0f )},
27- left_face_normal{Eigen::Vector3f (0 .0f , 0 .0f , 0 .0f )},
28- far_face_normal{Eigen::Vector3f (0 .0f , 0 .0f , 0 .0f )},
29- near_face_normal{Eigen::Vector3f (0 .0f , 0 .0f , 0 .0f )} {
17+ Frustum::Frustum (const util::Vector2s &viewport_size,
18+ const float near_distance,
19+ const float far_distance,
20+ const Eigen::Vector3f &camera_pos,
21+ const Eigen::Vector3f &cam_direction,
22+ const Eigen::Vector3f &up_direction,
23+ const float zoom_factor) {
24+ this ->update (viewport_size,
25+ near_distance,
26+ far_distance,
27+ camera_pos,
28+ cam_direction,
29+ up_direction,
30+ zoom_factor);
3031}
3132
32- void Frustum::update (util::Vector2s &viewport_size,
33- float near_distance,
34- float far_distance,
35- Eigen::Vector3f &scene_pos ,
36- Eigen::Vector3f cam_direction,
37- Eigen::Vector3f up_direction,
38- float real_zoom ) {
33+ void Frustum::update (const util::Vector2s &viewport_size,
34+ const float near_distance,
35+ const float far_distance,
36+ const Eigen::Vector3f &camera_pos ,
37+ const Eigen::Vector3f & cam_direction,
38+ const Eigen::Vector3f & up_direction,
39+ const float zoom_factor ) {
3940 // offsets are adjusted by zoom
4041 // this is the same calculation as for the projection matrix
4142 float halfscreenwidth = viewport_size[0 ] / 2 ;
4243 float halfscreenheight = viewport_size[1 ] / 2 ;
4344
44- float halfwidth = halfscreenwidth * real_zoom ;
45- float halfheight = halfscreenheight * real_zoom ;
45+ float halfwidth = halfscreenwidth * zoom_factor ;
46+ float halfheight = halfscreenheight * zoom_factor ;
4647
4748 Eigen::Vector3f direction = cam_direction.normalized ();
48- Eigen::Vector3f eye = scene_pos ;
49- Eigen::Vector3f center = scene_pos + direction;
49+ Eigen::Vector3f eye = camera_pos ;
50+ Eigen::Vector3f center = camera_pos + direction;
5051
5152 // calculate up (u) and right (s) vectors for camera
5253 // these define the camera plane in 3D space that the input
@@ -57,8 +58,9 @@ void Frustum::update(util::Vector2s &viewport_size,
5758 Eigen::Vector3f u = s.cross (f);
5859 u.normalize ();
5960
60- Eigen::Vector3f near_position = scene_pos + direction * near_distance;
61- Eigen::Vector3f far_position = scene_pos + direction * far_distance;
61+ // calculate distance of the camera position to the near and far plane
62+ Eigen::Vector3f near_position = camera_pos + direction * near_distance;
63+ Eigen::Vector3f far_position = camera_pos + direction * far_distance;
6264
6365 // The frustum is a cuboid box with 8 points defining it (4 on the near plane, 4 on the far plane)
6466 Eigen::Vector3f near_top_left = near_position - s * halfwidth + u * halfheight;
@@ -67,14 +69,15 @@ void Frustum::update(util::Vector2s &viewport_size,
6769 Eigen::Vector3f near_bottom_right = near_position + s * halfwidth - u * halfheight;
6870
6971 Eigen::Vector3f far_top_left = far_position - s * halfwidth + u * halfheight;
70- Eigen::Vector3f far_top_right = far_position + s * halfwidth + u * halfheight;
72+ // Eigen::Vector3f far_top_right = far_position + s * halfwidth + u * halfheight; // unused
7173 Eigen::Vector3f far_bottom_left = far_position - s * halfwidth - u * halfheight;
7274 Eigen::Vector3f far_bottom_right = far_position + s * halfwidth - u * halfheight;
7375
7476 // The near and far planes are easiest to compute, as they should be in the direction of the camera
7577 this ->near_face_normal = cam_direction.normalized ();
7678 this ->far_face_normal = -1 .0f * cam_direction.normalized ();
7779
80+ // The distance of the plane from the origin is the dot product of the normal and a point on the plane
7881 this ->near_face_distance = this ->near_face_normal .dot (near_position);
7982 this ->far_face_distance = this ->far_face_normal .dot (far_position);
8083
@@ -84,11 +87,14 @@ void Frustum::update(util::Vector2s &viewport_size,
8487 this ->top_face_normal = (near_top_right - near_top_left).cross (near_top_left - far_top_left);
8588 this ->bottom_face_normal = (near_bottom_left - far_bottom_left).cross (near_bottom_right - near_bottom_left);
8689
90+ // for orthographic projection, the normal of left/right should equal -s/s
91+ // and the normal of top/bottom should equal u/-u
8792 this ->left_face_normal .normalize ();
8893 this ->right_face_normal .normalize ();
8994 this ->top_face_normal .normalize ();
9095 this ->bottom_face_normal .normalize ();
9196
97+ // calculate the distance of the planes to the origin
9298 this ->left_face_distance = this ->left_face_normal .dot (near_bottom_left);
9399 this ->right_face_distance = this ->right_face_normal .dot (far_bottom_right);
94100 this ->top_face_distance = this ->top_face_normal .dot (near_top_right);
0 commit comments