-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLMstuff.h
161 lines (135 loc) · 4.88 KB
/
GLMstuff.h
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
////////////////////////////////////////////////////////////////////////////////
/// @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"
////////////////////////////////////////////////////////////////////////////////
// Global variables - avoid these
// Window
int g_width{1360};
int g_height{768};
RayTracer rt= RayTracer(g_width, g_height);//for testing purposes
//Scene
//Get help from Jory for this
// Framebuffer
std::unique_ptr<glm::vec4[]> g_frame{nullptr}; ///< Framebuffer
// 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);
g_frame = std::make_unique<glm::vec4[]>(g_width*g_height);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Callback for resize of window
///
/// Responsible for setting window size (viewport) and projection matrix.
void resize(GLFWwindow* window, int _w, int _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);
for(int i = 0; i < g_width*g_height; ++i)
g_frame[i] = glm::vec4(0.f, 0.4f, 0.f, 0.f);
//////////////////////////////////////////////////////////////////////////////
// Draw
// 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);
glDrawPixels(g_width, g_height, GL_RGBA, GL_FLOAT, g_frame.get());
}
////////////////////////////////////////////////////////////////////////////////
/// @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) {
if(_action == GLFW_PRESS) {
switch(_key) {
// Escape key : quit application
case GLFW_KEY_ESCAPE:
std::cout << "Closing window" << std::endl;
glfwSetWindowShouldClose(_window, GLFW_TRUE);
break;
// Arrow keys
case GLFW_KEY_LEFT:
case GLFW_KEY_RIGHT:
break;
// Unhandled
default:
std::cout << "Unhandled key: " << _key << std::endl;
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;
}