1 /**
  2  * potree.js 
  3  * http://potree.org
  4  *
  5  * Copyright 2012, Markus Sch�tz
  6  * Licensed under the GPL Version 2 or later.
  7  * - http://potree.org/wp/?page_id=7
  8  * - http://www.gnu.org/licenses/gpl-3.0.html
  9  *
 10  */
 11 
 12 /**
 13  * @class
 14  * This class is meant to be subclassed for every glsl shader.
 15  * uniforms and attributes have to be specified in initUniforms and initAttributes
 16  * 
 17  */
 18 function Shader(name, vertexShader, fragmentShader){
 19 	if (arguments[0] === inheriting) return;
 20 	
 21 	if(name == null){
 22 		name = "Shader_" + Shader.count;
 23 	}
 24 	
 25 	this.vertexShaderName = null;
 26 	this.fragmentShaderName = null;
 27 	this.program = null;
 28 	this.name = name;
 29 	this.setVertexShaderName(vertexShader);
 30 	this.setFragmentShaderName(fragmentShader);
 31 	
 32 	this.relink();
 33 	this.initUniforms();
 34 	this.initAttributes();
 35 	
 36 	ShaderManager.addShader(this);
 37 	
 38 	Shader.count++;
 39 }
 40 
 41 Shader.count = 0;
 42 
 43 /**
 44  * List of possible shader attributes
 45  * 
 46  * @class
 47  */
 48 function ShaderAttribute(){}
 49 
 50 ShaderAttribute.Position = 	"aVertexPosition";
 51 ShaderAttribute.Color = 	"aVertexColor";
 52 ShaderAttribute.Normal = 	"aVertexNormal";
 53 
 54 Shader.prototype.initUniforms = function(){
 55 	this.uniforms = {};
 56 	var count = gl.getProgramParameter(this.program, gl.ACTIVE_UNIFORMS);
 57 	for(var i = 0; i < count; i++){
 58 		var info = gl.getActiveUniform(this.program, i);
 59 		var name = info.name;
 60 		this.uniforms[name] = gl.getUniformLocation(this.program, name);
 61 	}
 62 };
 63 
 64 Shader.prototype.initAttributes = function(){
 65 	this.attributes = {};
 66 	var count = gl.getProgramParameter(this.program, gl.ACTIVE_ATTRIBUTES);
 67 	for(var i = 0; i < count; i++){
 68 		var info = gl.getActiveAttrib(this.program, i);
 69 		var name = info.name;
 70 		this.attributes[name] = gl.getAttribLocation(this.program, name);
 71 	}
 72 };
 73 
 74 Shader.prototype.relink = function(){
 75 	if(this.vertexShaderName == null){
 76 		console.log("Shader.vertexShaderName is null -> Shader won't be linked");
 77 	}
 78 	if(this.fragmentShaderName == null){
 79 		console.log("Shader.fragmentShaderName is null -> Shader won't be linked");
 80 	}
 81 	
 82 	if(this.program != null){
 83 		gl.deleteProgram(program);
 84 	}
 85 	this.program = gl.createProgram();
 86 	var vertexShader = ShaderManager.getVertexShader(this.vertexShaderName);
 87 	var fragmentShader = ShaderManager.getFragmentShader(this.fragmentShaderName);
 88 	gl.attachShader(this.program, vertexShader);
 89 	gl.attachShader(this.program, fragmentShader);
 90 	
 91 	gl.linkProgram(this.program);
 92 	if (!gl.getProgramParameter(this.program, gl.LINK_STATUS)) {
 93 		console.log(gl.getProgramInfoLog(this.program));
 94 		return;
 95 	}
 96 };
 97 
 98 Shader.prototype.setVertexShaderName = function(name){
 99 	this.vertexShaderName = name;
100 };
101 
102 Shader.prototype.setFragmentShaderName = function(name){
103 	this.fragmentShaderName = name;
104 };
105 
106