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
|
/*
* Copyright (C) 2018-2020 - Vito Caputo - <vcaputo@pengaru.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <SDL.h>
#include <play.h>
#include <stage.h>
#include "glad.h"
#include "plasma-node.h"
#include "shader-node.h"
#include "macros.h"
#include "m4f.h"
typedef struct plasma_node_t {
unsigned *maga;
float *gloom;
} plasma_node_t;
static const char *plasma_vs = ""
#ifdef __EMSCRIPTEN__
"#version 100\n"
"varying vec2 UV;"
#else
"#version 120\n"
#endif
"uniform mat4 projection_x;"
"attribute vec3 vertex;"
"attribute vec2 texcoord;"
"void main()"
"{"
#ifdef __EMSCRIPTEN__
" UV = texcoord;"
#else
" gl_TexCoord[0].xy = texcoord;"
#endif
" gl_Position = projection_x * vec4(vertex, 1.0);"
"}"
"";
// derived from https://www.bidouille.org/prog/plasma
static const char *plasma_fs = ""
#ifdef __EMSCRIPTEN__
"#version 100\n"
#else
"#version 120\n"
#endif
"#define PI 3.1415926535897932384626433832795\n"
#ifdef __EMSCRIPTEN__
"precision mediump float;"
"varying vec2 UV;"
#endif
"uniform float alpha;"
"uniform float time;"
"uniform float gloom;"
" float dostar(vec2 uv, float size) {"
" uv.x = abs(uv.x);"
" float a = 6.2832/5.;"
" float d1 = dot(uv, vec2(sin(a), cos(a)));"
" a = 3. * 6.2832/5.;"
" float d2 = dot(uv, vec2(sin(a), cos(a)));"
" a = 2. * 6.2832/5.;"
" float d4 = dot(uv, vec2(sin(a), cos(a)));"
" float d = min(max(d1, d2), max(uv.y, d4));"
#ifdef __EMSCRIPTEN__
/* GLSL 1.0 lacks fwidth() */
" float w = .001;"
#else
" float w = fwidth(d);"
#endif
" return smoothstep(w, -w, d - size);"
" }"
"void main() {"
" float v;"
" float stime = sin(time * .01) * 100.0;"
" vec3 col;"
#ifdef __EMSCRIPTEN__
" vec2 c = UV;"
#else
" vec2 c = gl_TexCoord[0].st;"
#endif
" vec2 cc = c;"
// this zooms the texture coords in and out a bit with time
" c *= (sin(stime * .01) *.5 + .5) * 3.0 + 1.0;"
// plasma calculations, stime instead of time directly to vary directions and speed
" v = sin((c.x + stime));"
" v += sin((c.y + stime) * .5);"
" v += sin((c.x + c.y +stime) * .5);"
" c += vec2(sin(stime * .33), cos(stime * .5)) * 3.0;"
" v += sin(sqrt(c.x * c.x + c.y * c.y + 1.0) + stime);"
" col = vec3(cos(PI * v + sin(time)), sin(PI * v + cos(time * .33)), cos(PI * v + sin(time * .66)));"
" gl_FragColor = vec4((col * .5 + .5) * (1. - gloom), alpha);"
"}"
"";
static const char *plasma_maga_fs = ""
#ifdef __EMSCRIPTEN__
"#version 100\n"
#else
"#version 120\n"
#endif
"#define PI 3.1415926535897932384626433832795\n"
#ifdef __EMSCRIPTEN__
"precision mediump float;"
"varying vec2 UV;"
#endif
"uniform float alpha;"
"uniform float time;"
"uniform float gloom;"
" float dostar(vec2 uv, float size) {"
" uv.x = abs(uv.x);"
" float a = 6.2832/5.;"
" float d1 = dot(uv, vec2(sin(a), cos(a)));"
" a = 3. * 6.2832/5.;"
" float d2 = dot(uv, vec2(sin(a), cos(a)));"
" a = 2. * 6.2832/5.;"
" float d4 = dot(uv, vec2(sin(a), cos(a)));"
" float d = min(max(d1, d2), max(uv.y, d4));"
#ifdef __EMSCRIPTEN__
/* GLSL 1.0 lacks fwidth() */
" float w = .001;"
#else
" float w = fwidth(d);"
#endif
" return smoothstep(w, -w, d - size);"
" }"
"void main() {"
" float v;"
" float stime = sin(time * .01) * 100.0;"
" float star, r, g, b;"
#ifdef __EMSCRIPTEN__
" vec2 c = UV;"
#else
" vec2 c = gl_TexCoord[0].st;"
#endif
" vec2 cc = c;"
// this zooms the texture coords in and out a bit with time
" c *= (sin(stime * .01) *.5 + .5) * 3.0 + 1.0;"
// plasma calculations, stime instead of time directly to vary directions and speed
" v = sin((c.x + stime));"
" v += sin((c.y + stime) * .5);"
" v += sin((c.x + c.y +stime) * .5);"
" c += vec2(sin(stime * .33), cos(stime * .5)) * 3.0;"
" v += sin(sqrt(c.x * c.x + c.y * c.y + 1.0) + stime);"
" star = dostar(mod(cc, .2) - .1, (sin(time + c.x + c.y) * .5 + .5) * .01 + .01);"
" b = smoothstep(.25, .8, cos(2.666 * PI + PI * v + sin(time)));"
" star *= b;"
" b = max(star, b);"
" r = max(smoothstep(.25, .8, cos(PI * v + sin(time))), star);"
" g = max(smoothstep(.25, .8, cos(1.333 * PI + PI * v + sin(time))), star);"
" gl_FragColor = vec4(max(r, g), g, max(b, g), 1.);"
"}"
"";
static void plasma_uniforms(void *uniforms_ctxt, void *render_ctxt, unsigned n_uniforms, const int *uniforms, const m4f_t *model_x, float alpha)
{
plasma_node_t *plasma = uniforms_ctxt;
play_t *play = render_ctxt;
glUniform1f(uniforms[0], alpha);
glUniform1f(uniforms[1], play_ticks(play, PLAY_TICKS_TIMER0) * .001f); // FIXME KLUDGE ALERT
glUniformMatrix4fv(uniforms[2], 1, GL_FALSE, &model_x->m[0][0]);
glUniform1f(uniforms[3], *(plasma->gloom));
}
/* create plasma rendering stage */
stage_t * plasma_node_new(const stage_conf_t *conf, m4f_t *projection_x, float *gloom, unsigned *maga)
{
plasma_node_t *ctxt;
/* FIXME TODO:
* shader-node doesn't cleanup uniforms_ctxt, so this is being leaked currently.
* but that doesn't really matter since plasma-node is only created once per game.
* The shortest path to cleaning this up would be to publicize the shader-node struct
* and implementation functions, and stitch it all together here, maybe with a nested
* shader_node_t in plasma_node_t. That's rather annoying, and perhaps highlights a
* shortcoming in the existing stage_t composability. It'd be nice if uniforms_ctxt
* could be typed with something embedding an optional free func, which shader_node_free()
* would then call to cleanup when set. It's unclear if that should just be a bespoke
* shader-node type, or something more generally defined by libstage. Either way, it's
* not terribly important for SARS in this singleton plasma-node case.
*/
ctxt = calloc(1, sizeof(plasma_node_t));
fatal_if(!ctxt, "unable to allocate plasma_node_t");
ctxt->gloom = gloom;
return shader_node_new_srcv(conf, 2,
(shader_src_conf_t[]){
{
.vs_src = plasma_vs,
.fs_src = plasma_fs,
.transform = projection_x,
.uniforms_func = plasma_uniforms,
.uniforms_ctxt = ctxt,
.n_uniforms = 4,
.uniforms = (const char *[]){
"alpha",
"time",
"projection_x",
"gloom",
},
}, {
.vs_src = plasma_vs,
.fs_src = plasma_maga_fs,
.transform = projection_x,
.uniforms_func = plasma_uniforms,
.uniforms_ctxt = ctxt,
.n_uniforms = 4,
.uniforms = (const char *[]){
"alpha",
"time",
"projection_x",
"gloom",
},
},
},
maga
);
}
|