-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComponentCamera.cpp
More file actions
487 lines (398 loc) · 12.8 KB
/
ComponentCamera.cpp
File metadata and controls
487 lines (398 loc) · 12.8 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include "Application.h"
#include "ModulePhysics3D.h"
#include "ComponentCamera.h"
#include "imgui\imgui.h"
#include "DebugDraw.h"
#include "GameObject.h"
#include "ComponentTransform.h"
#include "LayerSystem.h"
#include "ResourceFileRenderTexture.h"
#include "Assets.h"
#include "ModuleWindow.h"
#include "ModuleRenderer3D.h"
#include "ModuleGOManager.h"
#include "ModuleCamera3D.h"
#include "ModuleEditor.h"
#include "Brofiler\include\Brofiler.h"
ComponentCamera::ComponentCamera(ComponentType type, GameObject* game_object) : Component(type, game_object)
{
//Init frustrum
aspect_ratio = (float)App->window->GetScreenWidth() / (float)App->window->GetScreenHeight();
float vertical_fov = DegToRad(fov);
float horizontal_fov = 2.0f*atanf(tanf(vertical_fov / 2.0f) * aspect_ratio);
frustum.SetPerspective(horizontal_fov, vertical_fov);
frustum.SetKind(FrustumSpaceGL, FrustumRightHanded);
if (game_object)
{
float4x4 matrix = game_object->GetGlobalMatrix();
frustum.SetPos(matrix.TranslatePart());
frustum.SetFront(matrix.WorldZ());
frustum.SetUp(matrix.WorldY());
}
else
{
frustum.SetPos(float3(0, 0, 0));
frustum.SetFront(float3::unitZ);
frustum.SetUp(float3::unitY);
}
frustum.SetViewPlaneDistances(near_plane, far_plane);
frustum.SetVerticalFovAndAspectRatio(DegToRad(fov), aspect_ratio);
color = float3(0, 0, 0); //Black to clear the screen by default
OnTransformModified();
App->renderer3D->AddObserver(this);
App->camera->AddSceneCamera(this);
}
ComponentCamera::~ComponentCamera()
{
App->renderer3D->RemoveObserver(this);
if(render_texture)
render_texture->Unload();
App->camera->RemoveSceneCamera(this);
}
void ComponentCamera::PreUpdate()
{
UpdateCameraFrustum();
}
void ComponentCamera::Update()
{
if (App->StartInGame() == false && App->IsGameRunning() == false && App->physics->renderColliders)
g_Debug->AddFrustum(frustum, 30.0f, g_Debug->blue, 2.0f);
}
void ComponentCamera::OnInspector(bool debug)
{
string str = (string("Camera") + string("##") + std::to_string(uuid));
if (ImGui::CollapsingHeader(str.c_str(), ImGuiTreeNodeFlags_DefaultOpen))
{
if (ImGui::IsItemClicked(1))
{
ImGui::OpenPopup("delete##camera");
}
if (ImGui::BeginPopup("delete##camera"))
{
if (ImGui::MenuItem("Delete"))
{
Remove();
}
ImGui::EndPopup();
}
ImGui::Checkbox("Render terrain", &renderTerrain);
ImGui::Checkbox("Smooth follow", &smoothFollow);
if (smoothFollow)
{
ImGui::Text("Position follow speed:");
ImGui::DragFloat("##smoothFollowPos", &followMoveSpeed, 0.01f, 0.01f, 0.99f);
ImGui::Text("Rotation follow speed:");
ImGui::DragFloat("##smoothFollowRot", &followRotateSpeed, 0.01f, 0.01f, 0.99f);
}
ImGui::Separator();
ImGui::Text("Viewport (relative to screen)");
//ImGui::PushStyleColor(ImGuiCol_Text)
ImGui::Text("0 to 1 values");
if (ImGui::InputFloat("Position x", &viewport_rel_position.x, 0.0f, 1.0f))
{
math::Clamp(viewport_rel_position.x, 0.0f, 1.0f);
UpdateViewportDimensions();
}
if (ImGui::InputFloat("Position y", &viewport_rel_position.y, 0.0f, 1.0f))
{
math::Clamp(viewport_rel_position.y, 0.0f, 1.0f);
UpdateViewportDimensions();
}
if (ImGui::InputFloat("Size x ", &viewport_rel_size.x, 0.0f, 1.0f))
{
math::Clamp(viewport_rel_size.x, 0.0f, 1.0f);
UpdateViewportDimensions();
}
if (ImGui::InputFloat("Size y", &viewport_rel_size.y, 0.0f, 1.0f))
{
math::Clamp(viewport_rel_size.y, 0.0f, 1.0f);
UpdateViewportDimensions();
}
ImGui::Separator();
ImGui::Text("Frustum");
//Near plane
ImGui::Text("Near Plane: ");
float near_value = near_plane;
if (ImGui::SliderFloat("##near_p", &near_value, 0, 1, "%.3f", 0.05f))
SetNearPlane(near_value);
//Far plane
ImGui::Text("Far Plane: ");
float far_value = far_plane;
if (ImGui::SliderFloat("##far_p", &far_value, 0, 2000))
SetFarPlane(far_value);
//Field of view
ImGui::Text("Field of view: ");
float fov_value = fov;
if (ImGui::SliderFloat("##fov", &fov_value, 0, 180))
SetFOV(fov_value);
ImGui::Text("Background color: "); ImGui::SameLine();
float3 color = this->color;
if (ImGui::ColorEdit3("", color.ptr()))
{
this->color = color;
}
//LayerMask
App->go_manager->layer_system->DisplayLayerMask(layer_mask);
//RenderTexture
string ren_name = "RenderTexture: " + ((render_texture) ? render_texture_path : "none");
if (ImGui::BeginMenu(ren_name.data()))
{
vector<string> rentex_list;
App->editor->assets->GetAllFilesByType(FileType::RENDER_TEXTURE, rentex_list);
for (vector<string>::iterator rentex = rentex_list.begin(); rentex != rentex_list.end(); rentex++)
{
if (ImGui::MenuItem((*rentex).data()))
{
render_texture_path = (*rentex).data();
render_texture_path_lib = App->resource_manager->FindFile(render_texture_path);
render_texture = (ResourceFileRenderTexture*)App->resource_manager->LoadResource(render_texture_path_lib, ResourceFileType::RES_RENDER_TEX);
}
}
ImGui::EndMenu();
}
ImGui::Checkbox("Render skybox###render_skybox_cam", &render_skybox);
ImGui::Separator();
if (ImGui::Button("Remove ###cam_rem"))
{
Remove();
}
}
}
void ComponentCamera::OnTransformModified()
{
GameObject* game_object = GetGameObject();
if (game_object)
desiredTransform = game_object->transform->GetGlobalMatrix();
else
{
LOG("[ERROR] Component Camera is trying to update it's matrix but it is not attached to any game object.");
App->editor->DisplayWarning(WarningType::W_ERROR, "Component Camera is trying to update it's matrix but it is not attached to any game object" );
}
}
void ComponentCamera::OnNotify(void * entity, Event event)
{
if (event == Event::WINDOW_RESIZE)
{
UpdateViewportDimensions();
}
}
void ComponentCamera::UpdateViewportDimensions()
{
viewport_position.x = (float)App->window->GetScreenWidth() * viewport_rel_position.x;
viewport_position.y = (float)App->window->GetScreenHeight() * viewport_rel_position.y;
viewport_size.x = (float)App->window->GetScreenWidth() * viewport_rel_size.x;
viewport_size.y = (float)App->window->GetScreenHeight() * viewport_rel_size.y;
aspect_ratio = viewport_size.x / viewport_size.y;
frustum.SetVerticalFovAndAspectRatio(DegToRad(fov), aspect_ratio);
}
float ComponentCamera::GetNearPlane() const
{
return near_plane;
}
float ComponentCamera::GetFarPlane() const
{
return far_plane;
}
float ComponentCamera::GetFOV() const
{
return fov;
}
math::float3 ComponentCamera::GetFront() const
{
return frustum.Front();
}
math::float3 ComponentCamera::GetUp() const
{
return frustum.Up();
}
math::float3 ComponentCamera::GetPos()const
{
return frustum.Pos();
}
math::float4x4 ComponentCamera::GetProjectionMatrix() const
{
math::float4x4 matrix = frustum.ProjectionMatrix();
matrix.Transpose();
return matrix;
}
math::float3 ComponentCamera::GetBackgroundColor() const
{
return color;
}
math::float3 ComponentCamera::GetWorldRight() const
{
return frustum.WorldRight();
}
void ComponentCamera::SetNearPlane(float value)
{
if (value < far_plane && value > 0.0f)
near_plane = value;
frustum.SetViewPlaneDistances(near_plane, far_plane);
properties_modified = true;
}
void ComponentCamera::SetFarPlane(float value)
{
if (value > near_plane)
far_plane = value;
frustum.SetViewPlaneDistances(near_plane, far_plane);
properties_modified = true;
}
void ComponentCamera::SetFOV(float value)
{
//TODO: check for min -max values of fov
fov = value;
frustum.SetVerticalFovAndAspectRatio(DegToRad(fov), aspect_ratio);
properties_modified = true;
}
void ComponentCamera::SetAspectRatio(float value)
{
aspect_ratio = value;
SetFOV(fov);
//float horizontalFov = frustum.HorizontalFov();
//frustum.SetHorizontalFovAndAspectRatio(horizontalFov, value);
//properties_modified = true;
}
void ComponentCamera::LookAt(const math::float3 & point)
{
float3 look_direction = point - frustum.Pos();
float3x3 matrix = float3x3::LookAt(frustum.Front(), look_direction.Normalized(), frustum.Up(), float3::unitY);
frustum.SetFront(matrix.MulDir(frustum.Front()).Normalized());
frustum.SetUp(matrix.MulDir(frustum.Up()).Normalized());
properties_modified = true;
}
void ComponentCamera::Center(const float3& position, float distance)
{
float3 v = frustum.Front().Neg();
frustum.SetPos(position + v * distance);
}
void ComponentCamera::SetBackgroundColor(const math::float3 color)
{
this->color = color;
}
bool ComponentCamera::Intersects(const math::AABB & box)const
{
bool ret = true;
math::vec corners[8];
box.GetCornerPoints(corners);
math::Plane planes[6];
frustum.GetPlanes(planes);
for (int p = 0; p < 6; p++)
{
int count = 0;
for (int i = 0; i < 8; i++)
{
if (planes[p].IsOnPositiveSide(corners[i]))
count++;
}
if (count == 8)
{
ret = false;
break;
}
}
return ret;
}
math::float4x4 ComponentCamera::GetViewMatrix() const
{
math::float4x4 matrix = frustum.ViewMatrix();
return matrix.Transposed();
}
math::float4x4 ComponentCamera::GetWorldMatrix() const
{
return frustum.WorldMatrix();
}
int ComponentCamera::GetLayerMask() const
{
return layer_mask;
}
void ComponentCamera::Save(Data & file)const
{
Data data;
data.AppendInt("type", type);
data.AppendUInt("UUID", uuid);
data.AppendBool("active", active);
data.AppendFloat("near_plane", near_plane);
data.AppendFloat("far_plane", far_plane);
data.AppendFloat("fov", fov);
data.AppendFloat("aspect_ratio", aspect_ratio);
data.AppendFloat3("color", color.ptr());
data.AppendInt("layer_mask", layer_mask);
data.AppendString("render_texture_path", render_texture_path.data());
data.AppendString("render_texture_path_lib", render_texture_path_lib.data());
data.AppendFloat("viewport_rel_pos_x", viewport_rel_position.x);
data.AppendFloat("viewport_rel_pos_y", viewport_rel_position.y);
data.AppendFloat("viewport_rel_size_x", viewport_rel_size.x);
data.AppendFloat("viewport_rel_size_y", viewport_rel_size.y);
data.AppendBool("followSmooth", smoothFollow);
data.AppendFloat("followMovSpeed", followMoveSpeed);
data.AppendFloat("followRotSpeed", followRotateSpeed);
data.AppendBool("render_terrain", renderTerrain);
data.AppendBool("render_skybox", render_skybox);
file.AppendArrayValue(data);
}
void ComponentCamera::Load(Data & conf)
{
uuid = conf.GetUInt("UUID");
active = conf.GetBool("active");
near_plane = conf.GetFloat("near_plane");
far_plane = conf.GetFloat("far_plane");
fov = conf.GetFloat("fov");
aspect_ratio = conf.GetFloat("aspect_ratio");
color = conf.GetFloat3("color");
layer_mask = conf.GetInt("layer_mask");
render_texture_path = conf.GetString("render_texture_path");
render_texture_path_lib = conf.GetString("render_texture_path_lib");
viewport_rel_position.x = conf.GetFloat("viewport_rel_pos_x");
viewport_rel_position.y = conf.GetFloat("viewport_rel_pos_y");
viewport_rel_size.x = conf.GetFloat("viewport_rel_size_x");
viewport_rel_size.y = conf.GetFloat("viewport_rel_size_y");
smoothFollow = conf.GetBool("followSmooth");
followMoveSpeed = conf.GetFloat("followMovSpeed");
followRotateSpeed = conf.GetFloat("followRotSpeed");
renderTerrain = conf.GetBool("render_terrain");
render_skybox = conf.GetBool("render_skybox");
//Init frustrum
float vertical_fov = DegToRad(fov);
float horizontal_fov = 2.0f*atanf(tanf(vertical_fov / 2.0f) * aspect_ratio);
frustum.SetPerspective(horizontal_fov, vertical_fov);
frustum.SetKind(FrustumProjectiveSpace::FrustumSpaceGL, FrustumHandedness::FrustumRightHanded);
frustum.SetPos(float3::zero);
frustum.SetFront(float3::unitZ);
frustum.SetUp(float3::unitY);
frustum.SetViewPlaneDistances(near_plane, far_plane);
frustum.SetVerticalFovAndAspectRatio(DegToRad(fov), aspect_ratio);
UpdateViewportDimensions();
OnTransformModified();
//Init render texture
if (render_texture_path_lib.size() != 0)
render_texture = (ResourceFileRenderTexture*)App->resource_manager->LoadResource(render_texture_path_lib, ResourceFileType::RES_RENDER_TEX);
}
math::Ray ComponentCamera::CastCameraRay(math::float2 screen_pos)
{
float2 pos = screen_pos;
pos.x = 2.0f * pos.x / (float)App->window->GetScreenWidth() - 1.0f;
pos.y = 1.0f - 2.0f * pos.y / (float)App->window->GetScreenHeight();
Ray ray = frustum.UnProjectFromNearPlane(pos.x, pos.y);
return ray;
}
void ComponentCamera::UpdateCameraFrustum()
{
BROFILER_CATEGORY("ComponentCamera::UpdateCameraFrustum", Profiler::Color::MediumTurquoise)
if (smoothFollow == true)
{
float3 curr_pos, des_pos, scale;
Quat curr_rot, des_rot;
desiredTransform.Decompose(des_pos, des_rot, scale);
currentTransform.Decompose(curr_pos, curr_rot, scale);
Quat rotation = curr_rot.Lerp(des_rot, followRotateSpeed);
float3 position = curr_pos.Lerp(des_pos, followMoveSpeed);
currentTransform = float4x4::FromTRS(position, rotation, float3::one);
}
else
{
currentTransform = desiredTransform;
}
frustum.SetPos(currentTransform.TranslatePart());
frustum.SetFront(currentTransform.WorldZ());
frustum.SetUp(currentTransform.WorldY());
}