forked from paulo-pagliosa/Ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
355 lines (321 loc) · 9.34 KB
/
MainWindow.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
//[]---------------------------------------------------------------[]
//| |
//| Copyright (C) 2022, 2023 Paulo Pagliosa. |
//| |
//| This software is provided 'as-is', without any express or |
//| implied warranty. In no event will the authors be held liable |
//| for any damages arising from the use of this software. |
//| |
//| Permission is granted to anyone to use this software for any |
//| purpose, including commercial applications, and to alter it and |
//| redistribute it freely, subject to the following restrictions: |
//| |
//| 1. The origin of this software must not be misrepresented; you |
//| must not claim that you wrote the original software. If you use |
//| this software in a product, an acknowledgment in the product |
//| documentation would be appreciated but is not required. |
//| |
//| 2. Altered source versions must be plainly marked as such, and |
//| must not be misrepresented as being the original software. |
//| |
//| 3. This notice may not be removed or altered from any source |
//| distribution. |
//| |
//[]---------------------------------------------------------------[]
//
// OVERVIEW: MainWindow.cpp
// ========
// Source file for cg demo main window.
//
// Author: Paulo Pagliosa
// Last revision: 13/07/2023
#include "graphics/Application.h"
#include "graphics/AssetFolder.h"
#include "reader/SceneReader.h"
#include "SceneWriter.h"
#include "MainWindow.h"
/////////////////////////////////////////////////////////////////////
//
// MainWindow implementation
// ==========
MeshMap MainWindow::_defaultMeshes;
inline void
MainWindow::buildDefaultMeshes()
{
_defaultMeshes["Box"] = GLGraphics3::box();
_defaultMeshes["Sphere"] = GLGraphics3::sphere();
_defaultMeshes["Cylinder"] = GLGraphics3::cylinder();
_defaultMeshes["Cone"] = GLGraphics3::cone();
_defaultMeshes["Plane"] = GLGraphics3::quad();
}
void
MainWindow::initializeScene()
{
auto a = (float)width() / (float)height();
auto s = createCameraObject(a, "Main Camera");
s->transform()->setLocalPosition({0, 0, 10});
s = createLightObject(Light::Type::Directional, "Directional Light");
s->transform()->setLocalEulerAngles({50, 30, 0});
createDefaultPrimitiveObject("Box");
}
void
MainWindow::beginInitialize()
{
constexpr auto ffn = "fonts/Roboto-Regular.ttf";
auto fonts = ImGui::GetIO().Fonts;
fonts->AddFontFromFileTTF(Application::assetFilePath(ffn).c_str(), 16);
buildDefaultMeshes();
Assets::initialize();
Assets::meshes().insert(_defaultMeshes.begin(), _defaultMeshes.end());
_sceneFolder = AssetFolder::New("scenes/", ".scn");
}
void
MainWindow::createObjectMenu()
{
if (ImGui::BeginMenu("3D Object"))
{
if (ImGui::MenuItem("Box"))
createDefaultPrimitiveObject("Box");
if (ImGui::MenuItem("Sphere"))
createDefaultPrimitiveObject("Sphere");
if (ImGui::MenuItem("Cylinder"))
createDefaultPrimitiveObject("Cylinder");
if (ImGui::MenuItem("Cone"))
createDefaultPrimitiveObject("Cone");
if (ImGui::MenuItem("Plane"))
createDefaultPrimitiveObject("Plane");
ImGui::EndMenu();
}
}
Component*
MainWindow::addComponentMenu(const SceneObject&)
{
Component* component = nullptr;
if (ImGui::BeginMenu("3D Object"))
{
if (ImGui::MenuItem("Box"))
component = makeDefaultPrimitive("Box");
if (ImGui::MenuItem("Sphere"))
component = makeDefaultPrimitive("Sphere");
if (ImGui::MenuItem("Cylinder"))
component = makeDefaultPrimitive("Cylinder");
if (ImGui::MenuItem("Cone"))
component = makeDefaultPrimitive("Cone");
if (ImGui::MenuItem("Plane"))
component = makeDefaultPrimitive("Plane");
ImGui::EndMenu();
}
return component;
}
void
MainWindow::openSceneCommand()
{
if (ImGui::BeginListBox("##SceneList",
ImVec2{180, 8 * ImGui::GetTextLineHeightWithSpacing()}))
{
for (auto& file : _sceneFolder->files())
if (ImGui::Selectable(file->filename().c_str()))
{
auto filename = (_sceneFolder->path() / file->name()).string();
readScene(filename);
ImGui::CloseCurrentPopup();
}
ImGui::EndListBox();
}
}
void
MainWindow::readScene(const std::string& filename) try
{
util::SceneReader reader;
reader.setInput(filename);
reader.execute();
if (reader.scene() != nullptr)
{
SceneWindow::setScene(*reader.scene());
auto& materials = Assets::materials();
for (const auto& [name, m] : reader.materials)
materials[name] = m;
}
}
catch (const std::exception& e)
{
puts(e.what());
}
void
MainWindow::saveScene()
{
if (auto name = scene()->name(); *name)
{
auto filename = std::string{name} + ".scn";
auto path = (_sceneFolder->path() / filename).string();
util::SceneWriter{path.c_str()}.write(*scene());
_sceneFolder->addFile(filename.c_str());
}
}
inline void
MainWindow::fileMenu()
{
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("New Scene"))
newScene();
if (ImGui::BeginMenu("Open"))
{
openSceneCommand();
ImGui::EndMenu();
}
ImGui::Separator();
if (ImGui::MenuItem("Save"))
saveScene();
ImGui::Separator();
if (ImGui::MenuItem("Exit", "Alt+F4"))
{
shutdown();
}
ImGui::EndMenu();
}
}
inline void
MainWindow::viewMenu()
{
if (ImGui::BeginMenu("View"))
{
static const char* viewLabels[]{"Editor", "Ray Tracer"};
if (ImGui::BeginCombo("View", viewLabels[(int)_viewMode]))
{
for (auto i = 0; i < IM_ARRAYSIZE(viewLabels); ++i)
if (ImGui::Selectable(viewLabels[i], _viewMode == (ViewMode)i))
_viewMode = (ViewMode)i;
ImGui::EndCombo();
// TODO: change mode only if scene has changed
if (_viewMode == ViewMode::Editor)
_image = nullptr;
}
ImGui::Separator();
ImGui::MenuItem("Hierarchy Window", nullptr, &_showHierarchy);
ImGui::MenuItem("Inspector Window", nullptr, &_showInspector);
ImGui::MenuItem("Camera Preview", nullptr, &_showPreview);
ImGui::MenuItem("Assets Window", nullptr, &_showAssets);
ImGui::MenuItem("Editor View Settings", nullptr, &_showEditorView);
ImGui::EndMenu();
}
}
inline void
MainWindow::createMenu()
{
ImGui::BeginDisabled(!editHierarchy());
if (ImGui::BeginMenu("Create"))
{
if (ImGui::MenuItem("Material"))
createMaterial();
ImGui::Separator();
createObjectMenu();
ImGui::EndMenu();
}
ImGui::EndDisabled();
}
inline void
MainWindow::showOptions()
{
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.6f);
ImGui::showStyleSelector("Color Theme##Selector");
ImGui::colorEdit3("Selected Wireframe", _selectedWireframeColor[0]);
ImGui::colorEdit3("Selected Children Wireframe", _selectedWireframeColor[1]);
ImGui::PopItemWidth();
}
inline void
MainWindow::mainMenu()
{
if (ImGui::BeginMainMenuBar())
{
fileMenu();
createMenu();
viewMenu();
if (ImGui::BeginMenu("Ray Tracing"))
{
ImGui::DragInt("Max Recursion Level",
&_maxRecursionLevel,
1.0f,
0,
RayTracer::maxMaxRecursionLevel);
ImGui::DragFloat("Min Weight",
&_minWeight,
0.01f,
RayTracer::minMinWeight,
1.0f);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Tools"))
{
if (ImGui::BeginMenu("Options"))
{
showOptions();
ImGui::EndMenu();
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
}
void
MainWindow::gui()
{
mainMenu();
if (_viewMode == ViewMode::Renderer)
return;
constexpr auto evw = (float)360;
constexpr auto evh = (float)308;
constexpr auto lft = (float)0;
constexpr auto top = (float)23;
auto w = (float)width();
auto h = (float)height();
auto evy = h - evh;
// Hierarchy Window
ImGui::SetNextWindowPos({lft, top});
ImGui::SetNextWindowSize({evw, evy - top});
hierarchyWindow();
// Editor View
ImGui::SetNextWindowPos({lft, evy});
ImGui::SetNextWindowSize({evw, evh});
editorView();
constexpr auto iww = (float)420;
auto rgt = w - iww;
auto awh = h / 3;
auto awy = h - awh;
// Inspector Window
ImGui::SetNextWindowPos({rgt, top});
ImGui::SetNextWindowSize({iww, awy - top});
inspectorWindow();
// Assets Window
ImGui::SetNextWindowPos({rgt, awy});
ImGui::SetNextWindowSize({iww, awh});
assetWindow();
}
void
MainWindow::renderScene()
{
if (_viewMode != ViewMode::Renderer)
return;
auto camera = CameraProxy::current();
if (nullptr == camera)
camera = editor()->camera();
if (_image == nullptr)
{
_image = new GLImage{width(), height()};
if (_rayTracer == nullptr)
_rayTracer = new RayTracer{*scene(), *camera};
else
_rayTracer->setCamera(*camera);
_rayTracer->setMaxRecursionLevel(_maxRecursionLevel);
_rayTracer->setMinWeight(_minWeight);
_rayTracer->renderImage(*_image);
}
_image->draw(0, 0);
}
bool
MainWindow::onResize(int width, int height)
{
_viewMode = ViewMode::Editor;
_image = nullptr;
return true;
}