forked from bradleybauer/music_visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.cpp
455 lines (407 loc) · 14 KB
/
draw.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* Useful links about geometry shaders
* https://open.gl/geometry
* https://www.khronos.org/opengl/wiki/Geometry_Shader
* http://www.informit.com/articles/article.aspx?p=2120983&seqNum=2
*/
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::endl;
#include <chrono>
#include <string.h>
#include "draw.h"
#include "audio_data.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
static GLFWwindow* window;
static const int POINTS = VISUALIZER_BUFSIZE - 1;
static int max_output_vertices;
static const int COORDS_PER_POINT = 1;
static int point_indices[POINTS];
// window dimensions
static int wwidth = 400;
static int wheight = 400;
static GLuint vbo; // vertex buffer object
static GLuint vao; // vertex array object
static GLuint tex[4]; // textures
static GLint tex_loc[4]; // texture uniform locations
static GLuint fb; // frameuffer
static GLuint fbtex; // framebuffer texture
static GLint fbtex_loc; // framebuffer texture location in display program
static GLuint prev_pixels;
static GLint prev_pixels_loc;
static GLubyte last_pixels[4 * 1920 * 1080];
static GLuint buf_program;
static GLuint img_program;
// uniforms
static GLint num_points_U;
static GLint max_output_vertices_U;
static GLint resolution_U;
static GLint resolution_buf_U;
static GLint time_U;
static void fps() {
static auto prev_time = std::chrono::steady_clock::now();
static int counter = 0;
auto now = std::chrono::steady_clock::now();
counter++;
if (now > prev_time + std::chrono::seconds(1)) {
cout << "gfx fps: " << counter << endl;
counter = 0;
prev_time = now;
}
}
static long filelength(FILE* file) {
long numbytes;
long savedpos = ftell(file);
fseek(file, 0, SEEK_END);
numbytes = ftell(file);
fseek(file, savedpos, SEEK_SET);
return numbytes;
}
static bool readShaderFile(const char* filename, GLchar*& out) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
cout << "ERROR Cannot open shader file!" << endl;
return false;
}
int bytesinfile = filelength(file);
out = (GLchar*)malloc(bytesinfile + 1);
int bytesread = fread(out, 1, bytesinfile, file);
out[bytesread] = 0; // Terminate the string with 0
fclose(file);
return true;
}
// Vim tip ']p' -> reindented paste
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
// if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
// if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
// if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
// if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE)
}
static void window_size_callback(GLFWwindow* window, int width, int height) {
wwidth = width;
wheight = height;
glViewport(0, 0, wwidth, wheight);
// Resize framebuffer texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbtex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wwidth, wheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Resize prev_pixels texture
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, prev_pixels);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wwidth, wheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
memset(last_pixels, 255, sizeof(last_pixels));
}
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
}
static std::string VERT = R"(
#version 330
void main(){}
)";
static std::string GEOM = R"(
#version 330
precision highp float;
layout(points) in;
layout(triangle_strip, max_vertices = 6) out;
out vec2 p;
void main() {
/* 1------3
| \ |
| \ |
| \|
0------2 */
const vec2 p0 = vec2(-1., -1.);
const vec2 p1 = vec2(-1., 1.);
gl_Position = vec4(p0, 0., 1.);
p = p0 * .5 + .5;
EmitVertex(); // 0
gl_Position = vec4(p1, 0., 1.);
p = p1 * .5 + .5;
EmitVertex(); // 1
gl_Position = vec4(-p1, 0., 1.);
p = -p1 * .5 + .5;
EmitVertex(); // 2
EndPrimitive();
gl_Position = vec4(-p1, 0., 1.);
p = -p1 * .5 + .5;
EmitVertex(); // 2
gl_Position = vec4(p1, 0., 1.);
p = p1 * .5 + .5;
EmitVertex(); // 1
gl_Position = vec4(-p0, 0., 1.);
p = -p0 * .5 + .5;
EmitVertex(); // 3
EndPrimitive();
}
)";
static std::string FRAG = R"(
#version 330
precision highp float;
uniform sampler2D t0;
uniform sampler2D t1;
uniform vec2 R;
uniform float T;
in vec2 p;
out vec4 c;
//
// vec4 bg=vec4(0.);
// vec4 fg=vec4(0.,204./255.,1.,1.);
//
// vec4 bg = vec4(1.);
// vec4 fg = vec4(0., 204. / 255., 1., 1.);
//
// vec4 bg=vec4(1.);
// vec4 fg=vec4(204./255.,0.,.1,1.);
//
// vec4 bg=vec4(0.);
// vec4 fg=vec4(1.,1.,.1,1.);
//
vec4 bg=vec4(1);
vec4 fg=vec4(0);
//
// vec4 bg=vec4(0);
// vec4 fg=vec4(1);
//
const float MIX = .8;
const float bright = 6.;
void main() {
// ASPECT RATIO ADJUSTED
// vec2 U = gl_FragCoord.xy / R;
// U = U * 2. - 1.;
// U.x *= max(1., R.x / R.y);
// U.x = clamp(U.x, -1., 1.);
// U.y *= max(1., R.y / R.x);
// U.y = clamp(U.y, -1., 1.);
// U = U * .5 + .5;
// if (U.x == 1. || U.x == 0.)
// c = bg;
// else if (U.y == 1. || U.y == 0.)
// c = bg;
// else
// c = mix(bg, fg, bright*texture(t0, U).r);
// c = mix(c, texture(t1, p), MIX);
// NOT ASPECT RATIO ADJUSTED
c = mix(mix(bg, fg, bright * texture(t0, p).r), texture(t1, p), MIX);
// Kali transfor for fun
// vec2 U = gl_FragCoord.xy/R;
// U=U*2.-1.;
// U.x*=max(1.,R.x/R.y);
// U.y*=max(1.,R.y/R.x);
// for (int i = 0; i < 10; ++i) {
// U=abs(U)/dot(U,U) - vec2(.5+.5*cos(T/10.), .5+.5*sin(T/10.));
// }
// U.x = clamp(U.x,-1.,1.);
// U.y = clamp(U.y,-1.,1.);
// U=U*.5+.5;
// c=mix(mix(bg, 4.*fg, texture(t0, U).r), texture(t1, p), MIX);
}
)";
static bool compile_shader(GLchar* s, GLuint& sn, GLenum stype) {
sn = glCreateShader(stype);
glShaderSource(sn, 1, &s, NULL);
glCompileShader(sn);
GLint isCompiled = 0;
glGetShaderiv(sn, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE) {
GLint maxLength = 0;
glGetShaderiv(sn, GL_INFO_LOG_LENGTH, &maxLength);
vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(sn, maxLength, &maxLength, &errorLog[0]);
for (auto c : errorLog)
cout << c;
cout << endl;
glDeleteShader(sn);
return false;
}
return true;
}
static bool link_program(GLuint& pn, GLuint& vs, GLuint& gs, GLuint fs) {
pn = glCreateProgram();
glAttachShader(pn, gs);
glAttachShader(pn, fs);
glAttachShader(pn, vs);
glLinkProgram(pn);
GLint isLinked = 0;
glGetProgramiv(pn, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE) {
GLint maxLength = 0;
glGetProgramiv(pn, GL_INFO_LOG_LENGTH, &maxLength);
vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(pn, maxLength, &maxLength, &infoLog[0]);
for (auto c : infoLog)
cout << c;
cout << endl;
glDeleteProgram(pn);
glDeleteShader(vs);
glDeleteShader(fs);
glDeleteShader(gs);
return false;
}
return true;
}
void draw(struct audio_data* audio) {
fps();
static auto start_time = std::chrono::steady_clock::now();
auto now = std::chrono::steady_clock::now();
double elapsed = (now - start_time).count() / 1e9;
// Render to texture
glUseProgram(buf_program);
glUniform1i(num_points_U, POINTS);
glUniform1i(max_output_vertices_U, max_output_vertices);
glUniform2f(resolution_U, float(wwidth), float(wheight));
time_U = glGetUniformLocation(buf_program, "T");
glUniform1f(time_U, elapsed);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glClear(GL_COLOR_BUFFER_BIT);
// glActivateTexture activates a certain texture unit.
// each texture unit holds one texture of each dimension of texture
// {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBEMAP}
// because I'm using 4, 1 dimensional textures i need to store them in separate texture units
//
// glUniform1i(textureLoc, int) sets what texture unit the sampler in the shader reads from
//
// A texture binding created with glBindTexture remains active until a different texture is
// bound to the same target (in the active unit? I think), or until the bound texture is deleted
// with glDeleteTextures. So I do not need to rebind
// glBindTexture(GL_TEXTURE_1D, tex[X]);
#define TEXMACRO(X, Y) \
glActiveTexture(GL_TEXTURE0 + X); \
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, VISUALIZER_BUFSIZE, GL_RED, GL_FLOAT, audio->Y); \
glUniform1i(tex_loc[X], X);
audio->mtx.lock();
TEXMACRO(0, audio_l)
TEXMACRO(1, audio_r)
TEXMACRO(2, freq_l)
TEXMACRO(3, freq_r)
audio->mtx.unlock();
glDrawArrays(GL_POINTS, 0, POINTS);
// Render to screen
glUseProgram(img_program);
glBindFramebuffer(GL_FRAMEBUFFER, 0); // bind window manager's fbo
glClear(GL_COLOR_BUFFER_BIT);
glUniform2f(resolution_buf_U, float(wwidth), float(wheight));
glUniform1i(fbtex_loc, 0);
glUniform1i(prev_pixels_loc, 1);
glActiveTexture(GL_TEXTURE1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, wwidth, wheight, GL_RGBA, GL_UNSIGNED_BYTE,
last_pixels);
time_U = glGetUniformLocation(img_program, "T");
glUniform1f(time_U, elapsed);
glDrawArrays(GL_POINTS, 0, 1);
glReadPixels(0, 0, wwidth, wheight, GL_RGBA, GL_UNSIGNED_BYTE, last_pixels);
glfwSwapBuffers(window);
glfwPollEvents();
}
bool initialize_gl() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(wwidth, wheight, "float me", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSwapInterval(1);
glewExperimental = GL_TRUE;
glewInit();
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
cout << "Renderer: " << renderer << endl;
cout << "OpenGL version supported "<< version << endl;
glViewport(0, 0, wwidth, wheight);
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
bool ret = true;
GLchar* vs_c, *fs_c, *gs_c;
ret = readShaderFile("../shaders/vertex.glsl", vs_c);
ret = readShaderFile("../shaders/geom.glsl", gs_c);
ret = readShaderFile("../shaders/image.glsl", fs_c);
if (!ret) {
ret = readShaderFile("shaders/vertex.glsl", vs_c);
ret = readShaderFile("shaders/geom.glsl", gs_c);
ret = readShaderFile("shaders/image.glsl", fs_c);
if (!ret) {
ret = false;
return ret;
}
}
GLuint vs, gs, fs;
ret = compile_shader(vs_c, vs, GL_VERTEX_SHADER);
ret = compile_shader(gs_c, gs, GL_GEOMETRY_SHADER);
ret = compile_shader(fs_c, fs, GL_FRAGMENT_SHADER);
ret = link_program(buf_program, vs, gs, fs);
ret = compile_shader((GLchar*)VERT.data(), vs, GL_VERTEX_SHADER);
ret = compile_shader((GLchar*)GEOM.data(), gs, GL_GEOMETRY_SHADER);
ret = compile_shader((GLchar*)FRAG.data(), fs, GL_FRAGMENT_SHADER);
ret = link_program(img_program, vs, gs, fs);
if (!ret) {
cout << endl << endl << "Could not find shader file OR could not compile shaders." << endl << endl;
return ret;
}
glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES, &max_output_vertices);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE); // for nice lines?
glClearColor(0., 0., 0., 1.);
for (int i = 0; i < POINTS; ++i)
point_indices[i] = i;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, POINTS * sizeof(int), point_indices, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
const GLuint loc = glGetAttribLocation(buf_program, "_point_index");
glEnableVertexAttribArray(loc);
glVertexAttribIPointer(loc, COORDS_PER_POINT, GL_INT, 0, NULL);
// glDisableVertexAttribArray(loc);
num_points_U = glGetUniformLocation(buf_program, "num_points");
max_output_vertices_U = glGetUniformLocation(buf_program, "max_output_vertices");
resolution_U = glGetUniformLocation(buf_program, "R");
resolution_buf_U = glGetUniformLocation(img_program, "R");
time_U = glGetUniformLocation(buf_program, "T");
glGenTextures(4, tex);
#define ILOVEMACRO(X, Y) \
tex_loc[X] = glGetUniformLocation(buf_program, Y); \
glUniform1i(tex_loc[X], X); \
glActiveTexture(GL_TEXTURE0 + X); \
glBindTexture(GL_TEXTURE_1D, tex[X]); \
glTexImage1D(GL_TEXTURE_1D, 0, GL_R32F, VISUALIZER_BUFSIZE, 0, GL_RED, GL_FLOAT, NULL); \
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); \
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT); \
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); \
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
ILOVEMACRO(0, "SL")
ILOVEMACRO(1, "SR")
ILOVEMACRO(2, "FL")
ILOVEMACRO(3, "FR")
glGenTextures(1, &fbtex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbtex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wwidth, wheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbtex, 0);
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &prev_pixels);
glBindTexture(GL_TEXTURE_2D, prev_pixels);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wwidth, wheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// location of the sampler2D in the fragment shader
fbtex_loc = glGetUniformLocation(img_program, "t0");
prev_pixels_loc = glGetUniformLocation(img_program, "t1");
return ret;
}
bool should_loop() {
return !glfwWindowShouldClose(window);
}
void deinit_drawing() {
glfwDestroyWindow(window);
}