src/shader.js
/*
 * Load a complete WebGL program, null on error.
 * gl: WebGL context
 * p.vert: Name of vertex shader
 * p.frag: Name of fragment shader
 * p.attributes: Program attributes, separated by space, in order
 * p.uniforms: Program uniforms, separated by space
 */
Load a complete WebGL program, null on error.
Load a complete WebGL program null on error .
function loadProgram(gl, p) {
	var name = p.vert + ',' + p.frag;
	var specs = [{
		type: gl.VERTEX_SHADER,
		source: p.vert,
	}, {
		type: gl.FRAGMENT_SHADER,
		source: p.frag,
	}];
	var i;
	var program = gl.createProgram();
	for (i = 0; i < specs.length; i++) {
		var shader = loadShader(gl, specs[i]);
		if (!shader) {
			gl.deleteProgram(program);
			return null;
		}
		gl.attachShader(program, shader);
		gl.deleteShader(shader);
	}
	var attrib = p.attributes.split(' ');
	for (i = 0; i < attrib.length; i++) {
		gl.bindAttribLocation(program, i, attrib[i]);
	}
	gl.linkProgram(program);
	if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
		console.log('Errors for program: ', p.name);
		console.log(gl.getProgramInfoLog(program));
		gl.deleteProgram(program);
		return null;
	}
	var obj = {
		program: program,
		destroy: function(r) {
			if (this.program) {
				gl.deleteProgram(this.program);
			}
		},
	};
	if (p.uniforms) {
		var uniform = p.uniforms.split(' ');
		for (i = 0; i < uniform.length; i++) {
			var uname = uniform[i];
			var loc = gl.getUniformLocation(program, uname);
			if (!loc) {
				console.log('Missing uniform: ' + uname + ' (' + name + ')');
			}
			obj[uname] = loc;
		}
	}
	return obj;
}
function loadProgram ( gl , p ) { var name = p . vert + ',' + p . frag ; var specs = [ { type : gl . VERTEX_SHADER , source : p . vert , } , { type : gl . FRAGMENT_SHADER , source : p . frag , } ] ; var i ; var program = gl . createProgram ( ) ; for ( i = 0 ; i < specs . length ; i ++ ) { var shader = loadShader ( gl , specs [ i ] ) ; if ( ! shader ) { gl . deleteProgram ( program ) ; return null ; } gl . attachShader ( program , shader ) ; gl . deleteShader ( shader ) ; } var attrib = p . attributes . split ( ' ' ) ; for ( i = 0 ; i < attrib . length ; i ++ ) { gl . bindAttribLocation ( program , i , attrib [ i ] ) ; } gl . linkProgram ( program ) ; if ( ! gl . getProgramParameter ( program , gl . LINK_STATUS ) ) { console . log ( 'Errors for program: ' , p . name ) ; console . log ( gl . getProgramInfoLog ( program ) ) ; gl . deleteProgram ( program ) ; return null ; } var obj = { program : program , destroy : function ( r ) { if ( this . program ) { gl . deleteProgram ( this . program ) ; } } , } ; if ( p . uniforms ) { var uniform = p . uniforms . split ( ' ' ) ; for ( i = 0 ; i < uniform . length ; i ++ ) { var uname = uniform [ i ] ; var loc = gl . getUniformLocation ( program , uname ) ; if ( ! loc ) { console . log ( 'Missing uniform: ' + uname + ' (' + name + ')' ) ; } obj [ uname ] = loc ; } } return obj ; }
Load a complete WebGL program, null on error.
Load a complete WebGL program null on error .