-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
268 lines (208 loc) · 7.21 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
#include <cstdint>
#include <memory>
#include <unistd.h> // getopt
#include <error.h>
#define GL_GLEXT_PROTOTYPES 1
#define GL3_PROTOTYPES 1
#include "imgui.h"
#include "imgui_elements.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
#define GL_SILENCE_DEPRECATION
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <GLES2/gl2.h>
#endif
#include <GLFW/glfw3.h> // Will drag system OpenGL headers
#include "artfactory.h"
std::unique_ptr<Art> art;
static void
*xrealloc(void *p, size_t size)
{
void *ret;
if ((ret = realloc(p, size)) == NULL) {
error(1, errno, "out of memory");
}
return ret;
}
static GLFWwindow* window;
static int texture_size;
static int sw = 1024, sh = 1024;
bool get_window_size(int *w, int *h) {
bool ret = false;
int _w, _h;
glfwGetFramebufferSize(window, &_w, &_h);
if (_w != sw || _h != sh) {
ret = true;
if (w) *w = _w;
if (h) *h = _h;
}
sw = _w; sh = _h;
return ret;
}
uint32_t *image_data = NULL;
GLuint image_texture;
GLuint pboIds[2];
int pbo_index = 0;
ImVec4 clear_color = ImVec4(0, 0, 0, 1.00f);
void make_pbos() {
texture_size = art->tex_w * art->tex_h * 4;
image_data = (uint32_t*)xrealloc(image_data, texture_size);
// AUTHOR: Song Ho Ahn ([email protected])
// http://www.songho.ca/opengl/gl_pbo.html
glGenTextures(1, &image_texture);
glBindTexture(GL_TEXTURE_2D, image_texture);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,
0, GL_RGBA, art->tex_w, art->tex_h,
0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)image_data);
glBindTexture(GL_TEXTURE_2D, 0);
glGenBuffers(2, pboIds);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[0]);
glBufferData(GL_PIXEL_UNPACK_BUFFER, texture_size,
0, GL_STREAM_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[1]);
glBufferData(GL_PIXEL_UNPACK_BUFFER, texture_size,
0, GL_STREAM_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}
void destroy_pbos() {
glDeleteTextures(1, &image_texture);
glDeleteBuffers(2, pboIds);
}
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
int main(int argc, char *argv[])
{
int opt;
int vsync = 1;
while ((opt = getopt(argc, argv, "s")) != -1) {
switch (opt) {
case 's':
vsync = 0;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-s]\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
const char* glsl_version = "#version 100";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__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
window = glfwCreateWindow(sw, sh, "Dear ImGui screensaver", NULL, NULL);
if (window == NULL)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(vsync);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
ArtFactory af;
art = af.get_art();
get_window_size(0,0);
art->resized(sw, sh);
make_pbos();
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin(art->name());
if (af.render_gui())
{
art = af.get_art();
art->resized(sw, sh);
destroy_pbos();
make_pbos();
}
if (ImGui::CollapsingHeader("Clear Configuration"))
{
ScrollableSliderUInt("force clear every N frames", &art->clear_every, 0, 1024, "%d", 2);
ImGui::ColorEdit4("Clear color", (float*)&clear_color);
}
bool resize_pbo = art->gui();
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);
cpu_load_text();
ImGui::End();
if (get_window_size(0,0) || resize_pbo) {
art->resized(sw, sh);
destroy_pbos();
make_pbos();
}
int nexti = pbo_index;
pbo_index = pbo_index ? 0 : 1;
glBindTexture(GL_TEXTURE_2D, image_texture);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[pbo_index]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
art->tex_w, art->tex_h, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[nexti]);
glBufferData(GL_PIXEL_UNPACK_BUFFER, texture_size,
0, GL_STREAM_DRAW);
uint32_t* ptr = (uint32_t*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
assert(ptr);
art->draw(ptr);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
ImGui::GetBackgroundDrawList()->AddImage((void*)(intptr_t)image_texture,
ImVec2(0, 0), ImVec2(sw, sh),
ImVec2(0, 0), ImVec2((float)sw/art->tex_w, (float)sh/art->tex_h));
ImGui::Render();
glViewport(0, 0, sw, sh);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
++art->frame_number;
if (art->clear_every != 0 && art->frame_number % art->clear_every == 0)
art->clear();
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
destroy_pbos();
free(image_data);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}