-
Notifications
You must be signed in to change notification settings - Fork 2
/
gg.c
478 lines (411 loc) · 16.1 KB
/
gg.c
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* Fuer Elite - 64k Intro by Team210 at Underground Conference 9
* Copyright (C) 2018 Alexander Kraus <nr4@z10.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define DEBUG
const char *demoname = "Gross Gloss/Team210";
unsigned int muted = 0.;
int _fltused = 0;
#include "common.h"
void load_demo()
{
// Load loading bar shader
printf("++++ Creating Loading bar.\n");
int load_size = strlen(load_frag);
load_handle = glCreateShader(GL_FRAGMENT_SHADER);
load_program = glCreateProgram();
glShaderSource(load_handle, 1, (GLchar **)&load_frag, &load_size);
glCompileShader(load_handle);
printf("---> Load shader:\n");
#ifdef DEBUG
debug(load_handle);
#endif
glAttachShader(load_program, load_handle);
glLinkProgram(load_program);
printf("---> Load Program:\n");
#ifdef DEBUG
debugp(load_program);
#endif
glUseProgram(load_program);
load_progress_location = glGetUniformLocation(load_program, LOAD_VAR_IPROGRESS);
load_time_location = glGetUniformLocation(load_program, LOAD_VAR_ITIME);
load_resolution_location = glGetUniformLocation(load_program, LOAD_VAR_IRESOLUTION);
printf("++++ Loading bar created.\n");
// Load post processing shader
printf("++++ Creating Post Shader.\n");
int post_size = strlen(post_frag);
post_handle = glCreateShader(GL_FRAGMENT_SHADER);
post_program = glCreateProgram();
glShaderSource(post_handle, 1, (GLchar **)&post_frag, &post_size);
glCompileShader(post_handle);
printf("---> Post shader:\n");
#ifdef DEBUG
debug(post_handle);
#endif
glAttachShader(post_program, post_handle);
glLinkProgram(post_program);
printf("---> Post Program:\n");
#ifdef DEBUG
debugp(post_program);
#endif
glUseProgram(post_program);
post_channel0_location = glGetUniformLocation(post_program, POST_VAR_ICHANNEL0);
post_fsaa_location = glGetUniformLocation(post_program, POST_VAR_IFSAA);
post_resolution_location = glGetUniformLocation(post_program, POST_VAR_IRESOLUTION);
post_time_location = glGetUniformLocation(post_program, POST_VAR_ITIME);
printf("++++ Post shader created.\n");
// Load ui shader
printf("++++ Creating Ui Shader.\n");
int ui_size = strlen(ui_frag);
ui_handle = glCreateShader(GL_FRAGMENT_SHADER);
ui_program = glCreateProgram();
glShaderSource(ui_handle, 1, (GLchar **)&ui_frag, &ui_size);
glCompileShader(ui_handle);
printf("---> Ui shader:\n");
#ifdef DEBUG
debug(ui_handle);
#endif
glAttachShader(ui_program, ui_handle);
glLinkProgram(ui_program);
printf("---> Ui Program:\n");
#ifdef DEBUG
debugp(ui_program);
#endif
glUseProgram(ui_program);
ui_channel0_location = glGetUniformLocation(ui_program, UI_VAR_ICHANNEL0);
ui_resolution_location = glGetUniformLocation(ui_program, UI_VAR_IRESOLUTION);
ui_time_location = glGetUniformLocation(ui_program, UI_VAR_ITIME);
ui_maxtime_location = glGetUniformLocation(ui_program, UI_VAR_IMAXTIME);
ui_mouse_location = glGetUniformLocation(ui_program, UI_VAR_IMOUSE);
ui_playing_location = glGetUniformLocation(ui_program, UI_VAR_IPLAYING);
printf("++++ Ui shader created.\n");
// Create framebuffer for rendering first pass to
glGenFramebuffers(1, &first_pass_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, first_pass_framebuffer);
glGenTextures(1, &first_pass_texture);
glBindTexture(GL_TEXTURE_2D, first_pass_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
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, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, first_pass_texture, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
// Generate music framebuffer
// Initialize sequence texture
printf("sequence texture width is: %d\n", sequence_texture_size); // TODO: remove
glGenTextures(1, &sequence_texture_handle);
glBindTexture(GL_TEXTURE_2D, sequence_texture_handle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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, sequence_texture_size, sequence_texture_size, 0, GL_RGBA, GL_UNSIGNED_BYTE, sequence_texture);
glGenFramebuffers(1, &snd_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, snd_framebuffer);
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
unsigned int snd_texture;
glGenTextures(1, &snd_texture);
glBindTexture(GL_TEXTURE_2D, snd_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texs, texs, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, snd_texture, 0);
// Music allocs
nblocks1 = sample_rate * duration1 / block_size + 1;
music1_size = nblocks1 * block_size;
smusic1 = (float*)malloc(4 * music1_size);
short *dest = (short*)smusic1;
for (int i = 0; i < 2 * music1_size; ++i)
dest[i] = 0;
updateBar();
// Load music shader
LoadMusicThread(0);
updateBar();
// Load Logo 210 shader
// LoadDecayingfactoryThread(0);
// updateBar();
// SwapBuffers(hdc);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
LoadSymbols();
LoadPrograms();
LoadTextThread(0);
updateBar();
for (int music_block = 0; music_block < nblocks1; ++music_block)
{
glBindFramebuffer(GL_FRAMEBUFFER, snd_framebuffer);
glUseProgram(sfx_program);
printf("Rendering SFX block %d/%d -> %le\n", music_block, nblocks1, .5*(float)music_block / (float)nblocks1);
double tstart = (double)(music_block*block_size);
glViewport(0, 0, texs, texs);
glUniform1f(sfx_volumelocation, 1.);
glUniform1f(sfx_samplerate_location, (float)sample_rate);
glUniform1f(sfx_blockoffset_location, (float)tstart);
glUniform1i(sfx_texs_location, texs);
glUniform1i(sfx_sequence_texture_location, 0);
glUniform1f(sfx_sequence_width_location, sequence_texture_size);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sequence_texture_handle);
quad();
glReadPixels(0, 0, texs, texs, GL_RGBA, GL_UNSIGNED_BYTE, smusic1 + music_block * block_size);
glFlush();
unsigned short *buf = (unsigned short*)smusic1;
short *dest = (short*)smusic1;
if (!muted)
for (int j = 2 * music_block*block_size; j < 2 * (music_block + 1)*block_size; ++j)
dest[j] = (buf[j] - (1 << 15));
else
for (int j = 2 * music_block*block_size; j < 2 * (music_block + 1)*block_size; ++j)
dest[j] = 0.;
progress += .5/nblocks1;
updateBar();
}
glUseProgram(0);
initialize_sound();
}
unsigned long __stdcall LoadMusicThread( void *lpParam)
{
int sfx_size = strlen(sfx_frag);
sfx_handle = glCreateShader(GL_FRAGMENT_SHADER);
sfx_program = glCreateProgram();
glShaderSource(sfx_handle, 1, (GLchar **)&sfx_frag, &sfx_size);
glCompileShader(sfx_handle);
printf("---> SFX shader:\n");
#ifdef DEBUG
debug(sfx_handle);
#endif
glAttachShader(sfx_program, sfx_handle);
glLinkProgram(sfx_program);
printf("---> SFX program:\n");
#ifdef DEBUG
debugp(sfx_program);
#endif
glUseProgram(sfx_program);
sfx_samplerate_location = glGetUniformLocation(sfx_program, SFX_VAR_ISAMPLERATE);
sfx_blockoffset_location = glGetUniformLocation(sfx_program, SFX_VAR_IBLOCKOFFSET);
sfx_volumelocation = glGetUniformLocation(sfx_program, SFX_VAR_IVOLUME);
sfx_texs_location = glGetUniformLocation(sfx_program, SFX_VAR_ITEXSIZE);
sfx_sequence_texture_location = glGetUniformLocation(sfx_program, SFX_VAR_ISEQUENCE);
sfx_sequence_width_location = glGetUniformLocation(sfx_program, SFX_VAR_ISEQUENCEWIDTH);
printf("++++ SFX shader created.\n");
music_loading = 1;
progress += .1/NSHADERS;
return 0;
}
unsigned long __stdcall LoadLogo210Thread( void * lpParam)
{
int logo210_size = strlen(logo210_frag);
logo210_handle = glCreateShader(GL_FRAGMENT_SHADER);
logo210_program = glCreateProgram();
glShaderSource(logo210_handle, 1, (GLchar **)&logo210_frag, &logo210_size);
glCompileShader(logo210_handle);
printf("---> Logo 210 shader:\n");
#ifdef DEBUG
debug(logo210_handle);
#endif
glAttachShader(logo210_program, logo210_handle);
glLinkProgram(logo210_program);
printf("---> Logo 210 program:\n");
#ifdef DEBUG
debugp(logo210_program);
#endif
glUseProgram(logo210_program);
logo210_time_location = glGetUniformLocation(logo210_program, LOGO210_VAR_ITIME);
logo210_resolution_location = glGetUniformLocation(logo210_program, LOGO210_VAR_IRESOLUTION);
printf("++++ Logo 210 shader created.\n");
progress += .1/NSHADERS;
return 0;
}
unsigned long __stdcall LoadTextThread(void * lpParam)
{
int text_size = strlen(text_frag);
text_handle = glCreateShader(GL_FRAGMENT_SHADER);
text_program = glCreateProgram();
glShaderSource(text_handle, 1, (GLchar **)&text_frag, &text_size);
glCompileShader(text_handle);
printf("---> Text shader:\n");
#ifdef DEBUG
debug(text_handle);
#endif
glAttachShader(text_program, text_handle);
glLinkProgram(text_program);
printf("---> Text program:\n");
#ifdef DEBUG
debugp(text_program);
#endif
glUseProgram(text_program);
text_time_location = glGetUniformLocation(text_program, TEXT_VAR_ITIME);
text_resolution_location = glGetUniformLocation(text_program, TEXT_VAR_IRESOLUTION);
text_font_location= glGetUniformLocation(text_program, TEXT_VAR_IFONT);
text_font_width_location = glGetUniformLocation(text_program, TEXT_VAR_IFONTWIDTH);
text_channel0_location = glGetUniformLocation(text_program, TEXT_VAR_ICHANNEL0);
printf("++++ Text shader created.\n");
// Initialize font texture
printf("font texture width is: %d\n", font_texture_size); // TODO: remove
glGenTextures(1, &font_texture_handle);
glBindTexture(GL_TEXTURE_2D, font_texture_handle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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, font_texture_size, font_texture_size, 0, GL_RGBA, GL_UNSIGNED_BYTE, font_texture);
progress += .1/NSHADERS;
return 0;
}
void quad()
{
glBegin(GL_QUADS);
glVertex3f(-1,-1,0);
glVertex3f(-1,1,0);
glVertex3f(1,1,0);
glVertex3f(1,-1,0);
glEnd();
glFlush();
}
void updateBar()
{
glBindFramebuffer(GL_FRAMEBUFFER, first_pass_framebuffer);
// Render first pass
glViewport(0,0,w,h);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(load_program);
glUniform2f(load_resolution_location, w, h);
glUniform1f(load_progress_location, progress);
quad();
// Render second pass (Post processing) to screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0,w,h);
glUseProgram(post_program);
glUniform2f(post_resolution_location, w, h);
glUniform1f(post_fsaa_location, fsaa);
glUniform1i(post_channel0_location, 0);
glUniform1f(post_time_location, 0.);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, first_pass_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
quad();
glBindTexture(GL_TEXTURE_2D, 0);
flip_buffers();
}
// Pure opengl drawing code, essentially cross-platform
void draw()
{
glBindFramebuffer(GL_FRAMEBUFFER, first_pass_framebuffer);
float t = t_now;
if(t > t_end)
ExitProcess(0);
if(scene_override)
{
if(override_index == 1)
{
glUseProgram(graffiti_program);
glUniform1f(graffiti_iTime_location, t);
glUniform2f(graffiti_iResolution_location, w, h);
}
else if(override_index == 2)
{
t = t_now + 49.655;
glUseProgram(groundboxes_program);
glUniform1f(groundboxes_iTime_location, t-49.655);
glUniform2f(groundboxes_iResolution_location, w, h);
}
else if(override_index == 3)
{
t = t_now + 82.76;
glUseProgram(voronoidesign_program);
glUniform1f(voronoidesign_iTime_location, t-82.76);
glUniform2f(voronoidesign_iResolution_location, w, h);
}
else if(override_index == 4)
{
t = t_now + 99.31;
glUseProgram(bloodcells_program);
glUniform1f(bloodcells_iTime_location, t-99.31);
glUniform2f(bloodcells_iResolution_location, w, h);
}
else ExitProcess(0);
}
else
{
if(t < 49.655)
{
glUseProgram(graffiti_program);
glUniform1f(graffiti_iTime_location, t);
glUniform2f(graffiti_iResolution_location, w, h);
}
else if(t < 82.76)
{
glUseProgram(groundboxes_program);
glUniform1f(groundboxes_iTime_location, t-49.655);
glUniform2f(groundboxes_iResolution_location, w, h);
}
else if(t < 99.31)
{
glUseProgram(voronoidesign_program);
glUniform1f(voronoidesign_iTime_location, t-82.76);
glUniform2f(voronoidesign_iResolution_location, w, h);
}
else if(t < t_end)
{
glUseProgram(bloodcells_program);
glUniform1f(bloodcells_iTime_location, t-99.31);
glUniform2f(bloodcells_iResolution_location, w, h);
}
else ExitProcess(0);
}
quad();
// Render post processing to buffer
glUseProgram(post_program);
glUniform2f(post_resolution_location, w, h);
glUniform1f(post_fsaa_location, fsaa);
glUniform1i(post_channel0_location, 0);
glUniform1f(post_time_location, t);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, first_pass_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
quad();
// Render text to first pass framebuffer
glUseProgram(text_program);
glUniform2f(text_resolution_location, w, h);
glUniform1f(text_font_width_location, font_texture_size);
glUniform1f(text_time_location, t);
glUniform1i(text_channel0_location, 0);
glUniform1i(text_font_location, 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, first_pass_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, font_texture_handle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, font_texture_size, font_texture_size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
quad();
// render ui to screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glUseProgram(ui_program);
glUniform1f(ui_time_location, t);
glUniform1f(ui_maxtime_location, t_end);
glUniform2f(ui_mouse_location, mx, h-my);
glUniform2f(ui_resolution_location, w, h);
glUniform1f(ui_playing_location, paused?0.:1.);
glUniform1i(ui_channel0_location, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, first_pass_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
quad();
glBindTexture(GL_TEXTURE_2D, 0);
}