-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
128 lines (99 loc) · 3.32 KB
/
Copy pathCamera.cpp
File metadata and controls
128 lines (99 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// ===========================================================================
/// <summary>
/// Camera.cpp
/// DirectXIntroduction
/// created by Mehrdad Soleimanimajd on 25.08.2019
/// </summary>
/// <created>ʆϒʅ, 25.08.2019</created>
/// <changed>ʆϒʅ, 04.07.2023</changed>
// ===========================================================================
#include "Camera.h"
#include "Shared.h"
Camera::Camera (void)
{
try
{
// initialize the camera to the origin of the scene.
position.x = 0.0f;
position.y = 0.0f;
position.z = 0.0f;
rotation.x = 0.0f;
rotation.y = 0.0f;
rotation.z = 0.0f;
initialized = true;
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
const bool& Camera::isInitialized (void)
{
return initialized;
};
void Camera::setPosition (DirectX::XMFLOAT3& pos)
{
position.x = pos.x;
position.y = pos.y;
position.z = pos.z;
};
void Camera::setPosition (float x, float y, float z)
{
position.x = x;
position.y = y;
position.z = z;
};
void Camera::forwardBackward (float z)
{
position.z += z;
};
void Camera::renderCamera (void)
{
try
{
DirectX::XMFLOAT3 up, pos, lookAt;
DirectX::XMVECTOR upVector, positionVector, lookAtVector;
// vector setup: the one that points upwards
up.x = 0.0f;
up.y = 1.0f;
up.z = 0.0f;
upVector = DirectX::XMLoadFloat3 (&up); // load into structure
// camera position setup (in the world)
pos.x = position.x;
pos.y = position.y;
pos.z = position.z;
positionVector = DirectX::XMLoadFloat3 (&pos);
// camera looking setup (default)
lookAt.x = 0.0f;
lookAt.y = 0.0f;
lookAt.z = 1.0f;
lookAtVector = DirectX::XMLoadFloat3 (&lookAt);
float
pitch, // X axis
yaw, // Y axis
roll; // Z axis
// setup axes in radians
pitch = rotation.x * 0.01745329225f;
yaw = rotation.y * 0.01745329225f;
roll = rotation.z * 0.01745329225f;
// rotation matrix creation
DirectX::XMMATRIX rotationMatrix;
rotationMatrix = DirectX::XMMatrixRotationRollPitchYaw (pitch, yaw, roll);
// view correction at the origin: transformation of camera look and up vector by the rotation matrix
lookAtVector = DirectX::XMVector3TransformCoord (lookAtVector, rotationMatrix);
upVector = DirectX::XMVector3TransformCoord (upVector, rotationMatrix);
// rotated camera position translation: to the location of viewer
lookAtVector = DirectX::XMVectorAdd (positionVector, lookAtVector);
// finally: view matrix creation (from above updated vectors)
matrixView = DirectX::XMMatrixLookAtLH (positionVector, lookAtVector, upVector);
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
const DirectX::XMMATRIX& Camera::getView (void)
{
return matrixView;
};