-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
402 lines (341 loc) · 11.6 KB
/
main.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
////////////////////////////////////////////////////////////////////////////////
/// @file
/// @brief Contains main function to create a window and run engine that
/// repeatedly generates a framebuffer and displays it.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Includes
// STL
#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
// Engine
#include "GLInclude.h"
#include "RayTracer.h"
#include "Scene.h"
#include "Plane.h"
#include "Sphere.h"
#include "Object.h"
#include "Material.h"
/*
#include "CompileShaders.h"
*/
/*
////////////////////////////////////////////////////////////////////////////////
/// @brief Simple vertex class.
struct Vertex {
glm::vec4 pos; ///< Position
glm::vec4 col; ///< Color
Vertex(const glm::vec4& _pos, const glm::vec4& _col) :
pos{_pos}, col{_col}{}
};
*/
////////////////////////////////////////////////////////////////////////////////
// Global variables - avoid these
/*
// Rendering system
enum class Renderer {RayTracer, Rasterizer};
constexpr Renderer g_renderer = Renderer::Rasterizer;
*/
// Window
int g_width{1360};
int g_height{768};
Scene scn = Scene();
RayTracer rt= RayTracer(g_width, g_height);
// RayTracer
//GLuint g_texture; ///< Texture Object
//GLuint g_framebuffer; ///< Framebuffer Object
std::unique_ptr<glm::vec4[]> g_frame{nullptr}; ///< Framebuffer
/*
// Rasterizer
GLuint g_program{0}; ///< Shader program
GLuint g_vao{0}; ///< Vertex Array Object
GLuint g_vbo{0}; ///< Vertex Buffer Object
*/
// Frame rate
const unsigned int FPS = 60;
float g_frameRate{0.f};
std::chrono::high_resolution_clock::time_point g_frameTime{
std::chrono::high_resolution_clock::now()};
float g_delay{0.f};
float g_framesPerSecond{0.f};
////////////////////////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize GL settings
void
initialize(GLFWwindow* _window) {
glClearColor(0.f, 0.f, 0.f, 1.f);
scn.readFromFile("example.scene");
g_frame = std::make_unique<glm::vec4[]>(g_width*g_height);
//initialize method to Renderer (like initialize g_frame)
//code is from Proj02 when dealing w/ rasterizer
/*
switch(g_renderer) {
case Renderer::RayTracer:
// Generate texture
glGenTextures(1, &g_texture);
// Generate framebuffer
glGenFramebuffers(1, &g_framebuffer);
g_frame = std::make_unique<glm::vec4[]>(g_width*g_height);
break;
case Renderer::Rasterizer:
glEnable(GL_DEPTH_TEST);
g_program = compileProgram("Shaders/passthrough.vert",
"Shaders/passthrough.frag");
// Simple static data
static Vertex vertices[] = {
Vertex{glm::vec4{-1.f, -1.f, 0, 1}, glm::vec4{1.f, 0.f, 0.f, 1.f}},
Vertex{glm::vec4{ 1.f, 0.f, 0, 1}, glm::vec4{0.f, 1.f, 0.f, 1.f}},
Vertex{glm::vec4{ 0.f, 1.f, 0, 1}, glm::vec4{0.f, 0.f, 1.f, 1.f}}
};
// Generate vertex array
glGenVertexArrays(1, &g_vao);
glBindVertexArray(g_vao);
// Generate/specify vertex buffer
glGenBuffers(1, &g_vbo);
glBindBuffer(GL_ARRAY_BUFFER, g_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*3, vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (char*)NULL + 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (char*)NULL + sizeof(glm::vec4));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
break;
}
*/
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Callback for resize of window
///
/// Responsible for setting window size (viewport) and projection matrix.
void resize(GLFWwindow* window, int _w, int _h) {
/*
// Resize the framebuffer if we are ray tracing and the screen gets larger.
if (g_renderer == Renderer::RayTracer && g_width*g_height >= _w*_h) {
g_frame = std::make_unique<glm::vec4[]>(_w*_h);
}
*/
g_width = _w;
g_height = _h;
// Viewport
glfwGetFramebufferSize(window, &g_width, &g_height);
glViewport(0, 0, g_width, g_height);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Draw function for single frame
void
draw(GLFWwindow* _window, double _currentTime) {
//////////////////////////////////////////////////////////////////////////////
// Clear
glClear(GL_COLOR_BUFFER_BIT);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //from Proj02 file
/*
if(g_renderer == Renderer::RayTracer) {
// Not strictly necessary...
for(int i = 0; i < g_width*g_height; ++i)
g_frame[i] = glm::vec4(0.f, 0.4f, 0.f, 0.f);
}
*/
rt.clear();
//////////////////////////////////////////////////////////////////////////////
// Draw
//from Proj02 code
/*
switch(g_renderer) {
case Renderer::RayTracer:
// Simple static :P
for(int i = 0; i < g_width*g_height; ++i)
g_frame[i] = glm::vec4(float(rand())/RAND_MAX, float(rand())/RAND_MAX,
float(rand())/RAND_MAX, 1.f);
// Open GL3 way of copying an image from the CPU to the
// full screen on the GPU
glBindTexture(GL_TEXTURE_2D, g_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, g_width, g_height, 0, GL_RGBA,
GL_FLOAT, g_frame.get());
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, g_framebuffer);
glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, g_texture, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, g_framebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, g_width, g_height, 0, 0, g_width, g_height,
GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
break;
case Renderer::Rasterizer:
// Render the triangle
glBindVertexArray(g_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
break;
}
*/
rt.render(scn);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Main application loop
void
run(GLFWwindow* _window) {
using namespace std::chrono;
std::cout << "Starting main loop" << std::endl;
while(!glfwWindowShouldClose(_window)) {
draw(_window, glfwGetTime());
////////////////////////////////////////////////////////////////////////////
// Show
glfwSwapBuffers(_window);
glfwPollEvents();
////////////////////////////////////////////////////////////////////////////
// Record frame time
high_resolution_clock::time_point time = high_resolution_clock::now();
g_frameRate = duration_cast<duration<float>>(time - g_frameTime).count();
g_frameTime = time;
g_framesPerSecond = 1.f/(g_delay + g_frameRate);
printf("FPS: %6.2f\n", g_framesPerSecond);
////////////////////////////////////////////////////////////////////////////
// Delay to fix the frame-rate
g_delay = std::max(0.f, 1.f/FPS - g_frameRate);
std::this_thread::sleep_for(std::chrono::microseconds(
static_cast<long int>(g_delay*1000)));
}
std::cout << "Ending main loop" << std::endl;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Callback function for keyboard input
/// @param _window
/// @param _key Key
/// @param _scancode Platform specific keycode
/// @param _action Action, e.g., pressed or released
/// @param _mods Keyboard modifiers
void
keyCallback(GLFWwindow* _window, int _key, int _scancode,
int _action, int _mods) {
std::cout<<GLFW_KEY_W<<std::endl;
if(_action == GLFW_PRESS) {
switch(_key) {
//movement keys
case GLFW_KEY_W:
scn.getCam().moveZ(-1);
break;
case GLFW_KEY_S:
scn.getCam().moveZ(1);
break;
case GLFW_KEY_A:
scn.getCam().moveX(-1);
break;
case GLFW_KEY_D:
scn.getCam().moveX(1);
break;
case GLFW_KEY_SPACE:
scn.getCam().moveY(1);
break;
case GLFW_KEY_C:
scn.getCam().moveY(-1);
break;
case GLFW_KEY_LEFT:
scn.getCam().rotateLR(2);
break;
case GLFW_KEY_RIGHT:
scn.getCam().rotateLR(-2);
break;
case GLFW_KEY_UP:
scn.getCam().rotateUD(2);
break;
case GLFW_KEY_DOWN:
scn.getCam().rotateUD(-2);
break;
// Escape key : quit application
case GLFW_KEY_ESCAPE:
std::cout << "Closing window" << std::endl;
glfwSetWindowShouldClose(_window, GLFW_TRUE);
break;
// Arrow keys
default:
std::cout << "Unhandled key: " << _key << std::endl;
break;
}
}
}
/*
////////////////////////////////////////////////////////////////////////////////
/// @brief Callback function for window closing
/// @param _window
void
windowCloseCallback(GLFWwindow* _window) {
// Clean up memory by releasing GPU resources
switch(g_renderer) {
case Renderer::RayTracer:
glDeleteTextures(1, &g_texture);
glDeleteFramebuffers(1, &g_framebuffer);
break;
case Renderer::Rasterizer:
glDeleteBuffers(1, &g_vbo);
glDeleteVertexArrays(1, &g_vao);
break;
}
}
*/
////////////////////////////////////////////////////////////////////////////////
/// @brief Error handler
/// @param _code Error code
/// @param _msg Error message
void errorCallback(int _code, const char* _msg) {
std::cerr << "Error " << _code << ": " << _msg << std::endl;
}
////////////////////////////////////////////////////////////////////////////////
// Main
////////////////////////////////////////////////////////////////////////////////
/// @brief main
/// @param _argc Count of command line arguments
/// @param _argv Command line arguments
/// @return Application success status
int
main(int _argc, char** _argv) {
//////////////////////////////////////////////////////////////////////////////
// Initialize
Scene scn = Scene();
//rt.render(scn);
std::cout << "Initializing GLFWWindow" << std::endl;
// GLFW
glfwSetErrorCallback(errorCallback);
if(!glfwInit()) {
std::cerr << "GLFW Cannot initialize" << std::endl;
return 1;
}
/*
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
*/
GLFWwindow* window = glfwCreateWindow(
g_width, g_height, "Spiderling: A Rudamentary Game Engine", NULL, NULL);
if(!window) {
std::cerr << "GLFW Cannot create window" << std::endl;
return 1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwGetFramebufferSize(window, &g_width, &g_height);
glViewport(0, 0, g_width, g_height); // Initialize viewport
// Assign callback functions
std::cout << "Assigning Callback functions" << std::endl;
glfwSetFramebufferSizeCallback(window, resize);
glfwSetKeyCallback(window, keyCallback);
//glfwSetWindowCloseCallback(window, windowCloseCallback);
// Program initialize
std::cout << "Initializing application" << std::endl;
initialize(window);
//////////////////////////////////////////////////////////////////////////////
// Main loop
run(window);
//////////////////////////////////////////////////////////////////////////////
// Cleanup GLFW Window
std::cout << "Destroying GLFWWindow" << std::endl;
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}