Example usage for com.badlogic.gdx.graphics.g3d.model.still StillModel setMaterial

List of usage examples for com.badlogic.gdx.graphics.g3d.model.still StillModel setMaterial

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g3d.model.still StillModel setMaterial.

Prototype

@Override
    public void setMaterial(Material material) 

Source Link

Usage

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

License:Apache License

public static StillModel loadStillModel(FileHandle handle, ObjectMap<SubMesh, String> dependencies) {
    Chunk root = null;//w  w  w.  j a v  a2s .c o 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 stillmodel
        Chunk stillModel = root.getChild(G3dConstants.STILL_MODEL);
        if (stillModel == null)
            throw new GdxRuntimeException("No stillmodel chunk found");
        int numSubMeshes = stillModel.readInt();

        // read submeshes
        StillSubMesh[] meshes = new StillSubMesh[numSubMeshes];
        Chunk[] meshChunks = stillModel.getChildren(G3dConstants.STILL_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 name = subMesh.readString();
            String material = (dependencies == null) ? null : subMesh.readString();
            int primitiveType = 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 vertices
            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();

            StillSubMesh mesh = new StillSubMesh(name, new Mesh(true, numVertices, numIndices, vertAttribs),
                    primitiveType);
            mesh.mesh.setVertices(vertexData);
            mesh.mesh.setIndices(indexData);
            mesh.material = null;
            if (dependencies != null && material != null)
                dependencies.put(mesh, handle.parent().child(material) + ".material");
            meshes[i] = mesh;
        }

        StillModel model = new StillModel(meshes);
        if (dependencies == null)
            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) {
            }
    }
}