From c58ffe4ddc27ba867832fb26d8808c713344b3a4 Mon Sep 17 00:00:00 2001 From: "Kae..pl" Date: Thu, 22 Sep 2011 14:58:51 +0000 Subject: [PATCH] Fix a bug with animation when Y axis was UP. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8270 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../blender/AbstractBlenderHelper.java | 20 ++++++++++++++++++- .../scene/plugins/blender/BlenderLoader.java | 10 ++++++---- .../blender/animations/ArmatureHelper.java | 16 +++++++++++++++ .../blender/animations/CalculationBone.java | 2 +- .../plugins/blender/curves/CurvesHelper.java | 11 ---------- .../plugins/blender/objects/ObjectHelper.java | 18 ----------------- 6 files changed, 42 insertions(+), 35 deletions(-) diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.java index 4b30922e3..5d0209bac 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.java @@ -34,6 +34,8 @@ package com.jme3.scene.plugins.blender; import java.nio.FloatBuffer; import java.util.List; +import com.jme3.math.FastMath; +import com.jme3.math.Quaternion; import com.jme3.scene.plugins.blender.exceptions.BlenderFileException; import com.jme3.scene.plugins.blender.file.Pointer; import com.jme3.scene.plugins.blender.file.Structure; @@ -49,7 +51,11 @@ public abstract class AbstractBlenderHelper { /** The version of the blend file. */ protected final int blenderVersion; - + /** This variable indicates if the Y asxis is the UP axis or not. */ + protected boolean fixUpAxis; + /** Quaternion used to rotate data when Y is up axis. */ + protected Quaternion upAxisRotationQuaternion; + /** * This constructor parses the given blender version and stores the result. Some functionalities may differ in different blender * versions. @@ -60,6 +66,18 @@ public abstract class AbstractBlenderHelper { this.blenderVersion = Integer.parseInt(blenderVersion); } + /** + * This method sets the Y is UP axis. By default the UP axis is Z (just like in blender). + * @param fixUpAxis + * a variable that indicates if the Y asxis is the UP axis or not + */ + public void setyIsUpAxis(boolean fixUpAxis) { + this.fixUpAxis = fixUpAxis; + if(fixUpAxis) { + upAxisRotationQuaternion = new Quaternion().fromAngles(-FastMath.HALF_PI, 0, 0); + } + } + /** * This method clears the state of the helper so that it can be used for different calculations of another feature. */ diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/BlenderLoader.java b/engine/src/blender/com/jme3/scene/plugins/blender/BlenderLoader.java index 7cf159660..fd25d1357 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/BlenderLoader.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/BlenderLoader.java @@ -205,10 +205,12 @@ public class BlenderLoader extends AbstractBlenderLoader { // setting additional data to helpers if (blenderKey.isFixUpAxis()) { - ObjectHelper objectHelper = blenderContext.getHelper(ObjectHelper.class); - objectHelper.setyIsUpAxis(true); - CurvesHelper curvesHelper = blenderContext.getHelper(CurvesHelper.class); - curvesHelper.setyIsUpAxis(true); + AbstractBlenderHelper helper = blenderContext.getHelper(ObjectHelper.class); + helper.setyIsUpAxis(true); + helper = blenderContext.getHelper(CurvesHelper.class); + helper.setyIsUpAxis(true); + helper = blenderContext.getHelper(ArmatureHelper.class); + helper.setyIsUpAxis(true); } MaterialHelper materialHelper = blenderContext.getHelper(MaterialHelper.class); materialHelper.setFaceCullMode(blenderKey.getFaceCullMode()); diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/animations/ArmatureHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/animations/ArmatureHelper.java index 3893b6ab6..d760732f1 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/animations/ArmatureHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/animations/ArmatureHelper.java @@ -41,6 +41,7 @@ import java.util.logging.Logger; import com.jme3.animation.Bone; import com.jme3.animation.BoneTrack; import com.jme3.math.Matrix4f; +import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; import com.jme3.scene.plugins.blender.AbstractBlenderHelper; import com.jme3.scene.plugins.blender.BlenderContext; @@ -137,6 +138,21 @@ public class ArmatureHelper extends AbstractBlenderHelper { m.set(i, j, boneMat.get(j, i).floatValue()); } } + + if(fixUpAxis) { + Vector3f translation = m.toTranslationVector(); + Quaternion rotation = m.toRotationQuat(); + + float y = translation.y; + translation.y = translation.z; + translation.z = -y; + + rotation = upAxisRotationQuaternion.mult(rotation); + + m.setRotationQuaternion(rotation); + m.setTranslation(translation); + //TODO: what about scale ?? + } return m; } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/animations/CalculationBone.java b/engine/src/blender/com/jme3/scene/plugins/blender/animations/CalculationBone.java index 53067e685..7b4dd48b7 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/animations/CalculationBone.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/animations/CalculationBone.java @@ -113,7 +113,7 @@ public class CalculationBone extends Node { public void applyCalculatedTracks() { if(track != null) { - track.setKeyframes(track.getTimes(), translations, rotations);//TODO:scales + track.setKeyframes(track.getTimes(), translations, rotations, scales); } else { bone.setUserControl(true); bone.setUserTransforms(translations[0], rotations[0], scales[0]); diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/curves/CurvesHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/curves/CurvesHelper.java index d6f95efd7..a7dc96498 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/curves/CurvesHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/curves/CurvesHelper.java @@ -43,8 +43,6 @@ import com.jme3.util.BufferUtils; public class CurvesHelper extends AbstractBlenderHelper { private static final Logger LOGGER = Logger.getLogger(CurvesHelper.class.getName()); - /** This variable indicates if the Y asxis is the UP axis or not. */ - protected boolean fixUpAxis; /** Minimum basis U function degree for NURBS curves and surfaces. */ protected int minimumBasisUFunctionDegree = 4; /** Minimum basis V function degree for NURBS curves and surfaces. */ @@ -60,15 +58,6 @@ public class CurvesHelper extends AbstractBlenderHelper { super(blenderVersion); } - /** - * This method sets the Y is UP axis. By default the UP axis is Z (just like in blender). - * @param fixUpAxis - * a variable that indicates if the Y asxis is the UP axis or not - */ - public void setyIsUpAxis(boolean fixUpAxis) { - this.fixUpAxis = fixUpAxis; - } - /** * This method converts given curve structure into a list of geometries representing the curve. The list is used here because on object * can have several separate curves. diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/objects/ObjectHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/objects/ObjectHelper.java index 7dc764e17..0acad9fb1 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/objects/ObjectHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/objects/ObjectHelper.java @@ -42,7 +42,6 @@ import com.jme3.light.DirectionalLight; import com.jme3.light.Light; import com.jme3.light.PointLight; import com.jme3.light.SpotLight; -import com.jme3.math.FastMath; import com.jme3.math.Matrix4f; import com.jme3.math.Quaternion; import com.jme3.math.Transform; @@ -96,11 +95,6 @@ public class ObjectHelper extends AbstractBlenderHelper { invisibleTypes.add(OBJECT_TYPE_ARMATURE); } - /** This variable indicates if the Y asxis is the UP axis or not. */ - protected boolean fixUpAxis; - /** Quaternion used to rotate data when Y is up axis. */ - protected Quaternion upAxisRotationQuaternion; - /** * This constructor parses the given blender version and stores the result. Some functionalities may differ in * different blender versions. @@ -111,18 +105,6 @@ public class ObjectHelper extends AbstractBlenderHelper { super(blenderVersion); } - /** - * This method sets the Y is UP axis. By default the UP axis is Z (just like in blender). - * @param fixUpAxis - * a variable that indicates if the Y asxis is the UP axis or not - */ - public void setyIsUpAxis(boolean fixUpAxis) { - this.fixUpAxis = fixUpAxis; - if(fixUpAxis) { - upAxisRotationQuaternion = new Quaternion().fromAngles(-FastMath.HALF_PI, 0, 0); - } - } - /** * This method reads the given structure and createn an object that represents the data. * @param objectStructure