webgl-tools

changeset 7:b215a8d38818

fixed a bug in sanelg. can't remember what it was
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Aug 2011 00:08:40 +0300
parents 7b3ed284e4bb
children 056da157f21f
files sanegl.js
diffstat 1 files changed, 76 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/sanegl.js	Sat Jun 18 05:29:39 2011 +0300
     1.2 +++ b/sanegl.js	Thu Aug 18 00:08:40 2011 +0300
     1.3 @@ -34,14 +34,15 @@
     1.4  const GL_MAX_VERTS = 512;
     1.5  
     1.6  var gl_prim = 0;
     1.7 -var gl_vbuf = null, gl_nbuf = null, gl_cbuf = null, gl_tbuf = null;
     1.8 -var gl_vertex, gl_normal, gl_color, gl_texcoord;
     1.9 +var gl_vbuf = null, gl_nbuf = null, gl_cbuf = null, gl_tbuf = null, gl_abuf = null;
    1.10 +var gl_vertex, gl_normal, gl_color, gl_texcoord, gl_attrib;
    1.11  var gl_nverts, gl_vert_calls;
    1.12  var gl_curr_normal = new Float32Array(3);
    1.13  var gl_curr_color = new Float32Array(4);
    1.14  var gl_curr_texcoord = new Float32Array(2);
    1.15 -var gl_vloc, gl_nloc, gl_cloc, gl_tloc;
    1.16 -var gl_use_normal, gl_use_color, gl_use_texcoord;
    1.17 +var gl_curr_attrib = new Float32Array(4);
    1.18 +var gl_vloc, gl_nloc, gl_cloc, gl_tloc, gl_aloc;
    1.19 +var gl_use_normal, gl_use_color, gl_use_texcoord, gl_use_attrib;
    1.20  
    1.21  function glMatrixMode(mode)
    1.22  {
    1.23 @@ -184,11 +185,17 @@
    1.24  
    1.25  	loc = gl.getUniformLocation(prog, "normmat");
    1.26  	if(loc != -1) {
    1.27 -		var normmat = new Float32Array(16);
    1.28 -		m4_copy(normmat, gl_mat[GL_MODELVIEW][mvtop]);
    1.29 -		normmat[3] = normmat[7] = normmat[11] = normmat[12] = normmat[13] = normmat[14] = 0.0;
    1.30 -		normmat[15] = 1.0;
    1.31 -		gl.uniformMatrix4fv(loc, false, normmat);
    1.32 +		var normmat = new Float32Array(9);
    1.33 +		normmat[0] = gl_mat[GL_MODELVIEW][mvtop][0];
    1.34 +		normmat[1] = gl_mat[GL_MODELVIEW][mvtop][1];
    1.35 +		normmat[2] = gl_mat[GL_MODELVIEW][mvtop][2];
    1.36 +		normmat[3] = gl_mat[GL_MODELVIEW][mvtop][4];
    1.37 +		normmat[4] = gl_mat[GL_MODELVIEW][mvtop][5];
    1.38 +		normmat[5] = gl_mat[GL_MODELVIEW][mvtop][6];
    1.39 +		normmat[6] = gl_mat[GL_MODELVIEW][mvtop][8];
    1.40 +		normmat[7] = gl_mat[GL_MODELVIEW][mvtop][9];
    1.41 +		normmat[8] = gl_mat[GL_MODELVIEW][mvtop][10];
    1.42 +		gl.uniformMatrix3fv(loc, false, normmat);
    1.43  	}
    1.44  }
    1.45  
    1.46 @@ -198,6 +205,7 @@
    1.47  	gl_normal = new Float32Array(GL_MAX_VERTS * 3);
    1.48  	gl_color = new Float32Array(GL_MAX_VERTS * 4);
    1.49  	gl_texcoord = new Float32Array(GL_MAX_VERTS * 4);
    1.50 +	gl_attrib = new Float32Array(GL_MAX_VERTS * 4);
    1.51  
    1.52  	if(gl_vbuf == null) {
    1.53  		gl_vbuf = gl.createBuffer();
    1.54 @@ -219,6 +227,11 @@
    1.55  		gl.bindBuffer(gl.ARRAY_BUFFER, gl_tbuf);
    1.56  		gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
    1.57  		logmsg("texcoord buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
    1.58 +
    1.59 +		gl_abuf = gl.createBuffer();
    1.60 +		gl.bindBuffer(gl.ARRAY_BUFFER, gl_abuf);
    1.61 +		gl.bufferData(gl.ARRAY_BUFFER, GL_MAX_VERTS * 4 * Float32Array.BYTES_PER_ELEMENT, gl.STREAM_DRAW);
    1.62 +		logmsg("custom attrib buffer size: " + gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) + "\n");
    1.63  	}
    1.64  
    1.65  	gl_prim = prim;
    1.66 @@ -244,6 +257,8 @@
    1.67  	gl_use_normal = gl_nloc != -1;
    1.68  	gl_use_color = gl_cloc != -1;
    1.69  	gl_use_texcoord = gl_tloc != -1;
    1.70 +
    1.71 +	gl_aloc = -1;
    1.72  }
    1.73  
    1.74  function glEnd()
    1.75 @@ -301,6 +316,13 @@
    1.76  		gl.enableVertexAttribArray(gl_tloc);
    1.77  	}
    1.78  
    1.79 +	if(gl_aloc != -1) {
    1.80 +		gl.bindBuffer(gl.ARRAY_BUFFER, gl_abuf);
    1.81 +		gl.bufferSubData(gl.ARRAY_BUFFER, 0, gl_attrib);
    1.82 +		gl.vertexAttribPointer(gl_aloc, 4, gl.FLOAT, false, 0, 0);
    1.83 +		gl.enableVertexAttribArray(gl_aloc);
    1.84 +	}
    1.85 +
    1.86  	gl.drawArrays(gles_prim, 0, gl_nverts);
    1.87  
    1.88  	gl.disableVertexAttribArray(gl_vloc);
    1.89 @@ -313,6 +335,9 @@
    1.90  	if(gl_use_texcoord) {
    1.91  		gl.disableVertexAttribArray(gl_tloc);
    1.92  	}
    1.93 +	if(gl_aloc != -1) {
    1.94 +		gl.disableVertexAttribArray(gl_aloc);
    1.95 +	}
    1.96  }
    1.97  
    1.98  
    1.99 @@ -332,6 +357,9 @@
   1.100  		var n = nidx - 9;
   1.101  
   1.102  		for(i=0; i<4; i++) {
   1.103 +			if(gl_aloc != -1) {
   1.104 +				gl_attrib[vidx] = gl_attrib[v];
   1.105 +			}
   1.106  			if(gl_use_color) {
   1.107  				gl_color[vidx] = gl_color[v];
   1.108  			}
   1.109 @@ -347,6 +375,9 @@
   1.110  		v += 4;
   1.111  		n += 3;
   1.112  		for(i=0; i<4; i++) {
   1.113 +			if(gl_aloc != -1) {
   1.114 +				gl_attrib[vidx] = gl_attrib[v];
   1.115 +			}
   1.116  			if(gl_use_color) {
   1.117  				gl_color[vidx] = gl_color[v];
   1.118  			}
   1.119 @@ -384,7 +415,15 @@
   1.120  	if(gl_use_texcoord) {
   1.121  		gl_texcoord[vidx] = gl_curr_texcoord[0];
   1.122  		gl_texcoord[vidx + 1] = gl_curr_texcoord[1];
   1.123 -		gl_texcoord[vidx + 2] = gl_texcoord[vidx + 3] = 0.0;
   1.124 +		gl_texcoord[vidx + 2] = 0.0;
   1.125 +		gl_texcoord[vidx + 3] = 1.0;
   1.126 +	}
   1.127 +
   1.128 +	if(gl_aloc != -1) {
   1.129 +		gl_attrib[vidx] = gl_curr_attrib[0];
   1.130 +		gl_attrib[vidx + 1] = gl_curr_attrib[1];
   1.131 +		gl_attrib[vidx + 2] = gl_curr_attrib[2];
   1.132 +		gl_attrib[vidx + 3] = gl_curr_attrib[3];
   1.133  	}
   1.134  
   1.135  	gl_vert_calls++;
   1.136 @@ -438,3 +477,30 @@
   1.137  	gl_curr_texcoord[0] = s;
   1.138  	gl_curr_texcoord[1] = t;
   1.139  }
   1.140 +
   1.141 +function glVertexAttrib2f(loc, x, y)
   1.142 +{
   1.143 +	gl_aloc = loc;
   1.144 +	gl_curr_attrib[0] = x;
   1.145 +	gl_curr_attrib[1] = y;
   1.146 +	gl_curr_attrib[2] = 0.0;
   1.147 +	gl_curr_attrib[3] = 1.0;
   1.148 +}
   1.149 +
   1.150 +function glVertexAttrib3f(loc, x, y, z)
   1.151 +{
   1.152 +	gl_aloc = loc;
   1.153 +	gl_curr_attrib[0] = x;
   1.154 +	gl_curr_attrib[1] = y;
   1.155 +	gl_curr_attrib[2] = z;
   1.156 +	gl_curr_attrib[3] = 1.0;
   1.157 +}
   1.158 +
   1.159 +function glVertexAttrib4f(loc, x, y, z, w)
   1.160 +{
   1.161 +	gl_aloc = loc;
   1.162 +	gl_curr_attrib[0] = x;
   1.163 +	gl_curr_attrib[1] = y;
   1.164 +	gl_curr_attrib[2] = z;
   1.165 +	gl_curr_attrib[3] = w;
   1.166 +}