-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cpp
More file actions
147 lines (109 loc) · 3.75 KB
/
App.cpp
File metadata and controls
147 lines (109 loc) · 3.75 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
#include "App.hpp"
#include <tracy/Tracy.hpp>
#include "gui/ImGuiRenderer.hpp"
App::App()
{
glm::uvec2 initialRes = {1280, 720};
mainWindow = windowing.createWindow(OsWindow::CreateInfo{
.resolution = initialRes,
.resizeable = true,
.refreshCb =
[this]() {
// NOTE: this is only called when the window is being resized.
drawFrame();
FrameMark;
},
.resizeCb =
[this](glm::uvec2 res) {
if (res.x == 0 || res.y == 0)
return;
renderer->recreateSwapchain(res);
},
});
renderer.reset(new Renderer(initialRes));
auto instExts = windowing.getRequiredVulkanInstanceExtensions();
renderer->initVulkan(instExts);
auto surface = mainWindow->createVkSurface(etna::get_context().getInstance());
renderer->initFrameDelivery(
std::move(surface), [window = mainWindow.get()]() { return window->getResolution(); });
// TODO: this is bad design, this initialization is dependent on the current ImGui context, but we
// pass it implicitly here instead of explicitly. Beware if trying to do something tricky.
ImGuiRenderer::enableImGuiForWindow(mainWindow->native());
shadowCam.lookAt({-8, 10, 8}, {0, 0, 0}, {0, 1, 0});
mainCam.lookAt({0, 10, 10}, {0, 0, 0}, {0, 1, 0});
renderer->loadScene(GRAPHICS_COURSE_RESOURCES_ROOT "/scenes/low_poly_dark_town/scene.gltf");
}
void App::run()
{
double lastTime = windowing.getTime();
while (!mainWindow->isBeingClosed())
{
const double currTime = windowing.getTime();
const float diffTime = static_cast<float>(currTime - lastTime);
lastTime = currTime;
windowing.poll();
processInput(diffTime);
drawFrame();
FrameMark;
}
}
void App::processInput(float dt)
{
ZoneScoped;
if (mainWindow->keyboard[KeyboardKey::kEscape] == ButtonState::Falling)
mainWindow->askToClose();
if (is_held_down(mainWindow->keyboard[KeyboardKey::kLeftShift]))
camMoveSpeed = 10;
else
camMoveSpeed = 1;
if (mainWindow->keyboard[KeyboardKey::kL] == ButtonState::Falling)
controlShadowCam = !controlShadowCam;
if (mainWindow->mouse[MouseButton::mbRight] == ButtonState::Rising)
mainWindow->captureMouse = !mainWindow->captureMouse;
auto& camToControl = controlShadowCam ? shadowCam : mainCam;
moveCam(camToControl, mainWindow->keyboard, dt);
if (mainWindow->captureMouse)
rotateCam(camToControl, mainWindow->mouse, dt);
renderer->debugInput(mainWindow->keyboard);
}
void App::drawFrame()
{
ZoneScoped;
renderer->update(FramePacket{
.mainCam = mainCam,
.shadowCam = shadowCam,
.currentTime = static_cast<float>(windowing.getTime()),
});
renderer->drawFrame();
}
void App::moveCam(Camera& cam, const Keyboard& kb, float dt)
{
// Move position of camera based on WASD keys, and FR keys for up and down
glm::vec3 dir = {0, 0, 0};
if (is_held_down(kb[KeyboardKey::kS]))
dir -= cam.forward();
if (is_held_down(kb[KeyboardKey::kW]))
dir += cam.forward();
if (is_held_down(kb[KeyboardKey::kA]))
dir -= cam.right();
if (is_held_down(kb[KeyboardKey::kD]))
dir += cam.right();
if (is_held_down(kb[KeyboardKey::kF]))
dir -= cam.up();
if (is_held_down(kb[KeyboardKey::kR]))
dir += cam.up();
// NOTE: This is how you make moving diagonally not be faster than
// in a straight line.
cam.move(dt * camMoveSpeed * (length(dir) > 1e-9 ? normalize(dir) : dir));
}
void App::rotateCam(Camera& cam, const Mouse& ms, float /*dt*/)
{
// Rotate camera based on mouse movement
cam.rotate(camRotateSpeed * ms.capturedPosDelta.y, camRotateSpeed * ms.capturedPosDelta.x);
// Increase or decrease field of view based on mouse wheel
cam.fov -= zoomSensitivity * ms.scrollDelta.y;
if (cam.fov < 1.0f)
cam.fov = 1.0f;
if (cam.fov > 120.0f)
cam.fov = 120.0f;
}