Khaotic Engine Reborn
Loading...
Searching...
No Matches
camera_class.cpp
1
2// Filename: cameraclass.cpp
4#include "camera_class.h"
5
7{
8 position_x_ = 0.0f;
9 position_y_ = 0.0f;
10 position_z_ = 0.0f;
11
12 rotation_x_ = 0.0f;
13 rotation_y_ = 0.0f;
14 rotation_z_ = 0.0f;
15}
16
17
19{
20}
21
22
23camera_class::~camera_class()
24{
25}
26
27void camera_class::set_position(float x, float y, float z)
28{
29 position_x_ = x;
30 position_y_ = y;
31 position_z_ = z;
32 return;
33}
34
35
36void camera_class::set_rotation(float x, float y, float z)
37{
38 rotation_x_ = x;
39 rotation_y_ = y;
40 rotation_z_ = z;
41 return;
42}
43
45{
46 return XMFLOAT3(position_x_, position_y_, position_z_);
47}
48
49
51{
52 return XMFLOAT3(rotation_x_, rotation_y_, rotation_z_);
53}
54
56{
57 XMFLOAT3 up, position, lookAt;
58 XMVECTOR upVector, positionVector, lookAtVector;
59 float yaw, pitch, roll;
60 XMMATRIX rotationMatrix;
61
62
63 // Setup the vector that points upwards.
64 up.x = 0.0f;
65 up.y = 1.0f;
66 up.z = 0.0f;
67
68 // Load it into a XMVECTOR structure.
69 upVector = XMLoadFloat3(&up);
70
71 // Setup the position of the camera in the world.
72 position.x = position_x_;
73 position.y = position_y_;
74 position.z = position_z_;
75
76 // Load it into a XMVECTOR structure.
77 positionVector = XMLoadFloat3(&position);
78
79 // Setup where the camera is looking by default.
80 lookAt.x = 0.0f;
81 lookAt.y = 0.0f;
82 lookAt.z = 1.0f;
83
84 // Load it into a XMVECTOR structure.
85 lookAtVector = XMLoadFloat3(&lookAt);
86
87 // Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
88 pitch = rotation_x_ * 0.0174532925f;
89 yaw = rotation_y_ * 0.0174532925f;
90 roll = rotation_z_ * 0.0174532925f;
91
92 // Create the rotation matrix from the yaw, pitch, and roll values.
93 rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);
94
95 // Transform the lookAt and up vector by the rotation matrix so the view is correctly rotated at the origin.
96 lookAtVector = XMVector3TransformCoord(lookAtVector, rotationMatrix);
97 upVector = XMVector3TransformCoord(upVector, rotationMatrix);
98
99 // Translate the rotated camera position to the location of the viewer.
100 lookAtVector = XMVectorAdd(positionVector, lookAtVector);
101
102 // Finally create the view matrix from the three updated vectors.
103 view_matrix_ = XMMatrixLookAtLH(positionVector, lookAtVector, upVector);
104
105 return;
106}
107
108XMMATRIX camera_class::get_view_matrix(XMMATRIX& view_matrix) const
109{
110 view_matrix = view_matrix_;
111 return view_matrix;
112}
113
115{
116 XMFLOAT3 up, position, lookAt;
117 XMVECTOR upVector, positionVector, lookAtVector;
118 float yaw, pitch, roll;
119 XMMATRIX rotationMatrix;
120
121
122 // Setup the vector that points upwards.
123 up.x = 0.0f;
124 up.y = 1.0f;
125 up.z = 0.0f;
126
127 // Load it into a XMVECTOR structure.
128 upVector = XMLoadFloat3(&up);
129
130 // Setup the position of the camera in the world.
131 position.x = position_x_;
132 position.y = -position_y_ + (height * 2.0f);
133 position.z = position_z_;
134
135 // Load it into a XMVECTOR structure.
136 positionVector = XMLoadFloat3(&position);
137
138 // Setup where the camera is looking by default.
139 lookAt.x = 0.0f;
140 lookAt.y = 0.0f;
141 lookAt.z = 1.0f;
142
143 // Load it into a XMVECTOR structure.
144 lookAtVector = XMLoadFloat3(&lookAt);
145
146 // Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
147 pitch = (-1.0f * rotation_x_) * 0.0174532925f; // Invert for reflection
148 yaw = rotation_y_ * 0.0174532925f;
149 roll = rotation_z_ * 0.0174532925f;
150
151 // Create the rotation matrix from the yaw, pitch, and roll values.
152 rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);
153
154 // Transform the lookAt and up vector by the rotation matrix so the view is correctly rotated at the origin.
155 lookAtVector = XMVector3TransformCoord(lookAtVector, rotationMatrix);
156 upVector = XMVector3TransformCoord(upVector, rotationMatrix);
157
158 // Translate the rotated camera position to the location of the viewer.
159 lookAtVector = XMVectorAdd(positionVector, lookAtVector);
160
161 // Finally create the view matrix from the three updated vectors.
162 reflection_view_matrix_ = XMMatrixLookAtLH(positionVector, lookAtVector, upVector);
163
164 return;
165}
166
167void camera_class::get_reflection_view_matrix(XMMATRIX& reflectionViewMatrix) const
168{
169 reflectionViewMatrix = reflection_view_matrix_;
170 return;
171}
void render()
Updates the camera's view matrix based on its position and rotation. This method recalculates the vie...
void set_rotation(float, float, float)
Sets the rotation of the camera in 3D space.
void set_position(float, float, float)
Sets the position of the camera in 3D space.
void render_reflection(float)
Renders the reflection of the scene from the camera's perspective.
XMFLOAT3 get_rotation()
Gets the current rotation of the camera.
XMMATRIX get_view_matrix(XMMATRIX &view_matrix) const
Retrieves the current view matrix of the camera.
camera_class()
Default constructor for camera_class. Initializes the camera position and rotation to zero.
void get_reflection_view_matrix(XMMATRIX &) const
Retrieves the reflection view matrix of the camera.
XMFLOAT3 get_position()
Gets the current position of the camera.