Search in sources :

Example 1 with SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint 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 SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.

the class LoaderMD5Anim method buildFrameSkeleton.

private void buildFrameSkeleton(float[] frameData, Skeleton skeleton) {
    for (int i = 0; i < mNumJoints; ++i) {
        SkeletonJoint joint = new SkeletonJoint(mBaseFrame[i]);
        SkeletonJoint jointInfo = mJoints[i];
        joint.setParentIndex(jointInfo.getParentIndex());
        int j = 0;
        int startIndex = jointInfo.getStartIndex();
        if ((jointInfo.getFlags() & 1) == 1)
            joint.getPosition().x = frameData[startIndex + j++];
        if ((jointInfo.getFlags() & 2) == 2)
            joint.getPosition().z = frameData[startIndex + j++];
        if ((jointInfo.getFlags() & 4) == 4)
            joint.getPosition().y = frameData[startIndex + j++];
        if ((jointInfo.getFlags() & 8) == 8)
            joint.getOrientation().x = frameData[startIndex + j++];
        if ((jointInfo.getFlags() & 16) == 16)
            joint.getOrientation().z = frameData[startIndex + j++];
        if ((jointInfo.getFlags() & 32) == 32)
            joint.getOrientation().y = frameData[startIndex + j++];
        joint.getOrientation().computeW();
        if (// Has a parent joint
        joint.getParentIndex() >= 0) {
            SkeletonJoint parentJoint = skeleton.getJoint(joint.getParentIndex());
            Vector3 rotPos = parentJoint.getOrientation().multiply(joint.getPosition());
            //We don't clone here because nothing will be able to use the quaternion scratch before we do
            joint.getPosition().setAll(Vector3.addAndCreate(parentJoint.getPosition(), rotPos));
            joint.getOrientation().multiply(parentJoint.getOrientation());
            joint.getOrientation().normalize();
        }
        skeleton.setJoint(i, joint);
    }
}
Also used : SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) Vector3(org.rajawali3d.math.vector.Vector3) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)

Example 3 with SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.

the class LoaderMD5Anim method parseHierarchy.

private void parseHierarchy(BufferedReader buffer) {
    try {
        String line;
        int index = 0;
        while ((line = buffer.readLine()) != null) {
            line = line.replace("\t", " ");
            StringTokenizer parts = new StringTokenizer(line, " ");
            int numTokens = parts.countTokens();
            if (line.indexOf('}') > -1)
                return;
            if (numTokens == 0)
                continue;
            SkeletonJoint joint = new SkeletonJoint();
            joint.setIndex(index);
            joint.setName(parts.nextToken());
            joint.setParentIndex(Integer.parseInt(parts.nextToken()));
            joint.setFlags(Integer.parseInt(parts.nextToken()));
            joint.setStartIndex(Integer.parseInt(parts.nextToken()));
            mJoints[index++] = joint;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) ParsingException(org.rajawali3d.loader.ParsingException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.

the class LoaderMD5Mesh method calculateNormals.

private void calculateNormals() {
    for (int i = 0; i < mNumMeshes; ++i) {
        SkeletonMeshData mesh = mMeshes[i];
        int numTriangles = mesh.numTriangles;
        mesh.indices = new int[numTriangles * 3];
        int index = 0;
        for (int j = 0; j < numTriangles; ++j) {
            int[] triangle = mesh.triangles[j];
            int index0 = triangle[0];
            int index1 = triangle[2];
            int index2 = triangle[1];
            mesh.indices[index++] = index0;
            mesh.indices[index++] = index1;
            mesh.indices[index++] = index2;
            int index03 = index0 * 3;
            int index13 = index1 * 3;
            int index23 = index2 * 3;
            Vector3 v0 = new Vector3(mesh.vertices[index03], mesh.vertices[index03 + 1], mesh.vertices[index03 + 2]);
            Vector3 v1 = new Vector3(mesh.vertices[index13], mesh.vertices[index13 + 1], mesh.vertices[index13 + 2]);
            Vector3 v2 = new Vector3(mesh.vertices[index23], mesh.vertices[index23 + 1], mesh.vertices[index23 + 2]);
            Vector3 normal = Vector3.crossAndCreate(Vector3.subtractAndCreate(v2, v0), Vector3.subtractAndCreate(v1, v0));
            normal.inverse();
            mesh.boneVertices[index0].normal.add(normal);
            mesh.boneVertices[index1].normal.add(normal);
            mesh.boneVertices[index2].normal.add(normal);
        }
        int numVertices = mesh.numVertices;
        if (mesh.normals == null)
            mesh.normals = new float[numVertices * 3];
        for (int j = 0; j < numVertices; ++j) {
            BoneVertex vert = mesh.boneVertices[j];
            Vector3 normal = vert.normal.clone();
            vert.normal.normalize();
            normal.normalize();
            int normIndex = j * 3;
            mesh.normals[normIndex] = (float) normal.x;
            mesh.normals[normIndex + 1] = (float) normal.y;
            mesh.normals[normIndex + 2] = (float) normal.z;
            vert.normal.setAll(0, 0, 0);
            // so the animated normal can be computed faster
            for (int k = 0; k < vert.numWeights; ++k) {
                BoneWeight weight = mesh.boneWeights[vert.weightIndex + k];
                SkeletonJoint joint = mJoints[weight.jointIndex];
                //We don't clone here because nothing will be able to use the quaternion scratch before we do
                vert.normal.add(Vector3.scaleAndCreate(joint.getOrientation().multiply(normal), weight.weightValue));
            }
        }
    }
}
Also used : BoneWeight(org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D.BoneWeight) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) Vector3(org.rajawali3d.math.vector.Vector3) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) BoneVertex(org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D.BoneVertex)

Example 5 with SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.

the class LoaderMD5Mesh method parse.

@Override
public LoaderMD5Mesh 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.");
            throw new ParsingException(e);
        }
    }
    String line;
    try {
        while ((line = buffer.readLine()) != null) {
            StringTokenizer parts = new StringTokenizer(line, " ");
            int numTokens = parts.countTokens();
            if (numTokens == 0)
                continue;
            String type = parts.nextToken();
            if (type.equalsIgnoreCase(MD5_VERSION)) {
                if (RajLog.isDebugEnabled())
                    RajLog.d("MD5 Version: " + parts.nextToken());
            } 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_MESHES)) {
                mNumMeshes = Integer.parseInt(parts.nextToken());
                mMeshes = new SkeletonMeshData[mNumMeshes];
            } else if (type.equalsIgnoreCase(JOINTS)) {
                parseJoints(buffer);
            } else if (type.equals(MESH)) {
                parseMesh(buffer);
            }
        }
        buffer.close();
        buildBindPose();
        buildMeshes();
        calculateNormals();
        createObjects();
    } catch (Exception tme) {
        try {
            buffer.close();
        } catch (Exception ex) {
        }
        throw new ParsingException(tme);
    } finally {
        mMeshes = null;
        mJoints = null;
        mBindPoseMatrix = null;
        mInverseBindPoseMatrix = null;
    }
    return this;
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ParsingException(org.rajawali3d.loader.ParsingException) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) ParsingException(org.rajawali3d.loader.ParsingException) FileNotFoundException(java.io.FileNotFoundException) SkeletalAnimationException(org.rajawali3d.animation.mesh.SkeletalAnimationObject3D.SkeletalAnimationException)

Aggregations

SkeletonJoint (org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)17 FileNotFoundException (java.io.FileNotFoundException)6 ParsingException (org.rajawali3d.loader.ParsingException)6 StringTokenizer (java.util.StringTokenizer)5 Vector3 (org.rajawali3d.math.vector.Vector3)5 SkeletalAnimationFrame (org.rajawali3d.animation.mesh.SkeletalAnimationFrame)4 SkeletalAnimationException (org.rajawali3d.animation.mesh.SkeletalAnimationObject3D.SkeletalAnimationException)3 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 BoneVertex (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D.BoneVertex)2 BoneWeight (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D.BoneWeight)2 SkeletalAnimationSequence (org.rajawali3d.animation.mesh.SkeletalAnimationSequence)2 TextureException (org.rajawali3d.materials.textures.ATexture.TextureException)2 ArrayList (java.util.ArrayList)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