-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart2.html
More file actions
446 lines (429 loc) · 13.5 KB
/
part2.html
File metadata and controls
446 lines (429 loc) · 13.5 KB
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bulma@0.8.0/css/bulma.min.css"
/>
<link rel="stylesheet" href="main.css" />
<title>Waves</title>
</head>
<body>
<div class="container">
<h1>Euler Integration</h1>
<div class="header">
<div>
<h5>
Write a program given the variables listed that simulates the system
depicted.
</h5>
Number of balls :  <input
type="number"
id="number"
value="1"
min="1"
max="1000"
class="input is-small"
/>
Mass :  <input
type="number"
id="m"
value="10"
min="1"
max="100"
class="input is-small"
/><br />
Spring Constant (k) :  <input
type="number"
id="k"
value="1"
min="0"
max="100"
class="input is-small"
/>
Time step (dt) :  <input
type="number"
id="dt"
value="0.1"
min="0"
max="1"
step=".01"
class="input is-small"
/>
<br /><br />
</div>
<div id="code" class="columns is-vcentered">
<div class="column">
<b>Forever {</b><br />
      <b id="variables"
>y, v, y_left, y_right, m, k, dt</b
></br>
      <textarea
id="text"
rows="10"
cols="40"
>
// Write your code here.
// End each line with a ';'
v = 0.0;
y = 1.0;
</textarea
><br />
<b>}</b>
<br />
<button id="run" class="button">Run!</button>
</div>
<div class="column">
<canvas id="canvas"></canvas>
</div>
</div>
<button id="cheat" class="button is-danger">
Cheat. Do not click.
</button>
<a href="part3.html" class="button is-link">Next Exercise</a>
</div>
</div>
<div id="shaders">
<script type="x-shader" id="vs">
attribute vec2 av;
void main () {gl_Position = vec4(av*2.-1., 0., 1.);}
</script>
<script type="x-shader" id="fs">
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#define Q gl_FragColor
#define U gl_FragCoord.xy
uniform vec2 R;
uniform vec2 sR;
uniform sampler2D T;
uniform float I;
uniform float N;
float spring (vec2 u, vec2 a, vec2 b) {
vec2 v = b-a;
float r = clamp(dot(u-a,v)/dot(v,v),0.,1.);
vec2 p = a+(b-a)*r;
u += vec2(-v.y,v.x)*cos(16./sqrt(N)*6.2*r)*.02;
return length(u-p);
}
float get (float x) {
return texture2D(T,vec2(x,0)/sR).y/sqrt(N+2.);
}
float ballnSpring (vec2 u) {
float x = floor((u.x*0.5+0.5)*(N+1.0)+0.5);
vec2
a = vec2((x-1.)/(N+1.)*2.-1., get(x-1.)),
b = vec2((x+0.)/(N+1.)*2.-1., get(x+0.)),
c = vec2((x+1.)/(N+1.)*2.-1., get(x+1.));
return min(
length(u-b)-.1/(N),
min(spring(u,a,b)-.01/N, spring(u,b,c)-.01/N)
);
}
void main () {
vec2 u = 2.*(U-0.5*R)/R.x;
float b = ballnSpring (u);
b = smoothstep(.01,0.,b);
Q = vec4(vec3(b),1);
}
</script>
<script type="x-shader" id="sim">
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#define Q gl_FragColor
#define U gl_FragCoord.xy
uniform vec2 R;
uniform sampler2D T;
uniform float I;
uniform float N;
uniform float dt;
uniform float m;
uniform float k;
vec4 A (vec2 u) {
return texture2D(T,u/R);
}
void main () {
Q = A(U);
float y, v, y_left, y_right;
y = Q.y;
v = Q.x;
y_left = A(U-vec2(1,0)).y;
y_right = A(U+vec2(1,0)).y;
//INSERT
Q = vec4(v,y,0,1);
if (I < 1.) Q = vec4(0,1,0,0);
if (U.x<1.||U.x>N+1.) Q *= 0.;
}
</script>
</div>
<script id="gl-utilities">
class Framebuffer {
constructor(gl, n, type, w, h = w, filter = "LINEAR") {
this.gl = gl;
this.type = type;
this.n = n;
this.w = w;
this.h = h;
this.width = w;
this.height = h;
this.fb0 = gl.createFramebuffer();
this.renderbuffer = gl.createRenderbuffer();
this.texture = gl.createTexture();
gl.activeTexture(gl["TEXTURE" + this.n]);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.fb0);
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl[filter]);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl[filter]);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
w,
h,
0,
gl.RGBA,
this.type,
null
);
gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderbuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, w, h);
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
this.texture,
0
);
gl.framebufferRenderbuffer(
gl.FRAMEBUFFER,
gl.DEPTH_ATTACHMENT,
gl.RENDERBUFFER,
this.renderbuffer
);
}
read() {
this.route();
var pixels = new Float32Array(this.w * this.h * 4);
this.gl.readPixels(
0,
0,
this.w,
this.h,
this.gl.RGBA,
this.type,
pixels
);
return pixels;
}
write(typedArray) {
this.gl.activeTexture(this.gl["TEXTURE" + this.n]);
this.gl.texImage2D(
this.gl.TEXTURE_2D,
0,
this.gl.RGBA,
this.w,
this.h,
0,
this.gl.RGBA,
this.type,
typedArray
);
}
source(element) {
this.gl.activeTexture(this.gl["TEXTURE" + this.n]);
this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, true);
this.gl.texImage2D(
this.gl.TEXTURE_2D,
0,
this.gl.RGBA,
this.gl.RGBA,
this.gl.UNSIGNED_BYTE,
element
);
this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, false);
}
route() {
this.gl.activeTexture(this.gl["TEXTURE" + this.n]);
this.gl.bindTexture(this.gl.TEXTURE_2D, this.texture);
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.fb0);
this.gl.viewport(0, 0, this.w, this.h);
}
}
const createProgram = (gl, vstr, fstr) => {
vstr =
vstr.length < 20 && document.getElementById(vstr)
? document.getElementById(vstr).textContent
: vstr;
fstr =
fstr.length < 20 && document.getElementById(fstr)
? document.getElementById(fstr).textContent
: fstr;
let program = gl.createProgram();
let vshader = createShader(gl, vstr, gl.VERTEX_SHADER);
let fshader = createShader(gl, fstr, gl.FRAGMENT_SHADER);
gl.attachShader(program, vshader);
gl.attachShader(program, fshader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw gl.getProgramInfoLog(program);
}
return program;
};
const createShader = (gl, str, type) => {
let shader = gl.createShader(type);
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
var errorString = gl.getShaderInfoLog(shader);
alert(errorString + str.split("\n")[errorString.split(":")[2] - 1]);
throw gl.getShaderInfoLog(shader);
}
return shader;
};
const initAttrib = (gl, program) => {
gl.useProgram(program);
let attrib = gl.getAttribLocation(program, "av");
gl.enableVertexAttribArray(attrib);
gl.vertexAttribPointer(attrib, 2, gl.FLOAT, gl.FALSE, 0, 0);
return program;
};
const setUni = (gl, program, name, args, int = false) => {
gl.useProgram(program);
if (!program[name])
program[name] = gl.getUniformLocation(program, name);
if (int || typeof args == "boolean") gl.uniform1i(program[name], args);
else if (args.constructor == Array)
gl["uniform" + args.length + "fv"](program[name], args);
else if (typeof args == "number") gl.uniform1f(program[name], args);
else if (args.constructor == Framebuffer)
gl.uniform1i(program[name], args.n);
return setUni;
};
const draw = (
gl,
program,
dest,
clear = false,
type = gl.TRIANGLES,
a = 0,
b = 6
) => {
gl.useProgram(program);
if (dest.route == undefined) {
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, dest.width, dest.height);
} else dest.route();
if (clear) gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.drawArrays(type, a, b);
};
const initVerts = (gl, w = 0, h = w) => {
let arr = new Float32Array(w * h * 2 + 12),
sqr = [0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1],
i = 0;
for (j = 0; j < 12; j++) arr[i++] = sqr[j];
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
arr[i++] = x / w;
arr[i++] = y / h;
}
}
let buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW);
return arr;
};
</script>
<script type="text/javascript">
var canvas = document.getElementById("canvas"),
gl = canvas.getContext("webgl"),
ext = gl.getExtension("OES_texture_float"),
lin = gl.getExtension("OES_texture_float_linear"),
fbo_a = new Framebuffer(gl, 0, gl.FLOAT, 1002, 1, "NEAREST"),
fbo_b = new Framebuffer(gl, 1, gl.FLOAT, 1002, 1, "NEAREST"),
sim_a = createProgram(gl, "vs", "sim"),
sim_b = createProgram(gl, "vs", "sim"),
program = createProgram(gl, "vs", "fs"),
verts = initVerts(gl);
initAttrib(gl, program);
var I = 0,
N = Number(number.value);
(text = document.getElementById("text")),
(run = document.getElementById("run")),
(cheat = document.getElementById("cheat")),
(variables = document.getElementById("variables")),
(number = document.getElementById("number")),
(m = document.getElementById("m")),
(k = document.getElementById("k")),
(dt = document.getElementById("dt"));
function setI(I) {
setUni(gl, program, "I", I);
setUni(gl, sim_a, "I", I);
setUni(gl, sim_b, "I", I);
}
function setR() {
setUni(gl, program, "R", [canvas.width, canvas.height]);
setUni(gl, program, "sR", [fbo_a.w, fbo_a.h]);
setUni(gl, sim_a, "R", [fbo_b.w, fbo_b.h]);
setUni(gl, sim_b, "R", [fbo_a.w, fbo_a.h]);
}
function setTx() {
setUni(gl, program, "T", fbo_a);
setUni(gl, sim_a, "T", fbo_b);
setUni(gl, sim_b, "T", fbo_a);
}
function setN(N) {
setUni(gl, program, "N", N);
setUni(gl, sim_a, "N", N);
setUni(gl, sim_b, "N", N);
}
function setUnis() {
setR();
setI((I = 0));
setTx();
setN(Number(number.value));
setUni(gl, sim_a, "dt", Number(dt.value));
setUni(gl, sim_a, "k", Number(k.value));
setUni(gl, sim_a, "m", Number(m.value));
setUni(gl, sim_b, "dt", Number(dt.value));
setUni(gl, sim_b, "k", Number(k.value));
setUni(gl, sim_b, "m", Number(m.value));
}
number.onchange = dt.onchange = k.onchange = m.onchange = function() {
N = Number(number.value);
setUnis();
};
function setProgram() {
var t = text.value;
var c = document.getElementById("sim").textContent;
var c = c.replace("//INSERT", t);
sim_a = createProgram(gl, "vs", c);
sim_b = createProgram(gl, "vs", c);
setUnis();
}
run.onclick = setProgram;
cheat.onclick = function() {
text.value = "v += dt*k/m*( (y_left-y) + (y_right-y));\ny += dt*v;\n";
setProgram();
};
setUnis();
function animate() {
requestAnimationFrame(animate);
setI(I++);
for (var i = 0; i < Math.min(Math.sqrt(N), 100); i++) {
draw(gl, sim_a, fbo_a);
draw(gl, sim_b, fbo_b);
}
draw(gl, program, canvas);
}
animate();
</script>
</body>
</html>