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 MeshUtils() {
 16 
 17 }
 18 
 19 /**
 20  * box daten von:https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/sdk/demos/webkit/SpinningBox.html
 21  * 
 22  */
 23 MeshUtils.createRectangle = function() {
 24 	var mesh = new Mesh();
 25 	var subMesh = new SubMesh();
 26 	mesh.addSubMesh(subMesh);
 27 
 28 	// box
 29     //    v6----- v5
 30     //   /|      /|
 31     //  v1------v0|
 32     //  | |     | |
 33     //  | |v7---|-|v4
 34     //  |/      |/
 35     //  v2------v3
 36     //
 37     // vertex coords array
 38     var vertices = new Float32Array(
 39         [  1, 1, 1,  -1, 1, 1,  -1,-1, 1,   1,-1, 1,    // v0-v1-v2-v3 front
 40            1, 1, 1,   1,-1, 1,   1,-1,-1,   1, 1,-1,    // v0-v3-v4-v5 right
 41            1, 1, 1,   1, 1,-1,  -1, 1,-1,  -1, 1, 1,    // v0-v5-v6-v1 top
 42           -1, 1, 1,  -1, 1,-1,  -1,-1,-1,  -1,-1, 1,    // v1-v6-v7-v2 left
 43           -1,-1,-1,   1,-1,-1,   1,-1, 1,  -1,-1, 1,    // v7-v4-v3-v2 bottom
 44            1,-1,-1,  -1,-1,-1,  -1, 1,-1,   1, 1,-1 ]   // v4-v7-v6-v5 back
 45     );
 46 
 47     // normal array
 48     var normals = new Float32Array(
 49         [  0, 0, 1,   0, 0, 1,   0, 0, 1,   0, 0, 1,     // v0-v1-v2-v3 front
 50            1, 0, 0,   1, 0, 0,   1, 0, 0,   1, 0, 0,     // v0-v3-v4-v5 right
 51            0, 1, 0,   0, 1, 0,   0, 1, 0,   0, 1, 0,     // v0-v5-v6-v1 top
 52           -1, 0, 0,  -1, 0, 0,  -1, 0, 0,  -1, 0, 0,     // v1-v6-v7-v2 left
 53            0,-1, 0,   0,-1, 0,   0,-1, 0,   0,-1, 0,     // v7-v4-v3-v2 bottom
 54            0, 0,-1,   0, 0,-1,   0, 0,-1,   0, 0,-1 ]    // v4-v7-v6-v5 back
 55        );
 56 
 57 
 58     // texCoord array
 59     var texCoords = new Float32Array(
 60         [  1, 1,   0, 1,   0, 0,   1, 0,    // v0-v1-v2-v3 front
 61            0, 1,   0, 0,   1, 0,   1, 1,    // v0-v3-v4-v5 right
 62            1, 0,   1, 1,   0, 1,   0, 0,    // v0-v5-v6-v1 top
 63            1, 1,   0, 1,   0, 0,   1, 0,    // v1-v6-v7-v2 left
 64            0, 0,   1, 0,   1, 1,   0, 1,    // v7-v4-v3-v2 bottom
 65            0, 0,   1, 0,   1, 1,   0, 1 ]   // v4-v7-v6-v5 back
 66        );
 67 
 68     // index array
 69     var indices = new Uint8Array(
 70         [  0, 1, 2,   0, 2, 3,    // front
 71            4, 5, 6,   4, 6, 7,    // right
 72            8, 9,10,   8,10,11,    // top
 73           12,13,14,  12,14,15,    // left
 74           16,17,18,  16,18,19,    // bottom
 75           20,21,22,  20,22,23 ]   // back
 76       );
 77 	
 78 	subMesh.setVertexBufferData("POSITION", vertices);
 79 	subMesh.setVertexBufferData("NORMAL", normals);
 80 	subMesh.setVertexBufferData("TEXCOORD_0", texCoords);
 81 	subMesh.setIndexBufferData(indices);
 82 	
 83 	var material = MaterialManager.getMaterial("grid");
 84 	mesh.setMaterial(material);
 85 	
 86 	return mesh;
 87 };
 88 
 89 
 90 MeshUtils.createGrid = function(cellWidth, rows, columns) {
 91 	var mesh = new Mesh();
 92 	var subMesh = new SubMesh();
 93 	mesh.addSubMesh(subMesh);
 94 
 95 	
 96 	var lineCount = rows + columns + 2;
 97 	var vertices = new Float32Array(lineCount*2*3);
 98 	
 99 	{ // make column lines
100 		var x = - (cellWidth * columns / 2.0);
101 		var z = - (cellWidth * rows / 2.0);
102 		for(var i = 0; i <= columns; i++){
103 			var index = i*6;
104 			// start
105 			vertices[index+0] = x;
106 			vertices[index+1] = 0.0;
107 			vertices[index+2] = z;
108 			// end
109 			vertices[index+3] = x;
110 			vertices[index+4] = 0.0;
111 			vertices[index+5] = -z;
112 			
113 			x += cellWidth;
114 		}
115 	}
116 	
117 	{ // make row lines
118 		var x = - (cellWidth * rows / 2.0);
119 		var z = - (cellWidth * columns / 2.0);
120 		for(var i = columns+1; i <= (rows+columns+1); i++){
121 			var index = i*6;
122 			// start
123 			vertices[index+0] = x;
124 			vertices[index+1] = 0.0;
125 			vertices[index+2] = z;
126 			// end
127 			vertices[index+3] = -x;
128 			vertices[index+4] = 0.0;
129 			vertices[index+5] = z;
130 			
131 			z += cellWidth;
132 		}
133 	}
134 	
135 	subMesh.setVertexBufferData("POSITION", vertices);
136 	subMesh.setVertexCount(lineCount*2);
137 	//subMesh.setVertexCount(1+columns*2);
138 	mesh.setType(MeshType.LINES);
139 	
140 	var gridMaterial = new FlatMaterial("grid", [0.7, 0.7, 0.7, 1.0]);
141 //	var material = MaterialManager.getMaterial("grid");
142 	mesh.setMaterial(gridMaterial);
143 	
144 	return mesh;
145 };
146 
147 MeshUtils.createLine = function(start, end) {
148 	var mesh = new Mesh();
149 	var subMesh = new SubMesh();
150 	mesh.addSubMesh(subMesh);
151 
152 	var vertices = new Float32Array([
153 	                                 start[0], start[1], start[2],
154 	                                 end[0], end[1], end[2]
155 	                                 ]);
156 	
157 	subMesh.setVertexBufferData("POSITION", vertices);
158 	subMesh.setVertexCount(2);
159 	mesh.setType(MeshType.LINES);
160 	
161 	var material = ShaderManager.getShader("defaultFlat");
162 	mesh.setMaterial(material);
163 	
164 	return mesh;
165 };
166 
167 MeshUtils.createQuad = function(p1, p2, p3, p4){
168 	var mesh = new Mesh();
169 	var subMesh = new SubMesh();
170 	mesh.addSubMesh(subMesh);
171 
172 	var vertices = new Float32Array([
173 	                                 p1[0], p1[1], p1[2],
174 	                                 p2[0], p2[1], p2[2],
175 	                                 p3[0], p3[1], p3[2],
176 	                                 p1[0], p1[1], p1[2],
177 	                                 p3[0], p3[1], p3[2],
178 	                                 p4[0], p4[1], p4[2]
179 	                                 ]);
180 	
181 	subMesh.setVertexBufferData("POSITION", vertices);
182 	subMesh.setVertexCount(6);
183 	mesh.setType(MeshType.TRIANGLES);
184 	
185 	var material = ShaderManager.getShader("defaultFlat");
186 	mesh.setMaterial(material);
187 	
188 	return mesh;
189 };
190 
191 
192