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  */
 15 function ShaderManager() {
 16 
 17 }
 18 
 19 ShaderManager.vertexShader = new Object();
 20 ShaderManager.fragmentShader = new Object();
 21 ShaderManager.shader = new Array();
 22 
 23 ShaderManager.loadVertexShader = function(name, source) {
 24 	var vertexShader = ShaderManager.loadShader(gl.VERTEX_SHADER, source);
 25 	ShaderManager.vertexShader[name] = vertexShader;
 26 };
 27 
 28 ShaderManager.loadFragmentShader = function(name, source) {
 29 	var fragmentShader = ShaderManager.loadShader(gl.FRAGMENT_SHADER, source);
 30 	ShaderManager.fragmentShader[name] = fragmentShader;
 31 };
 32 
 33 ShaderManager.getVertexShader = function(name) {
 34 	if(ShaderManager.vertexShader[name] == null){
 35 		ShaderManager.loadVertexShader(name, ShaderManager.getShaderSource(name));
 36 	}
 37 	return ShaderManager.vertexShader[name];
 38 };
 39 
 40 ShaderManager.getFragmentShader = function(name) {
 41 	if(ShaderManager.fragmentShader[name] == null){
 42 		ShaderManager.loadFragmentShader(name, ShaderManager.getShaderSource(name));
 43 	}
 44 	return ShaderManager.fragmentShader[name];
 45 };
 46 
 47 ShaderManager.loadShader = function(shaderType, shaderSource) {
 48 	var shader = gl.createShader(shaderType);
 49 	if (!shader) {
 50 		return null;
 51 	}
 52 	gl.shaderSource(shader, shaderSource);
 53 	gl.compileShader(shader);
 54 
 55 	if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
 56 		alert(gl.getShaderInfoLog(shader));
 57 		return null;
 58 	}
 59 
 60 	return shader;
 61 };
 62 
 63 ShaderManager.getShaderSource = function(name){
 64 	var url = Config.shaderDir + "/" + name;
 65 	
 66 	return load_binary_resource(url);
 67 };
 68 
 69 ShaderManager.addShader = function(shader){
 70 	if(ShaderManager.getShader(shader.name) != null){
 71 		var message= "shader has already been created: " + shader.name;
 72 		Logger.error(message);
 73 		throw message;
 74 	}
 75 	
 76 	ShaderManager.shader.push(shader);
 77 };
 78 
 79 ShaderManager.getShader = function(name){
 80 	for(var i = 0; i < this.shader.length; i++){
 81 		var shader = this.shader[i];
 82 		if(shader.name == name){
 83 			return shader;
 84 		}
 85 	}
 86 	
 87 	return null;
 88 };