The Push/Pop, Over/Under, Flow/Flow


    Jun 07 2017 | 4:32 pm
    I'm having problems sorting out my glpushmatrix and glpopmatrix dance in the posted patch and code below. I'm new to Javascript, but not new to Max. Essentially list "segments" is a list of x-axis values that are combined with a list "speed" of y axis values that combine to draw a bunch of polygons forming a shape. "Segments" will also be combined with other parameter lists to form other shapes which I would like to transform, different colour, etc.
    How can I push and pop correctly to avoid the old over/under flow/flow errors?
    Edit: Max patch copy-compressed and pasted Javascript pasted below.
    // to parse the incoming data from and 
    // create a set of gl.sketch commands that draw the paramter line
    // as a set of polygons
    
    inlets = 1
    outlets = 1
    
    var mySketch = new JitterObject("jit.gl.sketch", "TEST");
    var xArray = new Array();
    
    function reset()
    {
        mySketch.reset(); // reset things as needed especially for testing
        xArray = [];
        yArray = [];
    }
    
    function segments()
    {
        xArray = arrayfromargs(arguments); // get the arguments for "segments" as an array
        mySketch.reset();
    }
    
    function speed()
    {	
    	yArray = arrayfromargs(arguments);
    	
    	// if I push and pop this I get "ob3d_draw_end popmatrix: GL Error: Stack overflow"
    	// if I don't I get no error unless calling a second,
    	// similar function like activityLevel below
    	
    	mySketch.glpushmatrix(); 
    	mySketch.glscale([2, 1, 1]);
    	mySketch.gltranslate([0, 0, -10]);
    	mySketch.glcolor([1., 1., 1.]);
    
    	for (var i = 0; i < xArray.length - 1; i++)
    	{
    		var j = i*2;
    		var x1 = ((xArray[i] - 300) * 0.01);
    		var x2 = ((xArray[i+1] - 300) * 0.01);
    		var y1 = ((yArray[j] - 0.5) * 2.);
    		var y2 = ((yArray[j+1] - 0.5) * 2.);
    
    		mySketch.glbegin("polygon");
    		mySketch.glvertex(x1, y1);
    		mySketch.glvertex(x1, -5);
    		mySketch.glvertex(x2, -5);
    		mySketch.glvertex(x2, y2);
    		mySketch.glend;
    		//post(x1, y1, ", ", x1, " -5, ", x2, " -5, ",  x2, y2, "\n");
    	}
    	mySketch.glpopmatrix();
    }
    
    /*
    function activityLevel()
    {	
    	yArray = arrayfromargs(arguments);
    	
    	//No problems if I don't tranform at all
    	
    	//mySketch.glpushmatrix();
    	//mySketch.glscale([2, 1, 1]);
    	//mySketch.gltranslate([-1, 0, -5]);
    	//mySketch.glcolor([.5, .75, 1.]);
    	
    	for (var i = 0; i < xArray.length - 1; i++)
    	{
    		var j = i*2;
    		var x1 = ((xArray[i] - 300) * 0.01);
    		var x2 = ((xArray[i+1] - 300) * 0.01);
    		var y1 = ((yArray[j] - 0.5) * 2.);
    		var y2 = ((yArray[j+1] - 0.5) * 2.)
    		
    		mySketch.glbegin("polygon");
    		mySketch.glvertex(x1, y1);
    		mySketch.glvertex(x1, -5);
    		mySketch.glvertex(x2, -5);
    		mySketch.glvertex(x2, y2);
    		mySketch.glend;
    		
    		//post(x1, y1, ", ", x1, " -5, ", x2, " -5, ",  x2, y2, "\n");
    	}
    	//mySketch.glpopmatrix();
    }
    */

    • Jun 09 2017 | 3:37 pm
      Well, all the other examples I can find appear to do all the rendering/draw in the javascript as well, which likely keeps a correct order of events/command list items. My project is mixed between object patching and this script, which is really just to parse some data into some shapes. That is, only needs to run at initialization. So I will try changing the script to just parse and format a set of messages to an external (to the script) jit.gl.sketch object like the js_jitterspline example and see if that helps sort things out.
    • Jun 09 2017 | 6:10 pm
      yeah, i'm not really sure why this is spitting out those errors. i don't see anything wrong with your script.
      any chance i can convince you to use jit.gl.mesh @draw_mode polygon instead of jit.gl.sketch for this type of thing?
    • Jun 09 2017 | 6:14 pm
      There is a very strong chance. I'll look into it, give it a whirl. Thanks for having a look.