Index: /issm/trunk-jpl/src/m/plot/plot_manager.js
===================================================================
--- /issm/trunk-jpl/src/m/plot/plot_manager.js	(revision 19738)
+++ /issm/trunk-jpl/src/m/plot/plot_manager.js	(revision 19739)
@@ -13,4 +13,13 @@
 	data=options.getfieldvalue('data');
 
+	//standard plot: initialize open Gl for each canvas: 
+	var canvas=document.getElementById(options.getfieldvalue('canvasid'));
+
+	// Initialize the GL context: 
+	var gl=initWebGL(canvas,options);
+	if(!gl){
+		throw Error("plotmodel error message: could not initialize open Gl!");
+	}
+	
 	//figure out if this is a special plot
 	if (typeof data === 'string'){
@@ -210,16 +219,6 @@
 	var datatype = dataresults[1];
 
-
-	//standard plot:
-	if (options.exist('asymsubplot')){
-		id=options.getfieldvalue('asymsubplot',i);
-		//subplot(nlines,ncols,id);
-	}
-	else{
-		//subplot(nlines,ncols,i);
-	}
-
 	//plot unit
-	plot_unit(x,y,z,elements,data2,is2d,isplanet,datatype,options);
+	plot_unit(x,y,z,elements,data2,is2d,isplanet,datatype,options,canvas,gl);
 
 	//apply all options
Index: /issm/trunk-jpl/src/m/plot/plot_unit.js
===================================================================
--- /issm/trunk-jpl/src/m/plot/plot_unit.js	(revision 19738)
+++ /issm/trunk-jpl/src/m/plot/plot_unit.js	(revision 19739)
@@ -1,3 +1,3 @@
-function plot_unit(x,y,z,elements,data,is2d,isplanet,datatype,options) {
+function plot_unit(x,y,z,elements,data,is2d,isplanet,datatype,options,canvas,gl) {
 	//PLOT_UNIT - unit plot, display data
 	//
@@ -7,19 +7,40 @@
 	//   See also: PLOTMODEL, PLOT_MANAGER
 
-	//edgecolor
-	edgecolor=options.getfieldvalue('edgecolor','none');
+	//declare variables:  {{{
+	var node;
+	var vertices = [];
+	var indices = [];
+	var colors = [];
+	var rgbcolor = [];
+	var xmin,xmax;
+	var ymin,ymax;
+	var zmin,zmax;
+	var datamin,datamax;
+	var scale;
+	//}}}
+
+	loadShaders(gl);
 	
-	/*First initialize webgl for the corresponding canvas: */
-	var canvas=document.getElementById(options.getfieldvalue('canvasid'));
+	//Initialize the buffer structure: 
+	node = Node(gl,options);
 
-	// Initialize the GL context: 
-	var gl=initWebGL(canvas,options);
+	//Compute coordinates and data range: 
+	xmin = ArrayMin(x);
+	xmax = ArrayMax(x);
+	ymin = ArrayMin(y);
+	ymax = ArrayMax(y);
+	zmin = ArrayMin(z);
+	zmax = ArrayMax(z);
+	datamin = ArrayMin(data);
+	datamax = ArrayMax(data);
 
-	// Only continue if WebGL is available and working
-	if (gl) {
-		loadShaders(gl);
-		var node=loadModel(gl,x,y,z,elements,data);
-		draw(gl,canvas,node);
-	}
+	//Compute scaling: 
+	var scale = 1 / (xmax - xmin);
+	node["scale"] = [scale, scale, scale];
+	node["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (-2 / scale)];
+	node["modelMatrix"] = recalculateModelMatrix(node);
+				
+	//some defaults:
+	colors.itemSize = 4;
 
 	switch(datatype){
@@ -46,5 +67,6 @@
 				  patch( 'Faces',[C A D],'Vertices', [x y z],'CData',data(pos),'FaceColor','flat','EdgeColor',edgecolor);*/
 			}
-			else{
+			else{ //2D triangular elements
+					
 				/*A=elements(pos,1); B=elements(pos,2); C=elements(pos,3);
 				  patch( 'Faces', [A B C], 'Vertices', [x y z],'CData', data(pos),'FaceColor','flat','EdgeColor',edgecolor);*/
@@ -70,9 +92,26 @@
 				  patch( 'Faces',[C A D],'Vertices', [x y z],'FaceVertexCData',data(:),'FaceColor','interp','EdgeColor',edgecolor);*/
 			}
-			else{
-				
-				/*A=elements(:,1); B=elements(:,2); C=elements(:,3); 
-				  patch( 'Faces', [A B C], 'Vertices', [x y z],'FaceVertexCData', data(:),'FaceColor','interp','EdgeColor',edgecolor);
-				  */
+			else{ //triangular elements
+			
+				vertices.itemSize = 3;
+				for(var i = 0; i < x.length; i++){
+					vertices[vertices.length] = x[i];
+					vertices[vertices.length] = y[i];
+					//vertices[vertices.length] = z[i];
+					vertices[vertices.length] = 0;
+
+					//handle mesh/qinterest size mismatch
+					rgbcolor = rgb(data[i], datamin, datamax);
+					colors[colors.length] = rgbcolor[0];
+					colors[colors.length] = rgbcolor[1];
+					colors[colors.length] = rgbcolor[2];
+					colors[colors.length] = 1.0;
+				}
+
+				//linearize the elements array: 
+				indices = indices.concat.apply(indices, elements); 
+				indices.itemSize = 1;
+				for(var i=0;i<indices.length;i++)indices[i]--; //matlab indices from 1, so decrement.
+
 			}
 			break;
@@ -91,3 +130,9 @@
 				throw Error(sprintf("%s%i%s\n",'case ',datatype,' not supported'));
 	}
+	
+	/*Initalize buffers: */
+	node["buffers"] = initBuffers(gl,[vertices, colors, indices]);
+	
+	/*Draw into the canvas:*/
+	draw(gl,options,canvas,node);
 }
Index: /issm/trunk-jpl/src/m/plot/plotmodel.js
===================================================================
--- /issm/trunk-jpl/src/m/plot/plotmodel.js	(revision 19738)
+++ /issm/trunk-jpl/src/m/plot/plotmodel.js	(revision 19739)
@@ -11,5 +11,4 @@
 	
 	//Get figure number and number of plots
-	figurenumber=options.figurenumber;
 	numberofplots=options.numberofplots;
 
@@ -35,8 +34,4 @@
 	if (numberofplots){
 
-		//Create figure : to be replaced by Dan's code here for the <div> segment of the html code?
-		//f=figure(figurenumber);clf;
-		//deal with visible off or on, figure position, etc...
-
 		//Go through all data plottable and close window if an error occurs
 		for (var i=0;i<numberofplots;i++){
Index: /issm/trunk-jpl/src/m/plot/webgl.js
===================================================================
--- /issm/trunk-jpl/src/m/plot/webgl.js	(revision 19738)
+++ /issm/trunk-jpl/src/m/plot/webgl.js	(revision 19739)
@@ -16,12 +16,4 @@
 		gl = null;
 	}
-
-
-	// Set clear color to black, fully opaque
-	var backgroundcolor=new RGBColor(options.getfieldvalue('backgroundcolor','white'));
-	if(backgroundcolor.ok){
-		gl.clearColor(backgroundcolor.r/255.0, backgroundcolor.g/255.0, backgroundcolor.b/255.0, 1.0);
-	}
-	else throw Error(sprintf("s%s%s\n","initWebGL error message: cound not find out background color for curent canvas ",canvas));
 
 	// Enable depth testing
@@ -51,66 +43,4 @@
 
 	return gl;
-} //}}}
-function loadModel(gl,x,y,z,elements,data){ //{{{
-	colorbar=colorbars["rainbow"];
-	model={"x":x,"y":y,"z":z,"elements":elements};	
-	sceneGraph={};
-	sceneGraph["model"] = sceneGraphNode(gl);
-	sceneGraph["model"]["shaderName"] = "colored";
-	sceneGraph["model"]["shader"] = shaders["colored"]["program"];
-	sceneGraph["model"]["useIndexBuffer"] = true;
-	
-	var meshVertices = [];
-	var meshColors = [];
-	var meshNormals = [];
-	var color = [];
-
-	var qmin = Math.min.apply(null,data);
-	var qmax = Math.max.apply(null,data);
-	var xmin = Math.min.apply(null,model["x"]);
-	var xmax = Math.max.apply(null,model["x"]);
-	var ymin = Math.min.apply(null,model["y"]);
-	var ymax = Math.max.apply(null,model["y"]);
-	var zmin = Math.min.apply(null,[0]);
-	var zmax = Math.max.apply(null,[0]);
-	
-	var scale = 1 / (xmax - xmin);
-	sceneGraph["model"]["scale"] = [scale, scale, scale];
-	sceneGraph["model"]["translation"] = [(xmin + xmax) / (-2 / scale), (ymin + ymax) / (-2 / scale), (zmin + zmax) / (-2 / scale)];
-	sceneGraph["model"]["modelMatrix"] = recalculateModelMatrix(sceneGraph["model"]);
-
-	for(var i = 0; i < model["x"].length; i++){
-		meshVertices[meshVertices.length] = model["x"][i];
-		meshVertices[meshVertices.length] = model["y"][i];
-		meshVertices[meshVertices.length] = 0.0;
-		
-		//initialize vertex normals
-		meshNormals[meshNormals.length] = 0.0;
-		meshNormals[meshNormals.length] = 0.0;
-		meshNormals[meshNormals.length] = 0.0;
-		
-		//handle mesh/qinterest size mismatch
-		color = rgb(data[i], qmin, qmax);
-		meshColors[meshColors.length] = color[0];
-		meshColors[meshColors.length] = color[1];
-		meshColors[meshColors.length] = color[2];
-		meshColors[meshColors.length] = 1.0;
-	}
-
-	var indices = [];
-	indices = indices.concat.apply(indices, model["elements"]);
-
-	for (var i = 0; i < indices.length; i += 3){
-		indices[i] -= 1;
-		indices[i + 1] -= 1;
-		indices[i + 2] -= 1;
-	}
-
-	meshVertices.itemSize = 3;
-	meshColors.itemSize = 4;
-	indices.itemSize = 1;
-
-	sceneGraph["model"]["buffers"] = initBuffers(gl,[meshVertices, meshColors, indices]);
-	return sceneGraph["model"];
 } //}}}
 function initBuffers(gl,arrays) { //{{{	
@@ -133,14 +63,16 @@
 	return bufferArray;
 } //}}}
-function sceneGraphNode(gl) { //{{{
-	//Primary object for storing common rendering information.
-	return {buffers:[],
-		shader:null,
+function Node(gl,options) { //{{{
+	
+	var node;
+
+	node= {buffers:[],
+		shader:shaders["colored"]["program"],
 		draw:null,
 		hideOcean:false,
 		level:0,
-		useIndexBuffer:false,
+		useIndexBuffer:true,
 		useOrthographic:false,
-	       	transparency:1.0,
+		transparency:1.0,
 		disableDepthTest:false, 
 		enableCullFace:false,
@@ -151,5 +83,10 @@
 		rotation:vec3.create(),
 		scale:vec3.fromValues(1, 1, 1),
-		modelMatrix:mat4.create()};
+		modelMatrix:mat4.create(),
+		shaderName:"colored",
+	};
+	
+	return node;
+
 } //}}}
 function recalculateModelMatrix(node) { //{{{
@@ -177,4 +114,7 @@
 } //}}}
 function rgb(value, min, max) { //{{{
+					
+	colorbar=colorbars["rainbow"];
+
 	value = clamp(value, min + 1.0, max);
 	var use_log_scale = false;
@@ -347,11 +287,20 @@
 	}
 } //}}}
-function draw(gl,canvas,node) { //{{{
-	window.requestAnimationFrame(function(time) {draw(gl,canvas,node)});
-	gl.viewport(0, 0, canvas.width, canvas.height);
-	gl.clear(gl.COLOR_BUFFER_BIT);
+function draw(gl,options,canvas,node) { //{{{
+
+	// Set clear color to black, fully opaque
+	var backgroundcolor=new RGBColor(options.getfieldvalue('backgroundcolor','white'));
+	if(backgroundcolor.ok){
+		gl.clearColor(backgroundcolor.r/255.0, backgroundcolor.g/255.0, backgroundcolor.b/255.0, 1.0);
+	}
+	else throw Error(sprintf("s%s%s\n","initWebGL error message: cound not find out background color for curent canvas ",canvas));
+	
+	// Clear the color as well as the depth buffer.
+	gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
+
+	window.requestAnimationFrame(function(time) {draw(gl,options,canvas,node)});
 	updateCameraMatrix(canvas);
-	
 	drawSceneGraphNode(gl, canvas, node);
+
 } //}}}
 //}}}
Index: /issm/trunk-jpl/src/wrappers/javascript/Makefile.am
===================================================================
--- /issm/trunk-jpl/src/wrappers/javascript/Makefile.am	(revision 19738)
+++ /issm/trunk-jpl/src/wrappers/javascript/Makefile.am	(revision 19739)
@@ -77,5 +77,5 @@
 					 ../ElementConnectivity/ElementConnectivity.cpp
 
-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
+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 -s ALLOW_MEMORY_GROWTH=1
 IssmModule_LDADD = ${deps} $(TRIANGLELIB) 
 #}}}
