-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
363 lines (314 loc) · 10.7 KB
/
main.cpp
File metadata and controls
363 lines (314 loc) · 10.7 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
#include <facade/defines.hpp>
#include <facade/util/data_provider.hpp>
#include <facade/util/error.hpp>
#include <facade/util/fixed_string.hpp>
#include <facade/util/logger.hpp>
#include <facade/vk/geometry.hpp>
#include <djson/json.hpp>
#include <facade/render/renderer.hpp>
#include <facade/scene/fly_cam.hpp>
#include <facade/engine/editor/inspector.hpp>
#include <facade/engine/editor/log.hpp>
#include <facade/engine/editor/scene_tree.hpp>
#include <facade/engine/engine.hpp>
#include <bin/shaders.hpp>
#include <filesystem>
#include <iostream>
#include <imgui.h>
using namespace facade;
namespace {
namespace fs = std::filesystem;
static constexpr auto test_json_v = R"(
{
"scene": 0,
"scenes" : [
{
"nodes" : [ 0 ]
}
],
"nodes" : [
{
"mesh" : 0
}
],
"meshes" : [
{
"primitives" : [ {
"attributes" : {
"POSITION" : 1
},
"indices" : 0,
"material" : 0
} ]
}
],
"buffers" : [
{
"uri" : "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAA=",
"byteLength" : 44
}
],
"bufferViews" : [
{
"buffer" : 0,
"byteOffset" : 0,
"byteLength" : 6,
"target" : 34963
},
{
"buffer" : 0,
"byteOffset" : 8,
"byteLength" : 36,
"target" : 34962
}
],
"accessors" : [
{
"bufferView" : 0,
"byteOffset" : 0,
"componentType" : 5123,
"count" : 3,
"type" : "SCALAR",
"max" : [ 2 ],
"min" : [ 0 ]
},
{
"bufferView" : 1,
"byteOffset" : 0,
"componentType" : 5126,
"count" : 3,
"type" : "VEC3",
"max" : [ 1.0, 1.0, 0.0 ],
"min" : [ 0.0, 0.0, 0.0 ]
}
],
"materials" : [
{
"pbrMetallicRoughness": {
"baseColorFactor": [ 1.000, 0.766, 0.336, 1.0 ],
"metallicFactor": 0.5,
"roughnessFactor": 0.1
}
}
],
"asset" : {
"version" : "2.0"
}
}
)";
struct MainMenu {
struct {
bool tree{};
bool stats{};
bool log{};
bool camera;
bool imgui_demo{};
} windows{};
struct {
editor::Log log{};
editor::Inspectee inspectee{};
} data{};
void change_vsync(Engine const& engine) const {
static constexpr vk::PresentModeKHR modes[] = {vk::PresentModeKHR::eFifo, vk::PresentModeKHR::eFifoRelaxed, vk::PresentModeKHR::eMailbox,
vk::PresentModeKHR::eImmediate};
static std::size_t index{0};
auto const next_mode = [&] {
while (true) {
index = (index + 1) % std::size(modes);
auto const ret = modes[index];
if (!engine.renderer().is_supported(ret)) { continue; }
return ret;
}
throw Error{"Invariant violated"};
}();
engine.renderer().request_mode(next_mode);
logger::info("Requesting present mode: [{}]", present_mode_str(next_mode));
}
void inspector(Scene& scene) {
bool show = true;
ImGui::SetNextWindowSize({400.0f, 400.0f}, ImGuiCond_Once);
if (auto window = editor::Window{data.inspectee.name.c_str(), &show}) { editor::SceneInspector{window, scene}.inspect(data.inspectee.id); }
if (!show) { data.inspectee = {}; }
}
void stats(Engine const& engine, float const dt) {
ImGui::SetNextWindowSize({250.0f, 200.0f}, ImGuiCond_Once);
if (auto window = editor::Window{"Frame Stats", &windows.stats}) {
auto const& stats = engine.renderer().frame_stats();
ImGui::Text("%s", FixedString{"Counter: {}", stats.frame_counter}.c_str());
ImGui::Text("%s", FixedString{"Triangles: {}", stats.triangles}.c_str());
ImGui::Text("%s", FixedString{"Draw calls: {}", stats.draw_calls}.c_str());
ImGui::Text("%s", FixedString{"FPS: {}", (stats.fps == 0 ? static_cast<std::uint32_t>(stats.frame_counter) : stats.fps)}.c_str());
ImGui::Text("%s", FixedString{"Frame time: {:.2f}ms", dt * 1000.0f}.c_str());
ImGui::Text("%s", FixedString{"GPU: {}", stats.gpu_name}.c_str());
ImGui::Text("%s", FixedString{"MSAA: {}x", to_int(stats.msaa)}.c_str());
if (ImGui::SmallButton("Vsync")) { change_vsync(engine); }
ImGui::SameLine();
ImGui::Text("%s", vsync_status(stats.mode).data());
}
}
void tree(Scene& scene) {
ImGui::SetNextWindowSize({250.0f, 350.0f}, ImGuiCond_Once);
if (auto window = editor::Window{"Scene", &windows.tree}) { editor::SceneTree{scene}.render(window, data.inspectee); }
}
void log() {
ImGui::SetNextWindowSize({600.0f, 200.0f}, ImGuiCond_Once);
if (auto window = editor::Window{"Log", &windows.log}) { data.log.render(window); }
}
void cam(Scene& scene) {
ImGui::SetNextWindowSize({600.0f, 200.0f}, ImGuiCond_Once);
if (auto window = editor::Window{"Camera", &windows.camera}) { scene.camera().transform; }
}
void display(Engine& engine, float const dt) {
if (auto main = editor::MainMenu{}) {
if (auto file = editor::Menu{main, "File"}) {
ImGui::Separator();
if (ImGui::MenuItem("Exit")) { engine.request_stop(); }
}
if (auto window = editor::Menu{main, "Window"}) {
if (ImGui::MenuItem("Tree")) { windows.tree = true; }
if (ImGui::MenuItem("Stats")) { windows.stats = true; }
if (ImGui::MenuItem("Log")) { windows.log = true; }
if (ImGui::MenuItem("Camera")) { windows.camera = true; }
if constexpr (debug_v) {
if (ImGui::MenuItem("ImGui demo")) { windows.imgui_demo = true; }
}
ImGui::Separator();
if (ImGui::MenuItem("Close All")) { windows = {}; }
}
}
if (windows.tree) { tree(engine.scene()); }
if (data.inspectee) { inspector(engine.scene()); }
if (windows.stats) { stats(engine, dt); }
if (windows.log) { log(); }
if (windows.camera) { cam(engine.scene()); }
if (windows.imgui_demo) { ImGui::ShowDemoWindow(&windows.imgui_demo); }
}
};
void log_prologue() {
auto const now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char buf[32]{};
std::strftime(buf, sizeof(buf), "%F %Z", std::localtime(&now));
logger::info("facade v{}.{}.{} | {} |", 0, 0, 0, buf);
}
fs::path find_gltf(fs::path root) {
if (root.extension() == ".gltf") { return root; }
for (auto const& it : fs::directory_iterator{root}) {
if (!it.is_regular_file()) { continue; }
auto path = it.path();
if (path.extension() == ".gltf") { return path; }
}
return {};
}
void run() {
auto engine = std::optional<Engine>{};
struct DummyDataProvider : DataProvider {
ByteBuffer load(std::string_view) const override { return {}; }
};
auto material_id = Id<Material>{};
auto node_id = Id<Node>{};
auto post_scene_load = [&](Scene& scene) {
scene.camera().transform.set_position({0.0f, 0.0f, 5.0f});
auto material = std::make_unique<LitMaterial>();
material->albedo = {1.0f, 0.0f, 0.0f};
material_id = scene.add(std::move(material));
auto static_mesh_id = scene.add(make_cubed_sphere(1.0f, 32));
auto mesh_id = scene.add(Mesh{.primitives = {Mesh::Primitive{static_mesh_id, material_id}}});
auto node = Node{};
node.attach(mesh_id);
node.instances.emplace_back().set_position({1.0f, -5.0f, -20.0f});
node.instances.emplace_back().set_position({-1.0f, 1.0f, 0.0f});
node_id = scene.add(std::move(node), 0);
};
auto init = [&] {
engine.emplace();
log_prologue();
auto lit = shaders::lit();
lit.id = "default";
engine->add_shader(lit);
engine->add_shader(shaders::unlit());
auto& scene = engine->scene();
scene.dir_lights.push_back(DirLight{.direction = glm::normalize(glm::vec3{-1.0f, -1.0f, -1.0f}), .diffuse = glm::vec3{5.0f}});
scene.load_gltf(dj::Json::parse(test_json_v), DummyDataProvider{});
post_scene_load(engine->scene());
engine->show(true);
};
init();
float const drot_z[] = {100.0f, -150.0f};
auto main_menu = MainMenu{};
struct {
LoadStatus status{};
std::string title{};
} loading{};
while (engine->running()) {
auto const& state = engine->poll();
auto const& input = state.input;
auto const dt = state.dt;
bool const mouse_look = input.mouse.held(GLFW_MOUSE_BUTTON_RIGHT);
if (input.keyboard.pressed(GLFW_KEY_ESCAPE)) { engine->request_stop(); }
glfwSetInputMode(engine->window(), GLFW_CURSOR, mouse_look ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
if (!state.file_drops.empty() && engine->load_status() == LoadStatus::eNone) {
auto path = find_gltf(state.file_drops.front());
if (!fs::is_regular_file(path)) {
logger::error("Failed to locate .gltf in path: [{}]", state.file_drops.front());
} else {
if (engine->load_async(path.generic_string(), [&] { post_scene_load(engine->scene()); })) {
loading.title = fmt::format("Loading {}...", path.filename().generic_string());
}
}
}
loading.status = engine->load_status();
if (loading.status > LoadStatus::eNone) {
auto const* main_viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos({0.0f, main_viewport->WorkPos.y + main_viewport->Size.y - 100.0f});
ImGui::SetNextWindowSize({main_viewport->Size.x, 100.0f});
auto window = editor::Window{loading.title.c_str(), nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize};
ImGui::Text("%s", load_status_str[loading.status].data());
ImGui::ProgressBar(load_progress(loading.status), ImVec2{-1.0f, 0.0f}, load_status_str[loading.status].data());
}
auto& camera = engine->scene().camera();
if (auto fly_cam = FlyCam{camera.transform}) {
if (input.keyboard.held(GLFW_KEY_A) || input.keyboard.held(GLFW_KEY_LEFT)) { fly_cam.move_right(-dt); }
if (input.keyboard.held(GLFW_KEY_D) || input.keyboard.held(GLFW_KEY_RIGHT)) { fly_cam.move_right(dt); }
if (input.keyboard.held(GLFW_KEY_W) || input.keyboard.held(GLFW_KEY_UP)) { fly_cam.move_front(-dt); }
if (input.keyboard.held(GLFW_KEY_S) || input.keyboard.held(GLFW_KEY_DOWN)) { fly_cam.move_front(dt); }
if (input.keyboard.held(GLFW_KEY_E)) { fly_cam.move_up(dt); }
if (input.keyboard.held(GLFW_KEY_Q)) { fly_cam.move_up(-dt); }
if (mouse_look) { fly_cam.rotate({input.mouse.delta_pos().x, -input.mouse.delta_pos().y}); }
}
if (input.keyboard.pressed(GLFW_KEY_R)) {
logger::info("Reloading...");
engine.reset();
init();
continue;
}
// TEMP CODE
if (auto* node = engine->scene().find(node_id)) {
node->instances[0].rotate(glm::radians(drot_z[0]) * dt, {0.0f, 1.0f, 0.0f});
node->instances[1].rotate(glm::radians(drot_z[1]) * dt, {1.0f, 0.0f, 0.0f});
}
main_menu.display(*engine, dt);
// TEMP CODE
engine->render();
}
}
} // namespace
int main() {
try {
auto logger_instance = logger::Instance{};
try {
run();
} catch (InitError const& e) {
logger::error("Initialization failure: {}", e.what());
return EXIT_FAILURE;
} catch (Error const& e) {
logger::error("Runtime error: {}", e.what());
return EXIT_FAILURE;
} catch (std::exception const& e) {
logger::error("Fatal error: {}", e.what());
return EXIT_FAILURE;
} catch (...) {
logger::error("Unknown error");
return EXIT_FAILURE;
}
} catch (std::exception const& e) { std::cerr << e.what() << '\n'; }
}