Changeset 19739


Ignore:
Timestamp:
11/17/15 14:58:52 (9 years ago)
Author:
Eric.Larour
Message:

CHG: better integration of webgl routines and plot_unit.
+ optimization of memory allocation during compilation of javascript modules.

Location:
issm/trunk-jpl/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/plot/plot_manager.js

    r19724 r19739  
    1313        data=options.getfieldvalue('data');
    1414
     15        //standard plot: initialize open Gl for each canvas:
     16        var canvas=document.getElementById(options.getfieldvalue('canvasid'));
     17
     18        // Initialize the GL context:
     19        var gl=initWebGL(canvas,options);
     20        if(!gl){
     21                throw Error("plotmodel error message: could not initialize open Gl!");
     22        }
     23       
    1524        //figure out if this is a special plot
    1625        if (typeof data === 'string'){
     
    210219        var datatype = dataresults[1];
    211220
    212 
    213         //standard plot:
    214         if (options.exist('asymsubplot')){
    215                 id=options.getfieldvalue('asymsubplot',i);
    216                 //subplot(nlines,ncols,id);
    217         }
    218         else{
    219                 //subplot(nlines,ncols,i);
    220         }
    221 
    222221        //plot unit
    223         plot_unit(x,y,z,elements,data2,is2d,isplanet,datatype,options);
     222        plot_unit(x,y,z,elements,data2,is2d,isplanet,datatype,options,canvas,gl);
    224223
    225224        //apply all options
  • issm/trunk-jpl/src/m/plot/plot_unit.js

    r19732 r19739  
    1 function plot_unit(x,y,z,elements,data,is2d,isplanet,datatype,options) {
     1function plot_unit(x,y,z,elements,data,is2d,isplanet,datatype,options,canvas,gl) {
    22        //PLOT_UNIT - unit plot, display data
    33        //
     
    77        //   See also: PLOTMODEL, PLOT_MANAGER
    88
    9         //edgecolor
    10         edgecolor=options.getfieldvalue('edgecolor','none');
     9        //declare variables:  {{{
     10        var node;
     11        var vertices = [];
     12        var indices = [];
     13        var colors = [];
     14        var rgbcolor = [];
     15        var xmin,xmax;
     16        var ymin,ymax;
     17        var zmin,zmax;
     18        var datamin,datamax;
     19        var scale;
     20        //}}}
     21
     22        loadShaders(gl);
    1123       
    12         /*First initialize webgl for the corresponding canvas: */
    13         var canvas=document.getElementById(options.getfieldvalue('canvasid'));
     24        //Initialize the buffer structure:
     25        node = Node(gl,options);
    1426
    15         // Initialize the GL context:
    16         var gl=initWebGL(canvas,options);
     27        //Compute coordinates and data range:
     28        xmin = ArrayMin(x);
     29        xmax = ArrayMax(x);
     30        ymin = ArrayMin(y);
     31        ymax = ArrayMax(y);
     32        zmin = ArrayMin(z);
     33        zmax = ArrayMax(z);
     34        datamin = ArrayMin(data);
     35        datamax = ArrayMax(data);
    1736
    18         // Only continue if WebGL is available and working
    19         if (gl) {
    20                 loadShaders(gl);
    21                 var node=loadModel(gl,x,y,z,elements,data);
    22                 draw(gl,canvas,node);
    23         }
     37        //Compute scaling:
     38        var scale = 1 / (xmax - xmin);
     39        node["scale"] = [scale, scale, scale];
     40        node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (-2 / scale)];
     41        node["modelMatrix"] = recalculateModelMatrix(node);
     42                               
     43        //some defaults:
     44        colors.itemSize = 4;
    2445
    2546        switch(datatype){
     
    4667                                  patch( 'Faces',[C A D],'Vertices', [x y z],'CData',data(pos),'FaceColor','flat','EdgeColor',edgecolor);*/
    4768                        }
    48                         else{
     69                        else{ //2D triangular elements
     70                                       
    4971                                /*A=elements(pos,1); B=elements(pos,2); C=elements(pos,3);
    5072                                  patch( 'Faces', [A B C], 'Vertices', [x y z],'CData', data(pos),'FaceColor','flat','EdgeColor',edgecolor);*/
     
    7092                                  patch( 'Faces',[C A D],'Vertices', [x y z],'FaceVertexCData',data(:),'FaceColor','interp','EdgeColor',edgecolor);*/
    7193                        }
    72                         else{
    73                                
    74                                 /*A=elements(:,1); B=elements(:,2); C=elements(:,3);
    75                                   patch( 'Faces', [A B C], 'Vertices', [x y z],'FaceVertexCData', data(:),'FaceColor','interp','EdgeColor',edgecolor);
    76                                   */
     94                        else{ //triangular elements
     95                       
     96                                vertices.itemSize = 3;
     97                                for(var i = 0; i < x.length; i++){
     98                                        vertices[vertices.length] = x[i];
     99                                        vertices[vertices.length] = y[i];
     100                                        //vertices[vertices.length] = z[i];
     101                                        vertices[vertices.length] = 0;
     102
     103                                        //handle mesh/qinterest size mismatch
     104                                        rgbcolor = rgb(data[i], datamin, datamax);
     105                                        colors[colors.length] = rgbcolor[0];
     106                                        colors[colors.length] = rgbcolor[1];
     107                                        colors[colors.length] = rgbcolor[2];
     108                                        colors[colors.length] = 1.0;
     109                                }
     110
     111                                //linearize the elements array:
     112                                indices = indices.concat.apply(indices, elements);
     113                                indices.itemSize = 1;
     114                                for(var i=0;i<indices.length;i++)indices[i]--; //matlab indices from 1, so decrement.
     115
    77116                        }
    78117                        break;
     
    91130                                throw Error(sprintf("%s%i%s\n",'case ',datatype,' not supported'));
    92131        }
     132       
     133        /*Initalize buffers: */
     134        node["buffers"] = initBuffers(gl,[vertices, colors, indices]);
     135       
     136        /*Draw into the canvas:*/
     137        draw(gl,options,canvas,node);
    93138}
  • issm/trunk-jpl/src/m/plot/plotmodel.js

    r19721 r19739  
    1111       
    1212        //Get figure number and number of plots
    13         figurenumber=options.figurenumber;
    1413        numberofplots=options.numberofplots;
    1514
     
    3534        if (numberofplots){
    3635
    37                 //Create figure : to be replaced by Dan's code here for the <div> segment of the html code?
    38                 //f=figure(figurenumber);clf;
    39                 //deal with visible off or on, figure position, etc...
    40 
    4136                //Go through all data plottable and close window if an error occurs
    4237                for (var i=0;i<numberofplots;i++){
  • issm/trunk-jpl/src/m/plot/webgl.js

    r19734 r19739  
    1616                gl = null;
    1717        }
    18 
    19 
    20         // Set clear color to black, fully opaque
    21         var backgroundcolor=new RGBColor(options.getfieldvalue('backgroundcolor','white'));
    22         if(backgroundcolor.ok){
    23                 gl.clearColor(backgroundcolor.r/255.0, backgroundcolor.g/255.0, backgroundcolor.b/255.0, 1.0);
    24         }
    25         else throw Error(sprintf("s%s%s\n","initWebGL error message: cound not find out background color for curent canvas ",canvas));
    2618
    2719        // Enable depth testing
     
    5143
    5244        return gl;
    53 } //}}}
    54 function loadModel(gl,x,y,z,elements,data){ //{{{
    55         colorbar=colorbars["rainbow"];
    56         model={"x":x,"y":y,"z":z,"elements":elements}; 
    57         sceneGraph={};
    58         sceneGraph["model"] = sceneGraphNode(gl);
    59         sceneGraph["model"]["shaderName"] = "colored";
    60         sceneGraph["model"]["shader"] = shaders["colored"]["program"];
    61         sceneGraph["model"]["useIndexBuffer"] = true;
    62        
    63         var meshVertices = [];
    64         var meshColors = [];
    65         var meshNormals = [];
    66         var color = [];
    67 
    68         var qmin = Math.min.apply(null,data);
    69         var qmax = Math.max.apply(null,data);
    70         var xmin = Math.min.apply(null,model["x"]);
    71         var xmax = Math.max.apply(null,model["x"]);
    72         var ymin = Math.min.apply(null,model["y"]);
    73         var ymax = Math.max.apply(null,model["y"]);
    74         var zmin = Math.min.apply(null,[0]);
    75         var zmax = Math.max.apply(null,[0]);
    76        
    77         var scale = 1 / (xmax - xmin);
    78         sceneGraph["model"]["scale"] = [scale, scale, scale];
    79         sceneGraph["model"]["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (-2 / scale)];
    80         sceneGraph["model"]["modelMatrix"] = recalculateModelMatrix(sceneGraph["model"]);
    81 
    82         for(var i = 0; i < model["x"].length; i++){
    83                 meshVertices[meshVertices.length] = model["x"][i];
    84                 meshVertices[meshVertices.length] = model["y"][i];
    85                 meshVertices[meshVertices.length] = 0.0;
    86                
    87                 //initialize vertex normals
    88                 meshNormals[meshNormals.length] = 0.0;
    89                 meshNormals[meshNormals.length] = 0.0;
    90                 meshNormals[meshNormals.length] = 0.0;
    91                
    92                 //handle mesh/qinterest size mismatch
    93                 color = rgb(data[i], qmin, qmax);
    94                 meshColors[meshColors.length] = color[0];
    95                 meshColors[meshColors.length] = color[1];
    96                 meshColors[meshColors.length] = color[2];
    97                 meshColors[meshColors.length] = 1.0;
    98         }
    99 
    100         var indices = [];
    101         indices = indices.concat.apply(indices, model["elements"]);
    102 
    103         for (var i = 0; i < indices.length; i += 3){
    104                 indices[i] -= 1;
    105                 indices[i + 1] -= 1;
    106                 indices[i + 2] -= 1;
    107         }
    108 
    109         meshVertices.itemSize = 3;
    110         meshColors.itemSize = 4;
    111         indices.itemSize = 1;
    112 
    113         sceneGraph["model"]["buffers"] = initBuffers(gl,[meshVertices, meshColors, indices]);
    114         return sceneGraph["model"];
    11545} //}}}
    11646function initBuffers(gl,arrays) { //{{{
     
    13363        return bufferArray;
    13464} //}}}
    135 function sceneGraphNode(gl) { //{{{
    136         //Primary object for storing common rendering information.
    137         return {buffers:[],
    138                 shader:null,
     65function Node(gl,options) { //{{{
     66       
     67        var node;
     68
     69        node= {buffers:[],
     70                shader:shaders["colored"]["program"],
    13971                draw:null,
    14072                hideOcean:false,
    14173                level:0,
    142                 useIndexBuffer:false,
     74                useIndexBuffer:true,
    14375                useOrthographic:false,
    144                 transparency:1.0,
     76                transparency:1.0,
    14577                disableDepthTest:false,
    14678                enableCullFace:false,
     
    15183                rotation:vec3.create(),
    15284                scale:vec3.fromValues(1, 1, 1),
    153                 modelMatrix:mat4.create()};
     85                modelMatrix:mat4.create(),
     86                shaderName:"colored",
     87        };
     88       
     89        return node;
     90
    15491} //}}}
    15592function recalculateModelMatrix(node) { //{{{
     
    177114} //}}}
    178115function rgb(value, min, max) { //{{{
     116                                       
     117        colorbar=colorbars["rainbow"];
     118
    179119        value = clamp(value, min + 1.0, max);
    180120        var use_log_scale = false;
     
    347287        }
    348288} //}}}
    349 function draw(gl,canvas,node) { //{{{
    350         window.requestAnimationFrame(function(time) {draw(gl,canvas,node)});
    351         gl.viewport(0, 0, canvas.width, canvas.height);
    352         gl.clear(gl.COLOR_BUFFER_BIT);
     289function draw(gl,options,canvas,node) { //{{{
     290
     291        // Set clear color to black, fully opaque
     292        var backgroundcolor=new RGBColor(options.getfieldvalue('backgroundcolor','white'));
     293        if(backgroundcolor.ok){
     294                gl.clearColor(backgroundcolor.r/255.0, backgroundcolor.g/255.0, backgroundcolor.b/255.0, 1.0);
     295        }
     296        else throw Error(sprintf("s%s%s\n","initWebGL error message: cound not find out background color for curent canvas ",canvas));
     297       
     298        // Clear the color as well as the depth buffer.
     299        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
     300
     301        window.requestAnimationFrame(function(time) {draw(gl,options,canvas,node)});
    353302        updateCameraMatrix(canvas);
    354        
    355303        drawSceneGraphNode(gl, canvas, node);
     304
    356305} //}}}
    357306//}}}
  • issm/trunk-jpl/src/wrappers/javascript/Makefile.am

    r19716 r19739  
    7777                                         ../ElementConnectivity/ElementConnectivity.cpp
    7878
    79 IssmModule_CXXFLAGS= -fPIC -D_DO_NOT_LOAD_GLOBALS_ --memory-init-file 0 $(AM_CXXFLAGS) $(CXXFLAGS) $(CXXOPTFLAGS) $(COPTFLAGS) -s EXPORTED_FUNCTIONS="['_TriMeshModule','_NodeConnectivityModule','_ElementConnectivityModule']"  -s DISABLE_EXCEPTION_CATCHING=0
     79IssmModule_CXXFLAGS= -fPIC -D_DO_NOT_LOAD_GLOBALS_ --memory-init-file 0 $(AM_CXXFLAGS) $(CXXFLAGS) $(CXXOPTFLAGS) $(COPTFLAGS) -s EXPORTED_FUNCTIONS="['_TriMeshModule','_NodeConnectivityModule','_ElementConnectivityModule']"  -s DISABLE_EXCEPTION_CATCHING=0 -s ALLOW_MEMORY_GROWTH=1
    8080IssmModule_LDADD = ${deps} $(TRIANGLELIB)
    8181#}}}
Note: See TracChangeset for help on using the changeset viewer.