-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.cpp
241 lines (195 loc) · 6.57 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "camera.h"
#include "config.h"
Camera::Camera(IDirect3DDevice9* pDevice, const D3DXVECTOR3& pos, const float pitch, const float yaw)
: mDevice{ pDevice }
, mPitch{ pitch }
, mYaw{ yaw }
, mPos{ pos }
{
rotate(0, 0);
}
void Camera::rotate(const float dPitch, const float dYaw)
{
mPitch += dPitch;
mYaw += dYaw;
constexpr float halfPi = 3.14f * 0.5f;
if (mPitch > halfPi)
mPitch = halfPi;
else if (mPitch < -halfPi)
mPitch = -halfPi;
D3DXMATRIX matY, matX;
D3DXMatrixRotationY(&matY, mYaw);
D3DXMatrixRotationX(&matX, mPitch);
const D3DXMATRIX matRot = matY * matX;
mDir = D3DXVECTOR3(matRot._13, matRot._23, matRot._33);
D3DXMATRIX view;
const D3DXVECTOR3 at(mPos + mDir);
const D3DXVECTOR3 yup(0, 1, 0);
D3DXMatrixLookAtLH(&view, &mPos, &at, &yup);
mRight = D3DXVECTOR3(view._11, view._21, view._31);
mUp = D3DXVECTOR3(view._12, view._22, view._32);
mDir = D3DXVECTOR3(view._13, view._23, view._33);
}
void Camera::moveRight(const float scale)
{
mPos += scale * mRight;
}
void Camera::moveForward(const float scale)
{
mPos += scale * mDir;
}
void Camera::moveUp(const float scale)
{
mPos += scale * mUp;
}
void Camera::setView() const
{
D3DXMATRIX view;
const D3DXVECTOR3 at(mPos + mDir);
const D3DXVECTOR3 yup(0, 1, 0);
D3DXMatrixLookAtLH(&view, &mPos, &at, &yup);
mDevice->SetTransform(D3DTS_VIEW, &view);
}
D3DXVECTOR3 Camera::getPos() const noexcept
{
return mPos;
}
D3DXVECTOR3 Camera::getDir() const noexcept
{
return mDir;
}
void Camera::setProjection() const
{
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH
(
&matProjection,
D3DXToRadian(Config::FOV),
static_cast<float>(Config::SCREEN_WIDTH) / static_cast<float>(Config::SCREEN_HEIGHT),
Config::NEAR_PLANE,
Config::FAR_PLANE
);
mDevice->SetTransform(D3DTS_PROJECTION, &matProjection);
}
void Camera::setFrustum()
{
D3DXMATRIX matProjection;
mDevice->GetTransform(D3DTS_PROJECTION, &matProjection);
D3DXMATRIX matView;
mDevice->GetTransform(D3DTS_VIEW, &matView);
const D3DXMATRIX matFrustum = matView * matProjection;
// Near plane
mFrustum[0].a = matFrustum._14 + matFrustum._13;
mFrustum[0].b = matFrustum._24 + matFrustum._23;
mFrustum[0].c = matFrustum._34 + matFrustum._33;
mFrustum[0].d = matFrustum._44 + matFrustum._43;
D3DXPlaneNormalize(&mFrustum[0], &mFrustum[0]);
// Far plane
mFrustum[1].a = matFrustum._14 - matFrustum._13;
mFrustum[1].b = matFrustum._24 - matFrustum._23;
mFrustum[1].c = matFrustum._34 - matFrustum._33;
mFrustum[1].d = matFrustum._44 - matFrustum._43;
D3DXPlaneNormalize(&mFrustum[1], &mFrustum[1]);
// Left plane
mFrustum[2].a = matFrustum._14 + matFrustum._11;
mFrustum[2].b = matFrustum._24 + matFrustum._21;
mFrustum[2].c = matFrustum._34 + matFrustum._31;
mFrustum[2].d = matFrustum._44 + matFrustum._41;
D3DXPlaneNormalize(&mFrustum[2], &mFrustum[2]);
// Right plane
mFrustum[3].a = matFrustum._14 - matFrustum._11;
mFrustum[3].b = matFrustum._24 - matFrustum._21;
mFrustum[3].c = matFrustum._34 - matFrustum._31;
mFrustum[3].d = matFrustum._44 - matFrustum._41;
D3DXPlaneNormalize(&mFrustum[3], &mFrustum[3]);
// Top plane
mFrustum[4].a = matFrustum._14 - matFrustum._12;
mFrustum[4].b = matFrustum._24 - matFrustum._22;
mFrustum[4].c = matFrustum._34 - matFrustum._32;
mFrustum[4].d = matFrustum._44 - matFrustum._42;
D3DXPlaneNormalize(&mFrustum[4], &mFrustum[4]);
// Bottom plane
mFrustum[5].a = matFrustum._14 + matFrustum._12;
mFrustum[5].b = matFrustum._24 + matFrustum._22;
mFrustum[5].c = matFrustum._34 + matFrustum._32;
mFrustum[5].d = matFrustum._44 + matFrustum._42;
D3DXPlaneNormalize(&mFrustum[5], &mFrustum[5]);
}
bool Camera::isPointInFrustum(const D3DXVECTOR3& point) const
{
for (const auto& plane : mFrustum)
if (D3DXPlaneDotCoord(&plane, &point) < 0.0f)
return false;
return true;
}
bool Camera::isCubeInFrustum(const float xCenter, const float yCenter, const float zCenter, const float radius) const
{
for (const auto& plane : mFrustum)
{
const D3DXVECTOR3 a((xCenter - radius), (yCenter - radius), (zCenter - radius));
if (D3DXPlaneDotCoord(&plane, &a) >= 0.0f)
continue;
const D3DXVECTOR3 b((xCenter + radius), (yCenter - radius), (zCenter - radius));
if (D3DXPlaneDotCoord(&plane, &b) >= 0.0f)
continue;
const D3DXVECTOR3 c((xCenter - radius), (yCenter + radius), (zCenter - radius));
if (D3DXPlaneDotCoord(&plane, &c) >= 0.0f)
continue;
const D3DXVECTOR3 d((xCenter + radius), (yCenter + radius), (zCenter - radius));
if (D3DXPlaneDotCoord(&plane, &d) >= 0.0f)
continue;
const D3DXVECTOR3 e((xCenter - radius), (yCenter - radius), (zCenter + radius));
if (D3DXPlaneDotCoord(&plane, &e) >= 0.0f)
continue;
const D3DXVECTOR3 f((xCenter + radius), (yCenter - radius), (zCenter + radius));
if (D3DXPlaneDotCoord(&plane, &f) >= 0.0f)
continue;
const D3DXVECTOR3 g((xCenter - radius), (yCenter + radius), (zCenter + radius));
if (D3DXPlaneDotCoord(&plane, &g) >= 0.0f)
continue;
const D3DXVECTOR3 h((xCenter + radius), (yCenter + radius), (zCenter + radius));
if (D3DXPlaneDotCoord(&plane, &h) >= 0.0f)
continue;
return false;
}
return true;
}
bool Camera::isSphereInFrustum(const D3DXVECTOR3& point, const float radius) const
{
for (const auto& plane : mFrustum)
if (D3DXPlaneDotCoord(&plane, &point) < -radius)
return false;
return true;
}
bool Camera::isCuboidInFrustum(const float xCenter, const float yCenter, const float zCenter, const float xSize, const float ySize, const float zSize) const
{
for (const auto& plane : mFrustum)
{
const D3DXVECTOR3 a((xCenter - xSize), (yCenter - ySize), (zCenter - zSize));
if (D3DXPlaneDotCoord(&plane, &a) >= 0.0f)
continue;
const D3DXVECTOR3 b((xCenter + xSize), (yCenter - ySize), (zCenter - zSize));
if (D3DXPlaneDotCoord(&plane, &b) >= 0.0f)
continue;
const D3DXVECTOR3 c((xCenter - xSize), (yCenter + ySize), (zCenter - zSize));
if (D3DXPlaneDotCoord(&plane, &c) >= 0.0f)
continue;
const D3DXVECTOR3 d((xCenter - xSize), (yCenter - ySize), (zCenter + zSize));
if (D3DXPlaneDotCoord(&plane, &d) >= 0.0f)
continue;
const D3DXVECTOR3 e((xCenter + xSize), (yCenter + ySize), (zCenter - zSize));
if (D3DXPlaneDotCoord(&plane, &e) >= 0.0f)
continue;
const D3DXVECTOR3 f((xCenter + xSize), (yCenter - ySize), (zCenter + zSize));
if (D3DXPlaneDotCoord(&plane, &f) >= 0.0f)
continue;
const D3DXVECTOR3 g((xCenter - xSize), (yCenter + ySize), (zCenter + zSize));
if (D3DXPlaneDotCoord(&plane, &g) >= 0.0f)
continue;
const D3DXVECTOR3 h((xCenter + xSize), (yCenter + ySize), (zCenter + zSize));
if (D3DXPlaneDotCoord(&plane, &h) >= 0.0f)
continue;
return false;
}
return true;
}