Search in sources :

Example 1 with ColladaLoader

use of org.terasology.rendering.collada.ColladaLoader in project Terasology by MovingBlocks.

the class ColladaMeshFormat method load.

@Override
public MeshData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    logger.info("Loading mesh for " + urn);
    ColladaLoader loader = new ColladaLoader();
    try (BufferedInputStream stream = inputs.get(0).openStream()) {
        loader.parseMeshData(stream);
    } catch (ColladaParseException e) {
        throw new IOException("Error loading collada mesh for " + urn, e);
    }
    MeshData data = new MeshData();
    TFloatList colorsMesh = data.getColors();
    TFloatList verticesMesh = data.getVertices();
    TFloatList texCoord0Mesh = data.getTexCoord0();
    TFloatList normalsMesh = data.getNormals();
    TIntList indicesMesh = data.getIndices();
    // Scale vertices coordinates by unitsPerMeter
    for (int i = 0; i < loader.getVertices().size(); i++) {
        float originalVertexValue = loader.getVertices().get(i);
        float adjustedVertexValue = (float) (originalVertexValue * loader.getUnitsPerMeter());
        verticesMesh.add(adjustedVertexValue);
    }
    colorsMesh.addAll(loader.getColors());
    texCoord0Mesh.addAll(loader.getTexCoord0());
    normalsMesh.addAll(loader.getNormals());
    indicesMesh.addAll(loader.getIndices());
    if (data.getVertices() == null) {
        throw new IOException("No vertices define");
    }
    if (((null == data.getColors()) || (0 == data.getColors().size())) && ((null == data.getTexCoord0()) || (0 == data.getTexCoord0().size()))) {
        throw new IOException("There must be either texture coordinates or vertex colors provided.");
    }
    if ((null != data.getTexCoord0()) && (0 != data.getTexCoord0().size())) {
        if (data.getTexCoord0().size() / 2 != data.getVertices().size() / 3) {
            throw new IOException("The number of tex coords (" + data.getTexCoord0().size() / 2 + ") does not match the number of vertices (" + data.getVertices().size() / 3 + ").");
        }
    }
    if ((null != data.getColors()) && (0 != data.getColors().size())) {
        if (data.getColors().size() / 4 != data.getVertices().size() / 3) {
            throw new IOException("The number of vertex colors (" + data.getColors().size() / 4 + ") does not match the number of vertices (" + data.getVertices().size() / 3 + ").");
        }
    }
    return data;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ColladaLoader(org.terasology.rendering.collada.ColladaLoader) TFloatList(gnu.trove.list.TFloatList) IOException(java.io.IOException) TIntList(gnu.trove.list.TIntList) ColladaParseException(org.terasology.rendering.collada.ColladaParseException)

Aggregations

TFloatList (gnu.trove.list.TFloatList)1 TIntList (gnu.trove.list.TIntList)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 ColladaLoader (org.terasology.rendering.collada.ColladaLoader)1 ColladaParseException (org.terasology.rendering.collada.ColladaParseException)1