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

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

Introduction

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

Prototype

int KEYFRAMED_SUBMESH

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

Click Source Link

Usage

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

License:Apache License

public static void export(KeyframedModel 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();//  w  w  w  .j a v  a 2 s .  c o m

    // write keyframed model
    writer.newChunk(G3dConstants.KEYFRAMED_MODEL);
    writer.writeInt(model.subMeshes.length);

    for (KeyframedSubMesh mesh : model.subMeshes) {
        // writes keyframed submesh
        writer.newChunk(G3dConstants.KEYFRAMED_SUBMESH);
        writer.writeString(mesh.name == null ? "" : mesh.name);
        writer.writeInt(mesh.primitiveType);
        writer.writeInt(mesh.animatedComponents);
        writer.writeInt(mesh.animations.size);

        // 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;
        float[] vertices = new float[numFloats];
        mesh.mesh.getVertices(vertices);
        writer.writeInt(mesh.mesh.getNumVertices());
        writer.writeFloats(vertices);
        writer.endChunk();

        // write indices
        writer.newChunk(G3dConstants.INDEX_LIST);
        int numShorts = mesh.mesh.getNumIndices();
        short[] indices = new short[numShorts];
        mesh.mesh.getIndices(indices);
        writer.writeInt(mesh.mesh.getNumIndices());
        writer.writeShorts(indices);
        writer.endChunk();

        // write animations
        for (String animationName : mesh.animations.keys()) {
            KeyframedAnimation animation = mesh.animations.get(animationName);

            // write keyframed animation
            writer.newChunk(G3dConstants.KEYFRAMED_ANIMATION);
            writer.writeString(animation.name);
            writer.writeFloat(animation.frameDuration);

            // write key frames
            writer.writeInt(animation.keyframes.length);
            for (Keyframe keyframe : animation.keyframes) {
                // write keyframed
                writer.newChunk(G3dConstants.KEYFRAMED_FRAME);
                writer.writeFloat(keyframe.timeStamp);
                writer.writeFloats(keyframe.vertices);
                writer.endChunk();
            }
            // end keyframed animation
            writer.endChunk();
        }

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

    // end keyframed 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 KeyframedModel loadKeyframedModel(FileHandle handle) {
    Chunk root = null;// w  w  w  .j av  a  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 keyframed model
        Chunk stillModel = root.getChild(G3dConstants.KEYFRAMED_MODEL);
        if (stillModel == null)
            throw new GdxRuntimeException("No stillmodel chunk found");
        int numSubMeshes = stillModel.readInt();

        // read submeshes
        KeyframedSubMesh[] meshes = new KeyframedSubMesh[numSubMeshes];
        Chunk[] meshChunks = stillModel.getChildren(G3dConstants.KEYFRAMED_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++) {
            // read submesh name and primitive type
            Chunk subMesh = meshChunks[i];
            String meshName = subMesh.readString();
            int primitiveType = subMesh.readInt();
            int animatedComponents = subMesh.readInt();
            int numAnimations = subMesh.readInt();

            // read attributes
            Chunk attributes = subMesh.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 = subMesh.getChild(G3dConstants.VERTEX_LIST);
            int numVertices = vertices.readInt();
            float[] vertexData = vertices.readFloats();

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

            // read animations
            ObjectMap<String, KeyframedAnimation> animations = new ObjectMap<String, KeyframedAnimation>();
            Chunk[] animationChunks = subMesh.getChildren(G3dConstants.KEYFRAMED_ANIMATION);
            if (numAnimations != animationChunks.length)
                throw new GdxRuntimeException(
                        "number of keyframed animations not equal to number specified in keyframed submesh chunk, was "
                                + animationChunks.length + ", expected " + numAnimations);
            for (int j = 0; j < numAnimations; j++) {
                Chunk animationChunk = animationChunks[j];
                String animationName = animationChunk.readString();
                float frameDuration = animationChunk.readFloat();

                // read keyframes
                int numKeyframes = animationChunk.readInt();
                Keyframe[] keyframes = new Keyframe[numKeyframes];
                Chunk[] keyframeChunks = animationChunk.getChildren(G3dConstants.KEYFRAMED_FRAME);
                if (numKeyframes != keyframeChunks.length)
                    throw new GdxRuntimeException(
                            "number of keyframes not equal to number specified in keyframed animation, was "
                                    + keyframeChunks.length + ", expected " + numKeyframes);
                for (int k = 0; k < numKeyframes; k++) {
                    Chunk keyframeChunk = keyframeChunks[k];
                    float timeStamp = keyframeChunk.readFloat();
                    float[] keyframeVertices = keyframeChunk.readFloats();

                    keyframes[k] = new Keyframe(timeStamp, keyframeVertices);
                }

                animations.put(animationName, new KeyframedAnimation(animationName, frameDuration, keyframes));
            }

            Mesh mesh = new Mesh(VertexDataType.VertexArray, false, numVertices, numIndices, vertAttribs);
            meshes[i] = new KeyframedSubMesh(meshName, mesh, vertexData, animations, animatedComponents,
                    primitiveType);
            mesh.setVertices(vertexData);
            mesh.setIndices(indexData);
        }

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