# HG changeset patch # User John Tsiombikas # Date 1313615320 -10800 # Node ID b215a8d388183cebfe7af710f0d11be576c7c4fd # Parent 7b3ed284e4bb2b8fd006343675dae5f3c7acdb2d fixed a bug in sanelg. can't remember what it was diff -r 7b3ed284e4bb -r b215a8d38818 sanegl.js --- a/sanegl.js Sat Jun 18 05:29:39 2011 +0300 +++ b/sanegl.js Thu Aug 18 00:08:40 2011 +0300 @@ -34,14 +34,15 @@ const GL_MAX_VERTS = 512; var gl_prim = 0; -var gl_vbuf = null, gl_nbuf = null, gl_cbuf = null, gl_tbuf = null; -var gl_vertex, gl_normal, gl_color, gl_texcoord; +var gl_vbuf = null, gl_nbuf = null, gl_cbuf = null, gl_tbuf = null, gl_abuf = null; +var gl_vertex, gl_normal, gl_color, gl_texcoord, gl_attrib; var gl_nverts, gl_vert_calls; var gl_curr_normal = new Float32Array(3); var gl_curr_color = new Float32Array(4); var gl_curr_texcoord = new Float32Array(2); -var gl_vloc, gl_nloc, gl_cloc, gl_tloc; -var gl_use_normal, gl_use_color, gl_use_texcoord; +var gl_curr_attrib = new Float32Array(4); +var gl_vloc, gl_nloc, gl_cloc, gl_tloc, gl_aloc; +var gl_use_normal, gl_use_color, gl_use_texcoord, gl_use_attrib; function glMatrixMode(mode) { @@ -184,11 +185,17 @@ loc = gl.getUniformLocation(prog, "normmat"); if(loc != -1) { - var normmat = new Float32Array(16); - m4_copy(normmat, gl_mat[GL_MODELVIEW][mvtop]); - normmat[3] = normmat[7] = normmat[11] = normmat[12] = normmat[13] = normmat[14] = 0.0; - normmat[15] = 1.0; - gl.uniformMatrix4fv(loc, false, normmat); + var normmat = new Float32Array(9); + normmat[0] = gl_mat[GL_MODELVIEW][mvtop][0]; + normmat[1] = gl_mat[GL_MODELVIEW][mvtop][1]; + normmat[2] = gl_mat[GL_MODELVIEW][mvtop][2]; + normmat[3] = gl_mat[GL_MODELVIEW][mvtop][4]; + normmat[4] = gl_mat[GL_MODELVIEW][mvtop][5]; + normmat[5] = gl_mat[GL_MODELVIEW][mvtop][6]; + normmat[6] = gl_mat[GL_MODELVIEW][mvtop][8]; + normmat[7] = gl_mat[GL_MODELVIEW][mvtop][9]; + normmat[8] = gl_mat[GL_MODELVIEW][mvtop][10]; + gl.uniformMatrix3fv(loc, false, normmat); } } @@ -198,6 +205,7 @@ gl_normal = new Float32Array(GL_MAX_VERTS * 3); gl_color = new Float32Array(GL_MAX_VERTS * 4); gl_texcoord = new Float32Array(GL_MAX_VERTS * 4); + gl_attrib = new Float32Array(GL_MAX_VERTS * 4); if(gl_vbuf == null) { gl_vbuf = gl.createBuffer(); @@ -219,6 +227,11 @@ gl.bindBuffer(gl.ARRAY_BUFFER, gl_tbuf); gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW); logmsg("texcoord buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n"); + + gl_abuf = gl.createBuffer(); + gl.bindBuffer(gl.ARRAY_BUFFER, gl_abuf); + gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW); + logmsg("custom attrib buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n"); } gl_prim = prim; @@ -244,6 +257,8 @@ gl_use_normal = gl_nloc != -1; gl_use_color = gl_cloc != -1; gl_use_texcoord = gl_tloc != -1; + + gl_aloc = -1; } function glEnd() @@ -301,6 +316,13 @@ gl.enableVertexAttribArray(gl_tloc); } + if(gl_aloc != -1) { + gl.bindBuffer(gl.ARRAY_BUFFER, gl_abuf); + gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_attrib); + gl.vertexAttribPointer(gl_aloc, 4, gl.FLOAT, false, 0, 0); + gl.enableVertexAttribArray(gl_aloc); + } + gl.drawArrays(gles_prim, 0, gl_nverts); gl.disableVertexAttribArray(gl_vloc); @@ -313,6 +335,9 @@ if(gl_use_texcoord) { gl.disableVertexAttribArray(gl_tloc); } + if(gl_aloc != -1) { + gl.disableVertexAttribArray(gl_aloc); + } } @@ -332,6 +357,9 @@ var n = nidx - 9; for(i=0; i<4; i++) { + if(gl_aloc != -1) { + gl_attrib[vidx] = gl_attrib[v]; + } if(gl_use_color) { gl_color[vidx] = gl_color[v]; } @@ -347,6 +375,9 @@ v += 4; n += 3; for(i=0; i<4; i++) { + if(gl_aloc != -1) { + gl_attrib[vidx] = gl_attrib[v]; + } if(gl_use_color) { gl_color[vidx] = gl_color[v]; } @@ -384,7 +415,15 @@ if(gl_use_texcoord) { gl_texcoord[vidx] = gl_curr_texcoord[0]; gl_texcoord[vidx + 1] = gl_curr_texcoord[1]; - gl_texcoord[vidx + 2] = gl_texcoord[vidx + 3] = 0.0; + gl_texcoord[vidx + 2] = 0.0; + gl_texcoord[vidx + 3] = 1.0; + } + + if(gl_aloc != -1) { + gl_attrib[vidx] = gl_curr_attrib[0]; + gl_attrib[vidx + 1] = gl_curr_attrib[1]; + gl_attrib[vidx + 2] = gl_curr_attrib[2]; + gl_attrib[vidx + 3] = gl_curr_attrib[3]; } gl_vert_calls++; @@ -438,3 +477,30 @@ gl_curr_texcoord[0] = s; gl_curr_texcoord[1] = t; } + +function glVertexAttrib2f(loc, x, y) +{ + gl_aloc = loc; + gl_curr_attrib[0] = x; + gl_curr_attrib[1] = y; + gl_curr_attrib[2] = 0.0; + gl_curr_attrib[3] = 1.0; +} + +function glVertexAttrib3f(loc, x, y, z) +{ + gl_aloc = loc; + gl_curr_attrib[0] = x; + gl_curr_attrib[1] = y; + gl_curr_attrib[2] = z; + gl_curr_attrib[3] = 1.0; +} + +function glVertexAttrib4f(loc, x, y, z, w) +{ + gl_aloc = loc; + gl_curr_attrib[0] = x; + gl_curr_attrib[1] = y; + gl_curr_attrib[2] = z; + gl_curr_attrib[3] = w; +}