-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentCamera.cpp
More file actions
306 lines (246 loc) · 7.54 KB
/
ComponentCamera.cpp
File metadata and controls
306 lines (246 loc) · 7.54 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "ComponentCamera.h"
#include "ComponentTransform.h"
#include "Application.h"
#include "ModuleWindow.h"
#include "ModuleProgram.h"
#include "ModuleCamera.h"
#include "ModuleRender.h"
#include "GameObject.h"
#include "SceneLoader.h"
#include <math.h>
#include "MathGeoLib/Geometry/Plane.h"
#include "debugdraw.h"
#include "GL/glew.h"
ComponentCamera::ComponentCamera(GameObject* go)
{
myGameObject = go;
myType = CAMERA;
frustum = new Frustum();
frustum->type = FrustumType::PerspectiveFrustum;
frustum->pos = float3::zero;
frustum->front = -float3::unitZ;
frustum->up = float3::unitY;
frustum->nearPlaneDistance = 0.1f;
frustum->farPlaneDistance = 50.0f;
frustum->verticalFov = (float)M_PI / 4.0f;
aspect = 1.0f;
frustum->horizontalFov = 2.0f * atanf(tanf(frustum->verticalFov * 0.5f) *aspect);
frustum->Translate(float3(0.0f, 0.0f, 0.0f));
proj = frustum->ProjectionMatrix();
view = frustum->ViewMatrix();
}
ComponentCamera::ComponentCamera(GameObject * go, ComponentCamera * comp)
{
myGameObject = go;
myType = CAMERA;
frustum = new Frustum(*comp->frustum);
proj = frustum->ProjectionMatrix();
view = frustum->ViewMatrix();
}
ComponentCamera::~ComponentCamera()
{
delete frustum;
}
void ComponentCamera::Update()
{
float3x3 quatAux = float3x3::zero;
float3 scaleAux = float3::zero;
myGameObject->myTransform->globalModelMatrix.Decompose(frustum->pos,quatAux, scaleAux);
frustum->up = float3::unitY * quatAux;
frustum->front = -float3::unitZ * quatAux;
view = frustum->ViewMatrix();
return;
}
bool ComponentCamera::CleanUp()
{
return true;
}
void ComponentCamera::SetFOV()
{
frustum->horizontalFov = 2.0f * atanf(tanf(frustum->verticalFov * 0.5f) *aspect);
proj = frustum->ProjectionMatrix();
}
void ComponentCamera::SetAspectRatio(int newWidth, int newHeight)
{
aspect = ((float)newWidth / newHeight);
frustum->horizontalFov = 2.0f * atanf(tanf(frustum->verticalFov * 0.5f) *aspect);
proj = frustum->ProjectionMatrix();
}
void ComponentCamera::Rotate(const float dx, const float dy)
{
if (dx != 0.0f)
{
float3x3 rotationY = float3x3::RotateY(dx);
frustum->front = rotationY.Transform(frustum->front).Normalized();
frustum->up = rotationY.Transform(frustum->up).Normalized();
}
if (dy != 0.0f)
{
float3x3 rotationX = float3x3::RotateAxisAngle(frustum->WorldRight(), dy);
frustum->up = rotationX.Transform(frustum->up).Normalized();
frustum->front = rotationX.Transform(frustum->front).Normalized();
}
}
void ComponentCamera::Move(const float3 &direction)
{
if (!direction.Equals(float3::zero))
frustum->Translate(direction);
}
void ComponentCamera::TranslateCameraToPoint(const float3 & newPos)
{
frustum->pos = newPos;
frustum->front = -float3::unitZ;
frustum->up = float3::unitY;
view = frustum->ViewMatrix();
}
void ComponentCamera::SetNearPlaneDistance(const float nearDist)
{
frustum->nearPlaneDistance = nearDist;
proj = frustum->ProjectionMatrix();
}
void ComponentCamera::SetFarPlaneDistance(const float farDist)
{
frustum->farPlaneDistance = farDist;
proj = frustum->ProjectionMatrix();
}
void ComponentCamera::LookAt(const float3 &target)
{
float3 dir = (target - frustum->pos).Normalized();
float3x3 rot = float3x3::LookAt(frustum->front, dir, frustum->up, float3::unitY);
frustum->front = rot.Transform(frustum->front).Normalized();
frustum->up = rot.Transform(frustum->up).Normalized();
}
void ComponentCamera::ComputeViewMatrix()
{
view = frustum->ViewMatrix();
return;
}
void ComponentCamera::ComputeProjMatrix()
{
proj = frustum->ProjectionMatrix();
return;
}
void ComponentCamera::DrawInspector()
{
if (ImGui::CollapsingHeader("Camera", ImGuiTreeNodeFlags_DefaultOpen))
{
//TODO: Make that isActive is used
ImGui::Checkbox("Active", &isActive);
ImGui::SameLine();
if(ImGui::Button("Remove Component",ImVec2(130,20)))
{
LOG("Removing Component Camera from %s", myGameObject->name);
myGameObject->components.erase(std::find(myGameObject->components.begin(), myGameObject->components.end(), this));
CleanUp();
delete this;
return;
}
ImGui::Combo(":Projection ", ¤tPerspective, perspectives, IM_ARRAYSIZE(perspectives));
ChangeFrustumType();
float auxNearPlane = frustum->nearPlaneDistance;
float auxFarPlane = frustum->farPlaneDistance;
ImGui::SliderFloat(":NearPlane dist", &auxNearPlane, 0.0001f, 9.0f);
ImGui::SliderFloat(":FarPlane dist", &auxFarPlane,10.0f, 1000.0f);
SetNearPlaneDistance(auxNearPlane);
SetFarPlaneDistance(auxFarPlane);
ImGui::SliderFloat(":FOV", &frustum->verticalFov, 0.0001f, 9.0f);
SetFOV();
}
return;
}
int ComponentCamera::AABBWithinFrustum(const AABB &aabb) const
{
//Tests if an AABB is within the frusum
//returns 0 if out, 1 if in and 2 if intersects
float3 corners[8];
aabb.GetCornerPoints(corners);
int iTotalIn = 0;
//test all 8 corners against the 6 planes of the frustum
// if all points are behind 1 specific plane, we are out
// if we are in with all points, then we are fully in
for(int p = 0; p < 6; ++p)
{
int iInCount = 8;
int iPtIn = 1;
for(int i = 0; i< 8; ++i)
{
//test this point against the planes
if(SideOfPlane(corners[i], frustum->GetPlane(p)) == FRONT)
{
iPtIn = 0;
--iInCount;
}
}
// were all the points outside of plane p?
if (iInCount == 0)
return AABB_OUT;
// check if they were all on the right side of the plane
iTotalIn += iPtIn;
}
// so if iTotalIn is 6, then all are inside the view
if (iTotalIn == 6)
return(AABB_IN);
return AABB_INTERSECT;
}
bool ComponentCamera::SideOfPlane(const float3 &point, const Plane &plane) const
{
float value = plane.normal.Dot(point);
value -= plane.d;
return (value >= 0.0f) ? true : false;
}
void ComponentCamera::OnSave(SceneLoader & loader)
{
//TODO: fix camera loading when saved
loader.AddUnsignedInt("Type", myType);
loader.AddUnsignedInt("isMainCamera", isMainCamera);
loader.AddUnsignedInt("Frustum Type", frustum->type);
loader.AddVec3f("Position", frustum->pos);
loader.AddVec3f("Front", frustum->front);
loader.AddVec3f("Up", frustum->up);
loader.AddFloat("Near Plane", frustum->nearPlaneDistance);
loader.AddFloat("Far Plane", frustum->farPlaneDistance);
loader.AddFloat("Vertical FOV", frustum->verticalFov);
loader.AddFloat("Horizontal FOV", frustum->horizontalFov);
}
void ComponentCamera::OnLoad(SceneLoader & loader)
{
frustum = new Frustum();
isMainCamera = loader.GetUnsignedInt("isMainCamera", isMainCamera);
frustum->type = (FrustumType)loader.GetUnsignedInt("Frustum Type", FrustumType::PerspectiveFrustum);
frustum->pos = loader.GetVec3f("Position", float3::zero);
frustum->front = loader.GetVec3f("Front", -float3::unitZ);
frustum->up = loader.GetVec3f("Up", float3::unitY);
frustum->nearPlaneDistance = loader.GetFloat("Near Plane", 0.1f);
frustum->farPlaneDistance = loader.GetFloat("Far Plane", 50.0f);
frustum->verticalFov = loader.GetFloat("Vertical FOV", (float)M_PI / 4.0f);
frustum->horizontalFov = loader.GetFloat("Horizontal FOV", 2.0f * atanf(tanf(frustum->verticalFov * 0.5f)));
proj = frustum->ProjectionMatrix();
view = frustum->ViewMatrix();
if(isMainCamera)
{
App->renderer->gameCamera = this;
}
}
void ComponentCamera::DrawFrustum()
{
//Draw frustum
float4x4 clipMatrix = proj * view;
dd::frustum(clipMatrix.Inverted(), float3(0, 0, 1));
return;
}
void ComponentCamera::ChangeFrustumType()
{
if (perspectives[currentPerspective] != usedPerspective)
{
if (currentPerspective == 0)
{
frustum->type = FrustumType::PerspectiveFrustum;
}
else
{
frustum->type = FrustumType::OrthographicFrustum;
}
usedPerspective = perspectives[currentPerspective];
}
return;
}