Browse Source

added vertexCount(), completed triCount()

Nicolas Cannasse 10 năm trước cách đây
mục cha
commit
6c87cc77b2

+ 9 - 0
h3d/prim/BigPrimitive.hx

@@ -62,6 +62,15 @@ class BigPrimitive extends Primitive {
 		return Std.int(count/3);
 	}
 
+	override function vertexCount() {
+		var count = 0;
+		for( b in buffers )
+			count += b.vertices;
+		if( tmpBuf != null )
+			count += Std.int(tmpBuf.length / stride);
+		return count;
+	}
+
 	public function flush() {
 		if( tmpBuf != null ) {
 			if( tmpBuf.length > 0 && tmpIdx.length > 0 ) {

+ 9 - 2
h3d/prim/FBXModel.hx

@@ -12,19 +12,26 @@ class FBXModel extends MeshPrimitive {
 	var curMaterial : Int;
 	var groupIndexes : Array<Indexes>;
 	var viewNormals : Bool;
+	var tcount = -1;
 
 	public function new(g) {
 		this.geom = g;
 		curMaterial = -1;
 	}
 
+	override public function triCount() {
+		if( buffer != null ) return super.triCount();
+		if( tcount < 0 ) tcount = Std.int(geom.getIndexes().idx.length / 3);
+		return tcount;
+	}
+
 	public function setSkin(skin) {
 		this.skin = skin;
 		skin.primitive = this;
 	}
 
-	public function getVerticesCount() {
-		return Std.int(geom.getVertices().length / 3);
+	override function vertexCount() {
+		return triCount() * 3; // vertices are always duplicated
 	}
 
 	override function getBounds() {

+ 8 - 0
h3d/prim/HMDModel.hx

@@ -15,6 +15,14 @@ class HMDModel extends MeshPrimitive {
 		this.entry = entry;
 	}
 
+	override function triCount() {
+		return Std.int(data.indexCount / 3);
+	}
+
+	override function vertexCount() {
+		return data.vertexCount;
+	}
+
 	override function getBounds() {
 		return data.bounds;
 	}

+ 8 - 0
h3d/prim/Plan2D.hx

@@ -4,6 +4,14 @@ class Plan2D extends Primitive {
 
 	function new() {
 	}
+	
+	override function triCount() {
+		return 2;
+	}
+	
+	override function vertexCount() {
+		return 4;
+	}
 
 	override function alloc( engine : h3d.Engine ) {
 		var v = new hxd.FloatBuffer();

+ 5 - 1
h3d/prim/Polygon.hx

@@ -159,11 +159,15 @@ class Polygon extends Primitive {
 		}
 	}
 
-	public override function triCount() {
+	override function triCount() {
 		var n = super.triCount();
 		if( n != 0 )
 			return n;
 		return Std.int((idx == null ? points.length : idx.length) / 3);
 	}
 
+	override function vertexCount() {
+		return points.length;
+	}
+	
 }

+ 4 - 0
h3d/prim/Primitive.hx

@@ -9,6 +9,10 @@ class Primitive {
 		return if( indexes != null ) Std.int(indexes.count / 3) else if( buffer == null ) 0 else Std.int(buffer.totalVertices() / 3);
 	}
 
+	public function vertexCount() {
+		return 0;
+	}
+
 	public function getBounds() : h3d.col.Bounds {
 		throw "not implemented";
 		return null;

+ 8 - 0
h3d/prim/Quads.hx

@@ -12,6 +12,14 @@ class Quads extends Primitive {
 		this.uvs = uvs;
 		this.normals = normals;
 	}
+	
+	override function triCount() {
+		return pts.length * 2;
+	}
+	
+	override function vertexCount() {
+		return pts.length;
+	}
 
 	public function scale( x : Float, y : Float, z : Float ) {
 		for( p in pts ) {

+ 16 - 0
h3d/prim/RawPrimitive.hx

@@ -2,6 +2,8 @@ package h3d.prim;
 
 class RawPrimitive extends Primitive {
 
+	var vcount : Int;
+	var tcount : Int;
 	public var onContextLost : Void -> { vbuf : hxd.FloatBuffer, stride : Int, ?ibuf : hxd.IndexBuffer, ?quads : Bool };
 
 	public function new( inf : { vbuf : hxd.FloatBuffer, stride : Int, ?ibuf : hxd.IndexBuffer, ?quads : Bool }, persist = false ) {
@@ -17,8 +19,22 @@ class RawPrimitive extends Primitive {
 		if( inf.ibuf == null ) flags.push(inf.quads ? Quads : Triangles);
 		if( inf.stride < 8 ) flags.push(RawFormat);
 		buffer = h3d.Buffer.ofFloats(inf.vbuf, inf.stride, flags);
+		vcount = buffer.vertices;
+		tcount = inf.ibuf != null ? Std.int(inf.ibuf.length / 3) : inf.quads ? vcount >> 1 : Std.int(vcount/3);
 		if( inf.ibuf != null )
 			indexes = h3d.Indexes.alloc(inf.ibuf);
+		else if( indexes != null ) {
+			indexes.dispose();
+			indexes = null;
+		}
+	}
+
+	override function triCount() {
+		return tcount;
+	}
+
+	override function verterCount() {
+		return vcount;
 	}
 
 }