-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
353 lines (283 loc) · 10.1 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
#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"
const char* vertexShaderSource = R"(
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
)";
const char* fragmentShaderSource = R"(
#version 330 core
out vec4 fragColor;
uniform vec4 vertexColor;
void main()
{
fragColor = vertexColor;
}
)";
std::unique_ptr<Art> art;
static GLFWwindow* window;
static int sw = 1024, sh = 1024;
bool get_window_size() {
int _w, _h;
glfwGetFramebufferSize(window, &_w, &_h);
bool resized = (_w != sw || _h != sh);
sw = _w; sh = _h;
return resized;
}
typedef uint32_t pixel_t;
pixel_t *image_data = NULL;
std::vector<pixel_t> image_data_vector;
#define texture_size (art->tex_w * art->tex_h * sizeof(pixel_t))
GLuint image_texture;
GLuint pboIds[2];
int pbo_index = 0;
GLuint shaderProgram;
// GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER, GL_MIRRORED_REPEAT, GL_REPEAT, GL_MIRROR_CLAMP_TO_EDGE
void init_shaders() {
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
void make_pbos() {
return;
image_data_vector.resize(texture_size);
image_data = image_data_vector.data();
// 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() {
return;
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);
}
char info[1024*4];
int main(int argc, char *argv[])
{
unsigned frate = 0;
int opt;
int vsync = 1;
int artarg = -1;
while ((opt = getopt(argc, argv, "sa:")) != -1) {
switch (opt) {
case 's':
vsync = 0;
break;
case 'a':
artarg = atoi(optarg);
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);
init_shaders();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
ImVec4 clear_color = ImVec4(0, 0, 0, 1.00f);
ImVec4 vertex_color = ImVec4(1, 0.5, 0.2, 1.00f);
ArtFactory af;
if (artarg != -1)
af.set_art(artarg);
art = af.get_art();
get_window_size();
art->resized(sw, sh);
make_pbos();
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
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_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", 1);
ScrollableSliderUInt("Max 1k frames before reinit", &art->max_kframes, 0, 1024, "%d", 1);
ImGui::ColorEdit4("Clear color", (float*)&clear_color);
ImGui::ColorEdit4("Vertex color", (float*)&vertex_color);
}
bool resize_pbo = art->gui();
if (art->frame_number % 120 == 0)
{
char *ti = info;
ti += cpu_load_text_now(info);
ti += sprintf(ti, "\n%.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);
}
ImGui::Text(info);
ImGui::End();
if (art->max_kframes)
resize_pbo |= art->frame_number > art->max_kframes*1024;
if (get_window_size() || resize_pbo) {
art->resized(sw, sh);
destroy_pbos();
make_pbos();
}
glUseProgram(shaderProgram);
int vertexColorLocation = glGetUniformLocation(shaderProgram, "vertexColor");
glUniform4f(vertexColorLocation, vertex_color.x, vertex_color.y, vertex_color.z, vertex_color.w);
art->draw(0);
#if 0
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, 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));
#endif
ImGui::Render();
glViewport(0, 0, sw, sh);
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();
glDeleteProgram(shaderProgram);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}