Procházet zdrojové kódy

fixes for negative zeroes in hmd

ncannasse před 11 roky
rodič
revize
42435ccc2c
2 změnil soubory, kde provedl 49 přidání a 48 odebrání
  1. 27 30
      hxd/fmt/fbx/HMDOut.hx
  2. 22 18
      hxd/fmt/hmd/Writer.hx

+ 27 - 30
hxd/fmt/fbx/HMDOut.hx

@@ -193,7 +193,7 @@ class HMDOut extends BaseLibrary {
 		// write data
 		g.vertexPosition = dataOut.length;
 		for( i in 0...vbuf.length )
-			dataOut.writeFloat(vbuf[i]);
+			writeFloat(vbuf[i]);
 		g.indexPosition = dataOut.length;
 		g.indexCounts = [];
 
@@ -338,12 +338,7 @@ class HMDOut extends BaseLibrary {
 
 			var q = m.toQuaternion(true);
 			q.normalize();
-			if( q.w < 0 ) {
-				q.x *= -1;
-				q.y *= -1;
-				q.z *= -1;
-				q.w *= -1;
-			}
+			if( q.w < 0 ) q.negate();
 			p.qx = q.x;
 			p.qy = q.y;
 			p.qz = q.z;
@@ -487,24 +482,23 @@ class HMDOut extends BaseLibrary {
 		var q = new h3d.Quat();
 		q.initRotateMatrix(m);
 		q.normalize();
-		if( q.w < 0 ) {
-			q.x *= -1;
-			q.y *= -1;
-			q.z *= -1;
-			q.w *= -1;
-		}
+		if( q.w < 0 ) q.negate();
 		p.sx = 1;
 		p.sy = 1;
 		p.sz = 1;
-		p.qx = q.x;
-		p.qy = q.y;
-		p.qz = q.z;
-		p.x = m._41;
-		p.y = m._42;
-		p.z = m._43;
+		p.qx = round(q.x);
+		p.qy = round(q.y);
+		p.qz = round(q.z);
+		p.x = round(m._41);
+		p.y = round(m._42);
+		p.z = round(m._43);
 		return p;
 	}
 
+	inline function writeFloat( f : Float ) {
+		dataOut.writeFloat( f == 0 ? 0 : f ); // prevent negative zero
+	}
+
 	function makeAnimation( anim : h3d.anim.Animation ) {
 		var a = new Animation();
 		a.name = anim.name;
@@ -515,6 +509,7 @@ class HMDOut extends BaseLibrary {
 		a.objects = [];
 		a.dataPosition = dataOut.length;
 		var objects : Array<h3d.anim.LinearAnimation.LinearObject> = cast @:privateAccess anim.objects;
+		objects.sort(function(o1, o2) return Reflect.compare(o1.objectName, o2.objectName));
 		for( obj in objects ) {
 			var o = new AnimationObject();
 			o.name = obj.objectName;
@@ -525,35 +520,37 @@ class HMDOut extends BaseLibrary {
 					o.flags.set(HasRotation);
 				if( obj.hasScale )
 					o.flags.set(HasScale);
+				if( obj.frames.length == 1 )
+					o.flags.set(SinglePosition);
 				for( f in obj.frames ) {
 					if( o.flags.has(HasPosition) ) {
-						dataOut.writeFloat(f.tx);
-						dataOut.writeFloat(f.ty);
-						dataOut.writeFloat(f.tz);
+						writeFloat(f.tx);
+						writeFloat(f.ty);
+						writeFloat(f.tz);
 					}
 					if( o.flags.has(HasRotation) ) {
 						var ql = Math.sqrt(f.qx * f.qx + f.qy * f.qy + f.qz * f.qz + f.qw * f.qw);
 						if( f.qw < 0 ) ql = -ql;
-						dataOut.writeFloat(f.qx / ql);
-						dataOut.writeFloat(f.qy / ql);
-						dataOut.writeFloat(f.qz / ql);
+						writeFloat(round(f.qx / ql));
+						writeFloat(round(f.qy / ql));
+						writeFloat(round(f.qz / ql));
 					}
 					if( o.flags.has(HasScale) ) {
-						dataOut.writeFloat(f.sx);
-						dataOut.writeFloat(f.sy);
-						dataOut.writeFloat(f.sz);
+						writeFloat(f.sx);
+						writeFloat(f.sy);
+						writeFloat(f.sz);
 					}
 				}
 			}
 			if( obj.uvs != null ) {
 				o.flags.set(HasUV);
 				for( f in obj.uvs )
-					dataOut.writeFloat(f);
+					writeFloat(f);
 			}
 			if( obj.alphas != null ) {
 				o.flags.set(HasAlpha);
 				for( f in obj.alphas )
-					dataOut.writeFloat(f);
+					writeFloat(f);
 			}
 			a.objects.push(o);
 		}

+ 22 - 18
hxd/fmt/hmd/Writer.hx

@@ -22,27 +22,31 @@ class Writer {
 		out.writeString(name);
  	}
 
+	inline function writeFloat( f : Float ) {
+		out.writeFloat( f == 0 ? 0 : f ); // prevent negative zero
+	}
+
 	function writePosition( p : Position, hasScale = true ) {
-		out.writeFloat(p.x);
-		out.writeFloat(p.y);
-		out.writeFloat(p.z);
-		out.writeFloat(p.qx);
-		out.writeFloat(p.qy);
-		out.writeFloat(p.qz);
+		writeFloat(p.x);
+		writeFloat(p.y);
+		writeFloat(p.z);
+		writeFloat(p.qx);
+		writeFloat(p.qy);
+		writeFloat(p.qz);
 		if( hasScale ) {
-			out.writeFloat(p.sx);
-			out.writeFloat(p.sy);
-			out.writeFloat(p.sz);
+			writeFloat(p.sx);
+			writeFloat(p.sy);
+			writeFloat(p.sz);
 		}
 	}
 
 	function writeBounds( b : h3d.col.Bounds ) {
-		out.writeFloat(b.xMin);
-		out.writeFloat(b.yMin);
-		out.writeFloat(b.zMin);
-		out.writeFloat(b.xMax);
-		out.writeFloat(b.yMax);
-		out.writeFloat(b.zMax);
+		writeFloat(b.xMin);
+		writeFloat(b.yMin);
+		writeFloat(b.zMin);
+		writeFloat(b.xMax);
+		writeFloat(b.yMax);
+		writeFloat(b.zMax);
 	}
 
 	function writeSkin( s : Skin ) {
@@ -95,7 +99,7 @@ class Writer {
 			writeName(m.diffuseTexture);
 			out.writeByte(m.blendMode.getIndex());
 			out.writeByte(m.culling.getIndex());
-			out.writeFloat(m.killAlpha == null ? 1 : m.killAlpha);
+			writeFloat(m.killAlpha == null ? 1 : m.killAlpha);
 		}
 
 		out.writeInt32(d.models.length);
@@ -119,8 +123,8 @@ class Writer {
 		for( a in d.animations ) {
 			writeName(a.name);
 			out.writeInt32(a.frames);
-			out.writeFloat(a.sampling);
-			out.writeFloat(a.speed);
+			writeFloat(a.sampling);
+			writeFloat(a.speed);
 			out.writeByte(a.loop?1:0);
 			out.writeInt32(a.dataPosition);
 			out.writeInt32(a.objects.length);