webgl-tools

changeset 2:9eb4c37ce415

- added set_uniform functions in glass.js - added normal matrix uniform in sanegl.js
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 16 Jun 2011 07:13:20 +0300
parents c52b17d412f3
children 56ae66e32998
files glass.js sanegl.js
diffstat 2 files changed, 91 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/glass.js	Wed Jun 15 20:45:50 2011 +0300
     1.2 +++ b/glass.js	Thu Jun 16 07:13:20 2011 +0300
     1.3 @@ -23,6 +23,8 @@
     1.4  var glass_start_time = 0;
     1.5  
     1.6  var glass_cb_disp = null;
     1.7 +var glass_cb_mouse = null;
     1.8 +var glass_cb_motion = null;
     1.9  
    1.10  function glass_init(canv_id, cons_id)
    1.11  {
    1.12 @@ -55,6 +57,21 @@
    1.13  
    1.14  function glass_start()
    1.15  {
    1.16 +	if(glass_cb_motion) {
    1.17 +		canvas.onmousemove = function(ev) {
    1.18 +			glass_cb_motion(ev.clientX, ev.clientY);
    1.19 +		}
    1.20 +	}
    1.21 +
    1.22 +	if(glass_cb_mouse) {
    1.23 +		canvas.onmousedown = function(ev) {
    1.24 +			glass_cb_mouse(ev.button, true, ev.clientX, ev.clientY);
    1.25 +		}
    1.26 +		canvas.onmouseup = function(ev) {
    1.27 +			glass_cb_mouse(ev.button, false, ev.clientX, ev.clientY);
    1.28 +		}
    1.29 +	}
    1.30 +
    1.31  	glass_start_time = get_msec();
    1.32  	glass_redraw();
    1.33  }
    1.34 @@ -64,6 +81,17 @@
    1.35  	glass_cb_disp = func;
    1.36  }
    1.37  
    1.38 +function glass_mouse_func(func)
    1.39 +{
    1.40 +	glass_cb_mouse = func;
    1.41 +}
    1.42 +
    1.43 +function glass_motion_func(func)
    1.44 +{
    1.45 +	glass_cb_motion = func;
    1.46 +}
    1.47 +
    1.48 +
    1.49  function glass_redraw()
    1.50  {
    1.51  	var msec = get_msec();
    1.52 @@ -123,17 +151,20 @@
    1.53  	xhr.send(null);
    1.54  
    1.55  	var src = xhr.responseText;
    1.56 -	return create_shader(src, type);
    1.57 +	return create_shader(src, type, name);
    1.58  }
    1.59  
    1.60 -function create_shader(src, type)
    1.61 +function create_shader(src, type, name)
    1.62  {
    1.63  	var sdr = gl.createShader(type);
    1.64  	gl.shaderSource(sdr, src);
    1.65  	gl.compileShader(sdr);
    1.66  
    1.67  	if(!gl.getShaderParameter(sdr, gl.COMPILE_STATUS)) {
    1.68 -		logmsg("failed to compile shader: " + src + ": " + gl.getShaderInfoLog(sdr));
    1.69 +		if(!name) {
    1.70 +			name = "<unnamed>";
    1.71 +		}
    1.72 +		logmsg("failed to compile shader: " + name + ": " + gl.getShaderInfoLog(sdr));
    1.73  		return null;
    1.74  	}
    1.75  	return sdr;
    1.76 @@ -166,6 +197,51 @@
    1.77  	return prog;
    1.78  }
    1.79  
    1.80 +function set_uniform1f(p, name, v)
    1.81 +{
    1.82 +	var loc = gl.getUniformLocation(p, name);
    1.83 +	if(loc != -1) {
    1.84 +		gl.useProgram(p);
    1.85 +		gl.uniform1f(loc, v);
    1.86 +	}
    1.87 +}
    1.88 +
    1.89 +function set_uniform2f(p, name, x, y)
    1.90 +{
    1.91 +	var loc = gl.getUniformLocation(p, name);
    1.92 +	if(loc != -1) {
    1.93 +		gl.useProgram(p);
    1.94 +		gl.uniform2f(loc, x, y);
    1.95 +	}
    1.96 +}
    1.97 +
    1.98 +function set_uniform3f(p, name, x, y, z)
    1.99 +{
   1.100 +	var loc = gl.getUniformLocation(p, name);
   1.101 +	if(loc != -1) {
   1.102 +		gl.useProgram(p);
   1.103 +		gl.uniform3f(loc, x, y, z);
   1.104 +	}
   1.105 +}
   1.106 +
   1.107 +function set_uniform4f(p, name, x, y, z, w)
   1.108 +{
   1.109 +	var loc = gl.getUniformLocation(p, name);
   1.110 +	if(loc != -1) {
   1.111 +		gl.useProgram(p);
   1.112 +		gl.uniform4f(loc, x, y, z, w);
   1.113 +	}
   1.114 +}
   1.115 +
   1.116 +function set_uniform_matrix4fv(p, name, mat)
   1.117 +{
   1.118 +	var loc = gl.getUniformLocation(p, name);
   1.119 +	if(loc != -1) {
   1.120 +		gl.useProgram(p);
   1.121 +		gl.uniformMatrix4fv(loc, false, mat);
   1.122 +	}
   1.123 +}
   1.124 +
   1.125  function logmsg(str)
   1.126  {
   1.127  	if(console) {
     2.1 --- a/sanegl.js	Wed Jun 15 20:45:50 2011 +0300
     2.2 +++ b/sanegl.js	Thu Jun 16 07:13:20 2011 +0300
     2.3 @@ -164,17 +164,26 @@
     2.4  
     2.5  	var loc = gl.getUniformLocation(prog, "mvmat");
     2.6  	if(loc != -1) {
     2.7 -		gl.uniformMatrix4fv(loc, gl.FALSE, gl_mat[GL_MODELVIEW][mvtop]);
     2.8 +		gl.uniformMatrix4fv(loc, false, gl_mat[GL_MODELVIEW][mvtop]);
     2.9  	}
    2.10  
    2.11  	loc = gl.getUniformLocation(prog, "projmat");
    2.12  	if(loc != -1) {
    2.13 -		gl.uniformMatrix4fv(loc, gl.FALSE, gl_mat[GL_PROJECTION][ptop]);
    2.14 +		gl.uniformMatrix4fv(loc, false, gl_mat[GL_PROJECTION][ptop]);
    2.15  	}
    2.16  
    2.17  	loc = gl.getUniformLocation(prog, "texmat");
    2.18  	if(loc != -1) {
    2.19 -		gl.uniformMatrix4fv(loc, gl.FALSE, gl_mat[GL_TEXTURE][ttop]);
    2.20 +		gl.uniformMatrix4fv(loc, false, gl_mat[GL_TEXTURE][ttop]);
    2.21 +	}
    2.22 +
    2.23 +	loc = gl.getUniformLocation(prog, "normmat");
    2.24 +	if(loc != -1) {
    2.25 +		var normmat = new Float32Array(16);
    2.26 +		m4_copy(normmat, gl_mat[GL_MODELVIEW][mvtop]);
    2.27 +		normmat[3] = normmat[7] = normmat[11] = normmat[12] = normmat[13] = normmat[14] = 0.0;
    2.28 +		normmat[15] = 1.0;
    2.29 +		gl.uniformMatrix4fv(loc, false, normmat);
    2.30  	}
    2.31  }
    2.32