source: issm/oecreview/Archive/20545-21336/ISSM-21093-21094.diff@ 21337

Last change on this file since 21337 was 21337, checked in by Mathieu Morlighem, 8 years ago

CHG: added Archive/20545-21336

File size: 28.6 KB
RevLine 
[21337]1Index: ../trunk-jpl/src/m/plot/plot_quiver.js
2===================================================================
3--- ../trunk-jpl/src/m/plot/plot_quiver.js (revision 21093)
4+++ ../trunk-jpl/src/m/plot/plot_quiver.js (revision 21094)
5@@ -10,7 +10,6 @@
6 var vertices = [];
7 var indices = [];
8 var colors = [];
9- var rgbcolor = [];
10 var xmin,xmax;
11 var ymin,ymax;
12 var zmin,zmax;
13@@ -65,7 +64,7 @@
14 canvas.nodes[canvas.nodes.length] = node;
15 scale = 1 / (xmax - xmin);
16 node["shaderName"] = "colored";
17- node["shader"] = gl["shaders"][node["shaderName"]]["program"];
18+ node["shader"] = gl["shaders"][node["shaderName"]];
19 node["lineWidth"] = options.getfieldvalue('linewidth',1);
20 node["scale"] = [scale, scale, scale * matrixscale];
21 node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
22@@ -76,9 +75,6 @@
23 node["maskEnabled"] = options.getfieldvalue('innermask','off') == 'on';
24 node["maskHeight"] = options.getfieldvalue('innermaskheight',150.0)*options.getfieldvalue('heightscale',1);
25 node["maskColor"] = options.getfieldvalue('innermaskcolor',[0.0,0.0,1.0,1.0]);
26-
27- //some defaults:
28- colors.itemSize = 4;
29
30 //retrieve some options
31 var edgecolor=new RGBColor(options.getfieldvalue('edgecolor','black'));
32@@ -91,7 +87,6 @@
33 else if (elements[0].length==4){ //tetras
34 }
35 else{ //2D triangular elements
36- vertices.itemSize = 3;
37 var xyz = vec3.create();
38 var xyz = vec3.create();
39 var direction = vec3.create();
40@@ -136,8 +131,5 @@
41 }
42 }
43 //}}}
44-
45- /*Initalize buffers: */
46- node["arrays"] = [vertices, colors];
47- node["buffers"] = initBuffers(gl, node["arrays"]);
48+ node["mesh"] = GL.Mesh.load({vertices:vertices, colors:colors});
49 }
50Index: ../trunk-jpl/src/m/plot/plot_overlay.js
51===================================================================
52--- ../trunk-jpl/src/m/plot/plot_overlay.js (revision 21093)
53+++ ../trunk-jpl/src/m/plot/plot_overlay.js (revision 21094)
54@@ -62,7 +62,7 @@
55 canvas.nodes[canvas.nodes.length] = node;
56 scale = 1 / (xmax - xmin);
57 node["shaderName"] = "unlit_textured";
58- node["shader"] = gl["shaders"][node["shaderName"]]["program"];
59+ node["shader"] = gl["shaders"][node["shaderName"]];
60 node["scale"] = [scale, scale, scale * matrixscale];
61 node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
62 node["modelMatrix"] = recalculateModelMatrix(node);
63@@ -72,10 +72,6 @@
64 node["maskEnabled"] = options.getfieldvalue('outermask','off') == 'on';
65 node["maskHeight"] = options.getfieldvalue('outermaskheight',150.0);
66 node["maskColor"] = options.getfieldvalue('outermaskcolor',[0.0,0.0,1.0,1.0]);
67-
68- //some defaults:
69- texcoords.itemSize = 2;
70- vertices.itemSize = 3;
71
72 //Handle outer radaroverlay
73 if (md.radaroverlay.outerx) {
74@@ -151,7 +147,6 @@
75 texcoords[texcoords.length] = (y[i] - modelylim[0]) / yrange;
76 }
77 }
78-
79 //linearize the elements array:
80 var element;
81 for(var i = 0; i < elements.length; i++){
82@@ -161,9 +156,5 @@
83 indices[indices.length] = element[1];
84 indices[indices.length] = element[2];
85 }
86- indices.itemSize = 1;
87-
88- /*Initalize buffers: */
89- node["arrays"] = [vertices, texcoords, indices];
90- node["buffers"] = initBuffers(gl,node["arrays"]);
91+ node["mesh"] = GL.Mesh.load({vertices:vertices, coords:texcoords, indices:indices});
92 }
93Index: ../trunk-jpl/src/m/plot/plot_mesh.js
94===================================================================
95--- ../trunk-jpl/src/m/plot/plot_mesh.js (revision 21093)
96+++ ../trunk-jpl/src/m/plot/plot_mesh.js (revision 21094)
97@@ -62,7 +62,7 @@
98 canvas.nodes[canvas.nodes.length] = node;
99 scale = 1 / (xmax - xmin);
100 node["shaderName"] = "colored";
101- node["shader"] = gl["shaders"][node["shaderName"]]["program"];
102+ node["shader"] = gl["shaders"][node["shaderName"]];
103 node["lineWidth"] = options.getfieldvalue('linewidth',1);
104 node["scale"] = [scale, scale, scale * matrixscale];
105 node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
106@@ -72,9 +72,6 @@
107 node["maskEnabled"] = options.getfieldvalue('innermask','off') == 'on';
108 node["maskHeight"] = options.getfieldvalue('innermaskheight',150.0)*options.getfieldvalue('heightscale',1);
109 node["maskColor"] = options.getfieldvalue('innermaskcolor',[0.0,0.0,1.0,1.0]);
110-
111- //some defaults:
112- colors.itemSize = 4;
113
114 //retrieve some options
115 var edgecolor = new RGBColor(options.getfieldvalue('edgecolor','black'));
116@@ -87,7 +84,6 @@
117 else if (elements[0].length==4){ //tetras
118 }
119 else{ //2D triangular elements
120- vertices.itemSize = 3;
121 var xyz = vec3.create();
122 var direction = vec3.create();
123 var vertex = vec3.create();
124@@ -134,11 +130,7 @@
125 indices[indices.length] = element[2];
126 indices[indices.length] = element[0];
127 }
128- indices.itemSize = 1;
129 }
130 //}}}
131-
132- /*Initalize buffers: */
133- node["arrays"] = [vertices, colors, indices];
134- node["buffers"] = initBuffers(gl, node["arrays"]);
135+ node["mesh"] = GL.Mesh.load({vertices:vertices, colors:colors, indices:indices});
136 }
137Index: ../trunk-jpl/src/m/plot/webgl.js
138===================================================================
139--- ../trunk-jpl/src/m/plot/webgl.js (revision 21093)
140+++ ../trunk-jpl/src/m/plot/webgl.js (revision 21094)
141@@ -14,18 +14,15 @@
142 }
143 function initWebGL(canvas,options) { //{{{
144 var gl;
145-
146 try {
147- // Try to grab the standard context. If it fails, fallback to experimental.
148- gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
149+ if (!canvas.gl) gl = GL.create({canvas:canvas});
150+ else gl = canvas.gl;
151 }
152- catch(e) {}
153-
154- // If we don't have a GL context, give up now
155- if (!gl) {
156- alert("Unable to initialize WebGL. Your browser may not support it.");
157+ catch(e) {
158+ console.log(e);
159+ return;
160 }
161-
162+
163 // Enable depth testing
164 gl.enable(gl.DEPTH_TEST);
165 // Near things obscure far things
166@@ -33,10 +30,6 @@
167 // Enable color blending/overlay
168 gl.enable(gl.BLEND);
169
170- // Allocate arrays equal to maximium number of attributes used by any one shader
171- gl.enableVertexAttribArray(0);
172- gl.enableVertexAttribArray(1);
173-
174 // Load shaders and store them in gl object
175 gl.shaders = loadShaders(gl);
176
177@@ -79,52 +72,13 @@
178
179 return gl;
180 } //}}}
181-function initBuffers(gl,arrays) { //{{{
182- var bufferArray = [];
183- for (var i = 0; i < arrays.length; i++) {
184- bufferArray[i] = gl.createBuffer();
185- bufferArray[i].itemSize = arrays[i].itemSize;
186- bufferArray[i].numItems = arrays[i].length/bufferArray[i].itemSize;
187-
188- if (bufferArray[i].itemSize > 1) {
189- gl.bindBuffer(gl.ARRAY_BUFFER, bufferArray[i]);
190- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(arrays[i]), gl.STATIC_DRAW);
191- }
192- else {
193- //TODO: identify index buffers uniquely (by name)
194- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferArray[i]);
195- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(arrays[i]), gl.STATIC_DRAW);
196- }
197- }
198- return bufferArray;
199-} //}}}
200 function initTexture(gl,imageSource) { //{{{
201- var texture = gl.createTexture();
202- texture.image = new Image();
203- texture.isLoaded = false;
204- texture.image.onload = function () {
205- handleLoadedTexture(gl,texture);
206- }
207- texture.image.src = imageSource;
208- return texture;
209+ return GL.Texture.fromURL(imageSource, {minFilter:gl.LINEAR_MIPMAP_LINEAR, magFilter:gl.LINEAR});
210 } //}}}
211-function handleLoadedTexture(gl,texture) { //{{{
212- gl.activeTexture(gl.TEXTURE0);
213- gl.bindTexture(gl.TEXTURE_2D, texture);
214- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
215- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
216- gl.generateMipmap(gl.TEXTURE_2D);
217- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR);
218- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
219- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
220- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
221- gl.bindTexture(gl.TEXTURE_2D, null);
222- texture.isLoaded = true;
223-} //}}}
224 function Node(gl,options) { //{{{
225 //Returns a Node object that contains default display states for webgl object
226 return {buffers:[],
227- shader:gl.shaders["colored"]["program"],
228+ shader:gl.shaders["colored"],
229 draw:null,
230 hideOcean:false,
231 level:0,
232@@ -194,137 +148,89 @@
233 var shaderNames = ["colored", "unlit_textured"];
234 shaders = {};
235 shaders["colored"] = {loaded:false, vsh:{}, fsh:{}};
236- shaders["colored"]["vsh"]["string"] =
237- ['attribute vec3 aVertexPosition;',
238- 'attribute vec4 aVertexColor;',
239- '',
240- 'uniform mat4 uMVPMatrix;',
241- 'uniform float uAlpha;',
242- '',
243- 'varying vec4 vColor;',
244- '',
245- 'void main(void) {',
246- ' gl_PointSize = 3.0;',
247- ' gl_Position = uMVPMatrix * vec4(aVertexPosition.xyz, 1.0);',
248- ' vColor = vec4(aVertexColor.xyz, uAlpha);',
249- '}'].join('\n');
250- shaders["colored"]["fsh"]["string"] =
251- ['precision mediump float;',
252- '',
253- 'varying vec4 vColor;',
254- '',
255- 'void main(void) {',
256- ' gl_FragColor = vColor;',
257- '}'].join('\n');
258- shaders["unlit_textured"] = {loaded:false, vsh:{}, fsh:{}};
259- shaders["unlit_textured"]["vsh"]["string"] =
260- ['attribute vec3 aVertexPosition;',
261- 'attribute vec2 aTextureCoord;',
262- '',
263- 'uniform mat4 uMVPMatrix;',
264- '',
265- 'varying vec2 vTextureCoord;',
266- 'varying float vZCoord;',
267- '',
268- 'void main(void) {',
269- ' gl_PointSize = 3.0;',
270- ' gl_Position = uMVPMatrix * vec4(aVertexPosition.xyz, 1.0);',
271- ' vTextureCoord = aTextureCoord;',
272- ' vZCoord = aVertexPosition.z;',
273- '}'].join('\n');
274- shaders["unlit_textured"]["fsh"]["string"] =
275- ['precision mediump float;',
276- '',
277- 'varying vec2 vTextureCoord;',
278- 'varying float vZCoord;',
279- '',
280- 'uniform sampler2D uColorSampler;',
281- 'uniform float uAlpha;',
282- 'uniform bool uMaskEnabled;',
283- 'uniform float uMaskHeight;',
284- 'uniform vec4 uMaskColor;',
285- '',
286- 'void main(void) {',
287- ' if (uMaskEnabled && (vZCoord < uMaskHeight)) {',
288- ' gl_FragColor = vec4(uMaskColor.xyz, uAlpha);',
289- ' }',
290- ' else {',
291- ' gl_FragColor = vec4(texture2D(uColorSampler, vec2(vTextureCoord.s, vTextureCoord.t)).rgb, uAlpha);',
292- ' }',
293- '}'].join('\n');
294- shaderNames.forEach(function(shaderName){
295- shaders[shaderName]["vsh"]["shader"] = getShaderByString(gl, shaders[shaderName]["vsh"]["string"], "vsh");
296- shaders[shaderName]["fsh"]["shader"] = getShaderByString(gl, shaders[shaderName]["fsh"]["string"], "fsh");
297-
298- shaders[shaderName]["program"] = gl.createProgram();
299- gl.attachShader(shaders[shaderName]["program"], shaders[shaderName]["vsh"]["shader"]);
300- gl.attachShader(shaders[shaderName]["program"], shaders[shaderName]["fsh"]["shader"]);
301- gl.linkProgram(shaders[shaderName]["program"]);
302-
303- if (!gl.getProgramParameter(shaders[shaderName]["program"], gl.LINK_STATUS)) {
304- alert("Could not initialise shaders");
305- }
306-
307- var vshStringArray = shaders[shaderName]["vsh"]["string"].split("\n");
308- var fshStringArray = shaders[shaderName]["fsh"]["string"].split("\n");
309- var line = "";
310- var property = "";
311- for (var i = 0; i < vshStringArray.length; i++) {
312- line = vshStringArray[i];
313- if (line.search("attribute") != -1) {
314- property = nameFromLine(line);
315- shaders[shaderName]["program"][property] = gl.getAttribLocation(shaders[shaderName]["program"], property);
316- }
317- else if (line.search("uniform") != -1) {
318- property = nameFromLine(line);
319- shaders[shaderName]["program"][property] = gl.getUniformLocation(shaders[shaderName]["program"], property);
320- }
321- else if (line.search("void main") != -1) {
322- break;
323- }
324- }
325- for (var i = 0; i < fshStringArray.length; i++) {
326- line = fshStringArray[i];
327- if (line.search("uniform") != -1) {
328- property = nameFromLine(line);
329- shaders[shaderName]["program"][property] = gl.getUniformLocation(shaders[shaderName]["program"], property);
330- }
331- else if (line.search("void main") != -1) {
332- break;
333- }
334- }
335- shaders[shaderName]["loaded"] = true;
336- });
337+ //basic phong shader
338+ shaders["colored"] = new Shader('\
339+ precision highp float;\
340+ attribute vec3 a_vertex;\
341+ attribute vec4 a_color;\
342+ uniform mat4 u_mvp;\
343+ uniform float u_alpha;\
344+ varying vec4 v_color;\
345+ void main() {\
346+ gl_PointSize = 3.0;\
347+ gl_Position = u_mvp * vec4(a_vertex.xyz, 1.0);\
348+ v_color = vec4(a_color.xyz, u_alpha);\
349+ }\
350+ ', '\
351+ precision mediump float;\
352+ varying vec4 v_color;\
353+ void main() {\
354+ gl_FragColor = v_color;\
355+ }\
356+ ');
357+
358+ shaders["unlit_textured"] = new Shader('\
359+ precision highp float;\
360+ attribute vec3 a_vertex;\
361+ attribute vec2 a_coord;\
362+ uniform mat4 u_mvp;\
363+ varying vec2 v_coord;\
364+ varying float v_z;\
365+ void main() {\
366+ gl_PointSize = 3.0;\
367+ gl_Position = u_mvp * vec4(a_vertex.xyz, 1.0);\
368+ v_coord = a_coord;\
369+ v_z = a_vertex.z;\
370+ }\
371+ ', '\
372+ precision mediump float;\
373+ varying vec2 v_coord;\
374+ varying float v_z;\
375+ uniform sampler2D u_texture;\
376+ uniform float u_alpha;\
377+ uniform bool u_maskEnabled;\
378+ uniform float u_maskHeight;\
379+ uniform vec4 u_maskColor;\
380+ void main() {\
381+ if (u_maskEnabled && (v_z < u_maskHeight)) {\
382+ gl_FragColor = vec4(u_maskColor.rgb, u_alpha);\
383+ }\
384+ else {\
385+ gl_FragColor = vec4(texture2D(u_texture, v_coord).rgb, u_alpha);\
386+ }\
387+ }\
388+ ');
389+ /*
390+ shaders["phong"] = new Shader('\
391+ precision highp float;\
392+ attribute vec3 a_vertex;\
393+ attribute vec3 a_normal;\
394+ attribute vec2 a_coord;\
395+ varying vec3 v_normal;\
396+ varying vec2 v_coord;\
397+ uniform mat4 u_mvp;\
398+ uniform mat4 u_model;\
399+ void main() {\
400+ v_coord = a_coord;\
401+ v_normal = (u_model * vec4(a_normal,0.0)).xyz;\
402+ gl_Position = u_mvp * vec4(a_vertex,1.0);\
403+ }\
404+ ', '\
405+ precision highp float;\
406+ varying vec3 v_normal;\
407+ varying vec2 v_coord;\
408+ uniform vec3 u_lightvector;\
409+ uniform vec4 u_color;\
410+ uniform sampler2D u_texture;\
411+ void main() {\
412+ vec3 N = normalize(v_normal);\
413+ vec4 color = u_color * texture2D( u_texture, v_coord);\
414+ gl_FragColor = color * max(0.0, dot(u_lightvector,N));\
415+ }\
416+ ');
417+ */
418 return shaders;
419 } //}}}
420-function getShaderByString(gl,str,type) { //{{{
421- var shader;
422- if (type == "fsh") {
423- shader = gl.createShader(gl.FRAGMENT_SHADER);
424- }
425- else if (type == "vsh") {
426- shader = gl.createShader(gl.VERTEX_SHADER);
427- }
428- else {
429- return null;
430- }
431-
432- gl.shaderSource(shader, str);
433- gl.compileShader(shader);
434-
435- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
436- alert(gl.getShaderInfoLog(shader));
437- return null;
438- }
439-
440- return shader;
441-} //}}}
442-function nameFromLine(line) { //{{{
443- //returns lowerCamelCase property name from shader line
444- var fullName = line.split(" ")[2];
445- return fullName.slice(0, fullName.search(";"));
446-} //}}}
447-//}}}
448 //{{{ Interface Functions
449 function onPan(ev,canvas,displaylog) { //{{{
450 ev.preventDefault();
451@@ -427,60 +333,31 @@
452 mat4.multiply(canvas.cameraMatrix, pMatrix, vMatrix);
453 }//}}}
454 function drawSceneGraphNode(canvas,node) { //{{{
455- if (!node["enabled"]) {
456- return;
457- }
458- if (node["texture"]) {
459- if (!node["texture"]["isLoaded"]) {
460- return;
461- }
462- }
463- var gl = canvas.gl;
464- bindAttributes(gl, node["shader"], node["buffers"]);
465+ if (!node["enabled"]) return;
466+
467 var mvpMatrix = mat4.create();
468 mat4.multiply(mvpMatrix, canvas.cameraMatrix, node["modelMatrix"]);
469- gl.uniformMatrix4fv(node["shader"]["uMVPMatrix"], false, mvpMatrix);
470- gl.uniform1f(node["shader"]["uAlpha"], node["alpha"]);
471- gl.uniform1i(node["shader"]["uMaskEnabled"], node["maskEnabled"]);
472- gl.uniform1f(node["shader"]["uMaskHeight"], node["maskHeight"]);
473- gl.uniform4fv(node["shader"]["uMaskColor"], node["maskColor"]);
474- if (node["texture"]) {
475- gl.activeTexture(gl.TEXTURE0);
476- gl.bindTexture(gl.TEXTURE_2D, node["texture"]);
477- gl.uniform1i(node["shader"]["uColorSampler"], 0);
478- }
479- if (node["disableDepthTest"]) {
480- gl.disable(gl.DEPTH_TEST);
481- }
482+
483+ if (node["texture"]) node["texture"].bind(0);
484+ if (node["disableDepthTest"]) gl.disable(gl.DEPTH_TEST);
485+
486 gl.lineWidth(node["lineWidth"]);
487- gl.blendFunc (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
488- if (node["useIndexBuffer"] == true) {
489- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, node["buffers"][node["buffers"].length - 1]);
490- gl.drawElements(node["drawMode"], node["buffers"][node["buffers"].length - 1].numItems, gl.UNSIGNED_SHORT, 0);
491- }
492- else {
493- gl.drawArrays(node["drawMode"], 0, node["buffers"][0].numItems);
494- }
495+ gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
496+
497+ node["shader"].uniforms({
498+ u_mvp: mvpMatrix,
499+ u_texture: 0,
500+ u_alpha: node["alpha"],
501+ u_maskEnabled: node["maskEnabled"],
502+ u_maskHeight: node["maskHeight"],
503+ u_maskColor: node["maskColor"]
504+ })
505+ if (node["useIndexBuffer"] == true) node["shader"].draw(node["mesh"], node["drawMode"], "indices");
506+ else node["shader"].draw(node["mesh"], node["drawMode"]);
507+
508 gl.enable(gl.DEPTH_TEST);
509 } //}}}
510-function bindAttributes(gl,shaderProgram,bufferArray) { //{{{
511- gl.useProgram(shaderProgram);
512- var arrayNumber = 0;
513- for (var propertyName in shaderProgram) {
514- if (propertyName[0] == "a") {
515- if (bufferArray[arrayNumber].itemSize > 1) {
516- gl.bindBuffer(gl.ARRAY_BUFFER, bufferArray[arrayNumber]);
517- gl.vertexAttribPointer(shaderProgram[propertyName], bufferArray[arrayNumber].itemSize, gl.FLOAT, false, 0, 0);
518- arrayNumber++;
519- }
520- }
521- }
522-} //}}}
523 function draw(canvas,options) { //{{{
524- if (canvas.nodes.length < 1) {
525- canvas.drawHandler = window.requestAnimationFrame(function(time) {draw(canvas,options)});
526- return;
527- }
528 // Ensure canvas and gl viewport sizes are the same
529 var displayWidth = canvas.clientWidth;
530 var displayHeight = canvas.clientHeight;
531@@ -492,20 +369,24 @@
532
533 if (canvas.textcanvas) canvas.textcanvas.draw(canvas);
534
535- var gl = canvas.gl;
536- gl.clearColor(canvas.backgroundcolor[0], canvas.backgroundcolor[1], canvas.backgroundcolor[2], canvas.backgroundcolor[3]);
537-
538- // Skip drawing of new frame if any texture is not yet loaded
539 var nodes = canvas.nodes;
540+ if (nodes.length < 1) {
541+ canvas.drawHandler = window.requestAnimationFrame(function(time) {draw(canvas,options)});
542+ return;
543+ }
544 for (var node in nodes) {
545- if (nodes[node]["texture"] && !nodes[node]["texture"]["isLoaded"]) {
546+ if (nodes[node]["texture"] && nodes[node]["texture"]["ready"] == false) {
547 canvas.drawHandler = window.requestAnimationFrame(function(time) {draw(canvas,options)});
548 return;
549 }
550 }
551- // Else, clear the color as well as the depth buffer for new frame
552+
553+ var gl = canvas.gl;
554+ gl.clearColor(canvas.backgroundcolor[0], canvas.backgroundcolor[1], canvas.backgroundcolor[2], canvas.backgroundcolor[3]);
555 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
556
557+
558+
559 updateCameraMatrix(canvas);
560
561 var drawPassNumber = 2;
562Index: ../trunk-jpl/src/m/plot/plot_unit.js
563===================================================================
564--- ../trunk-jpl/src/m/plot/plot_unit.js (revision 21093)
565+++ ../trunk-jpl/src/m/plot/plot_unit.js (revision 21094)
566@@ -7,16 +7,6 @@
567 // See also: PLOTMODEL, PLOT_MANAGER
568
569 //declare variables: {{{
570- var vertices = [];
571- var indices = [];
572- var texcoords = [];
573- var nanindices = {};
574- var xmin,xmax;
575- var ymin,ymax;
576- var zmin,zmax;
577- var datamin,datamax,datadelta;
578- var scale,matrixscale,vertexscale;
579-
580 //Process data and model
581 var meshresults = processmesh(md,data,options);
582 var x = meshresults[0];
583@@ -25,6 +15,16 @@
584 var elements = meshresults[3];
585 var is2d = meshresults[4];
586 var isplanet = meshresults[5];
587+
588+ var vertices = new Float32Array(x.length * 3);
589+ var texcoords = new Float32Array(x.length * 2);
590+ var indices = new Uint16Array(elements.length * 3);
591+ var nanindices = {};
592+ var xmin,xmax;
593+ var ymin,ymax;
594+ var zmin,zmax;
595+ var datamin,datamax,datadelta;
596+ var scale,matrixscale,vertexscale;
597
598 //Compue scaling through matrices for 2d meshes and vertices for 3d meshes
599 if (!md.geometry.surface) {
600@@ -64,7 +64,7 @@
601 canvas.nodes[canvas.nodes.length] = node;
602 scale = 1 / (xmax - xmin);
603 node["shaderName"] = "unlit_textured";
604- node["shader"] = gl["shaders"][node["shaderName"]]["program"];
605+ node["shader"] = gl["shaders"][node["shaderName"]];
606 node["scale"] = [scale, scale, scale * matrixscale];
607 node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (2 / scale)];
608 node["modelMatrix"] = recalculateModelMatrix(node);
609@@ -100,24 +100,21 @@
610 datamax = caxis[1];
611 datadelta = datamax - datamin;
612
613- vertices.itemSize = 3;
614- texcoords.itemSize = 2;
615-
616 var xyz = vec3.create();
617 var direction = vec3.create();
618 var vertex = vec3.create();
619 var magnitude;
620
621- for(var i = 0; i < x.length; i++){
622+ for(var i = 0, vindex = 0, tindex = 0; i < x.length; i++){
623 //Check for NaN values and remove from indices array as necessary, but preserve vertex array spacing
624 if (isNaN(x[i]) || isNaN(y[i]) || isNaN(z[i]) || isNaN(data[i])) {
625 nanindices[i] = i;
626- vertices[vertices.length] = vertex[0];
627- vertices[vertices.length] = vertex[1];
628- vertices[vertices.length] = vertex[2];
629+ vertices[vindex++] = vertex[0];
630+ vertices[vindex++] = vertex[1];
631+ vertices[vindex++] = vertex[2];
632
633- texcoords[texcoords.length] = 0.0;
634- texcoords[texcoords.length] = 0.0;
635+ texcoords[tindex++] = 0.0;
636+ texcoords[tindex++] = 0.0;
637 continue;
638 }
639 //Scale vertices
640@@ -125,30 +122,25 @@
641 magnitude = vec3.length(xyz) + md.geometry.surface[i] * vertexscale;
642 vec3.normalize(direction, xyz);
643 vec3.scale(vertex, direction, magnitude);
644- vertices[vertices.length] = vertex[0];
645- vertices[vertices.length] = vertex[1];
646- vertices[vertices.length] = vertex[2];
647+ vertices[vindex++] = vertex[0];
648+ vertices[vindex++] = vertex[1];
649+ vertices[vindex++] = vertex[2];
650
651- texcoords[texcoords.length] = 0.5;
652- texcoords[texcoords.length] = (data[i] - datamin) / datadelta;
653+ texcoords[tindex++] = 0.5;
654+ texcoords[tindex++] = (data[i] - datamin) / datadelta;
655 }
656
657 //linearize the elements array:
658 var element;
659- for(var i = 0; i < elements.length; i++){
660+ for(var i = 0, iindex = 0; i < elements.length; i++){
661 element = [elements[i][0] - 1, elements[i][1] - 1, elements[i][2] - 1];
662 if (element[0] in nanindices || element[1] in nanindices || element[2] in nanindices) continue;
663- indices[indices.length] = element[0];
664- indices[indices.length] = element[1];
665- indices[indices.length] = element[2];
666+ indices[iindex++] = element[0];
667+ indices[iindex++] = element[1];
668+ indices[iindex++] = element[2];
669 }
670- indices.itemSize = 1;
671-
672 }
673-
674- //Initalize buffers
675- node["arrays"] = [vertices, texcoords, indices];
676- node["buffers"] = initBuffers(gl,node["arrays"]);
677+ node["mesh"] = GL.Mesh.load({vertices:vertices, coords:texcoords, indices:indices});
678 break;
679 //}}}
680 //quiver plot {{{
681@@ -168,33 +160,26 @@
682 else if (elements[0].length==4){//tetras
683 }
684 else{ //triangular elements
685- vertices.itemSize = 3;
686-
687 var xyz = vec3.create();
688 var direction = vec3.create();
689 var vertex = vec3.create();
690 var magnitude;
691 var timestamps = data[data.length-1];
692- for(var i = 0; i < x.length; i++){
693+ for(var i = 0, vindex = 0, tindex = 0; i < x.length; i++){
694 //Check for NaN values and remove from indices array as necessary, but preserve vertex array spacing
695 if (isNaN(x[i]) || isNaN(y[i]) || isNaN(z[i]) || isNaN(data[i][0])) {
696 nanindices[i] = i;
697- vertices[vertices.length] = vertex[0];
698- vertices[vertices.length] = vertex[1];
699- vertices[vertices.length] = vertex[2];
700-
701- texcoords[texcoords.length] = 0.0;
702- texcoords[texcoords.length] = 0.0;
703- continue;
704 }
705- //Scale vertices
706- xyz = vec3.fromValues(x[i], y[i], z[i]);
707- magnitude = vec3.length(xyz) + md.geometry.surface[i] * vertexscale;
708- vec3.normalize(direction, xyz);
709- vec3.scale(vertex, direction, magnitude);
710- vertices[vertices.length] = vertex[0];
711- vertices[vertices.length] = vertex[1];
712- vertices[vertices.length] = vertex[2];
713+ else {
714+ //Scale vertices
715+ xyz = vec3.fromValues(x[i], y[i], z[i]);
716+ magnitude = vec3.length(xyz) + md.geometry.surface[i] * vertexscale;
717+ vec3.normalize(direction, xyz);
718+ vec3.scale(vertex, direction, magnitude);
719+ }
720+ vertices[vindex++] = vertex[0];
721+ vertices[vindex++] = vertex[1];
722+ vertices[vindex++] = vertex[2];
723 }
724 //Transpose data to obtain column addressable data matrix
725 data = data[0].map(function(col, i) {
726@@ -203,65 +188,54 @@
727 })
728 });
729 //Prevent evaluation of datasubarray min/max if caxis exists
730- if (options.exist('caxis')) {
731- caxis = options.getfieldvalue('caxis');
732- }
733- else {
734- caxis = [ArrayMin(data[0]),ArrayMax(data[0].slice(0,-1))];
735- }
736+ if (options.exist('caxis')) caxis = options.getfieldvalue('caxis');
737+ else caxis = [ArrayMin(data[0]),ArrayMax(data[0].slice(0,-1))];
738 if (options.getfieldvalue('log','off')!='off') caxis = [Math.log10(caxis[0])/Math.log10(options.getfieldvalue('log',10)),Math.log10(caxis[1])/Math.log10(options.getfieldvalue('log',10))];
739-
740+ //Prepare texcoords to hold array of data values
741+ texcoords = [];
742 for(var i = 0; i < data.length; i++){
743 datamin = caxis[0];
744 datamax = caxis[1];
745 datadelta = datamax - datamin;
746-
747 //Precalculate arrays for each datasubarray, trimming off timestamp value by using x.length instead of data[i].length
748- texcoords[i] = [];
749- texcoords[i].itemSize = 2;
750- for(var j = 0; j < x.length; j++){
751- texcoords[i][texcoords[i].length] = 0.5;
752- texcoords[i][texcoords[i].length] = (data[i][j] - datamin) / datadelta;
753+ texcoords[i] = new Float32Array(x.length * 2);
754+ for(var j = 0, index = 0; j < x.length; j++){
755+ texcoords[i][index++] = 0.5;
756+ texcoords[i][index++] = (data[i][j] - datamin) / datadelta;
757 }
758 }
759
760 //linearize the elements array:
761 var element;
762- for(var i = 0; i < elements.length; i++){
763+ for(var i = 0, iindex = 0; i < elements.length; i++){
764 element = [elements[i][0] - 1, elements[i][1] - 1, elements[i][2] - 1];
765 if (element[0] in nanindices || element[1] in nanindices || element[2] in nanindices) continue;
766- indices[indices.length] = element[0];
767- indices[indices.length] = element[1];
768- indices[indices.length] = element[2];
769+ indices[iindex++] = element[0];
770+ indices[iindex++] = element[1];
771+ indices[iindex++] = element[2];
772 }
773- indices.itemSize = 1;
774
775 //Initialize movie loop
776 node["movieInterval"] = 1000 / canvas.moviefps;
777 node["movieTimestamps"] = timestamps;
778 node["movieLength"] = timestamps.length;
779 node["movieFrame"] = 0;
780-
781 if (canvas["movieHandler"]) clearInterval(canvas["movieHandler"]);
782 canvas["movieHandler"] = setInterval(function () {
783 node["movieFrame"] = canvas["movieFrame"];
784 if (canvas["moviePlay"] && canvas["movieIncrement"]) {
785- if (canvas["movieReverse"]) {
786- node["movieFrame"] = (((node["movieFrame"] - 1) % node["movieLength"]) + node["movieLength"]) % node["movieLength"]; //Handle negative modulus
787- }
788- else {
789- node["movieFrame"] = (((node["movieFrame"] + 1) % node["movieLength"]) + node["movieLength"]) % node["movieLength"]; //Handle negative modulus
790- }
791+ if (canvas["movieReverse"]) node["movieFrame"] = (((node["movieFrame"] - 1) % node["movieLength"]) + node["movieLength"]) % node["movieLength"]; //Handle negative modulus
792+ else node["movieFrame"] = (((node["movieFrame"] + 1) % node["movieLength"]) + node["movieLength"]) % node["movieLength"]; //Handle negative modulus
793 }
794- if (canvas["timeLabel"]) {
795- canvas["timeLabel"].html(node["movieTimestamps"][node["movieFrame"]].toFixed(0) + " " + options.getfieldvalue("movietimeunit","yr"));
796- }
797+ if (canvas["timeLabel"]) canvas["timeLabel"].html(node["movieTimestamps"][node["movieFrame"]].toFixed(0) + " " + options.getfieldvalue("movietimeunit","yr"));
798 if (canvas["progressBar"]) {
799 canvas["progressBar"].val(node["movieFrame"]);
800 canvas["progressBar"].slider('refresh');
801 }
802- node["buffers"] = node["buffersArray"][node["movieFrame"]];
803 canvas["movieFrame"] = node["movieFrame"];
804+ var buffer = node["mesh"].getBuffer("coords");
805+ buffer.data = texcoords[node["movieFrame"]];
806+ buffer.upload(gl.DYNAMIC_DRAW);
807 }, node["movieInterval"]);
808 if (canvas["progressBar"]) {
809 canvas["movieFrame"] = 0;
810@@ -270,14 +244,7 @@
811 canvas["progressBar"].slider('refresh');
812 }
813 }
814-
815- //Initialize buffers
816- node["arrays"] = [vertices, texcoords, indices];
817- node["buffersArray"] = [];
818- for(var i = 0; i < timestamps.length; i++){
819- node["buffersArray"][i] = initBuffers(gl,[node["arrays"][0],node["arrays"][1][i],node["arrays"][2]]);
820- }
821- node["buffers"] = node["buffersArray"][0];
822+ node["mesh"] = GL.Mesh.load({vertices:vertices, coords:texcoords[0], indices:indices});
823 break;
824 //}}}
825 default:
Note: See TracBrowser for help on using the repository browser.