Example usage for com.badlogic.gdx.graphics.g3d.loaders.g3d G3dConstants SKELETON

List of usage examples for com.badlogic.gdx.graphics.g3d.loaders.g3d G3dConstants SKELETON

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g3d.loaders.g3d G3dConstants SKELETON.

Prototype

int SKELETON

To view the source code for com.badlogic.gdx.graphics.g3d.loaders.g3d G3dConstants SKELETON.

Click Source Link

Usage

From source file:com.xoppa.android.loaders.model.g3d.G3dxExporter.java

License:Apache License

public static void export(SkeletonModel model, FileHandle file) {
    ChunkWriter writer = new ChunkWriter();

    // write version info
    writer.newChunk(G3dConstants.VERSION_INFO);
    writer.writeByte(G3dConstants.MAJOR_VERSION);
    writer.writeByte(G3dConstants.MINOR_VERSION);
    writer.endChunk();//from w w w  .  ja  va  2 s  .  c o  m

    // write skeleton model
    writer.newChunk(G3dConstants.SKELETON_MODEL);
    writer.writeInt(model.subMeshes.length);

    for (SkeletonSubMesh mesh : model.subMeshes) {
        // writes skeleton submesh
        writer.newChunk(G3dConstants.SKELETON_SUBMESH);
        writer.writeString(mesh.name == null ? "" : mesh.name);
        writer.writeInt(mesh.primitiveType);

        // write vertex attributes
        writer.newChunk(G3dConstants.VERTEX_ATTRIBUTES);
        writer.writeInt(mesh.mesh.getVertexAttributes().size());
        for (int i = 0; i < mesh.mesh.getVertexAttributes().size(); i++) {
            VertexAttribute attribute = mesh.mesh.getVertexAttributes().get(i);
            writer.newChunk(G3dConstants.VERTEX_ATTRIBUTE);
            writer.writeInt(attribute.usage);
            writer.writeInt(attribute.numComponents);
            writer.writeString(attribute.alias);
            writer.endChunk();
        }
        writer.endChunk();

        // write static components, sort of like a bind pose mesh
        writer.newChunk(G3dConstants.VERTEX_LIST);
        int numFloats = mesh.mesh.getNumVertices() * mesh.mesh.getVertexSize() / 4;
        writer.writeInt(mesh.mesh.getNumVertices());
        writer.writeFloats(mesh.vertices);
        writer.endChunk();

        // write indices
        writer.newChunk(G3dConstants.INDEX_LIST);
        int numShorts = mesh.mesh.getNumIndices();
        writer.writeInt(numShorts);
        writer.writeShorts(mesh.indices);
        writer.endChunk();

        // write bone weight
        writer.newChunk(G3dConstants.BONE_WEIGHTS);
        writer.writeInt(mesh.boneWeights.length);
        for (float array[] : mesh.boneWeights) {
            writer.newChunk(G3dConstants.BONE_WEIGHT);
            writer.writeInt(array.length);
            writer.writeFloats(array);
            writer.endChunk();
        }
        writer.endChunk();

        // write bone assignment
        writer.newChunk(G3dConstants.BONE_ASSIGNMENTS);
        writer.writeInt(mesh.boneAssignments.length);
        for (int array[] : mesh.boneAssignments) {
            writer.newChunk(G3dConstants.BONE_ASSIGNMENT);
            writer.writeInt(array.length);
            writer.writeInts(array);
            writer.endChunk();
        }
        writer.endChunk();

        // end skeleton submesh
        writer.endChunk();
    }

    //Write Skeleton
    writer.newChunk(G3dConstants.SKELETON);

    //Write Skeleton hierarchy
    writer.newChunk(G3dConstants.SKELETON_HIERARCHY);
    writer.writeInt(model.skeleton.hierarchy.size);
    for (SkeletonJoint joint : model.skeleton.hierarchy) {
        writeSkeletonJoint(writer, joint);
    }

    // end Skeleton hierarchy
    writer.endChunk();

    //Write Skeleton animations
    writer.newChunk(G3dConstants.SKELETON_ANIMATIONS);

    writer.writeInt(model.skeleton.animations.size);
    for (String animationName : model.skeleton.animations.keys()) {
        writer.newChunk(G3dConstants.SKELETON_ANIMATION);
        writer.writeString(animationName);
        SkeletonAnimation animation = model.skeleton.animations.get(animationName);
        writer.writeFloat(animation.totalDuration);
        writer.writeInt(animation.perJointkeyFrames.length);
        for (SkeletonKeyframe array[] : animation.perJointkeyFrames) {
            writer.writeInt(array.length);
            for (SkeletonKeyframe frame : array) {
                writer.writeFloat(frame.timeStamp);
                writer.writeInt(frame.parentIndex);
                writer.writeFloat(frame.position.x);
                writer.writeFloat(frame.position.y);
                writer.writeFloat(frame.position.z);
                writer.writeFloat(frame.rotation.w);
                writer.writeFloat(frame.rotation.x);
                writer.writeFloat(frame.rotation.y);
                writer.writeFloat(frame.rotation.z);
                writer.writeFloat(frame.scale.x);
                writer.writeFloat(frame.scale.y);
                writer.writeFloat(frame.scale.z);
            }
        }
        writer.endChunk();
    }

    // end Skeleton animations
    writer.endChunk();

    // end Skeleton
    writer.endChunk();

    // end skeleton model
    writer.endChunk();

    // write to file
    OutputStream out = null;
    try {
        out = file.write(false);
        writer.writeToStream(out);
    } catch (IOException e) {
        throw new GdxRuntimeException("An error occured while exporting the still model, " + e.getMessage(), e);
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException e) {
            }
    }
}

From source file:com.xoppa.android.loaders.model.g3d.G3dxLoader.java

License:Apache License

public static SkeletonModel loadSkeletonModel(FileHandle handle) {
    Chunk root = null;// w ww .  ja va 2 s.co  m
    InputStream in = null;
    try {
        in = handle.read();
        root = ChunkReader.readChunks(in);

        // check root tag
        if (root.getId() != G3dConstants.G3D_ROOT)
            throw new GdxRuntimeException("Invalid root tag id: " + root.getId());

        // check version
        Chunk version = root.getChild(G3dConstants.VERSION_INFO);
        if (version == null)
            throw new GdxRuntimeException("No version chunk found");
        int major = version.readByte();
        int minor = version.readByte();
        if (major != 0 || minor != 1)
            throw new GdxRuntimeException("Invalid version, required 0.1, got " + major + "." + minor);

        // read skeleton model
        Chunk skeletonModel = root.getChild(G3dConstants.SKELETON_MODEL);
        if (skeletonModel == null)
            throw new GdxRuntimeException("No skeletonModel chunk found");
        int numSubMeshes = skeletonModel.readInt();

        // read submeshes
        SkeletonSubMesh[] meshes = new SkeletonSubMesh[numSubMeshes];
        Chunk[] meshChunks = skeletonModel.getChildren(G3dConstants.SKELETON_SUBMESH);
        if (meshChunks.length != numSubMeshes)
            throw new GdxRuntimeException(
                    "Number of submeshes not equal to number specified in still model chunk, expected "
                            + numSubMeshes + ", got " + meshChunks.length);
        for (int i = 0; i < numSubMeshes; i++) {
            Chunk subMeshChunk = meshChunks[i];

            // read attributes
            Chunk attributes = subMeshChunk.getChild(G3dConstants.VERTEX_ATTRIBUTES);
            if (attributes == null)
                throw new GdxRuntimeException("No vertex attribute chunk given");
            int numAttributes = attributes.readInt();
            Chunk[] attributeChunks = attributes.getChildren(G3dConstants.VERTEX_ATTRIBUTE);
            if (attributeChunks.length != numAttributes)
                new GdxRuntimeException(
                        "Number of attributes not equal to number specified in attributes chunk, expected "
                                + numAttributes + ", got " + attributeChunks.length);
            VertexAttribute[] vertAttribs = new VertexAttribute[numAttributes];
            for (int j = 0; j < numAttributes; j++) {
                vertAttribs[j] = new VertexAttribute(attributeChunks[j].readInt(), attributeChunks[j].readInt(),
                        attributeChunks[j].readString());
            }

            // read static components, sort of like a bind pose mesh
            Chunk vertices = subMeshChunk.getChild(G3dConstants.VERTEX_LIST);
            int numVertices = vertices.readInt();
            float[] meshVertices = vertices.readFloats();

            // read indices
            Chunk indices = subMeshChunk.getChild(G3dConstants.INDEX_LIST);
            int numIndices = indices.readInt();
            short[] meshIndices = indices.readShorts();

            //read bone weight
            Chunk boneWeights = subMeshChunk.getChild(G3dConstants.BONE_WEIGHTS);
            int numBonesWeights = boneWeights.readInt();
            Chunk[] boneWeightChunks = boneWeights.getChildren(G3dConstants.BONE_WEIGHT);
            if (attributeChunks.length != numAttributes)
                new GdxRuntimeException(
                        "Number of bone weights not equal to number specified in bone weights chunk, expected "
                                + numBonesWeights + ", got " + boneWeightChunks.length);
            float[][] meshBoneWeights = new float[numBonesWeights][];
            for (int j = 0; j < numBonesWeights; j++) {
                int count = boneWeightChunks[j].readInt();
                meshBoneWeights[j] = boneWeightChunks[j].readFloats();
            }

            //read bone assignment
            Chunk boneAssignments = subMeshChunk.getChild(G3dConstants.BONE_ASSIGNMENTS);
            int numBoneAssignments = boneAssignments.readInt();
            Chunk[] boneAssignmentChunks = boneAssignments.getChildren(G3dConstants.BONE_ASSIGNMENT);
            if (boneAssignmentChunks.length != numBoneAssignments)
                new GdxRuntimeException(
                        "Number of bone assignment not equal to number specified in bone assignment chunk, expected "
                                + numBoneAssignments + ", got " + boneAssignmentChunks.length);
            int[][] meshBoneAssignments = new int[numBoneAssignments][];
            for (int j = 0; j < numBoneAssignments; j++) {
                int count = boneAssignmentChunks[j].readInt();
                meshBoneAssignments[j] = boneAssignmentChunks[j].readInts();
            }

            SkeletonSubMesh subMesh = new SkeletonSubMesh(subMeshChunk.readString(),
                    new Mesh(false, numVertices, numIndices, vertAttribs), subMeshChunk.readInt());

            subMesh.indices = meshIndices;
            subMesh.boneAssignments = meshBoneAssignments;
            subMesh.boneWeights = meshBoneWeights;
            subMesh.vertices = meshVertices;

            subMesh.mesh.setVertices(subMesh.vertices);
            subMesh.mesh.setIndices(subMesh.indices);
            subMesh.skinnedVertices = new float[subMesh.vertices.length];
            System.arraycopy(subMesh.vertices, 0, subMesh.skinnedVertices, 0, subMesh.vertices.length);
            meshes[i] = subMesh;
        }

        //read Skeleton hierarchy
        Skeleton skeleton = new Skeleton();
        Chunk skeletonChunk = skeletonModel.getChild(G3dConstants.SKELETON);
        {
            // read Skeleton hierarchy
            Chunk hierarchy = skeletonChunk.getChild(G3dConstants.SKELETON_HIERARCHY);
            int numHierarchyJoints = hierarchy.readInt();
            for (int i = 0; i < numHierarchyJoints; i++) {
                skeleton.hierarchy.add(readSkeletonJoint(hierarchy));
            }

            // read Skeleton animations
            Chunk animations = skeletonChunk.getChild(G3dConstants.SKELETON_ANIMATIONS);
            int numAnimations = animations.readInt();
            Chunk[] animationChunks = animations.getChildren(G3dConstants.SKELETON_ANIMATION);
            if (animationChunks.length != numAnimations)
                new GdxRuntimeException(
                        "Number of animations not equal to number specified in animations chunk, expected "
                                + numAnimations + ", got " + animationChunks.length);
            for (int i = 0; i < numAnimations; i++) {
                Chunk animation = animationChunks[i];

                String name = animation.readString();
                float totalDuration = animation.readFloat();

                int numJoints = animation.readInt();
                SkeletonKeyframe perJointKeyFrames[][] = new SkeletonKeyframe[numJoints][];
                for (int j = 0; j < numJoints; j++) {
                    int numFrames = animation.readInt();
                    perJointKeyFrames[j] = new SkeletonKeyframe[numFrames];

                    for (int k = 0; k < numFrames; k++) {
                        SkeletonKeyframe frame = new SkeletonKeyframe();

                        frame.timeStamp = animation.readFloat();
                        frame.parentIndex = animation.readInt();
                        frame.position.x = animation.readFloat();
                        frame.position.y = animation.readFloat();
                        frame.position.z = animation.readFloat();
                        frame.rotation.w = animation.readFloat();
                        frame.rotation.x = animation.readFloat();
                        frame.rotation.y = animation.readFloat();
                        frame.rotation.z = animation.readFloat();
                        frame.scale.x = animation.readFloat();
                        frame.scale.y = animation.readFloat();
                        frame.scale.z = animation.readFloat();
                        perJointKeyFrames[j][k] = frame;
                    }
                }

                skeleton.animations.put(name, new SkeletonAnimation(name, totalDuration, perJointKeyFrames));
            }
        }
        skeleton.buildFromHierarchy();

        SkeletonModel model = new SkeletonModel(skeleton, meshes);
        model.setMaterial(new Material("default"));
        return model;
    } catch (IOException e) {
        throw new GdxRuntimeException(
                "Couldn't load skeleton model from '" + handle.name() + "', " + e.getMessage(), e);
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
            }
    }
}