This repository was archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
261 lines (241 loc) · 10.4 KB
/
main.cpp
File metadata and controls
261 lines (241 loc) · 10.4 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
//
// Created by petr on 2/12/20.
//
#include "UI/UI.h"
#include "common/GlslShaderLoader.h"
#include "ray_marching/CSG/CSGTree.h"
#include "ray_marching/RayMarcher.h"
#include "ray_marching/SceneManager.h"
#include "simulation/PhysicsObject.h"
#include "simulation/PhysicsSimulator.h"
#include <Camera.h>
#include <SDL2CPP/MainLoop.h>
#include <SDL2CPP/Window.h>
#include <SDL_video.h>
#include <argparse.hpp>
#include <error_handling/exceptions.h>
#include <fstream>
#include <geGL/DebugMessage.h>
#include <geGL/StaticCalls.h>
#include <geGL/geGL.h>
#include <memory>
#include <spdlog/spdlog.h>
#include <sstream>
#include <types/Range.h>
#include <utility>
#include <various/overload.h>
using namespace std::string_literals;
class SphereCollisionObject : public SDFCollisionObject {
public:
SphereCollisionObject(const glm::vec3 &position, float mass) : SDFCollisionObject(position, mass) {}
};
auto getDisplaySize() -> std::pair<unsigned int, unsigned int> {
SDL_DisplayMode displayMode;
if (SDL_GetDesktopDisplayMode(0, &displayMode) != 0) {
throw exc::Error("SDL_GetDesktopDisplayMode failed");
}
const auto w = static_cast<unsigned int>(displayMode.w * 0.8);
const auto h = static_cast<unsigned int>(displayMode.h * 0.8);
return {w, h};
}
auto loadMaterials(MaterialManager &materialManager, const std::string &assetsPath) -> void {
auto materialFileStream = std::ifstream{assetsPath + "/materials.json"};
auto materialJson = nlohmann::json{};
materialFileStream >> materialJson;
materialManager.loadFromJson(materialJson);
}
auto main(int argc, char **argv) -> int {
argparse::ArgumentParser argParser("Ray marching");
argParser.add_argument("-assets").help("path to assets folder").required();
argParser.add_argument("-shaders").help("path to shaders folder").required();
try {
argParser.parse_args(argc, argv);
} catch (const std::runtime_error &err) {
std::cout << err.what() << std::endl;
std::cout << argParser;
return 0;
}
const std::string shaderPath = argParser.get<std::string>("-shaders");
const std::string assetsPath = argParser.get<std::string>("-assets");
spdlog::set_level(spdlog::level::debug);
auto mainLoop = std::make_shared<sdl2cpp::MainLoop>();
auto [screenWidth, screenHeight] = getDisplaySize();
screenWidth *= 0.8;
screenHeight *= 0.8;
spdlog::info("Window size: {}x{}", static_cast<int>(screenWidth), static_cast<int>(screenHeight));
auto window = std::make_shared<sdl2cpp::Window>(screenWidth, screenHeight);
spdlog::info("OpenGL context version {}", 450);
window->createContext("rendering", 450);
mainLoop->addWindow("mainWindow", window);
ge::gl::init(SDL_GL_GetProcAddress);
ge::gl::setHighDebugMessage();
ge::gl::glClearColor(0, 0, 0, 1);
setShaderLocation(shaderPath);
ray_march::RayMarcher rayMarcher{{screenWidth, screenHeight}};
loadMaterials(rayMarcher.getMaterialManager(), assetsPath);
SceneManager sceneManager;
for (auto i : MakeRange::range('1', '4')) {
std::ifstream ifstream{assetsPath + "/scenes/scene"s + i + ".json"};
nlohmann::json sceneJson;
ifstream >> sceneJson;
sceneManager.loadFromJson(sceneJson, rayMarcher.getMaterialManager());
}
std::ifstream ifstream{assetsPath + "/scenes/scene_spheres.json"};
nlohmann::json sceneJson;
ifstream >> sceneJson;
sceneManager.loadFromJson(sceneJson, rayMarcher.getMaterialManager());
auto mainScene = sceneManager.getScene("scene1");
PhysicsSimulator simulation{};
float time = 0;
ui::UI ui{*window, *mainLoop, "450", rayMarcher.getMaterialManager(), mainScene->getCamera(), sceneManager};
bool isCameraControlled = false;
window->setEventCallback(SDL_MOUSEMOTION, [&isCameraControlled, &mainScene](const SDL_Event &event) {
if (isCameraControlled) {
mainScene->getCamera().ProcessMouseMovement(event.motion.xrel, event.motion.yrel);
return true;
}
return false;
});
window->setEventCallback(SDL_MOUSEBUTTONDOWN, [&isCameraControlled](const SDL_Event &event) {
isCameraControlled = event.button.button == SDL_BUTTON_RIGHT;
return isCameraControlled;
});
window->setEventCallback(SDL_MOUSEBUTTONUP, [&isCameraControlled](const SDL_Event &event) {
if (isCameraControlled && event.button.button == SDL_BUTTON_RIGHT) {
isCameraControlled = false;
return true;
}
return false;
});
// glm::vec3 velocity{0};
auto movementSpeed = 10.0f;
window->setEventCallback(SDL_KEYDOWN, [&movementSpeed, &mainScene, &simulation](const SDL_Event &event) {
const auto pressedKey = event.key.keysym.sym;
// const Uint8 *keyboard_state_array = SDL_GetKeyboardState(NULL);
switch (pressedKey) {
case SDLK_w:
// velocity += 0.01f * camera.Front;
mainScene->getCamera().ProcessKeyboard(Camera_Movement::FORWARD, movementSpeed);
break;
case SDLK_a:
// velocity += -0.01f * camera.Right;
mainScene->getCamera().ProcessKeyboard(Camera_Movement::RIGHT, movementSpeed);
break;
case SDLK_s:
// velocity += 0.01f * camera.Front;
mainScene->getCamera().ProcessKeyboard(Camera_Movement::BACKWARD, movementSpeed);
break;
case SDLK_d:
// velocity += 0.01f * camera.Right;
mainScene->getCamera().ProcessKeyboard(Camera_Movement::LEFT, movementSpeed);
break;
case SDLK_SPACE:
// velocity += glm::vec3{0, 0.5f, 0};
mainScene->getCamera().Position += mainScene->getCamera().Up * movementSpeed;
break;
case SDLK_TAB:
auto obj =
std::make_shared<SphereCollisionObject>(mainScene->getCamera().Position + mainScene->getCamera().Front * 20.0f, 100);
obj->setVelocity(mainScene->getCamera().Front * 1000.0f);
simulation.addObject(obj);
break;
}
return true;
});
window->setWindowEventCallback(SDL_WINDOWEVENT_RESIZED, [&screenWidth, &screenHeight, &rayMarcher](const SDL_Event &event) {
auto diff = screenWidth - event.window.data1;
diff += screenHeight - event.window.data2;
if (diff > 50) {
screenWidth = event.window.data1;
screenHeight = event.window.data2;
rayMarcher.changeRenderSize({screenWidth, screenHeight});
}
return true;
});
glm::vec3 lastCamPos = mainScene->getCamera().Position;
ui.getRenderSettingsPanel().setOnReloadShaderClicked([&rayMarcher] { rayMarcher.reloadShader(); });
mainScene->getCamera().Position = glm::vec3{0, 400, 0};
ui.onFrame();
mainLoop->setIdleCallback([&]() {
ge::gl::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
std::chrono::microseconds simTime;
if (ui.getScenePanel().isChoiceChanged()) {
mainScene = ui.getScenePanel().getSelectedScene();
}
if (ui.getScenePanel().isChoiceChanged()) {
simulation.setScene(mainScene);
}
{
const auto begin = std::chrono::steady_clock::now();
simulation.update(time);
std::vector<glm::vec4> spherePositions;
for (auto &obj : simulation.getObjects()) {
spherePositions.emplace_back(glm::vec4{obj->getPosition(), 1});
}
rayMarcher.physicsSphereCount = spherePositions.size();
if (rayMarcher.physicsSphereCount > 0) {
rayMarcher.buffer.setData(spherePositions);
}
movementSpeed = ui.getCameraPanel().getMovementSpeed();
// velocity += glm::vec3{0, -0.009, 0};
// camera.Position += velocity;
// if (glm::length(velocity) > 10) {
// velocity = glm::normalize(velocity) * 10.0f;
//}
float currentDistance = mainScene->getDistanceToScene(mainScene->getCamera().Position);
if (!ui.getCameraPanel().isClippingEnabled()) {
unsigned int iterationCount = 0;
while (currentDistance < 0) {
const auto surfaceNormal = mainScene->getNormal(mainScene->getCamera().Position);
const auto delta = surfaceNormal * glm::dot(lastCamPos - mainScene->getCamera().Position, surfaceNormal);
mainScene->getCamera().Position += delta;
mainScene->getCamera().Position += surfaceNormal * 0.0001f * float(iterationCount);
currentDistance = mainScene->getDistanceToScene(mainScene->getCamera().Position);
iterationCount += 100;
// if (-velocity.y > 0.1f) {
// velocity = glm::reflect(velocity, surfaceNormal) * 0.8f;
//} else {
// velocity -= surfaceNormal * glm::dot(velocity, surfaceNormal);
//}
}
lastCamPos = mainScene->getCamera().Position;
}
ui.getCameraPanel().setDistance(currentDistance);
const auto end = std::chrono::steady_clock::now();
simTime = std::chrono::duration_cast<std::chrono::microseconds>(end - begin);
}
const auto swapInterval = ui.getFPSPanel().isVsyncEnabled() ? 1 : 0;
SDL_GL_SetSwapInterval(swapInterval);
const auto currentFps = ui.getFPSPanel().getFPSCounter().current();
std::chrono::microseconds renderTime;
{
const auto begin = std::chrono::steady_clock::now();
if (!ui.getRenderSettingsPanel().isPaused()) {
rayMarcher.setRayStepLimit(ui.getRenderSettingsPanel().getRayStepLimit());
rayMarcher.setShadowRayStepLimit(ui.getRenderSettingsPanel().getShadowRayStepLimit());
rayMarcher.setMaxDrawDistance(ui.getRenderSettingsPanel().getMaxDrawDistance());
rayMarcher.setTime(time);
rayMarcher.setAmbientOcclusionEnabled(ui.getRenderSettingsPanel().isAmbientOcclusionEnabled());
rayMarcher.setAntiaAliasingType(ui.getRenderSettingsPanel().getAAType());
rayMarcher.setShadowType(ui.getRenderSettingsPanel().getShadowType());
rayMarcher.setAASize(ui.getRenderSettingsPanel().getAA());
rayMarcher.setMaxReflections(ui.getRenderSettingsPanel().getMaxReflections());
rayMarcher.setLightPosition(ui.getRenderSettingsPanel().getLightPosition());
rayMarcher.setUseOptimisedMarching(ui.getRenderSettingsPanel().isUseOptimisedRayMarching());
rayMarcher.setPixelRadius(ui.getRenderSettingsPanel().getPixelRadius());
rayMarcher.setRelaxationParameter(ui.getRenderSettingsPanel().getRelaxationParameter());
rayMarcher.setLogStepCount(ui.getRenderSettingsPanel().isLogStepCount());
rayMarcher.render(mainScene);
}
rayMarcher.show(ui.getRenderSettingsPanel().getSelectedTextureType());
ui.onFrame();
window->swap();
const auto end = std::chrono::steady_clock::now();
renderTime = std::chrono::duration_cast<std::chrono::microseconds>(end - begin);
}
time += 1 / currentFps * ui.getRenderSettingsPanel().getTimeScale();
});
spdlog::info("Starting main loop");
(*mainLoop)();
return 0;
}