@@ -11,85 +11,91 @@ namespace lepus
11
11
{
12
12
namespace gfx
13
13
{
14
- class Camera
15
- {
16
- protected:
17
- lepus::math::Transform m_Transform;
18
- float m_FOV;
19
- float m_Near, m_Far;
20
-
21
- public:
22
- Camera ()
23
- {
24
- m_FOV = LEPUS_GFX_CAMERA_DEFAULT_FOV;
25
- m_Near = LEPUS_GFX_CAMERA_DEFAULT_CLIP_NEAR;
26
- m_Far = LEPUS_GFX_CAMERA_DEFAULT_CLIP_FAR;
27
- m_Transform = lepus::math::Transform ();
28
- }
29
-
30
- // / @brief Gets field of view
31
- // / @return Camera's field of view (in degrees)
32
- inline float FOV () const
33
- {
34
- return m_FOV;
35
- }
36
-
37
- // / @brief Sets field of view
38
- // / @param fov New field of view (in degrees) to be used by this camera
39
- inline void FOV (float fov)
40
- {
41
- m_FOV = fov;
42
- }
43
-
44
- // / @brief Gets the Camera's Transform.
45
- // / @return A reference to the Transform used by the Camera.
46
- inline lepus::math::Transform& Transform () { return m_Transform; }
47
-
48
- // / @brief Generates a perspective projection matrix using the current FOV angle and near/far clipping planes.
49
- // / @return A 4x4 projection matrix that can be used to apply camera perspective to vertices.
50
- lepus::math::Matrix4x4 BuildPerspectiveMatrix () const
51
- {
52
- lepus::math::Matrix4x4 projMatrix = lepus::math::Matrix4x4 ();
53
-
54
- float hypot = tanf ((180 .f - m_FOV) * DEG2RAD * 0 .5f );
55
-
56
- projMatrix.set <0 , 0 >(hypot );
57
- projMatrix.set <1 , 1 >(hypot );
58
-
59
- projMatrix.set <2 , 2 >(-(m_Far / (m_Far - m_Near)));
60
- projMatrix.set <2 , 3 >(-((m_Far * m_Near) / (m_Far - m_Near)));
61
-
62
- projMatrix.set <3 , 2 >(-1 .f );
63
-
64
- return projMatrix;
65
- }
66
-
67
- // / @brief Generates a look-at view matrix using the Camera's Transform.
68
- // / @return A 4x4 view matrix that can be used to rotate and translate vertices according to eye position and camera orientation.
69
- lepus::math::Matrix4x4 BuildViewMatrix () const
70
- {
71
- lepus::math::Matrix4x4 pos = lepus::math::Matrix4x4::Identity ();
72
-
73
- auto f = m_Transform.Forward () * (-1 .f / m_Transform.Forward ().Magnitude ());
74
- auto s = m_Transform.Right () * (1 .f / m_Transform.Right ().Magnitude ());
75
- auto u = m_Transform.Up () * (1 .f / m_Transform.Up ().Magnitude ());
76
-
77
- auto e = m_Transform.Origin ();
78
-
79
- pos.set <0 , 3 >(-e.x ());
80
- pos.set <1 , 3 >(-e.y ());
81
- pos.set <2 , 3 >(-e.z ());
82
-
83
- lepus::math::Matrix4x4 lookAt = lepus::math::Matrix4x4::Identity ();
84
-
85
- lookAt.set <0 , 0 >(s.x ());lookAt.set <0 , 1 >(s.y ());lookAt.set <0 , 2 >(s.z ());
86
- lookAt.set <1 , 0 >(u.x ());lookAt.set <1 , 1 >(u.y ());lookAt.set <1 , 2 >(u.z ());
87
- lookAt.set <2 , 0 >(f.x ());lookAt.set <2 , 1 >(f.y ());lookAt.set <2 , 2 >(f.z ());
88
-
89
- return lookAt.Multiply (pos);
90
- }
91
- };
92
- }
93
- }
14
+ class Camera
15
+ {
16
+ protected:
17
+ lepus::math::Transform m_Transform;
18
+ float m_FOV;
19
+ float m_Near, m_Far;
20
+
21
+ public:
22
+ Camera ()
23
+ {
24
+ m_FOV = LEPUS_GFX_CAMERA_DEFAULT_FOV;
25
+ m_Near = LEPUS_GFX_CAMERA_DEFAULT_CLIP_NEAR;
26
+ m_Far = LEPUS_GFX_CAMERA_DEFAULT_CLIP_FAR;
27
+ m_Transform = lepus::math::Transform ();
28
+ }
29
+
30
+ // / @brief Gets field of view
31
+ // / @return Camera's field of view (in degrees)
32
+ inline float FOV () const
33
+ {
34
+ return m_FOV;
35
+ }
36
+
37
+ // / @brief Sets field of view
38
+ // / @param fov New field of view (in degrees) to be used by this camera
39
+ inline void FOV (float fov)
40
+ {
41
+ m_FOV = fov;
42
+ }
43
+
44
+ // / @brief Gets the Camera's Transform.
45
+ // / @return A reference to the Transform used by the Camera.
46
+ inline lepus::math::Transform& Transform () { return m_Transform; }
47
+
48
+ // / @brief Generates a perspective projection matrix using the current FOV angle and near/far clipping planes.
49
+ // / @return A 4x4 projection matrix that can be used to apply camera perspective to vertices.
50
+ lepus::math::Matrix4x4 BuildPerspectiveMatrix () const
51
+ {
52
+ lepus::math::Matrix4x4 projMatrix = lepus::math::Matrix4x4 ();
53
+
54
+ float hypot = tanf ((180 .f - m_FOV) * (float )(DEG2RAD) * 0 .5f );
55
+
56
+ projMatrix.set <0 , 0 >(hypot );
57
+ projMatrix.set <1 , 1 >(hypot );
58
+
59
+ projMatrix.set <2 , 2 >(-(m_Far / (m_Far - m_Near)));
60
+ projMatrix.set <2 , 3 >(-((m_Far * m_Near) / (m_Far - m_Near)));
61
+
62
+ projMatrix.set <3 , 2 >(-1 .f );
63
+
64
+ return projMatrix;
65
+ }
66
+
67
+ // / @brief Generates a look-at view matrix using the Camera's Transform.
68
+ // / @return A 4x4 view matrix that can be used to rotate and translate vertices according to eye position and camera orientation.
69
+ lepus::math::Matrix4x4 BuildViewMatrix () const
70
+ {
71
+ lepus::math::Matrix4x4 pos = lepus::math::Matrix4x4::Identity ();
72
+
73
+ auto f = m_Transform.Forward () * (-1 .f / m_Transform.Forward ().Magnitude ());
74
+ auto s = m_Transform.Right () * (1 .f / m_Transform.Right ().Magnitude ());
75
+ auto u = m_Transform.Up () * (1 .f / m_Transform.Up ().Magnitude ());
76
+
77
+ auto e = m_Transform.Origin ();
78
+
79
+ pos.set <0 , 3 >(-e.x ());
80
+ pos.set <1 , 3 >(-e.y ());
81
+ pos.set <2 , 3 >(-e.z ());
82
+
83
+ lepus::math::Matrix4x4 lookAt = lepus::math::Matrix4x4::Identity ();
84
+
85
+ lookAt.set <0 , 0 >(s.x ());
86
+ lookAt.set <0 , 1 >(s.y ());
87
+ lookAt.set <0 , 2 >(s.z ());
88
+ lookAt.set <1 , 0 >(u.x ());
89
+ lookAt.set <1 , 1 >(u.y ());
90
+ lookAt.set <1 , 2 >(u.z ());
91
+ lookAt.set <2 , 0 >(f.x ());
92
+ lookAt.set <2 , 1 >(f.y ());
93
+ lookAt.set <2 , 2 >(f.z ());
94
+
95
+ return lookAt.Multiply (pos);
96
+ }
97
+ };
98
+ } // namespace gfx
99
+ } // namespace lepus
94
100
95
101
#endif
0 commit comments