Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

allow loading the fbx data using left handed coordinates

  • Loading branch information...
commit c821626d3f66465a417847cc636dc645cae61968 1 parent 0bdfc28
Nicolas Cannasse authored December 24, 2012
3  h3d/fbx/Geometry.hx
@@ -30,10 +30,11 @@ class Geometry {
30 30
 		return uvs;
31 31
 	}
32 32
 	
  33
+	@:access(h3d.fbx.Library.leftHand)
33 34
 	public function getGeomTranslate() {
34 35
 		for( p in lib.getParent(root, "Model").getAll("Properties70.P") )
35 36
 			if( p.props[0].toString() == "GeometricTranslation" )
36  
-				return new h3d.Point(p.props[4].toFloat(), p.props[5].toFloat(), p.props[6].toFloat());
  37
+				return new h3d.Point(p.props[4].toFloat() * (lib.leftHand ? -1 : 1), p.props[5].toFloat(), p.props[6].toFloat());
37 38
 		return null;
38 39
 	}
39 40
 
47  h3d/fbx/Library.hx
@@ -10,14 +10,27 @@ class DefaultMatrixes {
10 10
 	
11 11
 	public function new() {
12 12
 	}
  13
+
  14
+	public static inline function rightHandToLeft( m : h3d.Matrix ) {
  15
+		// if [x,y,z] is our original point and M the matrix
  16
+		// in right hand we have [x,y,z] * M = [x',y',z']
  17
+		// we need to ensure that left hand matrix convey the x axis flip,
  18
+		// in order to have [-x,y,z] * M = [-x',y',z']
  19
+		m._12 *= -1;
  20
+		m._13 *= -1;
  21
+		m._21 *= -1;
  22
+		m._31 *= -1;
  23
+		m._41 *= -1;
  24
+	}
13 25
 	
14  
-	public function toMatrix() {
  26
+	public function toMatrix(leftHand) {
15 27
 		var m = new h3d.Matrix();
16 28
 		m.identity();
17 29
 		if( scale != null ) m.scale(scale.x, scale.y, scale.z);
18 30
 		if( rotate != null ) m.rotate(rotate.x, rotate.y, rotate.z);
19 31
 		if( preRot != null ) m.rotate(preRot.x, preRot.y, preRot.z);
20  
-		if( trans != null ) m.translate(trans.x,trans.y,trans.z);
  32
+		if( trans != null ) m.translate(trans.x * (leftHand ? -1 : 1), trans.y, trans.z);
  33
+		if( leftHand ) rightHandToLeft(m);
21 34
 		return m;
22 35
 	}
23 36
 	
@@ -29,6 +42,7 @@ class Library {
29 42
 	var ids : IntHash<FbxNode>;
30 43
 	var connect : IntHash<Array<Int>>;
31 44
 	var invConnect : IntHash<Array<Int>>;
  45
+	var leftHand : Bool;
32 46
 	
33 47
 	var defaultModelMatrixes : Hash<DefaultMatrixes>;
34 48
 	
@@ -55,6 +69,27 @@ class Library {
55 69
 			init(c);
56 70
 	}
57 71
 	
  72
+	function convertPoints( a : Array<Float> ) {
  73
+		var p = 0;
  74
+		for( i in 0...Std.int(a.length / 3) ) {
  75
+			a[p] = -a[p];
  76
+			p++;
  77
+			a[p] = a[p];
  78
+			p++;
  79
+			a[p] = a[p];
  80
+			p++;
  81
+		}
  82
+	}
  83
+	
  84
+	public function leftHandConvert() {
  85
+		if( leftHand ) return;
  86
+		leftHand = true;
  87
+		for( g in root.getAll("Objects.Geometry") ) {
  88
+			convertPoints(g.get("Vertices").getFloats());
  89
+			convertPoints(g.get("LayerElementNormal.Normals").getFloats());
  90
+		}
  91
+	}
  92
+	
58 93
 	function init( n : FbxNode ) {
59 94
 		switch( n.name ) {
60 95
 		case "Connections":
@@ -156,6 +191,7 @@ class Library {
156 191
 		if( root != null ) {
157 192
 			var l = new Library();
158 193
 			l.load(root);
  194
+			if( leftHand ) l.leftHandConvert();
159 195
 			l.defaultModelMatrixes = defaultModelMatrixes;
160 196
 			return l.loadAnimation(animName);
161 197
 		}
@@ -324,6 +360,10 @@ class Library {
324 360
 							m.translate(def.trans.x, def.trans.y, def.trans.z);
325 361
 					} else
326 362
 						m.translate(ctx[tp-1], cty[tp-1], ctz[tp-1]);
  363
+
  364
+					if( leftHand )
  365
+						DefaultMatrixes.rightHandToLeft(m);
  366
+						
327 367
 					curMat = m;
328 368
 				}
329 369
 				frames[f] = curMat;
@@ -404,7 +444,7 @@ class Library {
404 444
 			o.name = model.getName();
405 445
 			var m = getDefaultMatrixes(model);
406 446
 			if( m.trans != null || m.rotate != null || m.scale != null || m.preRot != null )
407  
-				o.defaultTransform = m.toMatrix();
  447
+				o.defaultTransform = m.toMatrix(leftHand);
408 448
 			hobjects.set(model.getId(), o);
409 449
 			objects.push( { model : model, obj : o } );
410 450
 		}
@@ -497,6 +537,7 @@ class Library {
497 537
 				hskins.set(def.getId(), skin);
498 538
 			}
499 539
 			j.transPos = h3d.Matrix.L(subDef.get("Transform").getFloats());
  540
+			if( leftHand ) DefaultMatrixes.rightHandToLeft(j.transPos);
500 541
 			
501 542
 			var weights = subDef.getAll("Weights");
502 543
 			if( weights.length > 0 ) {
59  samples/fbx/Viewer.hx
@@ -27,10 +27,18 @@ class Viewer {
27 27
 	
28 28
 	var curFbx : h3d.fbx.Library;
29 29
 	var curFbxFile : String;
  30
+	var curData : String;
  31
+	
  32
+	var rightHand : Bool;
  33
+	var applySkin : Bool;
  34
+	var showBones : Bool;
30 35
 	
31 36
 	function new() {
32 37
 		time = 0;
33 38
 		view = 4;
  39
+		rightHand = false;
  40
+		applySkin = true;
  41
+		showBones = false;
34 42
 		tf = new flash.text.TextField();
35 43
 		tf.x = tf.y = 5;
36 44
 		tf.textColor = 0xFFFFFF;
@@ -43,8 +51,9 @@ class Viewer {
43 51
 	}
44 52
 	
45 53
 	function onReady() {
46  
-		flash.Lib.current.addEventListener(flash.events.Event.ENTER_FRAME, function(_) onUpdate());
  54
+		flash.Lib.current.addEventListener(flash.events.Event.ENTER_FRAME, function (_) onUpdate());
47 55
 		flash.Lib.current.stage.addEventListener(flash.events.KeyboardEvent.KEY_DOWN, function(k:flash.events.KeyboardEvent ) {
  56
+			var reload = false;
48 57
 			var c = k.keyCode;
49 58
 			if( c == K.NUMPAD_ADD )
50 59
 				view++;
@@ -59,8 +68,18 @@ class Viewer {
59 68
 				var data = FbxTree.toString(curFbx.getRoot());
60 69
 				var f = new flash.net.FileReference();
61 70
 				f.save(data, curFbxFile.substr(0, -4) + "_tree.txt");
  71
+			} else if( c == K.R ) {
  72
+				rightHand = !rightHand;
  73
+				reload = true;
  74
+			} else if( c == K.S ) {
  75
+				applySkin = !applySkin;
  76
+				reload = true;
  77
+			} else if( c == K.K ) {
  78
+				showBones = !showBones;
  79
+				reload = true;
62 80
 			}
63  
-				
  81
+			if( reload && curData != null )
  82
+				loadData(curData);
64 83
 		});
65 84
 		scene = new h3d.scene.Scene();
66 85
 		scene.addPass(new Axis());
@@ -94,16 +113,36 @@ class Viewer {
94 113
 		var f = new flash.net.FileReference();
95 114
 		f.addEventListener(flash.events.Event.COMPLETE, function(_) {
96 115
 			curFbxFile = f.name;
97  
-			curFbx = new h3d.fbx.Library();
98  
-			curFbx.loadTextFile(f.data.readUTFBytes(f.data.length));
99  
-			scene = curFbx.makeScene(textureLoader);
100  
-			scene.playAnimation(curFbx.loadAnimation());
101  
-			scene.addPass(new Axis());
  116
+			loadData(f.data.readUTFBytes(f.data.length));
102 117
 		});
103 118
 		f.addEventListener(flash.events.Event.SELECT, function(_) f.load());
104 119
 		f.browse([new flash.net.FileFilter("FBX File", "*.fbx")]);
105 120
 	}
106 121
 	
  122
+	function loadData( data : String ) {
  123
+		curFbx = new h3d.fbx.Library();
  124
+		curData = data;
  125
+		var fbx = h3d.fbx.Parser.parse(data);
  126
+		curFbx.load(fbx);
  127
+		if( !rightHand )
  128
+			curFbx.leftHandConvert();
  129
+		var time = scene == null ? 0 : (scene.currentAnimation == null ? 0 : scene.currentAnimation.time);
  130
+		scene = curFbx.makeScene(textureLoader, 3);
  131
+		if( applySkin )
  132
+			scene.playAnimation(curFbx.loadAnimation(), time );
  133
+		if( showBones )
  134
+			showBonesRec(scene);
  135
+		scene.addPass(new Axis());
  136
+	}
  137
+	
  138
+	function showBonesRec( o : h3d.scene.Object ) {
  139
+		var s = flash.Lib.as(o, h3d.scene.Skin);
  140
+		if( s != null )
  141
+			s.showJoints = true;
  142
+		for( i in 0...o.numChildren )
  143
+			showBonesRec(o.getChildAt(i));
  144
+	}
  145
+	
107 146
 	function onUpdate() {
108 147
 		if( !engine.begin() )
109 148
 			return;
@@ -121,7 +160,7 @@ class Viewer {
121 160
 			camera.target.set(0, 0, height);
122 161
 		case 2:
123 162
 			var K = Math.sqrt(2);
124  
-			camera.pos.set(dist, 0, height);
  163
+			camera.pos.set(rightHand ? -dist : dist, 0, height);
125 164
 			camera.up.set(0, 0, 1);
126 165
 			camera.target.set(0, 0, height);
127 166
 		case 3:
@@ -137,10 +176,10 @@ class Viewer {
137 176
 		default:
138 177
 			view = 0;
139 178
 		}
  179
+		camera.rightHanded = rightHand;
140 180
 		if( !pause )
141 181
 			time++;
142  
-		tf.text = ""+engine.fps;
143  
-		camera.rightHanded = true;
  182
+		tf.text = (camera.rightHanded ? "R " : "")+engine.fps;
144 183
 		engine.render(scene);
145 184
 	}
146 185
 	

0 notes on commit c821626

Please sign in to comment.
Something went wrong with that request. Please try again.