Search in sources :

Example 1 with MeshLoadException

use of io.xol.chunkstories.api.exceptions.content.MeshLoadException in project chunkstories by Hugobros3.

the class MeshStore method getMesh.

@Override
public Mesh getMesh(String meshName) {
    Mesh mesh = meshes.get(meshName);
    if (mesh == null) {
        Asset a = modsManager.getAsset(meshName);
        try {
            // mesh = loader.loadMeshFromAsset(a);
            mesh = loader.load(a);
        } catch (MeshLoadException e) {
            e.printStackTrace();
            logger().error("Mesh " + meshName + " couldn't be load using MeshLoader " + loader.getClass().getName() + " ,stack trace above.");
            return null;
        }
        meshes.put(meshName, mesh);
    }
    return mesh;
}
Also used : AnimatableMesh(io.xol.chunkstories.api.mesh.AnimatableMesh) Mesh(io.xol.chunkstories.api.mesh.Mesh) Asset(io.xol.chunkstories.api.content.Asset) MeshLoadException(io.xol.chunkstories.api.exceptions.content.MeshLoadException)

Example 2 with MeshLoadException

use of io.xol.chunkstories.api.exceptions.content.MeshLoadException in project chunkstories by Hugobros3.

the class WavefrontLoader method loadMeshFromAsset.

public Mesh loadMeshFromAsset(Asset asset) throws MeshLoadException {
    List<float[]> vertices = new ArrayList<float[]>();
    List<float[]> texcoords = new ArrayList<float[]>();
    List<float[]> normals = new ArrayList<float[]>();
    /*
		// Reset values
		vertices.clear();
		texcoords.clear();
		normals.clear();*/
    Map<String, Integer> groupsSizesMap = new HashMap<String, Integer>();
    Map<String, List<float[]>> tempGroups = new HashMap<String, List<float[]>>();
    boolean hasGroups = false;
    int groupSize = 0;
    String group = "root";
    int totalVertices = 0;
    int line = 0;
    try {
        // Read the actual file
        BufferedReader reader = new BufferedReader(asset.reader());
        String[] splitted;
        String[] e;
        String l;
        float[] v, t, n;
        while ((l = reader.readLine()) != null) {
            line++;
            if (!l.startsWith("#")) {
                splitted = l.split(" ");
                // Parse the various vertices attributes
                if (l.startsWith("vt")) {
                    // Note that we invert Y texture coordinates from blender to ogl
                    texcoords.add(new float[] { Float.parseFloat(splitted[1]), (1 - Float.parseFloat(splitted[2])) });
                } else if (l.startsWith("vn")) {
                    normals.add(new float[] { Float.parseFloat(splitted[1]), Float.parseFloat(splitted[2]), Float.parseFloat(splitted[3]) });
                } else if (l.startsWith("v")) {
                    vertices.add(new float[] { Float.parseFloat(splitted[1]), Float.parseFloat(splitted[2]), Float.parseFloat(splitted[3]) });
                } else // Vertices group manager
                if (l.startsWith("g")) {
                    hasGroups = true;
                    // If the current group contains vertices, note the size and put it in the hashmap
                    if (groupSize > 0) {
                        // All is simple if this is the first time we are done with this group ...
                        if (!groupsSizesMap.containsKey(group))
                            groupsSizesMap.put(group, groupSize);
                        else // But if that group already exists it means that it's vertices are splitted arround the file and we need to add up
                        // the sizes and then write them in the correct order in the final buffer !
                        {
                            int i = groupsSizesMap.get(group) + groupSize;
                            groupsSizesMap.remove(group);
                            groupsSizesMap.put(group, i);
                        }
                    }
                    // Resets group size and change the current group name
                    groupSize = 0;
                    group = splitted[1];
                } else if (l.startsWith("f")) {
                    groupSize++;
                    // No support for quads, only triangles.
                    if (splitted.length == 4) {
                        for (// For each vertex of
                        int i = 1; // For each vertex of
                        i <= 3; // For each vertex of
                        i++) // the triangle.
                        {
                            e = splitted[i].split("/");
                            // Gets the various properties of the Obj file
                            v = vertices.get(Integer.parseInt(e[0]) - 1);
                            t = texcoords.get(Integer.parseInt(e[1]) - 1);
                            n = normals.get(Integer.parseInt(e[2]) - 1);
                            // Add the face to the current vertex group
                            if (!tempGroups.containsKey(group))
                                tempGroups.put(group, new ArrayList<float[]>());
                            tempGroups.get(group).add(new float[] { v[0], v[1], v[2], t[0], t[1], n[0], n[1], n[2] });
                        }
                        totalVertices += 3;
                    }
                }
            }
        }
        // Same logic as above, terminates the last group
        if (groupSize > 0) {
            if (!groupsSizesMap.containsKey(group))
                groupsSizesMap.put(group, groupSize);
            else {
                int i = groupsSizesMap.get(group) + groupSize;
                groupsSizesMap.remove(group);
                groupsSizesMap.put(group, i);
            }
        }
        reader.close();
        // BufferUtils.createFloatBuffer(3 * totalVertices);
        FloatBuffer verticesBuffer = ByteBuffer.allocateDirect(3 * totalVertices * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
        // BufferUtils.createFloatBuffer(2 * totalVertices);
        FloatBuffer textureCoordinatesBuffer = ByteBuffer.allocateDirect(2 * totalVertices * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
        // BufferUtils.createFloatBuffer(3 * totalVertices);
        FloatBuffer normalsBuffer = ByteBuffer.allocateDirect(3 * totalVertices * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
        // Iterates over each group name in order of apparition
        for (String gName : tempGroups.keySet()) {
            for (float[] gData : tempGroups.get(gName)) {
                verticesBuffer.put(gData[0]);
                verticesBuffer.put(gData[1]);
                verticesBuffer.put(gData[2]);
                textureCoordinatesBuffer.put(gData[3]);
                textureCoordinatesBuffer.put(gData[4]);
                normalsBuffer.put(gData[5]);
                normalsBuffer.put(gData[6]);
                normalsBuffer.put(gData[7]);
            // System.out.println(gData[0]);
            }
        }
        verticesBuffer.flip();
        textureCoordinatesBuffer.flip();
        normalsBuffer.flip();
        if (hasGroups)
            // new MultiPartMesh(verticesBuffer, textureCoordinatesBuffer, normalsBuffer, groupsSizesMap);
            return null;
        else
            return new Mesh(verticesBuffer, textureCoordinatesBuffer, normalsBuffer);
    // return new ObjMeshRenderable(totalVertices, verticesBuffer, textureCoordinatesBuffer, normalsBuffer, groupsSizesMap);
    /*}*/
    } catch (Exception e) {
        // God damnit
        logger().error("Error loading model at line " + line);
        e.printStackTrace();
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Mesh(io.xol.chunkstories.api.mesh.Mesh) FloatBuffer(java.nio.FloatBuffer) MeshLoadException(io.xol.chunkstories.api.exceptions.content.MeshLoadException) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

MeshLoadException (io.xol.chunkstories.api.exceptions.content.MeshLoadException)2 Mesh (io.xol.chunkstories.api.mesh.Mesh)2 Asset (io.xol.chunkstories.api.content.Asset)1 AnimatableMesh (io.xol.chunkstories.api.mesh.AnimatableMesh)1 BufferedReader (java.io.BufferedReader)1 FloatBuffer (java.nio.FloatBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1