Search in sources :

Example 1 with SkeletalAnimationFrame

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;
}
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 SkeletalAnimationFrame

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

Example 3 with SkeletalAnimationFrame

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);
    }
}
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)

Example 4 with SkeletalAnimationFrame

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

Example 5 with SkeletalAnimationFrame

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();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) SkeletalAnimationFrame(org.rajawali3d.animation.mesh.SkeletalAnimationFrame) Skeleton(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.Skeleton) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) ParsingException(org.rajawali3d.loader.ParsingException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

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