Array incompatibility Javascript -
when construct square array have , pass new float32array, error, however, when pass temp float32array (and manually assign numtriangles 6), works properly... asdf logged console in both attempts, not sure why happening.
var square = [[-1,-1,],[1,-1],[-1,1],[-1,1],[1,-1],[1,1]]; var numtriangles = square.length; square = square.join(); var temp = [-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]; if (square == temp) { console.log("asdf"); } gl.bufferdata(gl.array_buffer, new float32array(square),gl.static_draw); gl.enablevertexattribarray(positionlocation); gl.vertexattribpointer(positionlocation, 2, gl.float, false, 0, 0); gl.drawarrays(gl.triangles, 0, numtriangles); //numtriangles == 6
the method join()
produces string, not array. float32array cannot take neither string or 2-dimensional array argument.
a hackish way around parse string after join() json:
var square = [[-1,-1],[1,-1],[-1,1],[-1,1],[1,-1],[1,1]], float; float = new float32array(json.parse("[" + square.join() + "]")); console.log(float);
however, has performance impacts. better way loop through array , flatten while updating typed array:
var square = [[-1,-1],[1,-1],[-1,1],[-1,1],[1,-1],[1,1]], len = square.length, float = new float32array(len * 2), = 0, t = 0; while(i < len) { float[t++] = square[i][0]; float[t++] = square[i++][1]; } console.log(float);
the latter has more code many times faster first approach.
ps: have comma in array should not there.
Comments
Post a Comment