-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
303 lines (272 loc) · 7.64 KB
/
Copy pathmain.cpp
File metadata and controls
303 lines (272 loc) · 7.64 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
#include "Camera.hpp"
#include "GLRenderer.hpp"
#include "Object.hpp"
#include "RayRenderer.hpp"
#include "Scene.hpp"
#include "ShaderProgram.hpp"
#include <glbinding/gl/gl.h>
#include <glbinding/Binding.h>
#include <imgui/imgui.h>
#include "imgui_impl_sdl_gl3.h"
#include <SDL2/SDL.h>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include <unordered_map>
using namespace gl;
int winWidth=700, winHeight=700;
SDL_Window *window;
SDL_GLContext context;
void CheckSDLError(int line);
bool createWindow();
void loadScene(std::string, Scene&, Camera&, GLRenderer&);
//tab to change
//0 -> Scene View
//1 -> Raytracer rendering View
/*enum ViewMode
{
Scene = 0,
Render = 1
};*/
enum ViewMode
{
SCENE,
RENDER
};
ViewMode viewMode=SCENE;
std::mutex pMutex;
int main(int argc, char *argv[]){
if(argc != 2){
std::cout << "Usage: ./luksja scene.json\n";
return -1;
}
if (!createWindow())
return -1;
Scene scene(argv[1]);
glEnable(GL_DEPTH_TEST);
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui_ImplSdlGL3_Init(window);
ImGui::StyleColorsDark();
Camera cam(scene.camera.viewPoint, scene.camera.up
/*, scene.camera.front*/, scene.camera.fov);
cam.setPerspectiveMatrix(winWidth, winHeight);
GLRenderer glRenderer(scene);
RayRenderer rayRenderer;
double now = SDL_GetPerformanceCounter();
double last = 0.0;
std::unordered_map<SDL_Keycode, bool> mKeys;
bool loop = true;
while (loop){
last = now;
now = SDL_GetPerformanceCounter();
float dt = (float)((now - last)
/ SDL_GetPerformanceFrequency());
SDL_Event event;
while (SDL_PollEvent(&event)){
ImGui_ImplSdlGL3_ProcessEvent(&event);
if (event.type == SDL_QUIT)
loop = false;
else if (event.type == SDL_KEYDOWN){
if(event.key.keysym.sym == SDLK_r){
if(viewMode == RENDER){
std::cout << "I am already rendering\n";
}
else{
viewMode = RENDER;
auto renderFunc =
[&rayRenderer, &scene, &cam](std::mutex& m) -> void{
rayRenderer.draw(scene, cam, m);
};
std::thread rayRenderThread(renderFunc, std::ref(pMutex));
rayRenderThread.detach();
}
}
if(event.key.keysym.sym == SDLK_TAB){
if(viewMode == RENDER)
viewMode = SCENE;
else
viewMode = RENDER;
}
mKeys[event.key.keysym.sym] = true;
}
else if(event.type == SDL_KEYUP){
if(mKeys.find(event.key.keysym.sym) != mKeys.end())
mKeys[event.key.keysym.sym]=false;
}
else if(event.type == SDL_MOUSEMOTION &&
(event.motion.state & SDL_BUTTON_RMASK)){
cam.processMouseMovement(event.motion.xrel, -event.motion.yrel);
}
else if(event.type == SDL_MOUSEWHEEL){
cam.processMouseScroll(event.wheel.y);
cam.setPerspectiveMatrix(winWidth, winHeight);
}
else if(event.type == SDL_WINDOWEVENT){
if(event.window.event == SDL_WINDOWEVENT_RESIZED){
SDL_GetWindowSize(window, &winWidth, &winHeight);
glViewport(0, 0, winWidth, winHeight);
cam.setPerspectiveMatrix(winWidth, winHeight);
}
}
}
pMutex.lock();
if(rayRenderer.previewDirty){
rayRenderer.updateTexture();
rayRenderer.previewDirty=false;
}
else if(rayRenderer.renderDone){
rayRenderer.updateTexture();
rayRenderer.renderDone = false;
}
pMutex.unlock();
if(mKeys[SDLK_a])
cam.processKeyboard(LEFT, dt);
if(mKeys[SDLK_d])
cam.processKeyboard(RIGHT, dt);
if(mKeys[SDLK_w])
cam.processKeyboard(FORWARD, dt);
if(mKeys[SDLK_s])
cam.processKeyboard(BACKWARD, dt);
ImGui_ImplSdlGL3_NewFrame(window);
if(viewMode == SCENE){
ImGui::Begin("Camera:");
glm::vec3 pos = cam.position;
glm::vec3 dir = cam.front;
glm::vec3 up = cam.up;
if(ImGui::InputFloat3("pos", glm::value_ptr(pos)))
cam.position = pos;
if(ImGui::InputFloat3("front", glm::value_ptr(dir)))
cam.front = dir;
if(ImGui::InputFloat3("right", glm::value_ptr(cam.right)))
std::cout << "not implemented yet\n";
if(ImGui::InputFloat3("up", glm::value_ptr(up)))
cam.up = up;
ImGui::InputFloat("fov", &cam.fov);
ImGui::End();
auto funcGui = [&](std::shared_ptr<Object> o){
if(ImGui::Button(o->name.c_str())){
o->guiVisible = !o->guiVisible;
}
if(o->guiVisible){
ImGui::Begin(o->name.c_str());
o->renderGui();
ImGui::End();
}
};
ImGui::Begin("Objects:");
for(const auto& o : scene.objects){
funcGui(o);
}
for(const auto& o: scene.lights){
funcGui(o);
}
ImGui::End();
}
else{
ImGui::Begin("Render view");
static float imgScaleX=1.0, imgScaleY=1.0;
ImGui::SliderFloat("Image Scale X",
&imgScaleX, 0.0f, 1.0f, "ratio = %.3f");
ImGui::SliderFloat("Image Scale Y",
&imgScaleY, 0.0f, 1.0f, "ratio = %.3f");
ImGui::Image((void*)((int*)rayRenderer.render.id),
ImVec2(rayRenderer.imageWidth*imgScaleX,
rayRenderer.imageHeight*imgScaleY),
ImVec2(0,0), ImVec2(1,1), ImColor(255,255,255,255),
ImColor(255,255,255,128));
ImGui::End();
}
ImGui::SetNextWindowPos(ImVec2(0, winHeight-70));
ImGui::SetNextWindowSize(ImVec2(winWidth, 70));
ImGui::Begin("scene", 0, ImGuiWindowFlags_NoTitleBar);
const char* items[] = {"3D", "Image"};
static const char* item_current = items[viewMode];
item_current = items[viewMode];
if(ImGui::BeginCombo("View", item_current, 0)){
for (int n = 0; n < IM_ARRAYSIZE(items); n++){
bool is_selected = (item_current == items[n]);
if(ImGui::Selectable(items[n], is_selected)){
item_current = items[n];
if(n == 0)
viewMode = SCENE;
else if(n == 1)
viewMode = RENDER;
}
if(is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
if(viewMode == RENDER){
if(rayRenderer.progress < 0.99){
ImGui::Text("progress: %d%%",
int(round(100.0*rayRenderer.progress)));
}
else{
ImGui::Text("Done!");
}
}
ImGui::End();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(viewMode == SCENE){
glRenderer.draw(scene, cam);
}
ImGui::Render();
ImGui_ImplSdlGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
}
ImGui_ImplSdlGL3_Shutdown();
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
void loadScene(std::string fpath, Scene& scn, Camera& cam,
GLRenderer& glRenderer){
//scn = Scene(fpath.c_str());
scn.loadFromFile(fpath.c_str());
cam = Camera(scn.camera.viewPoint, scn.camera.up,
scn.camera.fov);
cam.setPerspectiveMatrix(winWidth, winHeight);
glRenderer.setScene(scn);
}
bool createWindow(){
if (SDL_Init(SDL_INIT_VIDEO) < 0){
std::cout << "Failed to init SDL\n";
return false;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetSwapInterval(1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
window = SDL_CreateWindow("luksja raytracer",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
winWidth, winHeight, SDL_WINDOW_OPENGL);
if (!window){
std::cout << "Unable to create window\nkruci!!!\n";
CheckSDLError(__LINE__);
return false;
}
context = SDL_GL_CreateContext(window);
glbinding::Binding::initialize();
return true;
}
void CheckSDLError(int line = -1){
std::string error = SDL_GetError();
if (error != ""){
std::cout << "SDL Error : " << error << std::endl;
if (line != -1)
std::cout << "\nLine : " << line << std::endl;
SDL_ClearError();
}
}