-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
509 lines (432 loc) · 18.4 KB
/
Copy pathindex.html
File metadata and controls
509 lines (432 loc) · 18.4 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
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
507
508
509
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello WebGL</title>
<meta name="generator" content="BBEdit 10.5" />
<!-- load matrix manipulation helper methods -->
<script type="text/javascript" src="glMatrix_util.js"></script>
<script type="text/javascript" src="webgl-utils.js"></script>
<script type="text/javascript" src="ReadClawAscii.js"></script>
<script type="text/javascript" src="MeshGeometry.js"></script>
<script type="text/javascript" src="Mesh.js"></script>
<!-- define our shaders -->
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
uniform vec3 uColormap[256];
uniform float uColormapSize;
uniform float uColormapLength;
uniform float uColorMin;
uniform float uColorMax;
varying float vZ;
uniform sampler2D uSampler;
void main(void) {
if(vZ<uColorMin){
gl_FragColor = texture2D(uSampler, vec2(0.0, 0.5));
}else if(vZ>uColorMax){
gl_FragColor = texture2D(uSampler, vec2((0.5+uColormapSize-1.0)/uColormapLength, 0.5));
}else{
float float_index = (vZ-uColorMin)*(uColormapSize-1.0)/(uColorMax-uColorMin);
float index = floor(float_index);
float t = float_index-index;
vec4 low_color = texture2D(uSampler, vec2((0.5+index)/uColormapLength, 0.5));
vec4 high_color = texture2D(uSampler, vec2((0.5+index+1.0)/uColormapLength, 0.5));
gl_FragColor = mix(low_color, high_color, t);
}
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
uniform mat4 uMVMatrix;
uniform mat4 uRotMatrix;
uniform mat4 uPMatrix;
varying float vZ;
void main(void) {
vZ = aVertexPosition.z;
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}
</script>
<script>
<!-- define Javascript functions for drawing WebGL items -->
var gl;
var patches = new Array();
var geometry;
var mesh;
function initWebGLContext(aname) {
gl = null;
var canvas = document.getElementById(aname);
try {
// Try to grab the standard context. If it fails, fallback to experimental.
gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
}
catch(e) {}
// If we don't have a GL context, give up now
if (!gl) {
alert("Unable to initialize WebGL. Your browser may not support it.");
gl = null;
}
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
return gl;
}
// define the function to initial WebGL and Setup Geometry Objects
function initGLScene()
{
// Initialize the WebGL Context - the gl engine for drawing things.
var gl = initWebGLContext("hellowebgl"); // The id of the Canvas Element
if (!gl) // if fails simply return
{
return;
}
// succeeded in initializing WebGL system
gl.getExtension('OES_standard_derivatives');
return gl;
}
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var shaderProgram;
var boxShaderProgram;
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
// main shader
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(shaderProgram);
//main shader
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
shaderProgram.rotMatrixUniform = gl.getUniformLocation(shaderProgram, "uRotMatrix");
shaderProgram.colormapUniform = gl.getUniformLocation(shaderProgram, "uColormap");
shaderProgram.colormapSizeUniform = gl.getUniformLocation(shaderProgram, "uColormapSize");
shaderProgram.colormapLengthUniform = gl.getUniformLocation(shaderProgram, "uColormapLength");
shaderProgram.colorMinUniform = gl.getUniformLocation(shaderProgram, "uColorMin");
shaderProgram.colorMaxUniform = gl.getUniformLocation(shaderProgram, "uColorMax");
shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
}
var jet = [
0.000000,0.000000,0.562500,
0.000000,0.000000,0.625000,
0.000000,0.000000,0.687500,
0.000000,0.000000,0.750000,
0.000000,0.000000,0.812500,
0.000000,0.000000,0.875000,
0.000000,0.000000,0.937500,
0.000000,0.000000,1.000000,
0.000000,0.062500,1.000000,
0.000000,0.125000,1.000000,
0.000000,0.187500,1.000000,
0.000000,0.250000,1.000000,
0.000000,0.312500,1.000000,
0.000000,0.375000,1.000000,
0.000000,0.437500,1.000000,
0.000000,0.500000,1.000000,
0.000000,0.562500,1.000000,
0.000000,0.625000,1.000000,
0.000000,0.687500,1.000000,
0.000000,0.750000,1.000000,
0.000000,0.812500,1.000000,
0.000000,0.875000,1.000000,
0.000000,0.937500,1.000000,
0.000000,1.000000,1.000000,
0.062500,1.000000,0.937500,
0.125000,1.000000,0.875000,
0.187500,1.000000,0.812500,
0.250000,1.000000,0.750000,
0.312500,1.000000,0.687500,
0.375000,1.000000,0.625000,
0.437500,1.000000,0.562500,
0.500000,1.000000,0.500000,
0.562500,1.000000,0.437500,
0.625000,1.000000,0.375000,
0.687500,1.000000,0.312500,
0.750000,1.000000,0.250000,
0.812500,1.000000,0.187500,
0.875000,1.000000,0.125000,
0.937500,1.000000,0.062500,
1.000000,1.000000,0.000000,
1.000000,0.937500,0.000000,
1.000000,0.875000,0.000000,
1.000000,0.812500,0.000000,
1.000000,0.750000,0.000000,
1.000000,0.687500,0.000000,
1.000000,0.625000,0.000000,
1.000000,0.562500,0.000000,
1.000000,0.500000,0.000000,
1.000000,0.437500,0.000000,
1.000000,0.375000,0.000000,
1.000000,0.312500,0.000000,
1.000000,0.250000,0.000000,
1.000000,0.187500,0.000000,
1.000000,0.125000,0.000000,
1.000000,0.062500,0.000000,
1.000000,0.000000,0.000000,
0.937500,0.000000,0.000000,
0.875000,0.000000,0.000000,
0.812500,0.000000,0.000000,
0.750000,0.000000,0.000000,
0.687500,0.000000,0.000000,
0.625000,0.000000,0.000000,
0.562500,0.000000,0.000000,
0.500000,0.000000,0.000000];
// create our basic model and view matrix
var mvMatrix = mat4.create();
var rotMatrix = mat4.create();
var mvMatrixStack = [];
// create our projection matrix for projecting from 3D to 2D.
var pMatrix = mat4.create();
//other unifomrs
var dirLight = [0.0, -1.0, 0.0];
var shaderType = 0;
var textureType = 0;
var ni=32;
var nj=16;
var ambientWeight=0.5;
var diffuseWeight=0.5;
var phongWeight=0.5;
var useBumpMap=false;
var nShiny=10;
var colormap = [0.0,1.0,0.0,
0.0,0.0,1.0];
var colorMin=0.0;
var colorMax=1.0;
var colormapTexture;
function mvPushMatrix() {
var copy = mat4.create();
mat4.set(mvMatrix, copy);
mvMatrixStack.push(copy);
}
function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
}
function setMatrixUniforms()
{
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
gl.uniformMatrix4fv(shaderProgram.rotMatrixUniform, false, rotMatrix);
gl.uniform1f(shaderProgram.colormapSizeUniform, colormapTexture.length);
gl.uniform1f(shaderProgram.colormapLengthUniform, colormapTexture.data.length/4);
gl.uniform1f(shaderProgram.colorMaxUniform, colorMax);
gl.uniform1f(shaderProgram.colorMinUniform, colorMin);
}
// create and initialize our geometry objects
var gridVertexPositionBuffer;
var gridVertexTextureCoordBuffer;
var gridVertexIndexBuffer;
var gridVertexNormalBuffer;
var radius = 1.27374; //1000 km
function reloadGeometry(in_patches)
{
patches = in_patches;
mesh = new Mesh(patches);
geometry = new MeshGeometry(gl,mesh);
colorMax = mesh.max_value;
colorMin = mesh.min_value;
}
// Initialize our texture data and prepare it for rendering
function initTextures()
{
colormapTexture = gl.createTexture();
updateColormap(colormap);
}
function updateColormap(colormap){
colormapTexture.data = new Uint8Array(4*Math.pow(2,Math.ceil(Math.log2(colormap.length/3))));
colormapTexture.length = colormap.length/3;
for(var i=0;i<colormap.length/3;i++){
colormapTexture.data[4*i+0] = Math.round(255*colormap[3*i+0]);
colormapTexture.data[4*i+1] = Math.round(255*colormap[3*i+1]);
colormapTexture.data[4*i+2] = Math.round(255*colormap[3*i+2]);
colormapTexture.data[4*i+3] = 255;
}
gl.bindTexture(gl.TEXTURE_2D, colormapTexture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, colormapTexture.data.length/4, 1, 0,
gl.RGBA, gl.UNSIGNED_BYTE, colormapTexture.data);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
}
function handleLoadedTexture(texture)
{
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
}
var xRot = 0;
var yRot = 0;
var zRot = 0;
var scroll = 0;
var enableBox = false;
function initListeners(){
var canvas = document.getElementById("hellowebgl");
var xPrev, yPrev;
var rotating = false;
var panning = false;
canvas.addEventListener("mousedown",function(e){
xPrev = e.pageX;
yPrev = e.pageY;
rotating = e.button==0;
panning = e.button==2;
},false);
canvas.addEventListener("mouseup",function(e){
rotating=false;
panning=false;
},false);
canvas.addEventListener("mouseleave",function(e){
rotating=false;
panning=false;
},false);
canvas.addEventListener("mousemove",function(e){
if(rotating){
var xDelta = e.pageX-xPrev;
var yDelta = e.pageY-yPrev;
xPrev = e.pageX;
yPrev = e.pageY;
xRot += yDelta*360/canvas.height;
yRot += xDelta*360/canvas.width;
//limit rotation
if(xRot>90){
xRot = 90;
}else if(xRot<-90){
xRot = -90;
}
}
if(panning){
}
},false);
canvas.addEventListener("wheel",function(e){
scroll += e.deltaY/1000;
},false)
document.getElementById('file').addEventListener('change',function(evt){
var file = evt.target.files[0];
ReadClawAscii.readFile(file,reloadGeometry);
},false)
}
//Initialize everything for starting up a simple webGL application
function startHelloWebGL()
{
// attach 'Handler' functions to handle events generated by the canvas.
// for when the browser is resized or closed.
// first initialize webgl components
var gl = initGLScene();
// now build basic geometry objects.
initShaders();
reloadGeometry(patches);
initTextures(colormap);
gl.clearColor(0.4,0.4,0.4,1.0);
gl.enable(gl.DEPTH_TEST);
// Draw the Scene
Frames();
// If doing an animation need to add code to rotate our geometry
initListeners();
}
// This function draws a basic webGL scene
// first it clears the framebuffer.
// then we define our View positions for our camera using WebGL matrices.
// OpenGL has convenience methods for this such as glPerspective().
// finally we call the gl draw methods to draw our defined geometry objects.
function drawScene() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if(!geometry.empty) {
mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
mat4.identity(mvMatrix);
mat4.translate(mvMatrix, [0.0, 0.0, -5.0]);
var scale = 1.0*Math.pow(10,-scroll);
mat4.scale(mvMatrix, [scale,scale,scale]);
mat4.identity(rotMatrix);
mat4.rotate(rotMatrix, xRot/180.0*3.1415, [1, 0, 0]);
mat4.rotate(rotMatrix, yRot/180.0*3.1415, [0, 1, 0]);
//rotate to graphics coords
mat4.rotate(rotMatrix, -90/180.0*3.1415, [1, 0, 0]);
mat4.multiply(mvMatrix,rotMatrix,mvMatrix);
//move to center
mat4.translate(mvMatrix, [-(mesh.tree.xmin+mesh.tree.xmax)/2.0, -(mesh.tree.ymin+mesh.tree.ymax)/2.0, 0.0]);
gl.useProgram(shaderProgram);
gl.bindBuffer(gl.ARRAY_BUFFER, geometry.positionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, geometry.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, colormapTexture);
gl.uniform1i(shaderProgram.samplerUniform, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, geometry.vertexIndexBuffer);
setMatrixUniforms();
var ext = gl.getExtension('OES_element_index_uint');
gl.drawElements(gl.TRIANGLES, geometry.vertexIndexBuffer.numItems, gl.UNSIGNED_INT, 0);
}
}
var lastTime = 0;
var sun_pos = 0;
function animate() {
var timeNow = new Date().getTime();
if (lastTime != 0) {
var elapsed = timeNow - lastTime;
//xRot=50;
var rate = 8; //rpm
sun_pos+=360*rate*elapsed/(60*1000);
sun_pos=sun_pos%360;
var sun_rads = sun_pos/180.0*3.1415;
dirLight = [-Math.sin(sun_rads),0,-Math.cos(sun_rads)];
}
lastTime = timeNow;
}
function Frames() {
requestAnimFrame(Frames);
drawScene();
animate();
}
</script>
</head>
<!-- declare the "body" of the HTML document-->
<!-- the onload attribute specifies a javascript function to execute when the body tag is loaded into the browser -->
<body onload="startHelloWebGL()">
<h1>Surface Plot!</h1>
<!-- embed the HTML5 Canvas Drawing object on the page.-->
<!-- Name the canvas 'hellowebgl' for the javascript code. -->
<canvas id="hellowebgl" width="640" height="480">
If you're seeing this your web browser doesn't support the <canvas>>
element. Ouch!
</canvas>
<br />
<input type="file" id="file" />
</body>
</html>