Преглед изворни кода

added centered(), auto add normals

ncannasse пре 6 година
родитељ
комит
2bff7a54de
1 измењених фајлова са 16 додато и 32 уклоњено
  1. 16 32
      h3d/prim/Cylinder.hx

+ 16 - 32
h3d/prim/Cylinder.hx

@@ -5,21 +5,31 @@ class Cylinder extends Quads {
 
 	var segs : Int;
 
-	public function new( segs : Int, ray = 1.0, height = 1.0 ) {
+	public function new( segs : Int, ray = 1.0, height = 1.0, centered = false ) {
 		var pts = new Array();
+		var normals = new Array();
 		var ds = Math.PI * 2 / segs;
 		this.segs = segs;
+		var z0 = centered ? -height * 0.5 : 0;
+		var z1 = centered ? -z0 : height;
 		for( s in 0...segs ) {
 			var a = s * ds;
 			var a2 = (s + 1) * ds;
 			var x = Math.cos(a) * ray, y = Math.sin(a) * ray;
 			var x2 = Math.cos(a2) * ray, y2 = Math.sin(a2) * ray;
-			pts.push(new Point(x, y, 0));
-			pts.push(new Point(x2, y2, 0));
-			pts.push(new Point(x, y, height));
-			pts.push(new Point(x2, y2, height));
+			pts.push(new Point(x, y, z0));
+			pts.push(new Point(x2, y2, z0));
+			pts.push(new Point(x, y, z1));
+			pts.push(new Point(x2, y2, z1));
+
+			var n0 = new Point(Math.cos(a), Math.sin(a), 0);
+			var n1 = new Point(Math.cos(a2), Math.sin(a2), 0);
+			normals.push(n0);
+			normals.push(n1);
+			normals.push(n0);
+			normals.push(n1);
 		}
-		super(pts);
+		super(pts,normals);
 	}
 
 	override function addUVs() {
@@ -34,30 +44,4 @@ class Cylinder extends Quads {
 		}
 	}
 
-	override public function addNormals() {
-		normals = new Array();
-		var ds = Math.PI * 2 / segs;
-		for( s in 0...segs ) {
-			//var ac = (s + 0.5) * ds;
-
-			var ac0 = (s - 0.5) * ds;
-			var ac1 = (s+ 0.5) * ds;
-
-			//var n = new Point(Math.cos(ac), Math.sin(ac), 0);
-			var n0 = new Point(Math.cos(ac0), Math.sin(ac0), 0);
-			var n1 = new Point(Math.cos(ac1), Math.sin(ac1), 0);
-			/*
-			normals.push(n);
-			normals.push(n);
-			normals.push(n);
-			normals.push(n);
-			*/
-
-			normals.push(n0);
-			normals.push(n1);
-			normals.push(n0);
-			normals.push(n1);
-		}
-	}
-
 }