Using jit.submatrix in Javascript
Oct 17 2011 | 3:09 pm
I am trying to use the matrixcalc() method on a jit.submatrix inside a Javascript and it seems not to behave as I expect.
The goal of this little project is to take text and convert it into a texture for further use (rotation/translation/scaling) in OpenGL, all using procedural Javascript coding.
Here's the code in question
var matrix = new JitterObject("jit.matrix"), render = new JitterObject("jit.gl.render", matrix.name), text2d = new JitterObject("jit.gl.text2d", matrix.name), findbounds = new JitterObject("jit.findbounds"), submatrix = new JitterObject("jit.submatrix"); // Prepare up OpenGL context (jit.gl.render and jit.matrix) render.erase_color = [0, 0, 0, 1]; render.blend_enable = 1; matrix.dim = [500, 28]; // Set up text (jit.gl.text2d) text2d.color = [1, 1, 1, 1]; text2d.font("Chalkduster"); text2d.size(14); text2d.position = render.screentoworld(0, 14); text2d.text("Testing a text"); // Render the text render.erase(); render.drawswap(); // Let jit.findbounds do its stuff findbounds.min = [.5, .5, .5, .5]; findbounds.max = [1, 1, 1, 1]; findbounds.matrixcalc(matrix); // findbounds.boundmin and findbounds.boundmax now have the dimensions of the text // Set up submatrix and process main matrix submatrix.offset = [findbounds.boundmin[0], findbounds.boundmin[1]]; submatrix.dim = [findbounds.boundmax[0] - findbounds.boundmin[0], findbounds.boundmax[1] - indbounds.boundmin[1] ]; // Variables texMatrix and textureID are declared globally texMatrix = new JitterObject("jit.matrix", 4, "char", submatrix.dim[0], submatrix.dim[1]); submatrix.matrixcalc(matrix, texMatrix); // Is this working? textureID = texMatrix.name + "Tex"; outlet(0, "render", "texture", textureID, "jit_matrix", texMatrix.name);
This takes some inspiration from Vanille Béchamel's example of measuring the start and end of rendered text inside Javascript. I am trying to extend that to create a submatrix that is just the area of the original matrix containing that text so that I can use it as a texture. The point of *that* exercise being that rendering text has been significantly slowing down my redraw rate in 3D scenes using several texts, and using textures in OpenGL land is supposed to give better performance.
Any suggestions?