From 06f5cab5ba6d1b695f168e5ebfd032c8873cd8b8 Mon Sep 17 00:00:00 2001 From: "Kae..pl" Date: Thu, 4 Apr 2013 19:00:21 +0000 Subject: [PATCH] Bugfix: fixed an issue with skeleton loading when the armature was not a parent of an object it should animate. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10511 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../blender/animations/BoneContext.java | 7 +---- .../blender/modifiers/ArmatureModifier.java | 14 ++++++--- .../plugins/blender/objects/ObjectHelper.java | 31 +++++++++++++++++-- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/animations/BoneContext.java b/engine/src/blender/com/jme3/scene/plugins/blender/animations/BoneContext.java index 40c44ba87..b5e08721e 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/animations/BoneContext.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/animations/BoneContext.java @@ -25,8 +25,6 @@ public class BoneContext { private Structure boneStructure; /** Bone's name. */ private String boneName; - /** This variable indicates if the Y axis should be the UP axis. */ - private boolean fixUpAxis; /** The bone's armature matrix. */ private Matrix4f armatureMatrix; /** The parent context. */ @@ -93,7 +91,6 @@ public class BoneContext { ObjectHelper objectHelper = blenderContext.getHelper(ObjectHelper.class); armatureMatrix = objectHelper.getMatrix(boneStructure, "arm_mat", true); - fixUpAxis = blenderContext.getBlenderKey().isFixUpAxis(); this.computeRestMatrix(objectToArmatureMatrix); List childbase = ((Structure) boneStructure.getFieldValue("childbase")).evaluateListBase(blenderContext); for (Structure child : childbase) { @@ -112,10 +109,8 @@ public class BoneContext { private void computeRestMatrix(Matrix4f objectToArmatureMatrix) { if (parent != null) { inverseParentMatrix = parent.inverseTotalTransformation.clone(); - } else if (fixUpAxis) { - inverseParentMatrix = objectToArmatureMatrix.clone(); } else { - inverseParentMatrix = Matrix4f.IDENTITY.clone(); + inverseParentMatrix = objectToArmatureMatrix.clone(); } restMatrix = armatureMatrix.clone(); diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/modifiers/ArmatureModifier.java b/engine/src/blender/com/jme3/scene/plugins/blender/modifiers/ArmatureModifier.java index a69103f6b..80dfd4278 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/modifiers/ArmatureModifier.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/modifiers/ArmatureModifier.java @@ -94,10 +94,14 @@ import com.jme3.util.BufferUtils; bonesPoseChannels.put(pBone.getOldMemoryAddress(), poseChannel); } + Matrix4f objectToArmatureTransformation = Matrix4f.IDENTITY; ObjectHelper objectHelper = blenderContext.getHelper(ObjectHelper.class); - Matrix4f armatureObjectMatrix = objectHelper.getMatrix(armatureObject, "obmat", true); - Matrix4f inverseMeshObjectMatrix = objectHelper.getMatrix(objectStructure, "obmat", true).invertLocal(); - Matrix4f objectToArmatureTransformation = armatureObjectMatrix.multLocal(inverseMeshObjectMatrix); + + if(objectHelper.isLineage(armatureObject, objectStructure, blenderContext)) { + Matrix4f armatureObjectMatrix = objectHelper.getMatrix(armatureObject, "obmat", blenderContext.getBlenderKey().isFixUpAxis()); + Matrix4f inverseMeshObjectMatrix = objectHelper.getMatrix(objectStructure, "obmat", true).invertLocal(); + objectToArmatureTransformation = armatureObjectMatrix.multLocal(inverseMeshObjectMatrix); + } List bonebase = ((Structure) armatureStructure.getFieldValue("bonebase")).evaluateListBase(blenderContext); List bonesList = new ArrayList(); @@ -202,11 +206,11 @@ import com.jme3.util.BufferUtils; mesh.setBuffer(buffers[0]); mesh.setBuffer(buffers[1]); - VertexBuffer bindNormalBuffer = (meshContext.getBindNormalBuffer(materialIndex)); + VertexBuffer bindNormalBuffer = meshContext.getBindNormalBuffer(materialIndex); if (bindNormalBuffer != null) { mesh.setBuffer(bindNormalBuffer); } - VertexBuffer bindPoseBuffer = (meshContext.getBindPoseBuffer(materialIndex)); + VertexBuffer bindPoseBuffer = meshContext.getBindPoseBuffer(materialIndex); if (bindPoseBuffer != null) { mesh.setBuffer(bindPoseBuffer); } 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 f0511ae48..bf243aa08 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 @@ -174,9 +174,9 @@ public class ObjectHelper extends AbstractBlenderHelper { List curves = curvesHelper.toCurve(curveData, blenderContext); result = new Node(name); for (Geometry curve : curves) { - ((Node) result).attachChild(curve); + result.attachChild(curve); } - ((Node) result).setLocalTransform(t); + result.setLocalTransform(t); } break; case OBJECT_TYPE_LAMP: @@ -244,13 +244,38 @@ public class ObjectHelper extends AbstractBlenderHelper { Properties properties = this.loadProperties(objectStructure, blenderContext); // the loaded property is a group property, so we need to get each value and set it to Spatial if (result instanceof Spatial && properties != null && properties.getValue() != null) { - this.applyProperties((Spatial) result, properties); + this.applyProperties(result, properties); } } } return result; } + /** + * Method tells if the structure1 is a lineage of structure2. + * + * @param structure1 + * the first structure + * @param structure2 + * the second structure + * @return true if the first structure is a lineage of the second + * structure and false otherwise + * @throws BlenderFileException + * thrown when problems with reading the blend file occur + */ + public boolean isLineage(Structure structure1, Structure structure2, BlenderContext blenderContext) throws BlenderFileException { + Pointer pParent = (Pointer) structure2.getFieldValue("parent"); + while (pParent.isNotNull()) { + long oma = pParent.getOldMemoryAddress(); + if (structure1.getOldMemoryAddress().longValue() == oma) { + return true; + } + structure2 = blenderContext.getFileBlock(oma).getStructure(blenderContext); + pParent = (Pointer) structure2.getFieldValue("parent"); + } + return false; + } + /** * This method calculates local transformation for the object. Parentage is taken under consideration. * @param objectStructure