-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.html
executable file
·506 lines (432 loc) · 14.3 KB
/
test.html
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Spinning WebGL Box</title>
<style>
body, html {
margin: 0px;
width: 100%;
height: 100%;
overflow: hidden;
}
#framerate {
position: absolute;
top: 10px;
left: 10px;
background-color: rgba(0,0,0,0.3);
padding: 1em;
color: white;
}
.controls {
position: absolute;
bottom: 10px;
left: 10px;
background-color: rgba(0,0,0,0.3);
padding: 1em;
color: white;
}
#example {
width: 100%;
height: 100%;
}
</style>
<script type="text/javascript" src="js/webgl-utils.js"></script>
<script type="text/javascript" src="js/webgl-debug.js"></script>
<script src="js/J3DI.js"> </script>
<script src="js/J3DIMath.js" type="text/javascript"> </script>
<script id="vshader" type="x-shader/x-vertex">
uniform mat4 u_modelViewMatrix;
uniform mat4 u_modelViewMatrixInv;
uniform mat4 u_projMatrix;
uniform mat4 u_normalMatrix;
uniform vec3 lightDir;
attribute vec4 vPosition;
varying vec3 clipping_space_eye;
varying vec3 clipping_space_dir;
void main()
{
gl_Position = u_projMatrix * u_modelViewMatrix * vPosition;
// vec4 transNormal = u_normalMatrix * vec4(vNormal, 1);
// eye_space_eye = ( gl_ModelViewMatrix * gl_Vertex ).xyz;
// eye_space_dir = ( eye_space_eye );
clipping_space_eye = vPosition.xyz;
clipping_space_dir = ( clipping_space_eye - ( u_modelViewMatrixInv * vec4( 0.0, 0.0, 0.0, 1.0 ) ).xyz );
// surface_space_eye = ( surface_transform_inverse * vPosition ).xyz;
// surface_space_dir = ( surface_space_eye - ( surface_transform_inverse * gl_ModelViewMatrixInverse * vec4( 0.0, 0.0, 0.0, 1.0 ) ).xyz );
}
</script>
<script id="fshader" type="x-shader/x-fragment">
#ifdef GL_ES
//precision mediump float;
precision highp float;
#endif
#define DEGREE 3
#define SIZE (DEGREE + 1)
#define METHOD_SPHERE_TRACING
//#define METHOD_BUDAN_FOURIER
varying vec3 clipping_space_eye;
varying vec3 clipping_space_dir;
struct polynomial
{
float a[ SIZE ];
};
float F( vec3 p ) { float x=p.x, y=p.y, z=p.z; return (x*x + y*y +z*z) + 16.0*x*y*z-1.0/(16.0*4.0*16.0); }
float F1( vec3 p, vec3 d ) { float x=p.x, y=p.y, z=p.z, u=d.x, v=d.y, w=d.z; return 16.0*y*z*u + 16.0*x*z*v + 16.0*x*y*w + 2.0*x*u + 2.0*y*v + 2.0*z*w; }
float F2( vec3 p, vec3 d ) { float x=p.x, y=p.y, z=p.z, u=d.x, v=d.y, w=d.z; return 32.0*z*u*v + 32.0*y*u*w + 32.0*x*v*w + 2.0*u*u + 2.0*v*v + 2.0*w*w; }
float F3( vec3 p, vec3 d ) { float x=p.x, y=p.y, z=p.z, u=d.x, v=d.y, w=d.z; return 96.0*u*v*w; }
float eval_f0( in vec3 p ) { return F(p); }
float eval_f1( in vec3 p ) { return F1( p, clipping_space_dir ); }
float eval_f2( in vec3 p ) { return F2( p, clipping_space_dir ); }
float eval_f3( in vec3 p ) { return F3( p, clipping_space_dir ); }
vec3 gradient( vec3 p ) { return vec3( 2.0*p.x+16.0*p.y*p.z, 2.0*p.y+16.0*p.x*p.z, 2.0*p.z+16.0*p.x*p.y ); }
vec2 clip( in vec3 eye, in vec3 dir )
{
//Compute A, B and C coefficients
float r = 1.0;
float a = dot(dir, dir);
float b = 2.0 * dot(dir, eye);
float c = dot(eye, eye) - (r * r);
//Find discriminant
float disc = b * b - 4.0 * a * c;
// if discriminant is negative there are no real roots, so return
// false as ray misses sphere
if (disc < 0.0)
return vec2( 0.0, 0.0 );
// compute q as described above
float distSqrt = sqrt(disc);
float q;
if (b < 0.0)
q = (-b - distSqrt)/2.0;
else
q = (-b + distSqrt)/2.0;
// compute t0 and t1
float t0 = q / a;
float t1 = c / q;
// make sure t0 is smaller than t1
if (t0 > t1)
{
// if t0 is bigger than t1 swap them around
float temp = t0;
t0 = t1;
t1 = temp;
}
// if t1 is less than zero, the object is in the ray's negative direction
// and consequently the ray misses the sphere
if (t1 < 0.0)
return vec2( 0.0, 0.0 );
else
return vec2( t0, t1 );
}
vec3 pOnRay( in float t ) { return clipping_space_eye + t * clipping_space_dir; }
#ifdef METHOD_SPHERE_TRACING
// interval operations
vec2 mult_01( vec2 i ) { return vec2( min( 0.0, i[ 0 ] ), max( 0.0, i[ 1 ] ) ); }
vec2 make_n11( float f ) { return vec2( min( -f, f ), max( -f, f ) ); }
vec2 taylor_bound_df_deg3( vec2 i_x )
{
float x_0 = ( i_x[ 0 ] + i_x[ 1 ] ) * 0.5;
float x_1 = ( i_x[ 1 ] - i_x[ 0 ] ) * 0.5;
vec3 p_0 = pOnRay( x_0 );
// base case
vec2 bound = vec2( eval_f3( p_0 ) );
bound = vec2( eval_f1( p_0 ) ) + make_n11( x_1 * eval_f2( p_0 ) ) + ( 0.5 * x_1 * x_1 ) * mult_01( bound );
return bound;
}
float intersect( vec3 o, vec3 d, vec2 clip )
{
float x0 = clip.x;
float x1 = clip.y;
vec2 df_bound = abs( taylor_bound_df_deg3( vec2( x0, x1 ) ) );
float lipschitz = max( df_bound[ 0 ], df_bound[ 1 ] );
float x = x0;
float last_x = x - 1.0;
float f = abs( eval_f0( pOnRay( x ) ) );
for( int i = 0; i < 1000; i++ )
{
if( !(x - last_x > 0.000001 && x < x1) )
break;
last_x = x;
x = x + f / lipschitz;
f = abs( eval_f0( pOnRay( x ) ) );
df_bound = abs( taylor_bound_df_deg3( vec2( x0, x1 ) ) );
lipschitz = max( df_bound[ 0 ], df_bound[ 1 ] );
}
return x;
}
#endif
#ifdef METHOD_BUDAN_FOURIER
int budan_fourier_sc( float t )
{
vec3 p = pOnRay( t );
float f0 = eval_f0( p );
float f1 = eval_f1( p );
float f2 = eval_f2( p );
float f3 = eval_f3( p );
return (f0*f1<0.0?1:0)+(f1*f2<0.0?1:0)+(f2*f3<0.0?1:0);
}
int budan_fourier( float l, float r )
{
return budan_fourier_sc( l ) - budan_fourier_sc( r );
}
float bisect( float lowerBound, float upperBound )
{
float center = lowerBound;
float old_center = upperBound;
float fl = eval_f0( pOnRay( lowerBound ) );
float fu = eval_f0( pOnRay( upperBound ) );
for( int k = 0; k < 100; k++ )
{
if( abs( upperBound - lowerBound ) < 0.0001 )
break;
old_center = center;
center = 0.5 * ( lowerBound + upperBound );
float fc = eval_f0( pOnRay( center ) );
if( fc * fl < 0.0 )
{
upperBound = center;
fu = fc;
}
else if( fc == 0.0 )
{
break;
}
else
{
lowerBound = center;
fl = fc;
}
}
return ( upperBound + lowerBound ) * 0.5;
}
int mod( int a, int b ) { int quot = a / b; return a - b*quot; }
float intersect( vec3 o, vec3 d, vec2 clip )
{
float size = 0.5;
int id = 0;
bool bisect_at_end = false;
bool go_right = false;
float scale;
for( int i = 0; i < 100; i++ )
{
if( size >= 1.0 )
break;
if( go_right )
{
id /= 2;
size *= 2.0;
if( mod( id, 2 ) == 0 )
{
id++;
go_right = false;
}
continue;
}
scale = (clip[1]-clip[0]) * size;
int v = budan_fourier( clip[0]+ scale * float( id ), clip[0]+scale * float( id + 1 ) );
if( v > 1 )
{
// go deeper on left side
id *= 2;
size /= 2.0;
}
else if( v == 0 )
{
// go right
go_right = true;
}
else if( v == 1 )
{
bisect_at_end = true;
break;
}
else
{
return clip[0]+( scale * float( id ) );
}
}
//scale = (clip[1]-clip[0]); id=0; bisect_at_end=true;
if( bisect_at_end )
return bisect( clip[0]+ scale * float( id ), clip[0]+scale * float( id + 1 ) );
else
return clip[1]+1.0;
}
#endif
void main()
{
vec2 clipping_interval = clip( clipping_space_eye, clipping_space_dir );
if( clipping_interval.x >= clipping_interval.y )
discard;
float t = intersect( clipping_space_eye, clipping_space_dir, clipping_interval );
if( t > clipping_interval.y )
discard;
vec3 color;
if( t - clipping_interval.x < 0.005 || clipping_interval.y - t < 0.005 )
{
color = vec3( 0.75, 0.25, 0.25 );
}
else
{
vec3 inter = clipping_space_eye + t*clipping_space_dir;
vec3 nor = normalize(gradient( inter ));
float dif = .5 + .5*dot( nor, vec3(0.57703,0.57703,0.57703) );
float ao;
ao = max( 1.0-ao*0.005, 0.0 );
color = vec3(1.0,.9,.5)*dif*ao + .5*vec3(.6,.7,.8)*ao;
}
gl_FragColor = vec4(color,1.0);
}
</script>
<script>
var g = {};
function init()
{
// Initialize
var gl = initWebGL(
// The id of the Canvas Element
"example");
if (!gl) {
return;
}
var program = simpleSetup(
gl,
// The ids of the vertex and fragment shaders
"vshader", "fshader",
// The vertex attribute names used by the shaders.
// The order they appear here corresponds to their index
// used later.
[ "vNormal", "vColor", "vPosition"],
// The clear color and depth values
[ 0, 0, 0.5, 1 ], 10000);
// Create a box. On return 'gl' contains a 'box' property with
// the BufferObjects containing the arrays for vertices,
// normals, texture coords, and indices.
g.box = makeBox(gl);
// Create some matrices to use later and save their locations in the shaders
g.mvMatrix = new J3DIMatrix4();
g.mvMatrixInv = new J3DIMatrix4();
g.u_normalMatrixLoc = gl.getUniformLocation(program, "u_normalMatrix");
g.normalMatrix = new J3DIMatrix4();
g.u_modelViewMatrixLoc =
gl.getUniformLocation(program, "u_modelViewMatrix");
g.u_modelViewMatrixInvLoc =
gl.getUniformLocation(program, "u_modelViewMatrixInv");
g.u_projMatrixLoc =
gl.getUniformLocation(program, "u_projMatrix");
// Enable all of the vertex attribute arrays.
gl.enableVertexAttribArray(0);
gl.enableVertexAttribArray(1);
gl.enableVertexAttribArray(2);
// Set up all the vertex attributes for vertices, normals and texCoords
gl.bindBuffer(gl.ARRAY_BUFFER, g.box.vertexObject);
gl.vertexAttribPointer(2, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, g.box.normalObject);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, g.box.texCoordObject);
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
// Bind the index array
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, g.box.indexObject);
return gl;
}
var requestId;
var play;
function reshape(gl)
{
// change the size of the canvas's backing store to match the size it is displayed.
var canvas = document.getElementById('example');
if (canvas.clientWidth == canvas.width && canvas.clientHeight == canvas.height)
return;
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
// Set the viewport and projection matrix for the scene
gl.viewport(0, 0, canvas.clientWidth, canvas.clientHeight);
g.perspectiveMatrix = new J3DIMatrix4();
g.perspectiveMatrix.perspective(30, canvas.clientWidth / canvas.clientHeight, 1, 10000);
}
function drawPicture(gl)
{
// Make sure the canvas is sized correctly.
reshape(gl);
// Clear the canvas
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Make a model/view matrix.
g.mvMatrix.makeIdentity();
g.mvMatrix.lookat(0, 0, 7, 0, 0, 0, 0, 1, 0);
g.mvMatrix.rotate(20, 1,0,0);
g.mvMatrix.rotate(currentAngle, 0,1,0);
g.mvMatrix.setUniform(gl, g.u_modelViewMatrixLoc, false);
g.mvMatrixInv.load(g.mvMatrix);
g.mvMatrixInv.invert();
g.mvMatrixInv.setUniform(gl, g.u_modelViewMatrixInvLoc, false);
// Construct the normal matrix from the model-view matrix and pass it in
g.normalMatrix.load(g.mvMatrix);
g.normalMatrix.invert();
g.normalMatrix.transpose();
g.normalMatrix.setUniform(gl, g.u_normalMatrixLoc, false);
// Construct the model-view * projection matrix and pass it in
g.perspectiveMatrix.setUniform(gl, g.u_projMatrixLoc, false);
// Draw the cube
gl.drawElements(gl.TRIANGLES, g.box.numIndices, gl.UNSIGNED_BYTE, 0);
// Show the framerate
framerate.snapshot();
currentAngle += incAngle;
if (currentAngle > 360)
currentAngle -= 360;
}
function start()
{
var c = document.getElementById("example");
//c = WebGLDebugUtils.makeLostContextSimulatingCanvas(c);
// tell the simulator when to lose context.
//c.loseContextInNCalls(1);
c.addEventListener('webglcontextlost', handleContextLost, false);
c.addEventListener('webglcontextrestored', handleContextRestored, false);
var gl = init();
if (!gl) {
return;
}
currentAngle = 0;
incAngle = 0.5;
framerate = new Framerate("framerate");
var f = function() {
drawPicture(gl);
requestId = window.requestAnimFrame(f, c);
};
f();
function handleContextLost(e) {
e.preventDefault();
clearLoadingImages();
if (requestId !== undefined) {
window.cancelAnimFrame(requestId);
requestId = undefined;
}
}
function handleContextRestored() {
init();
f();
}
play = f;
}
function pause()
{
if (requestId !== undefined) {
window.cancelAnimFrame(requestId);
requestId = undefined;
}
}
</script>
<style type="text/css">
canvas {
border: 2px solid black;
}
</style>
</head>
<body onload="start()">
<canvas id="example">
If you're seeing this your web browser doesn't support the <canvas>> element. Ouch!
</canvas>
<div id="framerate"></div>
<div id="pause" class="controls" onCLick="pause(); document.getElementById('pause').style.display='none'; document.getElementById('play').style.display='block';">Pause</div>
<div id="play" class="controls" style="display:none;" onCLick="play(); document.getElementById('play').style.display='none'; document.getElementById('pause').style.display='block';">Play</div>
</body>
</html>