-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.cpp
617 lines (512 loc) · 18.5 KB
/
app.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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#include "app.h"
#include <QDebug>
#include <QJsonDocument>
#include <QDir>
#include "inputhandler.h"
#include "scene.h"
#include "entitymanager.h"
#include "inputsystem.h"
#include "physicssystem.h"
#include "scriptsystem.h"
#include "Instrumentor.h"
App::App()
{
PROFILE_BEGIN_SESSION("Startup", "Profile-Startup");
PROFILE_FUNCTION();
mSoundManager = std::unique_ptr<SoundManager>(new SoundManager());
mSoundListener = std::make_unique<SoundListener>();
mMainWindow = std::make_unique<MainWindow>();
mRenderer = mMainWindow->getRenderer();
connect(mMainWindow.get(), &MainWindow::toggleWireframe, mRenderer, &Renderer::toggleWireframe);
connect(mMainWindow.get(), &MainWindow::shutUp, this, &App::toggleMute);
connect(mMainWindow.get(), &MainWindow::play, this, &App::onPlay);
connect(mMainWindow.get(), &MainWindow::stop, this, &App::onStop);
connect(mMainWindow.get(), &MainWindow::quitting, this, &App::saveSession);
connect(mRenderer, &Renderer::initDone, this, &App::initTheRest);
connect(mRenderer, &Renderer::windowUpdated, this, &App::updatePerspective);
mEventHandler = std::make_shared<InputHandler>(mRenderer);
connect(mEventHandler.get(), &InputHandler::escapeKeyPressed, mMainWindow.get(), &MainWindow::close);
connect(mEventHandler.get(), &InputHandler::mousePress, this, &App::mousePicking);
mRenderer->installEventFilter(mEventHandler.get());
}
App::~App()
{
PROFILE_END_SESSION();
}
// Slot called from Renderer when its done with initialization
void App::initTheRest()
{
PROFILE_FUNCTION();
mResourceManager = std::unique_ptr<ResourceManager>(new ResourceManager{});
mWorld = std::unique_ptr<World>(new World{});
connect(mMainWindow.get(), &MainWindow::newScene, this, &App::newScene);
connect(mMainWindow.get(), &MainWindow::saveScene, this, &App::saveScene);
connect(mMainWindow.get(), &MainWindow::loadScene, this, &App::loadScene);
connect(mWorld->getEntityManager().get(), &EntityManager::updateUI, mMainWindow.get(), &MainWindow::updateUI);
connect(mWorld.get(), &World::updateSceneName, mMainWindow.get(), &MainWindow::setSceneName);
// Load editor session data
loadSession("session.json");
if (!mWorld->isSceneValid())
mWorld->initBlankScene();
// Setup update loop
connect(&mUpdateTimer, &QTimer::timeout, this, &App::update);
mDeltaTimer.start();
mFPSTimer.start();
mSoundManager->checkOpenALError();
// Send skybox data to renderer
auto skyboxMesh = ResourceManager::instance().getMesh("skybox");
auto skyboxMaterial = std::make_shared<Material>();
auto skyShader = ResourceManager::instance().getShader("skybox");
skyboxMaterial->mShader = skyShader;
auto texture = ResourceManager::instance().getTexture("skybox");
skyboxMaterial->mTextures.push_back({texture->id(), texture->mType});
mRenderer->mSkyboxMesh = skyboxMesh;
mRenderer->mSkyboxMaterial = skyboxMaterial;
// Send axis data to renderer
auto axisMesh = ResourceManager::instance().getMesh("axis");
axisMesh->mRenderType = GL_LINES;
auto axisMaterial = std::make_shared<Material>();
axisMaterial->mShader = ResourceManager::instance().getShader("axis");
mRenderer->mAxisMesh = axisMesh;
mRenderer->mAxisMaterial = axisMaterial;
// Used by the post processor only atm.
if(!QDir("../Inngine2019/Settings").exists())
{
QDir().mkdir("../Inngine2019/Settings");
}
// ---------- Postprocessing setup ---------------------------
initPostprocessorSettings();
PROFILE_END_SESSION();
mUpdateTimer.start(16); // Simulates 60ish fps
PROFILE_BEGIN_SESSION("Editor", "Profile-Editor");
}
void App::toggleMute(bool mode)
{
if (mSoundListener)
mSoundListener->setMute(mode);
}
void App::mousePicking()
{
if(mCurrentlyPlaying)
return;
PROFILE_FUNCTION();
auto mousePoint = mRenderer->mapFromGlobal(QCursor::pos());
auto cameras = mWorld->getEntityManager()->getCameraComponents();
auto meshes = mWorld->getEntityManager()->getMeshComponents();
auto transforms = mWorld->getEntityManager()->getTransformComponents();
if (!cameras.empty())
{
auto entity = mRenderer->getMouseHoverObject(gsl::ivec2{mousePoint.x(), mousePoint.y()}, meshes, transforms, cameras.front());
bool found{false};
for (auto it = mMainWindow->mTreeDataCache.begin(); it != mMainWindow->mTreeDataCache.end(); ++it)
{
if (it->second.entityId == entity)
{
mMainWindow->setSelected(&it->second);
found = true;
break;
}
}
if(!found)
{
mMainWindow->setSelected(nullptr);
}
}
}
void App::update()
{
// Not exactly needed now, but maybe this should be here? Timer does call this function every 16 ms.
if(currentlyUpdating)
return;
PROFILE_FUNCTION();
currentlyUpdating = true;
mDeltaTime = mDeltaTimer.restart() / 1000.f;
mRenderer->mTimeSinceStart += mDeltaTime;
calculateFrames();
auto& transforms = mWorld->getEntityManager()->getTransformComponents();
auto& physics = mWorld->getEntityManager()->getPhysicsComponents();
auto& colliders = mWorld->getEntityManager()->getColliderComponents();
auto currentCamera = mWorld->getCurrentCamera(mCurrentlyPlaying);
auto cameraTransform = mWorld->getEntityManager()->getComponent<TransformComponent>(currentCamera->entityId);
{
PROFILE_SCOPE("Camera");
// Camera:
/* Note: Current camera depends on if the engine is in editor-mode or not.
* Editor camera is handled in C++, game camera is handled through script.
*/
mEventHandler->updateMouse(mCurrentlyPlaying);
if(!mCurrentlyPlaying)
{
InputSystem::HandleEditorCameraInput(mDeltaTime, *cameraTransform, *currentCamera);
}
CameraSystem::updateLookAtRotation(*cameraTransform, *currentCamera);
CameraSystem::updateCameraViewMatrices(transforms, mWorld->getEntityManager()->getCameraComponents());
}
std::vector<HitInfo> hitInfos;
{
PROFILE_SCOPE("Physics");
// Physics:
/* Note: Physics calculation should be happening on a separate thread
* and instead of sending references to the lists we should take copies
* and then later apply those copies to the original lists.
*/
if (mCurrentlyPlaying)
hitInfos = PhysicsSystem::UpdatePhysics(transforms, physics, colliders, mDeltaTime);
}
{
PROFILE_SCOPE("Sounds");
// Sound:
// Note: Sound listener is using the active camera view matrix (for directions) and transform (for position)
auto& sounds = mWorld->getEntityManager()->getSoundComponents();
mSoundListener->update(*currentCamera, *cameraTransform);
mSoundManager->UpdatePositions(transforms, sounds);
mSoundManager->UpdateVelocities(physics, sounds);
}
{
PROFILE_SCOPE("Bounds");
// Calculate mesh bounds
// Note: This is only done if the transform has changed.
mWorld->getEntityManager()->UpdateBounds();
// -------- Frustum culling here -----------
}
{
PROFILE_SCOPE("Rendering");
// Rendering
auto& renders = mWorld->getEntityManager()->getMeshComponents();
mRenderer->render(renders, transforms, *currentCamera,
mWorld->getEntityManager()->getDirectionalLightComponents(),
mWorld->getEntityManager()->getSpotLightComponents(),
mWorld->getEntityManager()->getPointLightComponents(),
mWorld->getEntityManager()->getParticleComponents());
}
// JAVASCRIPT HERE
if(mCurrentlyPlaying)
{
PROFILE_SCOPE("Scripting");
auto& scripts = mWorld->getEntityManager()->getScriptComponents();
ScriptSystem::get()->updateJSComponents(scripts);
auto& inputs = mWorld->getEntityManager()->getInputComponents();
ScriptSystem::get()->update(scripts, inputs, mEventHandler->inputPressedStrings,
mEventHandler->inputReleasedStrings, mEventHandler->MouseOffset,
hitInfos, mDeltaTime);
ScriptSystem::get()->updateCPPComponents(scripts);
mEventHandler->inputReleasedStrings.clear();
mWorld->getEntityManager()->removeEntitiesMarked();
static unsigned int garbageCounter{0};
garbageCounter++;
if (garbageCounter - 1 > ScriptSystem::get()->garbageCollectionFrequency)
{
garbageCounter = 0;
ScriptSystem::get()->takeOutTheTrash(scripts);
}
}
currentlyUpdating = false;
}
void App::quit()
{
saveSession();
mMainWindow->close();
}
void App::updatePerspective()
{
PROFILE_FUNCTION();
CameraSystem::updateCameraProjMatrices(mWorld->getEntityManager()->getCameraComponents(),
FOV, static_cast<float>(mRenderer->width()) / mRenderer->height(), 0.1f, 100.f);
}
// Called when play action is pressed while not playing in UI
void App::onPlay()
{
PROFILE_END_SESSION();
PROFILE_BEGIN_SESSION("Play", "Profile-Play");
PROFILE_FUNCTION();
mCurrentlyPlaying = true;
saveSession();
mWorld->saveTemp();
auto currentCamera = mWorld->getCurrentCamera(mCurrentlyPlaying);
if(auto mesh = mWorld->getEntityManager()->getComponent<MeshComponent>(currentCamera->entityId))
{
mesh->isVisible = false;
}
mMainWindow->setSelected(nullptr);
auto sounds = mWorld->getEntityManager()->getSoundComponents();
mSoundManager->playOnStartup(sounds);
}
// Called when play action is pressed while playing in UI
void App::onStop()
{
PROFILE_END_SESSION();
PROFILE_BEGIN_SESSION("Editor", "Profile-Editor");
PROFILE_FUNCTION();
mCurrentlyPlaying = false;
auto& scripts = mWorld->getEntityManager()->getScriptComponents();
ScriptSystem::get()->endPlay(scripts);
auto sounds = mWorld->getEntityManager()->getSoundComponents();
mSoundManager->stop(sounds);
mRenderer->EditorCurrentEntitySelected = nullptr;
auto currentCamera = mWorld->getCurrentCamera(mCurrentlyPlaying);
if(auto mesh = mWorld->getEntityManager()->getComponent<MeshComponent>(currentCamera->entityId))
{
mesh->isVisible = true;
}
ScriptSystem::get()->initGarbageCollection();
// Reset JS states.
for(auto& comp : mWorld->getEntityManager()->getScriptComponents())
{
if(comp.JSEntity)
{
delete comp.JSEntity;
comp.JSEntity = nullptr;
}
}
mWorld->loadTemp();
updatePerspective();
}
void App::calculateFrames()
{
++mFrameCounter;
mTotalDeltaTime += mDeltaTime;
double elapsed = mFPSTimer.elapsed();
if(elapsed >= 1000)
{
mMainWindow->updateStatusBar(mRenderer->getNumberOfVerticesDrawn(), (mTotalDeltaTime / mFrameCounter) * 1000.f, mFrameCounter / mTotalDeltaTime);
mFrameCounter = 0;
mTotalDeltaTime = 0;
mFPSTimer.restart();
}
}
void App::loadSession(const std::string &path)
{
QFile file(QString::fromStdString(path));
if(!file.open(QIODevice::ReadOnly))
{
return;
}
QJsonDocument document = QJsonDocument::fromJson(file.readAll());
if(!document.isObject())
{
return;
}
QJsonObject mainObject = document.object();
if(mainObject.isEmpty())
{
return;
}
if (mWorld)
{
auto value = mainObject["DefaultMap"];
if (value.isString())
{
mWorld->loadScene(value.toString().toStdString());
}
}
}
void App::saveSession()
{
QFile file{"session.json"};
if (!file.open(QIODevice::ReadWrite | QIODevice::Truncate))
{
qDebug() << "Failed to save session file.";
return;
}
QJsonObject mainObject{};
if (mWorld)
if (auto path = mWorld->sceneFilePath())
mainObject["DefaultMap"] = QString::fromStdString(path.value());
file.write(QJsonDocument{mainObject}.toJson());
}
void App::newScene()
{
mWorld->newScene();
updatePerspective();
}
void App::loadScene(const std::string& path)
{
mWorld->loadScene(path);
updatePerspective();
}
void App::saveScene(const std::string& path)
{
mWorld->saveScene(path);
saveSession();
updatePerspective();
}
void App::initPostprocessorSettings()
{
QFile file("../Inngine2019/Settings/postprocessorsettings.json");
if(!file.exists())
{
writeDefaultPostprocessorSettings();
}
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "ERROR Postprocessor init: Failed to open '../Inngine2019/Settings/postprocessorsettings.json'!";
return;
}
mRenderer->mPostprocessor->setTextureFormat(GL_RGBA16F);
mRenderer->mPostprocessor->outputToDefault = true;
// Bloom setup
mRenderer->mBloomEffect->outputToDefault = false;
mRenderer->mBloomEffect->setTextureFormat(GL_RGBA16F);
mRenderer->mBloomEffect->autoUpdateSize = false;
// Basically the sample size, but also indirectly scales the bloom effect
mRenderer->mBloomEffect->setSize({512, 256});
// Outline effect setup
mRenderer->mOutlineeffect->outputToDefault = false;
QJsonDocument document = QJsonDocument::fromJson(file.readAll());
QJsonArray mainObject = document.array();
for(auto ref : mainObject)
{
auto obj = ref.toObject();
auto postprocessor = obj["postprocessor"];
auto shader = obj["shader"].toString().toStdString();
auto params = retreiveParameters(obj["parameters"].toObject());
if(postprocessor == "main")
{
mRenderer->mPostprocessor->steps.emplace_back(
std::make_shared<Material>(ResourceManager::instance().getShader(shader), params)
);
}
else if(postprocessor == "bloom")
{
mRenderer->mBloomEffect->steps.emplace_back(
std::make_shared<Material>(ResourceManager::instance().getShader(shader), params)
);
}
else if(postprocessor == "outlineeffect")
{
mRenderer->mOutlineeffect->steps.emplace_back(
std::make_shared<Material>(ResourceManager::instance().getShader(shader), params)
);
}
}
file.close();
std::vector<std::pair<std::string, Postprocessor*>> pairs;
pairs.emplace_back(std::make_pair<std::string, Postprocessor*>("Main Post Processor", mRenderer->mPostprocessor.get()));
pairs.emplace_back(std::make_pair<std::string, Postprocessor*>("Bloom effect", mRenderer->mBloomEffect.get()));
pairs.emplace_back(std::make_pair<std::string, Postprocessor*>("Outline effect", mRenderer->mOutlineeffect.get()));
mMainWindow->addGlobalPostProcessing(pairs);
}
void App::writeDefaultPostprocessorSettings()
{
// Each one of these objects represents a step in a global postprocessor.
// Only three objects should be in each step.
// 1. The postprocessor the step should be added to
// 2. The shader that should be used.
// 3. An object containing all parameters that should be editable on runtime.
QJsonObject gamma
{
{"postprocessor", "main"},
{"shader", "gammaCorrection"},
{"parameters",
QJsonObject
{
{"gamma", 1.4},
{"exposure", 0.8}
}
}
};
QJsonObject extract
{
{"postprocessor", "bloom"},
{"shader", "extractThreshold"},
{"parameters",
QJsonObject
{
{"threshold", 1.0},
{"multFactor", 3.0}
}
}
};
QJsonObject gaussainBlur1
{
{"postprocessor", "bloom"},
{"shader", "gaussianBlur"},
{"parameters",
QJsonObject
{
{"horizontal", false}
}
}
};
QJsonObject gaussainBlur2
{
{"postprocessor", "bloom"},
{"shader", "gaussianBlur"},
{"parameters",
QJsonObject
{
{"horizontal", true}
}
}
};
QJsonObject outline
{
{"postprocessor", "outlineeffect"},
{"shader", "ui_singleColor"},
{"parameters",
QJsonObject
{
{"p_color", QJsonArray{1.0, 1.0, 0.0}}
}
}
};
QJsonObject blur
{
{"postprocessor", "outlineeffect"},
{"shader", "blur"},
{"parameters",
QJsonObject
{
{"radius", 2.0}
}
}
};
QFile file("../Inngine2019/Settings/postprocessorsettings.json");
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "ERROR Postprocessor init: Failed to open '../Inngine2019/Settings/postprocessorsettings.json'!";
return;
}
QJsonArray mainArray{gamma, extract, gaussainBlur1, gaussainBlur2, outline, blur};
QJsonDocument document(mainArray);
file.write(document.toJson());
file.close();
}
std::map<std::string, ShaderParamType> App::retreiveParameters(QJsonObject object)
{
std::map<std::string, ShaderParamType> params;
foreach(const QString& key, object.keys())
{
auto value = object.value(key);
switch (value.type())
{
case QJsonValue::Type::Bool:
params[key.toStdString()] = value.toBool();
break;
case QJsonValue::Type::Array:
{
auto array = value.toArray();
switch (array.size())
{
case 2:
params[key.toStdString()] = gsl::vec2(static_cast<float>(array[0].toDouble()), static_cast<float>(array[1].toDouble()));
break;
case 3:
params[key.toStdString()] = gsl::vec3(static_cast<float>(array[0].toDouble()), static_cast<float>(array[1].toDouble()), static_cast<float>(array[2].toDouble()));
break;
case 4:
params[key.toStdString()] = gsl::vec4(static_cast<float>(array[0].toDouble()), static_cast<float>(array[1].toDouble()), static_cast<float>(array[2].toDouble()), static_cast<float>(array[3].toDouble()));
break;
}
break;
}
case QJsonValue::Type::Double:
params[key.toStdString()] = static_cast<float>(value.toDouble());
break;
default:
qDebug() << "Value is something else!";
break;
}
}
return params;
}