-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.cpp
174 lines (141 loc) · 4.34 KB
/
app.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
#include "app.h"
using namespace std;
namespace gui {
bool initialized = false;
GLFWwindow* currentGLFWWindow = nullptr;
int window_width = 0, window_height = 0;
ImVec4 clear_color = ImColor(96, 96, 96);
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
static void resize_callback(GLFWwindow* window, int new_width, int new_height) {
glViewport(0, 0, window_width = new_width, window_height = new_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, window_width, window_height, 0.0, 0.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
/**
* A callback function for GLFW to execute when an internal error occurs with the
* library.
*/
void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
void destroy() {
cout << "Closing application";
// Close ImGui
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
// Close GLFW
if (currentGLFWWindow) glfwDestroyWindow(currentGLFWWindow);
glfwTerminate();
}
void setup() {
// Setup window
glfwSetErrorCallback(error_callback);
if (!glfwInit())
return;
// Decide GL+GLSL versions
#if __APPLE__
// GL 3.2 + GLSL 150
const char* glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
// Create window with graphics context
currentGLFWWindow = glfwCreateWindow(1280, 720, "Raytracing in One Weekend", NULL, NULL);
if (currentGLFWWindow == NULL)
return;
glfwMakeContextCurrent(currentGLFWWindow);
glfwSwapInterval(3); // Enable vsync
if (!gladLoadGL()) {
// GLAD failed
cerr << "GLAD failed to initialize :(";
//Use the terminate() function to safely close the application
destroy();
return;
}
//Sets the number of screen updates to wait before swapping the buffers of a window
//Handles vertical synchronization
setSwapInterval(1);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui_ImplGlfw_InitForOpenGL(currentGLFWWindow, true);
ImGui_ImplOpenGL3_Init(glsl_version);
setStyle();
//Initialize UI
initUI();
}
void render() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
populateUI(); //defined in ui.h, implemented in ui.cpp
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(currentGLFWWindow, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
//Show the newly rendered content and replace the buffer
glfwSwapBuffers(currentGLFWWindow);
}
void begin()
{
setup();
while (!glfwWindowShouldClose(currentGLFWWindow))
{
render();
glfwPollEvents();
}
destroy();
}
float estimateSystemScale()
{
//Set scale based on scale of monitor
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
float scale = 2.f;
glfwGetMonitorContentScale(monitor, &scale, nullptr);
return scale;
}
GLFWwindow* getCurrentWindow()
{
return nullptr;
}
void setSwapInterval(int newSwapInterval)
{
glfwSwapInterval(newSwapInterval);
}
void setClearColor(int red, int green, int blue, int alpha)
{
clear_color.w = (float)red;
clear_color.x = (float)green;
clear_color.y = (float)blue;
clear_color.z = (float)alpha;
}
void setGuiScale(float guiScale) {
int fbw, fbh, ww, wh;
glfwGetFramebufferSize(currentGLFWWindow, &fbw, &fbh);
glfwGetWindowSize(currentGLFWWindow, &ww, &wh);
float pixelRatio = (float)fbw / (float)ww;
ImGui::GetIO().FontGlobalScale = guiScale / pixelRatio;
}
}