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 render an object without illumination
 14  * @augments Material
 15  * @author Markus Sch�tz
 16  */
 17 function FlatMaterial(name, color){
 18 	Material.call(this, name);
 19 	this.flatShader = new Shader( name, "flatShader.vs", "flatShader.fs");
 20 	
 21 	if(color != null){
 22 		this.color = color;
 23 	}else{
 24 		this.color = [1.0, 0.0, 0.0, 1.0];
 25 	}
 26 }
 27 
 28 FlatMaterial.prototype = new Material(inheriting);
 29 
 30 FlatMaterial.prototype.setColor = function(color){
 31 	this.color = color;
 32 };
 33 
 34 /**
 35  * 
 36  * 
 37  * @param object may be either a SubMesh or a PointcloudOctree
 38  * @param sceneNode
 39  * @param camera
 40  */
 41 FlatMaterial.prototype.render = function(object, sceneNode, camera){
 42 	if(object instanceof AABB){
 43 		this.renderAABB(object, sceneNode, camera);
 44 	}else if(object instanceof SubMesh){
 45 		this.renderSubMesh(object, sceneNode, camera);
 46 	}
 47 };
 48 
 49 FlatMaterial.prototype.renderAABB = function(aabb, sceneNode, camera){
 50 	var shader = this.flatShader;
 51 	
 52 	if(aabb.min == null || aabb.max == null){
 53 		return;
 54 	}
 55 	
 56 //	if(aabb.vbo == null){
 57 		aabb.updateVBO();
 58 //	}
 59 	
 60 	gl.useProgram(shader.program);
 61 	
 62 	// uniforms
 63 //	gl.uniformMatrix4fv(shader.uWorld, false, sceneNode.globalTransformation);
 64 	gl.uniformMatrix4fv(shader.uWorld, false, M4x4.I);
 65 	gl.uniformMatrix4fv(shader.uView, false, camera.viewMatrix);
 66 	gl.uniformMatrix4fv(shader.uProjection, false, camera.projectionMatrix);
 67 	gl.uniform4f(shader.uColor, this.color[0], this.color[1], this.color[2], this.color[3]);
 68 	
 69 	gl.enableVertexAttribArray(shader.aVertexPosition);
 70 	
 71 	gl.bindBuffer(gl.ARRAY_BUFFER, aabb.vbo);
 72 	gl.vertexAttribPointer(shader.aVertexPosition, 3, gl.FLOAT, false, 0, 0);
 73 	
 74 	// linien zeichnen
 75 	gl.drawArrays(gl.LINES, 0, aabb.points);
 76 	
 77 	Potree.drawCalls++;
 78 	Potree.drawnLines += 12;
 79 	
 80 };
 81 
 82 FlatMaterial.prototype.renderSubMesh = function(subMesh, meshNode, camera){
 83 	var shader = this.flatShader;
 84 	
 85 	var scene = camera.scene;
 86 	var mesh = meshNode.mesh;
 87 	gl.useProgram(shader.program);
 88 
 89 	// uniforms
 90 	gl.uniformMatrix4fv(shader.uWorld, false, meshNode.globalTransformation);
 91 	gl.uniformMatrix4fv(shader.uView, false, camera.viewMatrix);
 92 	gl.uniformMatrix4fv(shader.uProjection, false, camera.projectionMatrix);
 93 	var viewPos = camera.globalPosition;
 94 	gl.uniform3f(shader.uViewPos, viewPos[0], viewPos[1], viewPos[2]);
 95 	gl.uniform4f(shader.uColor, this.color[0], this.color[1], this.color[2], this.color[3]);
 96 	
 97 	// vertex attributes
 98 	gl.enableVertexAttribArray(shader.aVertexPosition);
 99 	gl.bindBuffer(gl.ARRAY_BUFFER, subMesh.vbos["POSITION"]);
100 	gl.vertexAttribPointer(shader.aVertexPosition, 3, gl.FLOAT, false, 0, 0);
101 	
102 	if(subMesh.vbos["TEXCOORD_0"] != null && shader.aTextureCoord != null ){
103 		gl.enableVertexAttribArray(shader.aTextureCoord);
104 		gl.bindBuffer(gl.ARRAY_BUFFER, subMesh.vbos["TEXCOORD_0"]);
105 		gl.vertexAttribPointer(shader.aTextureCoord, 2, gl.FLOAT, false, 0, 0);
106 	}
107 	
108 	if(subMesh.ibo != null){
109 		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, subMesh.ibo);
110 		gl.drawElements(mesh.glType, subMesh.indices.length, gl.UNSIGNED_BYTE, 0);
111 		Potree.drawCalls += 1;
112 	}else if(subMesh.vertexCount != null){
113 		gl.lineWidth(10.0);
114 		gl.drawArrays(mesh.glType, 0, subMesh.vertexCount);
115 		Potree.drawCalls += 1;
116 	}
117 };