public Node findByName(String targetName) { Node result = null; if (this.name.equals(targetName)) { result = this; } else { for (Node child : children) { result = child.findByName(targetName); if (result != null) { break; } } } return result; }
public int getAnimationFrames() { int numFrames = this.transformations.size(); for (Node child : children) { int childFrame = child.getAnimationFrames(); numFrames = Math.max(numFrames, childFrame); } return numFrames; }
public static Matrix4f getParentTransforms(Node node, int framePos) { if (node == null) { return new Matrix4f(); } else { Matrix4f parentTransform = new Matrix4f(getParentTransforms(node.getParent(), framePos)); List<Matrix4f> transformations = node.getTransformations(); Matrix4f nodeTransform; int transfSize = transformations.size(); if (framePos < transfSize) { nodeTransform = transformations.get(framePos); } else if ( transfSize > 0 ) { nodeTransform = transformations.get(transfSize - 1); } else { nodeTransform = new Matrix4f(); } return parentTransform.mul(nodeTransform); } }
private static List<AnimatedFrame> buildAnimationFrames(List<Bone> boneList, Node rootNode, Matrix4f rootTransformation) { int numFrames = rootNode.getAnimationFrames(); List<AnimatedFrame> frameList = new ArrayList<>(); for (int i = 0; i < numFrames; i++) { AnimatedFrame frame = new AnimatedFrame(); frameList.add(frame); int numBones = boneList.size(); for (int j = 0; j < numBones; j++) { Bone bone = boneList.get(j); Node node = rootNode.findByName(bone.getBoneName()); Matrix4f boneMatrix = Node.getParentTransforms(node, i); boneMatrix.mul(bone.getOffsetMatrix()); boneMatrix = new Matrix4f(rootTransformation).mul(boneMatrix); frame.setMatrix(j, boneMatrix); } } return frameList; }
private static Node processNodesHierarchy(AINode aiNode, Node parentNode) { String nodeName = aiNode.mName().dataString(); Node node = new Node(nodeName, parentNode); int numChildren = aiNode.mNumChildren(); PointerBuffer aiChildren = aiNode.mChildren(); for (int i = 0; i < numChildren; i++) { AINode aiChildNode = AINode.create(aiChildren.get(i)); Node childNode = processNodesHierarchy(aiChildNode, node); node.addChild(childNode); } return node; }
private static void buildTransFormationMatrices(AINodeAnim aiNodeAnim, Node node) { int numFrames = aiNodeAnim.mNumPositionKeys(); AIVectorKey.Buffer positionKeys = aiNodeAnim.mPositionKeys(); AIVectorKey.Buffer scalingKeys = aiNodeAnim.mScalingKeys(); AIQuatKey.Buffer rotationKeys = aiNodeAnim.mRotationKeys(); for (int i = 0; i < numFrames; i++) { AIVectorKey aiVecKey = positionKeys.get(i); AIVector3D vec = aiVecKey.mValue(); Matrix4f transfMat = new Matrix4f().translate(vec.x(), vec.y(), vec.z()); AIQuatKey quatKey = rotationKeys.get(i); AIQuaternion aiQuat = quatKey.mValue(); Quaternionf quat = new Quaternionf(aiQuat.x(), aiQuat.y(), aiQuat.z(), aiQuat.w()); transfMat.rotate(quat); if (i < aiNodeAnim.mNumScalingKeys()) { aiVecKey = scalingKeys.get(i); vec = aiVecKey.mValue(); transfMat.scale(vec.x(), vec.y(), vec.z()); } node.addTransformation(transfMat); } }
private static List<AnimatedFrame> buildAnimationFrames(List<Bone> boneList, Node rootNode, Matrix4f rootTransformation) { int numFrames = rootNode.getAnimationFrames(); List<AnimatedFrame> frameList = new ArrayList<>(); for (int i = 0; i < numFrames; i++) { AnimatedFrame frame = new AnimatedFrame(); frameList.add(frame); int numBones = boneList.size(); for (int j = 0; j < numBones; j++) { Bone bone = boneList.get(j); Node node = rootNode.findByName(bone.getBoneName()); Matrix4f boneMatrix = Node.getParentTransforms(node, i); boneMatrix.mul(bone.getOffsetMatrix()); boneMatrix = new Matrix4f(rootTransformation).mul(boneMatrix); frame.setMatrix(j, boneMatrix); } } return frameList; }
private static Node processNodesHierarchy(AINode aiNode, Node parentNode) { String nodeName = aiNode.mName().dataString(); Node node = new Node(nodeName, parentNode); int numChildren = aiNode.mNumChildren(); PointerBuffer aiChildren = aiNode.mChildren(); for (int i = 0; i < numChildren; i++) { AINode aiChildNode = AINode.create(aiChildren.get(i)); Node childNode = processNodesHierarchy(aiChildNode, node); node.addChild(childNode); } return node; }
private static void buildTransFormationMatrices(AINodeAnim aiNodeAnim, Node node) { int numFrames = aiNodeAnim.mNumPositionKeys(); AIVectorKey.Buffer positionKeys = aiNodeAnim.mPositionKeys(); AIVectorKey.Buffer scalingKeys = aiNodeAnim.mScalingKeys(); AIQuatKey.Buffer rotationKeys = aiNodeAnim.mRotationKeys(); for (int i = 0; i < numFrames; i++) { AIVectorKey aiVecKey = positionKeys.get(i); AIVector3D vec = aiVecKey.mValue(); Matrix4f transfMat = new Matrix4f().translate(vec.x(), vec.y(), vec.z()); AIQuatKey quatKey = rotationKeys.get(i); AIQuaternion aiQuat = quatKey.mValue(); Quaternionf quat = new Quaternionf(aiQuat.x(), aiQuat.y(), aiQuat.z(), aiQuat.w()); transfMat.rotate(quat); if (i < aiNodeAnim.mNumScalingKeys()) { aiVecKey = scalingKeys.get(i); vec = aiVecKey.mValue(); transfMat.scale(vec.x(), vec.y(), vec.z()); } node.addTransformation(transfMat); } }
public static Matrix4f getParentTransforms(Node node, int framePos) { if (node == null) { return new Matrix4f(); } else { Matrix4f parentTransform = new Matrix4f(getParentTransforms(node.getParent(), framePos)); List<Matrix4f> transformations = node.getTransformations(); Matrix4f nodeTransform; int transfSize = transformations.size(); if (framePos < transfSize) { nodeTransform = transformations.get(framePos); } else if ( transfSize > 0 ) { nodeTransform = transformations.get(transfSize - 1); } else { nodeTransform = new Matrix4f(); } return parentTransform.mul(nodeTransform); } }
public Node findByName(String targetName) { Node result = null; if (this.name.equals(targetName)) { result = this; } else { for (Node child : children) { result = child.findByName(targetName); if (result != null) { break; } } } return result; }
public int getAnimationFrames() { int numFrames = this.transformations.size(); for (Node child : children) { int childFrame = child.getAnimationFrames(); numFrames = Math.max(numFrames, childFrame); } return numFrames; }
private static Map<String, Animation> processAnimations(AIScene aiScene, List<Bone> boneList, Node rootNode, Matrix4f rootTransformation) { Map<String, Animation> animations = new HashMap<>(); // Process all animations int numAnimations = aiScene.mNumAnimations(); PointerBuffer aiAnimations = aiScene.mAnimations(); for (int i = 0; i < numAnimations; i++) { AIAnimation aiAnimation = AIAnimation.create(aiAnimations.get(i)); // Calculate transformation matrices for each node int numChanels = aiAnimation.mNumChannels(); PointerBuffer aiChannels = aiAnimation.mChannels(); for (int j = 0; j < numChanels; j++) { AINodeAnim aiNodeAnim = AINodeAnim.create(aiChannels.get(j)); String nodeName = aiNodeAnim.mNodeName().dataString(); Node node = rootNode.findByName(nodeName); buildTransFormationMatrices(aiNodeAnim, node); } List<AnimatedFrame> frames = buildAnimationFrames(boneList, rootNode, rootTransformation); Animation animation = new Animation(aiAnimation.mName().dataString(), frames, aiAnimation.mDuration()); animations.put(animation.getName(), animation); } return animations; }
private static Map<String, Animation> processAnimations(AIScene aiScene, List<Bone> boneList, Node rootNode, Matrix4f rootTransformation) { Map<String, Animation> animations = new HashMap<>(); // Process all animations int numAnimations = aiScene.mNumAnimations(); PointerBuffer aiAnimations = aiScene.mAnimations(); for (int i = 0; i < numAnimations; i++) { AIAnimation aiAnimation = AIAnimation.create(aiAnimations.get(i)); // Calculate transformation matrices for each node int numChanels = aiAnimation.mNumChannels(); PointerBuffer aiChannels = aiAnimation.mChannels(); for (int j = 0; j < numChanels; j++) { AINodeAnim aiNodeAnim = AINodeAnim.create(aiChannels.get(j)); String nodeName = aiNodeAnim.mNodeName().dataString(); Node node = rootNode.findByName(nodeName); buildTransFormationMatrices(aiNodeAnim, node); } List<AnimatedFrame> frames = buildAnimationFrames(boneList, rootNode, rootTransformation); Animation animation = new Animation(aiAnimation.mName().dataString(), frames, aiAnimation.mDuration()); animations.put(animation.getName(), animation); } return animations; }