use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame in project Rajawali by Rajawali.
the class LoaderMD5Anim method parse.
public LoaderMD5Anim parse() throws ParsingException {
super.parse();
BufferedReader buffer = null;
if (mFile == null) {
InputStream fileIn = mResources.openRawResource(mResourceId);
buffer = new BufferedReader(new InputStreamReader(fileIn));
} else {
try {
buffer = new BufferedReader(new FileReader(mFile));
} catch (FileNotFoundException e) {
RajLog.e("[" + getClass().getCanonicalName() + "] Could not find file.");
e.printStackTrace();
}
}
mSequence = new SkeletalAnimationSequence(mAnimationName);
SkeletalAnimationFrame[] frames = null;
String line;
try {
while ((line = buffer.readLine()) != null) {
line = line.replace("\t", " ");
StringTokenizer parts = new StringTokenizer(line, " ");
int numTokens = parts.countTokens();
if (numTokens == 0)
continue;
String type = parts.nextToken();
if (type.equalsIgnoreCase(MD5_VERSION)) {
} else if (type.equalsIgnoreCase(COMMAND_LINE)) {
} else if (type.equalsIgnoreCase(NUM_JOINTS)) {
mNumJoints = Integer.parseInt(parts.nextToken());
mJoints = new SkeletonJoint[mNumJoints];
} else if (type.equalsIgnoreCase(NUM_FRAMES)) {
mSequence.setNumFrames(Integer.parseInt(parts.nextToken()));
frames = new SkeletalAnimationFrame[mSequence.getNumFrames()];
} else if (type.equalsIgnoreCase(FRAME_RATE)) {
mSequence.setFrameRate(Integer.parseInt(parts.nextToken()));
} else if (type.equalsIgnoreCase(NUM_ANIMATED_COMPONENTS)) {
mNumAnimatedComponents = Integer.parseInt(parts.nextToken());
} else if (type.equalsIgnoreCase(HIERARCHY)) {
parseHierarchy(buffer);
} else if (type.equalsIgnoreCase(BOUNDS)) {
parseBounds(frames, buffer);
} else if (type.equalsIgnoreCase(FRAME)) {
parseFrame(frames, Integer.parseInt(parts.nextToken()), buffer);
} else if (type.equalsIgnoreCase(BASEFRAME)) {
mBaseFrame = new SkeletonJoint[mNumJoints];
parseBaseFrame(buffer);
}
}
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
mSequence.setFrames(frames);
return this;
}
use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame in project Rajawali by Rajawali.
the class BlockSkeletonAnimation method parseBlock.
public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
// Lookup name
mLookupName = dis.readVarString();
// Number of animation poses
mNumFrames = dis.readUnsignedShort();
// skip block properties
dis.readProperties(null);
SkeletalAnimationFrame[] frames = new SkeletalAnimationFrame[mNumFrames];
double[] frameDurations = new double[mNumFrames];
for (int i = 0; i < mNumFrames; i++) {
long poseAddr = dis.readUnsignedInt();
int duration = dis.readUnsignedShort();
// TODO: can animation frames be shared between animations? Clone?
SkeletalAnimationFrame frame = lookup(blockHeader, poseAddr);
frame.setFrameIndex(i);
frameDurations[i] = duration;
frames[i] = frame;
}
// skip user properties
dis.readProperties(null);
mSkelAnim = new SkeletalAnimationSequence(mLookupName);
mSkelAnim.setFrameData(frameDurations);
mSkelAnim.setFrames(frames);
}
use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame in project Rajawali by Rajawali.
the class BlockAnimator method buildSkeleton.
// apply joint hierarchy to joint pose frames
private void buildSkeleton(BlockHeader blockHeader, long skelAddr) throws ParsingException {
SkeletonJoint[] joints = lookupSkeleton(blockHeader, skelAddr);
SkeletalAnimationSequence[] skelAnims = new SkeletalAnimationSequence[mAnimSet.length];
for (int i = 0; i < mAnimSet.length; i++) skelAnims[i] = (SkeletalAnimationSequence) mAnimSet[i];
Matrix4 scratch1 = new Matrix4();
Matrix4 scratch2 = new Matrix4();
for (SkeletalAnimationSequence skelSeq : skelAnims) {
for (SkeletalAnimationFrame frame : skelSeq.getFrames()) {
SkeletonJoint[] poses = frame.getSkeleton().getJoints();
// apply parent transforms
for (int i = 0; i < poses.length; i++) {
// matrix and index already set, need parent & other attribs
poses[i].setParentIndex(joints[i].getParentIndex());
if (// has parent joint
poses[i].getParentIndex() >= 0) {
SkeletonJoint parentPose = poses[poses[i].getParentIndex()];
scratch1.setAll(parentPose.getMatrix()).multiply(scratch2.setAll(poses[i].getMatrix()));
poses[i].setMatrix(scratch1.getDoubleValues());
} else
scratch1.setAll(poses[i].getMatrix());
// assign pos + rot from final matrix
scratch1.getTranslation(poses[i].getPosition());
poses[i].getOrientation().fromMatrix(scratch1);
poses[i].getOrientation().computeW();
}
}
}
for (int i = 0; i < mTargets.length; i++) {
SkeletalAnimationObject3D obj = (SkeletalAnimationObject3D) mTargets[i];
// assigns INVBP, builds BP, sets joints
obj.setJointsWithInverseBindPoseMatrices(joints);
for (int j = 0; j < obj.getNumChildren(); j++) {
SkeletalAnimationChildObject3D child = (SkeletalAnimationChildObject3D) obj.getChildAt(j);
SkeletalAnimationMaterialPlugin plugin = new SkeletalAnimationMaterialPlugin(child.getNumJoints(), child.getMaxBoneWeightsPerVertex());
child.getMaterial().addPlugin(plugin);
}
obj.setAnimationSequences(skelAnims);
obj.setAnimationSequence(mActive);
if (mAutoPlay)
obj.play(true);
}
}
use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame in project Rajawali by Rajawali.
the class BlockSkeletonPose method parseBlock.
public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
// Lookup name
mLookupName = dis.readVarString();
// Number of transforms
mNumTransforms = dis.readUnsignedShort();
// skip block properties
dis.readProperties(null);
SkeletonJoint[] poses = new SkeletonJoint[mNumTransforms];
// parse transformations; same order as joints
for (int i = 0; i < mNumTransforms; i++) {
SkeletonJoint pose = new SkeletonJoint();
if (dis.readBoolean()) {
// keep raw matrix for poses, extract pos + quat later in BlockAnimator
dis.readMatrix3D(transformMatrix, blockHeader.globalPrecisionMatrix, false);
pose.setMatrix(transformMatrix.getDoubleValues());
}
pose.setIndex(i);
poses[i] = pose;
}
// skip user properties
dis.readProperties(null);
mPose = new SkeletalAnimationFrame();
mPose.getSkeleton().setJoints(poses);
mPose.setName(mLookupName);
}
use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame in project Rajawali by Rajawali.
the class LoaderMD5Anim method parseFrame.
private void parseFrame(SkeletalAnimationFrame[] frames, int frameIndex, BufferedReader buffer) {
try {
String line;
SkeletalAnimationFrame frame = frames[frameIndex];
frame.setFrameIndex(frameIndex);
Skeleton skeleton = frame.getSkeleton();
SkeletonJoint[] joints = new SkeletonJoint[mNumJoints];
float[] frameData = new float[mNumAnimatedComponents];
int index = 0;
while ((line = buffer.readLine()) != null) {
line = line.replace("\t", " ");
StringTokenizer parts = new StringTokenizer(line, " ");
if (line.indexOf('}') > -1) {
skeleton.setJoints(joints);
buildFrameSkeleton(frameData, skeleton);
return;
}
while (parts.hasMoreTokens()) {
frameData[index++] = Float.parseFloat(parts.nextToken());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations