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  * @author Markus Sch�tz
 15  */
 16 MeshType = {
 17 	TRIANGLES : 0,
 18 	LINES: 1,
 19 	POINTS: 2
 20 };
 21 
 22 /**
 23  * 
 24  * @param name
 25  * @class
 26  * @author Markus Sch�tz
 27  */
 28 function Mesh(name){
 29 	this.name = name;
 30 	this.subMeshes = new Array();
 31 	this.setType(MeshType.TRIANGLES);
 32 	
 33 	// depending on MeshType, this can be either lineWidth or pointSize
 34 	this.primitiveSize = 1.0;
 35 }
 36 
 37 Mesh.prototype.setType = function(type){
 38 	this.type = type;
 39 	if(type == MeshType.TRIANGLES){
 40 		this.glType = gl.TRIANGLES;
 41 	}else if(type == MeshType.LINES){
 42 		this.glType = gl.LINES;
 43 	}else if(type == MeshType.POINTS){
 44 		this.glType = gl.POINTS;
 45 	}else{
 46 		throw "unknown mesh type: " + type + ". use one of the MeshType members.";
 47 	}
 48 };
 49 
 50 Mesh.prototype.render = function(meshNode, renderQueue, camera){
 51 	for(var i = 0; i < this.subMeshes.length; i++){
 52 		var subMesh = this.subMeshes[i];
 53 		subMesh.render(meshNode, renderQueue, camera);
 54 	}
 55 };
 56 
 57 Mesh.prototype.addSubMesh = function(subMesh){
 58 	this.subMeshes.push(subMesh);
 59 };
 60 
 61 Mesh.prototype.setMaterial = function(material){
 62 	for(var i = 0; i < this.subMeshes.length; i++){
 63 		var subMesh = this.subMeshes[i];
 64 		subMesh.setMaterial(material);
 65 	}
 66 };
 67 
 68 /**
 69  * 
 70  * @param mesh
 71  * @class
 72  * @author Markus Sch�tz
 73  */
 74 function SubMesh(mesh){
 75 	this.mesh = mesh;
 76 	// beinhaltet alle vertex buffer des meshes
 77 	this.vbos = new Object();
 78 	// index buffer
 79 	this.ibo = null;
 80 	this.vertexCount = 0;
 81 	this.material = null;
 82 	this.indices = null;
 83 }
 84 
 85 SubMesh.prototype.setMaterial = function(material){
 86 	this.material = material;
 87 };
 88 
 89 
 90 SubMesh.prototype.setVertexBufferData = function(name, data){
 91 	// wenn vertex buffer noch nicht vorhanden -> neuen erstellen
 92 	if(this.vbos[name] == null){
 93 		this.vbos[name] = gl.createBuffer();
 94 	}
 95 
 96 	gl.bindBuffer(gl.ARRAY_BUFFER, this.vbos[name]);
 97 	gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
 98 };
 99 
100 SubMesh.prototype.setIndexBufferData = function(data){
101 	if(this.ibo == null){
102 		this.ibo = gl.createBuffer();
103 	}
104 	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.ibo);
105 	gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, gl.STATIC_DRAW);
106 	this.indices = data;
107 };
108 
109 SubMesh.prototype.setVertexCount = function(vertexCount){
110 	this.vertexCount = vertexCount;
111 };
112 
113 SubMesh.prototype.render = function(meshNode, renderQueue, camera){
114 	if(renderQueue.preferredMaterial != null){
115 		renderQueue.preferredMaterial.renderSubMesh(this, meshNode, renderQueue, camera);
116 	}else{
117 		this.material.renderSubMesh(this, meshNode, renderQueue, camera);
118 	}
119 };
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138