-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
360 lines (286 loc) · 11.3 KB
/
main.cpp
File metadata and controls
360 lines (286 loc) · 11.3 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
// Copyright (c) 2015 Pierre Fourgeaud
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "main/main.h"
#include <consolelogger.h>
#include <filelogger.h>
#include <logger.h>
#include "core/enginecontext.h"
#include "core/image.h"
#include "core/resourcecache.h"
#include "core/resourceloader.h"
#include "core/time.h"
#include "graphics/batch.h"
#include "graphics/camera.h"
#include "graphics/cube.h"
#include "graphics/light.h"
#include "graphics/material.h"
#include "graphics/mesh.h"
#include "graphics/model.h"
#include "graphics/plane.h"
#include "graphics/renderwindow.h"
#include "graphics/scene.h"
#include "graphics/shader.h"
#include "graphics/shaderprogram.h"
#include "graphics/skybox.h"
#include "graphics/technique.h"
#include "graphics/viewport.h"
#include "input/input.h"
#include "ui/button.h"
#include "ui/font.h"
#include "ui/label.h"
#include "ui/ui.h"
#include "ui/window.h"
#include "rendersystems/GL/rendersystemGL.h"
#ifdef DRIVER_PNG
#include "drivers/png/imagecodec_png.h"
#endif // DRIVER_PNG
#ifdef DRIVER_JPG
#include "drivers/jpg/imagecodec_jpg.h"
#endif // DRIVER_JPG
#ifdef DRIVER_DDS
#include "drivers/dds/imagecodec_dds.h"
#endif // DRIVER_DDS
#ifdef DRIVER_TGA
#include "drivers/tga/imagecodec_tga.h"
#endif // DRIVER_TGA
#ifdef DRIVER_ASSIMP
#include "drivers/assimp/modelcodec_assimp.h"
#endif // DRIVER_ASSIMP
#ifdef DRIVER_PUGIXML
#include "drivers/xml/serializablecodec_xml.h"
#endif // DRIVER_PUGIXML
const uint32_t g_Width = 1280;
const uint32_t g_Height = 800;
namespace CodeHero {
Main::Main() {
_Initialize();
}
Main::~Main() {
_Cleanup();
}
Error Main::Start() {
LOGD2 << "[>] Main::Start()" << std::endl;
// Create the context that will be the base of everything coming after
m_pContext = std::shared_ptr<EngineContext>(new EngineContext);
// Register objects first
Scene::RegisterObject(m_pContext);
Node::RegisterObject(m_pContext);
Light::RegisterObject(m_pContext);
Shader::RegisterObject(m_pContext);
Texture::RegisterObject(m_pContext);
Technique::RegisterObject(m_pContext);
Material::RegisterObject(m_pContext);
Skybox::RegisterObject(m_pContext);
Model::RegisterObject(m_pContext);
Mesh::RegisterObject(m_pContext);
Plane::RegisterObject(m_pContext);
Cube::RegisterObject(m_pContext);
// Initialize the time as soon as possible
Time* time = new Time(m_pContext);
time->Initialize();
m_pContext->RegisterSubsystem(time);
ResourceCache* rc = new ResourceCache(m_pContext);
rc->Initialize();
m_pContext->RegisterSubsystem(rc);
ResourceLoader<Image>* rlImage = new ResourceLoader<Image>(m_pContext);
rlImage->Initialize();
m_pContext->RegisterSubsystem(rlImage);
ResourceLoader<Model>* rlModel = new ResourceLoader<Model>(m_pContext);
rlModel->Initialize();
m_pContext->RegisterSubsystem(rlModel);
ResourceLoader<Serializable>* rlSerializable = new ResourceLoader<Serializable>(m_pContext);
rlSerializable->Initialize();
m_pContext->RegisterSubsystem(rlSerializable);
// m_pRS.reset(new RenderSystemGL);
RenderSystem* rs = new RenderSystemGL(m_pContext);
Error error = rs->Initialize();
m_pContext->RegisterSubsystem(rs);
if (!error) {
_LoadDrivers();
std::shared_ptr<RenderWindow> mainWindow = rs->CreateWindow(g_Width, g_Height);
// This should be after window since some event might be tight to the window
Input* input = new Input(m_pContext);
input->Initialize();
m_pContext->RegisterSubsystem(input);
// Then we should add the input handle to the window
mainWindow->SetInputHandler(m_pContext->GetSubsystem<Input>());
}
LOGD2 << "[<] Main::Start()" << std::endl;
return error;
}
Error Main::Run() {
LOGD2 << "[>] Main::Run()" << std::endl;
// Cache the time to avoid repeating the query every loop
Time* time = m_pContext->GetSubsystem<Time>();
RenderSystem* rs = m_pContext->GetSubsystem<RenderSystem>();
rs->SetCullMode(true);
std::shared_ptr<RenderWindow> mainWindow = rs->GetWindow();
UI ui(m_pContext);
auto label = std::make_shared<Label>(m_pContext);
label->SetPosition(15.0f, 15.0f);
auto button = std::make_shared<Button>(m_pContext);
button->SetPosition(15.0f, 55.0f);
button->SetSize({150.0f, 50.0f});
auto window = std::make_shared<Window>(m_pContext);
window->SetPosition(20.0f, 185.0f);
window->SetSize({300.0f, 400.0f});
window->SetHeader("Info");
ui.AddChild(window);
window->AddChild(label);
window->AddChild(button);
// We should be checking for the return values here. Being an example code, we will skip that
// for now
auto textShaderVert = Shader::Create(m_pContext);
textShaderVert->Load("./codehero/shaders/text_basic.vert");
auto textShaderVertInst = textShaderVert->GetInstance({});
auto textShaderFrag = Shader::Create(m_pContext);
textShaderFrag->Load("./codehero/shaders/text_basic.frag");
auto textShaderFragInst = textShaderFrag->GetInstance({});
auto textShader = rs->CreateShaderProgram();
textShader->Attach(textShaderVert->GetInstance({}))
.Attach(textShaderFrag->GetInstance({}))
.Link();
textShader->Use();
Matrix4 ortho = Matrix4::MakeProjectionOrtho(0, g_Width, g_Height, 0);
rs->SetShaderParameter("projection", ortho);
auto lastTime = time->GetTimeMilliseconds();
int nbFrames = 0;
int fps = 0;
std::shared_ptr<Scene> scene =
m_pContext->GetSubsystem<ResourceLoader<Serializable>>()->Load<Scene>(
"./resources/samples/scene_test1.xml");
std::shared_ptr<Node> nanoNode = scene->CreateChild();
nanoNode->Translate({1.0f, -12.0f, 4.7f});
auto nanoMdl = m_pContext->GetSubsystem<ResourceLoader<Model>>()->Load(
"./resources/models/nanosuit/nanosuit.obj");
nanoNode->AddDrawable(nanoMdl);
std::shared_ptr<Node> houseNode = scene->CreateChild();
houseNode->Translate({-15.0f, 0.0f, 5.0f});
houseNode->SetRotation(Quaternion(0.0f, 180.0f, 0.0f));
auto houseMdl = m_pContext->GetSubsystem<ResourceLoader<Model>>()->Load(
"./resources/models/small-house-diorama/Dio.obj");
houseNode->AddDrawable(houseMdl);
std::shared_ptr<Node> rockNode = scene->CreateChild();
rockNode->Translate({-2.8f, -12.0f, 3.2f});
auto rockMdl =
m_pContext->GetSubsystem<ResourceLoader<Model>>()->Load("./resources/models/rock/rock.obj");
rockNode->AddDrawable(rockMdl);
std::shared_ptr<Node> planetNode = scene->CreateChild();
planetNode->Translate({12.8f, 12.0f, 23.2f});
auto planetMdl = m_pContext->GetSubsystem<ResourceLoader<Model>>()->Load(
"./resources/models/planet/planet.obj");
planetNode->AddDrawable(planetMdl);
auto camera = std::make_shared<Camera>(m_pContext);
camera->SetFov(45.0f);
camera->SetAspectRatio((float)g_Width / (float)g_Height);
camera->SetNearClip(0.1f);
camera->SetFarClip(100.0f);
std::shared_ptr<Node> cameraNode = scene->CreateChild();
cameraNode->AddDrawable(camera);
cameraNode->SetPosition({0.0f, 3.0f, -16.5f});
float yaw = 0.0f;
float pitch = 15.0f;
cameraNode->SetRotation(Quaternion(pitch, yaw, 0.0f));
auto mainViewport = std::make_shared<Viewport>(0, 0, g_Width, g_Height);
mainViewport->SetCamera(camera);
mainViewport->SetScene(scene);
rs->RegisterViewport(mainViewport);
// Save the input to avoid doing a query at every frame
Input* input = m_pContext->GetSubsystem<Input>();
auto previous = time->GetTimeMilliseconds();
while (!mainWindow->ShouldClose()) {
input->Update();
rs->ClearFrameBuffer();
auto now = time->GetTimeMilliseconds();
auto timeStep = (now - previous) / 1000.0f; // In seconds
previous = now;
nbFrames++;
if (now - lastTime >= 1000) {
fps = nbFrames;
nbFrames = 0;
lastTime = now;
}
// Check the inputs
if (input->IsKeyPressed(Key::K_ESC)) {
// On ESC we exit the example application
mainWindow->SetShouldClose(true);
}
if (input->IsKeyPressed(Key::K_W)) {
cameraNode->Translate(Vector3::Forward * 10.0f * timeStep);
}
if (input->IsKeyPressed(Key::K_S)) {
cameraNode->Translate(Vector3::Backward * 10.0f * timeStep);
}
if (input->IsKeyPressed(Key::K_D)) {
cameraNode->Translate(Vector3::Right * 10.0f * timeStep);
}
if (input->IsKeyPressed(Key::K_A)) {
cameraNode->Translate(Vector3::Left * 10.0f * timeStep);
}
// Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch
// between -90 and 90 degrees
Vector2 mouseMove = input->GetMouseMove();
yaw += 0.1f * mouseMove.x();
pitch += 0.1f * mouseMove.y();
pitch = Clamp(pitch, -90.0f, 90.0f);
// Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to
// zero
cameraNode->SetRotation(Quaternion(pitch, yaw, 0.0f));
label->SetText("FPS: " + std::to_string(fps));
// TODO(pierre) This is for now, as we don't have a proper scene rendering
// This should be removed ASAP
cameraNode->Update();
rs->Render();
textShader->Use();
ui.Update();
ui.Render();
mainWindow->SwapBuffers();
input->EndFrame();
}
LOGD2 << "[<] Main::Run()" << std::endl;
return Error::OK;
}
void Main::_Initialize() {
SimpleLogger::ReportingLevel() = ELogLevel::Debug1;
m_pFileLogger.reset(new FileLogger());
SimpleLogger::AddListener(m_pFileLogger.get());
m_pConsoleLogger.reset(new ConsoleLogger());
SimpleLogger::AddListener(m_pConsoleLogger.get());
LOGD2 << "[=] Main::_Initialize()" << std::endl;
}
void Main::_Cleanup() {
m_pContext->GetSubsystem<RenderSystem>()->Cleanup();
_UnloadDrivers();
}
void Main::_LoadDrivers() {
LOGI << "Loading drivers..." << std::endl;
#ifdef DRIVER_PNG
m_pContext->GetSubsystem<ResourceLoader<Image>>()->AddCodec(new ImageCodecPNG(m_pContext));
#endif // DRIVER_PNG
#ifdef DRIVER_JPG
m_pContext->GetSubsystem<ResourceLoader<Image>>()->AddCodec(new ImageCodecJPG(m_pContext));
#endif // DRIVER_JPG
#ifdef DRIVER_DDS
m_pContext->GetSubsystem<ResourceLoader<Image>>()->AddCodec(new ImageCodecDDS(m_pContext));
#endif // DRIVER_DDS
#ifdef DRIVER_TGA
m_pContext->GetSubsystem<ResourceLoader<Image>>()->AddCodec(new ImageCodecTGA(m_pContext));
#endif // DRIVER_TGA
#ifdef DRIVER_ASSIMP
m_pContext->GetSubsystem<ResourceLoader<Model>>()->AddCodec(new ModelCodecAssimp(m_pContext));
#endif // DRIVER_ASSIMP
#ifdef DRIVER_PUGIXML
m_pContext->GetSubsystem<ResourceLoader<Serializable>>()->AddCodec(
new SerializableCodecXML(m_pContext));
#endif // DRIVER_PUGIXML
LOGI << "Drivers loaded..." << std::endl;
}
void Main::_UnloadDrivers() {
LOGI << "Unloading drivers..." << std::endl;
m_pContext->GetSubsystem<ResourceLoader<Image>>()->ClearCodecs();
m_pContext->GetSubsystem<ResourceLoader<Model>>()->ClearCodecs();
m_pContext->GetSubsystem<ResourceLoader<Serializable>>()->ClearCodecs();
LOGI << "Drivers unloaded..." << std::endl;
}
} // namespace CodeHero