Search in sources :

Example 1 with SkeletalAnimationSequence

use of org.rajawali3d.animation.mesh.SkeletalAnimationSequence 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;
}
Also used : SkeletalAnimationSequence(org.rajawali3d.animation.mesh.SkeletalAnimationSequence) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SkeletalAnimationFrame(org.rajawali3d.animation.mesh.SkeletalAnimationFrame) FileNotFoundException(java.io.FileNotFoundException) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) ParsingException(org.rajawali3d.loader.ParsingException) FileNotFoundException(java.io.FileNotFoundException) StringTokenizer(java.util.StringTokenizer) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 2 with SkeletalAnimationSequence

use of org.rajawali3d.animation.mesh.SkeletalAnimationSequence 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);
}
Also used : SkeletalAnimationSequence(org.rajawali3d.animation.mesh.SkeletalAnimationSequence) SkeletalAnimationFrame(org.rajawali3d.animation.mesh.SkeletalAnimationFrame)

Example 3 with SkeletalAnimationSequence

use of org.rajawali3d.animation.mesh.SkeletalAnimationSequence 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);
    }
}
Also used : SkeletalAnimationSequence(org.rajawali3d.animation.mesh.SkeletalAnimationSequence) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) SkeletalAnimationFrame(org.rajawali3d.animation.mesh.SkeletalAnimationFrame) SkeletalAnimationChildObject3D(org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D) SkeletalAnimationMaterialPlugin(org.rajawali3d.materials.plugins.SkeletalAnimationMaterialPlugin) SkeletalAnimationObject3D(org.rajawali3d.animation.mesh.SkeletalAnimationObject3D) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) Matrix4(org.rajawali3d.math.Matrix4)

Aggregations

SkeletalAnimationFrame (org.rajawali3d.animation.mesh.SkeletalAnimationFrame)3 SkeletalAnimationSequence (org.rajawali3d.animation.mesh.SkeletalAnimationSequence)3 SkeletonJoint (org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StringTokenizer (java.util.StringTokenizer)1 SkeletalAnimationChildObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D)1 SkeletalAnimationObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationObject3D)1 ParsingException (org.rajawali3d.loader.ParsingException)1 SkeletalAnimationMaterialPlugin (org.rajawali3d.materials.plugins.SkeletalAnimationMaterialPlugin)1 Matrix4 (org.rajawali3d.math.Matrix4)1