1 
  2 /**
  3  * potree.js 
  4  * http://potree.org
  5  *
  6  * Copyright 2012, Markus Sch�tz
  7  * Licensed under the GPL Version 2 or later.
  8  * - http://potree.org/wp/?page_id=7
  9  * - http://www.gnu.org/licenses/gpl-3.0.html
 10  *
 11  */
 12 
 13 /**
 14  * @class
 15  */
 16 function Sphere(name, parent){
 17 	SceneNode.call(this, name, parent);
 18 	var tesselation = 24;
 19 	this._mesh = null;
 20 	this.tesselation = tesselation;
 21 }
 22 
 23 Sphere.prototype = new SceneNode(inheriting);
 24 
 25 Object.defineProperty(Sphere.prototype, "mesh", {
 26 	get: function(){
 27 		if(this._mesh  == null){
 28 			this._mesh  = new Mesh("sphere");
 29 			var subMesh = new SubMesh(this._mesh);
 30 			this._mesh .addSubMesh(subMesh);
 31 			
 32 			
 33 			var vertices = [];
 34 			var normals = [];
 35 			var texCoords = [];
 36 			var indices = [];
 37 			
 38 			var angleStep = Math.PI / this.tesselation ;
 39 			var steps = (2*Math.PI) / angleStep;
 40 			var slices = this.tesselation;
 41 			
 42 			var i = 0;
 43 			for(var u = 0; u <= slices; u++){
 44 				for(var step = 0; step < steps; step++){
 45 					var angle = step*angleStep;
 46 					
 47 					var lala = Math.cos((Math.PI * u) / slices);
 48 					var x = Math.cos(angle)*Math.sqrt(1-lala*lala);
 49 					var y = Math.sin(angle)*Math.sqrt(1-lala*lala);
 50 					var z = lala;
 51 					
 52 					vertices.push(x);
 53 					vertices.push(y);
 54 					vertices.push(z);
 55 
 56 					normals.push(x);
 57 					normals.push(y);
 58 					normals.push(z);
 59 					
 60 					texCoords.push((x/2) + 0.5);
 61 					texCoords.push((y/2) * 0.5);
 62 //					texCoords.push(0);
 63 //					texCoords.push(0);
 64 					
 65 					if(i >= steps){
 66 						indices.push(i-steps);
 67 						indices.push(i);
 68 						if(step == steps-1){
 69 							indices.push(i+1-steps);
 70 						}else{
 71 							indices.push(i+1);
 72 						}
 73 						
 74 						if(step == steps-1){
 75 							var index = Math.max(0, i+1-steps-steps);
 76 							indices.push(index);
 77 						}else{
 78 							indices.push(i-steps+1);
 79 						}
 80 						indices.push(i-steps);
 81 						if(step == steps-1){
 82 							indices.push(i+1-steps);
 83 						}else{
 84 							indices.push(i+1);
 85 						}
 86 					}
 87 					
 88 					i++;
 89 				}
 90 			}
 91 			
 92 			subMesh.setVertexBufferData("POSITION", new Float32Array(vertices));
 93 			subMesh.setVertexBufferData("NORMAL", new Float32Array(normals));
 94 			subMesh.setVertexBufferData("TEXCOORD_0", new Float32Array(texCoords));
 95 			subMesh.setIndexBufferData(new Uint16Array(indices));
 96 			
 97 			var material = MaterialManager.getMaterial("default");
 98 			this._mesh.setMaterial(material);
 99 		}
100 		
101 		return this._mesh;
102 	}
103 });
104 
105 Sphere.prototype.render = function(renderQueue, camera){
106 	this.mesh.render(this, renderQueue, camera);
107 };