From aedbc04cc0c605496fc5aa12ba074643ef16d6d9 Mon Sep 17 00:00:00 2001 From: Renanse Date: Thu, 14 Mar 2013 20:51:52 -0500 Subject: Updates to handle items attached to skeletons. --- .../model/collada/jdom/ColladaAnimUtils.java | 16 +++++++ .../model/collada/jdom/ColladaImporter.java | 7 +++ .../model/collada/jdom/ColladaNodeUtils.java | 56 ++++++++++++++++------ .../model/collada/jdom/data/DataCache.java | 10 ++++ 4 files changed, 75 insertions(+), 14 deletions(-) (limited to 'ardor3d-collada') diff --git a/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaAnimUtils.java b/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaAnimUtils.java index c8d8439..da9773f 100644 --- a/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaAnimUtils.java +++ b/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaAnimUtils.java @@ -29,6 +29,7 @@ import org.jdom.Attribute; import org.jdom.DataConversionException; import org.jdom.Element; +import com.ardor3d.extension.animation.skeletal.AttachmentPoint; import com.ardor3d.extension.animation.skeletal.Joint; import com.ardor3d.extension.animation.skeletal.Skeleton; import com.ardor3d.extension.animation.skeletal.SkeletonPose; @@ -310,6 +311,9 @@ public class ColladaAnimUtils { skPose = new SkeletonPose(ourSkeleton); _dataCache.getSkeletonPoseMapping().put(ourSkeleton, skPose); + // attach any attachment points found for the skeleton's joints + addAttachments(skPose); + // Skeleton's default to bind position, so update the global transforms. skPose.updateTransforms(); } @@ -560,6 +564,18 @@ public class ColladaAnimUtils { } } + private void addAttachments(final SkeletonPose skPose) { + final Skeleton skeleton = skPose.getSkeleton(); + for (final Joint joint : skeleton.getJoints()) { + if (_dataCache.getAttachmentPoints().containsKey(joint)) { + for (final AttachmentPoint point : _dataCache.getAttachmentPoints().get(joint)) { + point.setJointIndex(joint.getIndex()); + skPose.addPoseListener(point); + } + } + } + } + /** * Construct morph mesh(es) from the element and attach them (under a single new Node) to the given parent * Node. diff --git a/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaImporter.java b/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaImporter.java index 89208cf..0298984 100644 --- a/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaImporter.java +++ b/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaImporter.java @@ -240,6 +240,11 @@ public class ColladaImporter { colladaAnimUtils.parseLibraryAnimations(collada); } + // reattach attachments to scene + if (scene != null) { + colladaNodeUtils.reattachAttachments(scene); + } + // set our scene into storage colladaStorage.setScene(scene); @@ -399,6 +404,8 @@ public class ColladaImporter { return new Text(""); } + default: + break; } } catch (final NoSuchElementException e) { throw new ColladaException("Number of values in collada array does not match its count attribute: " diff --git a/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaNodeUtils.java b/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaNodeUtils.java index 477381f..eb5ae6f 100644 --- a/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaNodeUtils.java +++ b/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/ColladaNodeUtils.java @@ -12,10 +12,12 @@ package com.ardor3d.extension.model.collada.jdom; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; import java.util.logging.Logger; import org.jdom.Element; +import com.ardor3d.extension.animation.skeletal.AttachmentPoint; import com.ardor3d.extension.animation.skeletal.Joint; import com.ardor3d.extension.animation.skeletal.Skeleton; import com.ardor3d.extension.model.collada.jdom.data.AssetData; @@ -92,23 +94,21 @@ public class ColladaNodeUtils { } } - // build a list of joints - one list per skeleton - final List> jointCollection = Lists.newArrayList(); + // build a list of joints - one list per skeleton - and build a skeleton for each joint list. for (final JointNode jointChildNode : _dataCache.getRootJointNode().getChildren()) { final List jointList = Lists.newArrayList(); buildJointLists(jointChildNode, jointList); - jointCollection.add(jointList); - } - - // build a skeleton for each joint list. - for (final List jointList : jointCollection) { final Joint[] joints = jointList.toArray(new Joint[jointList.size()]); final Skeleton skeleton = new Skeleton(joints[0].getName() + "_skeleton", joints); - logger.fine(skeleton.getName()); + if (logger.isLoggable(Level.FINE)) { + logger.fine("skeleton created: " + skeleton.getName()); + } for (final Joint joint : jointList) { _dataCache.getJointSkeletonMapping().put(joint, skeleton); - logger.fine("- Joint " + joint.getName() + " - index: " + joint.getIndex() + " parent index: " - + joint.getParentIndex()); + if (logger.isLoggable(Level.FINE)) { + logger.fine("- Joint " + joint.getName() + " - index: " + joint.getIndex() + " parent index: " + + joint.getParentIndex()); + } } _dataCache.addSkeleton(skeleton); } @@ -242,10 +242,7 @@ public class ColladaNodeUtils { */ @SuppressWarnings("unchecked") private Node buildNode(final Element dNode, JointNode jointNode) { - NodeType nodeType = NodeType.NODE; - if (dNode.getAttribute("type") != null) { - nodeType = Enum.valueOf(NodeType.class, dNode.getAttributeValue("type")); - } + final NodeType nodeType = getNodeType(dNode); final JointNode jointChildNode; if (nodeType == NodeType.JOINT) { String name = dNode.getAttributeValue("name"); @@ -311,6 +308,11 @@ public class ColladaNodeUtils { final Node subNode = getNode(in, jointNode); if (subNode != null) { node.attachChild(subNode); + if (nodeType == NodeType.JOINT + && getNodeType(_colladaDOMUtil.findTargetWithId(in.getAttributeValue("url"))) == NodeType.NODE) { + // make attachment + createJointAttachment(jointChildNode, node, subNode); + } } } @@ -319,6 +321,10 @@ public class ColladaNodeUtils { final Node subNode = buildNode(n, jointNode); if (subNode != null) { node.attachChild(subNode); + if (nodeType == NodeType.JOINT && getNodeType(n) == NodeType.NODE) { + // make attachment + createJointAttachment(jointChildNode, node, subNode); + } } } @@ -328,6 +334,22 @@ public class ColladaNodeUtils { return node; } + protected void createJointAttachment(final JointNode jointChildNode, final Node node, final Node subNode) { + final AttachmentPoint attach = new AttachmentPoint("attach-" + node.getName(), (short) 0, subNode, + new Transform(subNode.getTransform())); + _dataCache.addAttachmentPoint(jointChildNode.getJoint(), attach); + // we will attach to scene instead. + subNode.removeFromParent(); + } + + private NodeType getNodeType(final Element dNode) { + if (dNode.getAttribute("type") != null) { + return Enum.valueOf(NodeType.class, dNode.getAttributeValue("type")); + } else { + return NodeType.NODE; + } + } + /** * Combines a list of transform elements into an Ardor3D Transform object. * @@ -375,4 +397,10 @@ public class ColladaNodeUtils { } return new Transform().fromHomogeneousMatrix(finalMat); } + + public void reattachAttachments(final Node scene) { + for (final AttachmentPoint point : _dataCache.getAttachmentPoints().values()) { + scene.attachChild(point.getAttachment()); + } + } } diff --git a/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/data/DataCache.java b/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/data/DataCache.java index 1785590..880a0cd 100644 --- a/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/data/DataCache.java +++ b/ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/data/DataCache.java @@ -19,6 +19,7 @@ import java.util.regex.Pattern; import org.jdom.Element; import org.jdom.xpath.XPath; +import com.ardor3d.extension.animation.skeletal.AttachmentPoint; import com.ardor3d.extension.animation.skeletal.Joint; import com.ardor3d.extension.animation.skeletal.Skeleton; import com.ardor3d.extension.animation.skeletal.SkeletonPose; @@ -64,6 +65,7 @@ public class DataCache { private final Map _skeletonPoseMapping; private final List _skeletons; private final List _controllers; + private final Multimap _attachmentPoints = ArrayListMultimap.create(); public DataCache() { _boundMaterials = Maps.newHashMap(); @@ -226,4 +228,12 @@ public class DataCache { public void setMeshVertMap(final Mesh geometry, final VertMap map) { _meshVertMap.put(geometry, map); } + + public void addAttachmentPoint(final Joint joint, final AttachmentPoint attach) { + _attachmentPoints.put(joint, attach); + } + + public Multimap getAttachmentPoints() { + return _attachmentPoints; + } } -- cgit v1.2.3