浏览代码

slightly updated BigPrimitive API, documented

ncannasse 10 年之前
父节点
当前提交
3a50390911
共有 2 个文件被更改,包括 46 次插入34 次删除
  1. 39 26
      h3d/prim/BigPrimitive.hx
  2. 7 8
      h3d/scene/Graphics.hx

+ 39 - 26
h3d/prim/BigPrimitive.hx

@@ -1,18 +1,22 @@
 package h3d.prim;
 
 /**
-	Enable to add more than 65K triangles into a single primitive.
+	Vertex buffers are limited to 65K vertexes because of the 16-bits limitation of the index buffers.
+	BigPrimitive allows you to easily create large buffers by spliting the buffers.
 **/
 class BigPrimitive extends Primitive {
 
+	var isRaw : Bool;
 	var stride : Int;
 	var buffers : Array<Buffer>;
 	var allIndexes : Array<Indexes>;
 	var tmpBuf : hxd.FloatBuffer;
 	var tmpIdx : hxd.IndexBuffer;
 	var bounds : h3d.col.Bounds;
+	var startIndex : Int;
 
-	public function new(stride) {
+	public function new(stride, isRaw=false) {
+		this.isRaw = isRaw;
 		buffers = [];
 		allIndexes = [];
 		bounds = new h3d.col.Bounds();
@@ -20,19 +24,25 @@ class BigPrimitive extends Primitive {
 		if( stride < 3 ) throw "Minimum stride = 3";
 	}
 
+	/**
+		Call begin() before starting to add vertexes/indexes to the primitive.
+		The count value is the number of vertexes you will add, it will automatically flush() if it doesn't fit into the current buffer.
+	**/
 	public function begin(count) {
-		if( currentVerticesCount() + count >= 65535 )
+		startIndex = tmpBuf == null ? 0 : Std.int(tmpBuf.length / stride);
+		if( startIndex + count >= 65535 ) {
+			if( count >= 65535 ) throw "Too many vertices in begin()";
 			flush();
+		}
 		if( tmpBuf == null ) {
 			tmpBuf = new hxd.FloatBuffer();
 			tmpIdx = new hxd.IndexBuffer();
 		}
 	}
 
-	public function currentVerticesCount() {
-		return tmpBuf == null ? 0 : Std.int(tmpBuf.length / stride);
-	}
-
+	/**
+		This is similar to addVertexValue for X Y and Z, but will also update the bounds if you wish to have them calculated.
+	**/
 	public inline function addPoint(x, y, z) {
 		tmpBuf.push(x);
 		tmpBuf.push(y);
@@ -44,15 +54,14 @@ class BigPrimitive extends Primitive {
 		bounds.addPos(x, y, z);
 	}
 
-	public inline function addVerticeValue(v) {
+	public inline function addVertexValue(v) {
 		tmpBuf.push(v);
 	}
 
 	public inline function addIndex(i) {
-		tmpIdx.push(i);
+		tmpIdx.push(i + startIndex);
 	}
 
-
 	override function triCount() {
 		var count = 0;
 		for( i in allIndexes )
@@ -71,16 +80,21 @@ class BigPrimitive extends Primitive {
 		return count;
 	}
 
+	/**
+		Flush the current buffer.
+		It is required to call begin() after a flush()
+	**/
 	public function flush() {
 		if( tmpBuf != null ) {
 			if( tmpBuf.length > 0 && tmpIdx.length > 0 ) {
 				var b = h3d.Buffer.ofFloats(tmpBuf, stride);
-				if( stride < 8 ) b.flags.set(RawFormat);
+				if( isRaw ) b.flags.set(RawFormat);
 				buffers.push(b);
 				allIndexes.push(h3d.Indexes.alloc(tmpIdx));
 			}
 			tmpBuf = null;
 			tmpIdx = null;
+			startIndex = 0;
 		}
 	}
 
@@ -110,28 +124,27 @@ class BigPrimitive extends Primitive {
 		tmpIdx = null;
 	}
 
+	/**
+		Adds a complete object to the primitive, with custom position,scale,rotation.
+		See addSub for complete documentation.
+	**/
 	public function add( buf : hxd.FloatBuffer, idx : hxd.IndexBuffer, dx : Float = 0. , dy : Float = 0., dz : Float = 0., rotation = 0., scale = 1., stride = -1 ) {
 		return addSub(buf, idx, 0, 0, Std.int(buf.length / (stride < 0 ? this.stride : stride)), Std.int(idx.length / 3), dx, dy, dz, rotation, scale, stride);
 	}
-
+	/**
+		Adds a buffer to the primitive, with custom position,scale,rotation.
+		The buffer can have more stride than the BigPrimitive, but not less.
+		It is assumed that the buffer contains [X,Y,Z,NX,NY,NZ,U,V,R,G,B] (depending on his stride) so the different offsets are applied to the corresponding components.
+		However if the stride is 5, we assume [X,Y,Z,U,V]
+	**/
 	@:noDebug
 	public function addSub( buf : hxd.FloatBuffer, idx : hxd.IndexBuffer, startVert, startTri, nvert, triCount, dx : Float = 0. , dy : Float = 0., dz : Float = 0., rotation = 0., scale = 1., stride = -1, deltaU = 0., deltaV = 0., color = 1. ) {
-		if( tmpBuf == null ) {
-			tmpBuf = new hxd.FloatBuffer();
-			tmpIdx = new hxd.IndexBuffer();
-		}
 		if( stride < 0 ) stride = this.stride;
-		var start = Std.int(tmpBuf.length / this.stride);
-		if( start + nvert >= 65535 ) {
-			if( nvert >= 65535 ) throw "Too many vertices in buffer";
-			flush();
-			tmpBuf = new hxd.FloatBuffer();
-			tmpIdx = new hxd.IndexBuffer();
-			start = 0;
-		}
+		if( stride < this.stride ) throw "only stride >= " + this.stride+" allowed";
+		begin(nvert);
+		var start = startIndex;
 		var cr = Math.cos(rotation);
 		var sr = Math.sin(rotation);
-		if( stride < this.stride ) throw "only stride >= " + this.stride+" allowed";
 		for( i in 0...nvert ) {
 			var p = (i + startVert) * stride;
 			var x = buf[p++];
@@ -175,7 +188,7 @@ class BigPrimitive extends Primitive {
 				tmpBuf.push(tnx);
 				tmpBuf.push(tny);
 				tmpBuf.push(nz);
-				tmpBuf.push(buf[p++]);
+				tmpBuf.push(buf[p++] + deltaU);
 			case 8, 9, 10:
 				var nx = buf[p++];
 				var ny = buf[p++];

+ 7 - 8
h3d/scene/Graphics.hx

@@ -57,7 +57,6 @@ class Graphics extends Mesh {
 
 	public function lineTo( x : Float, y : Float, z : Float ) {
 		bprim.begin(4);
-		var start = bprim.currentVerticesCount();
 		var nx = x - curX;
 		var ny = y - curY;
 		var nz = z - curZ;
@@ -66,7 +65,7 @@ class Graphics extends Mesh {
 		bprim.addBounds(x, y, z);
 
 		inline function push(v) {
-			bprim.addVerticeValue(v);
+			bprim.addVertexValue(v);
 		}
 
 		inline function add(u, v) {
@@ -92,12 +91,12 @@ class Graphics extends Mesh {
 		add(1, 0);
 		add(1, 1);
 
-		bprim.addIndex(start);
-		bprim.addIndex(start + 1);
-		bprim.addIndex(start + 2);
-		bprim.addIndex(start + 2);
-		bprim.addIndex(start + 3);
-		bprim.addIndex(start + 1);
+		bprim.addIndex(0);
+		bprim.addIndex(1);
+		bprim.addIndex(2);
+		bprim.addIndex(2);
+		bprim.addIndex(3);
+		bprim.addIndex(1);
 
 		curX = x;
 		curY = y;