forked from ScruffyFurn/SumoDX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
174 lines (135 loc) · 4.47 KB
/
Camera.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved
#include "pch.h"
#include "../GameObjects/Camera.h"
using namespace DirectX;
#undef min // Use __min instead.
#undef max // Use __max instead.
//--------------------------------------------------------------------------------------
Camera::Camera()
{
// Setup the view matrix.
SetViewParams(
XMFLOAT3(0.0f, 0.0f, 0.0f), // Default eye position.
XMFLOAT3(0.0f, 0.0f, 1.0f), // Default look at position.
XMFLOAT3(0.0f, 1.0f, 0.0f) // Default up vector.
);
// Setup the projection matrix.
SetProjParams(XM_PI / 4, 1.0f, 1.0f, 1000.0f);
}
//--------------------------------------------------------------------------------------
void Camera::LookDirection (_In_ XMFLOAT3 lookDirection)
{
XMFLOAT3 lookAt;
lookAt.x = m_eye.x + lookDirection.x;
lookAt.y = m_eye.y + lookDirection.y;
lookAt.z = m_eye.z + lookDirection.z;
SetViewParams(m_eye, lookAt, m_up);
}
//--------------------------------------------------------------------------------------
void Camera::Eye(_In_ XMFLOAT3 eye)
{
SetViewParams(eye, m_lookAt, m_up);
}
//--------------------------------------------------------------------------------------
void Camera::SetViewParams(
_In_ XMFLOAT3 eye,
_In_ XMFLOAT3 lookAt,
_In_ XMFLOAT3 up
)
{
m_eye = eye;
m_lookAt = lookAt;
m_up = up;
// Calculate the view matrix.
XMMATRIX view = XMMatrixLookAtLH(
XMLoadFloat3(&m_eye),
XMLoadFloat3(&m_lookAt),
XMLoadFloat3(&m_up)
);
XMVECTOR det;
XMMATRIX inverseView = XMMatrixInverse(&det, view);
XMStoreFloat4x4(&m_viewMatrix, view);
XMStoreFloat4x4(&m_inverseView, inverseView);
// The axis basis vectors and camera position are stored inside the
// position matrix in the 4 rows of the camera's world matrix.
// To figure out the yaw/pitch of the camera, we just need the Z basis vector.
XMFLOAT3 zBasis;
XMStoreFloat3(&zBasis, inverseView.r[2]);
m_cameraYawAngle = atan2f(zBasis.x, zBasis.z);
float len = sqrtf(zBasis.z * zBasis.z + zBasis.x * zBasis.x);
m_cameraPitchAngle = atan2f(zBasis.y, len);
}
//--------------------------------------------------------------------------------------
void Camera::SetProjParams(
_In_ float fieldOfView,
_In_ float aspectRatio,
_In_ float nearPlane,
_In_ float farPlane
)
{
// Set attributes for the projection matrix.
m_fieldOfView = fieldOfView;
m_aspectRatio = aspectRatio;
m_nearPlane = nearPlane;
m_farPlane = farPlane;
XMStoreFloat4x4(
&m_projectionMatrix,
XMMatrixPerspectiveFovLH(
m_fieldOfView,
m_aspectRatio,
m_nearPlane,
m_farPlane
)
);
}
//--------------------------------------------------------------------------------------
DirectX::XMMATRIX Camera::View()
{
return XMLoadFloat4x4(&m_viewMatrix);
}
//--------------------------------------------------------------------------------------
DirectX::XMMATRIX Camera::Projection()
{
return XMLoadFloat4x4(&m_projectionMatrix);
}
//--------------------------------------------------------------------------------------
DirectX::XMMATRIX Camera::World()
{
return XMLoadFloat4x4(&m_inverseView);
}
//--------------------------------------------------------------------------------------
DirectX::XMFLOAT3 Camera::Eye()
{
return m_eye;
}
//--------------------------------------------------------------------------------------
DirectX::XMFLOAT3 Camera::LookAt()
{
return m_lookAt;
}
//--------------------------------------------------------------------------------------
float Camera::NearClipPlane()
{
return m_nearPlane;
}
//--------------------------------------------------------------------------------------
float Camera::FarClipPlane()
{
return m_farPlane;
}
//--------------------------------------------------------------------------------------
float Camera::Pitch()
{
return m_cameraPitchAngle;
}
//--------------------------------------------------------------------------------------
float Camera::Yaw()
{
return m_cameraYawAngle;
}
//--------------------------------------------------------------------------------------