Search in sources :

Example 1 with TFloatList

use of gnu.trove.list.TFloatList 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)

Example 2 with TFloatList

use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.

the class ObjMeshFormat method processData.

private MeshData processData(List<Vector3f> rawVertices, List<Vector3f> rawNormals, List<Vector2f> rawTexCoords, List<Vector3i[]> rawIndices) throws IOException {
    MeshData result = new MeshData();
    TFloatList vertices = result.getVertices();
    TFloatList texCoord0 = result.getTexCoord0();
    TFloatList normals = result.getNormals();
    TIntList indices = result.getIndices();
    int vertCount = 0;
    for (Vector3i[] face : rawIndices) {
        for (Vector3i indexSet : face) {
            if (indexSet.x > rawVertices.size()) {
                throw new IOException("Vertex index out of range: " + indexSet.x);
            }
            Vector3f vertex = rawVertices.get(indexSet.x - 1);
            vertices.add(vertex.x);
            vertices.add(vertex.y);
            vertices.add(vertex.z);
            if (indexSet.y != -1) {
                if (indexSet.y > rawTexCoords.size()) {
                    throw new IOException("TexCoord index out of range: " + indexSet.y);
                }
                Vector2f texCoord = rawTexCoords.get(indexSet.y - 1);
                texCoord0.add(texCoord.x);
                texCoord0.add(1 - texCoord.y);
            }
            if (indexSet.z != -1) {
                if (indexSet.z > rawNormals.size()) {
                    throw new IOException("Normal index out of range: " + indexSet.z);
                }
                Vector3f normal = rawNormals.get(indexSet.z - 1);
                normals.add(normal.x);
                normals.add(normal.y);
                normals.add(normal.z);
            }
        }
        for (int i = 0; i < face.length - 2; ++i) {
            indices.add(vertCount);
            indices.add(vertCount + i + 1);
            indices.add(vertCount + i + 2);
        }
        vertCount += face.length;
    }
    return result;
}
Also used : Vector2f(org.terasology.math.geom.Vector2f) Vector3f(org.terasology.math.geom.Vector3f) TFloatList(gnu.trove.list.TFloatList) Vector3i(org.terasology.math.geom.Vector3i) IOException(java.io.IOException) TIntList(gnu.trove.list.TIntList)

Example 3 with TFloatList

use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.

the class SkeletalMeshDataBuilder method addMesh.

public SkeletalMeshDataBuilder addMesh(Bone bone, MeshData data) {
    TFloatList meshVertices = data.getVertices();
    TIntList meshIndices = data.getIndices();
    TFloatList texCoord0 = data.getTexCoord0();
    int weightsStart = weights.size();
    addBone(bone);
    for (int i = 0; i < meshVertices.size() / 3; i++) {
        float x = meshVertices.get(i * 3);
        float y = meshVertices.get(i * 3 + 1);
        float z = meshVertices.get(i * 3 + 2);
        Vector3f pos = new Vector3f(x, y, z);
        BoneWeight weight = new BoneWeight(pos, 1, bone.getIndex());
        // TODO Meshes may contain normal vectors and we may copy them to the weight here
        // - but they are recalculated later on in either case. needs some rework
        addWeight(weight);
        vertexStartWeights.add(weightsStart + i);
        vertexWeightCounts.add(1);
        uvs.add(new Vector2f(texCoord0.get(i * 2), texCoord0.get(i * 2 + 1)));
    }
    for (int i = 0; i < meshIndices.size(); i++) {
        indices.add(meshIndices.get(i) + weightsStart);
    }
    return this;
}
Also used : Vector2f(org.terasology.math.geom.Vector2f) Vector3f(org.terasology.math.geom.Vector3f) TFloatList(gnu.trove.list.TFloatList) TIntList(gnu.trove.list.TIntList)

Example 4 with TFloatList

use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.

the class ModifiableValueTypeHandler method deserialize.

@Override
public Optional<ModifiableValue> deserialize(PersistedData data) {
    if (data.isArray()) {
        PersistedDataArray vals = data.getAsArray();
        if (vals.isNumberArray()) {
            TFloatList floatList = vals.getAsFloatArray();
            ModifiableValue modifiableValue = new ModifiableValue(floatList.get(0));
            if (floatList.size() == 4) {
                modifiableValue.setPreModifier(floatList.get(1));
                modifiableValue.setMultiplier(floatList.get(2));
                modifiableValue.setPostModifier(floatList.get(3));
            }
            return Optional.of(modifiableValue);
        }
    }
    return Optional.empty();
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) ModifiableValue(org.terasology.engine.utilities.modifiable.ModifiableValue) TFloatList(gnu.trove.list.TFloatList)

Example 5 with TFloatList

use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.

the class GLTFAnimationFormat method getFloats.

private TFloatList getFloats(GLTF gltf, List<byte[]> loadedBuffers, int accessorIndex) throws IOException {
    GLTFAccessor accessor = gltf.getAccessors().get(accessorIndex);
    GLTFBufferView bufferView = gltf.getBufferViews().get(accessor.getBufferView());
    TFloatList floats = new TFloatArrayList();
    readBuffer(loadedBuffers.get(bufferView.getBuffer()), accessor, bufferView, floats);
    return floats;
}
Also used : GLTFBufferView(org.terasology.engine.rendering.gltf.model.GLTFBufferView) TFloatList(gnu.trove.list.TFloatList) GLTFAccessor(org.terasology.engine.rendering.gltf.model.GLTFAccessor) TFloatArrayList(gnu.trove.list.array.TFloatArrayList)

Aggregations

TFloatList (gnu.trove.list.TFloatList)23 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)12 TIntList (gnu.trove.list.TIntList)6 Vector3f (org.joml.Vector3f)5 GLTFAccessor (org.terasology.engine.rendering.gltf.model.GLTFAccessor)4 GLTFBufferView (org.terasology.engine.rendering.gltf.model.GLTFBufferView)4 JsonParseException (com.google.gson.JsonParseException)3 IOException (java.io.IOException)3 Quaternionf (org.joml.Quaternionf)3 PersistedDataArray (org.terasology.persistence.typeHandling.PersistedDataArray)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 ArrayList (java.util.ArrayList)2 Matrix4f (org.joml.Matrix4f)2 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)2 AABBf (org.terasology.joml.geom.AABBf)2 Vector2f (org.terasology.math.geom.Vector2f)2 Vector3f (org.terasology.math.geom.Vector3f)2 PersistedDataMap (org.terasology.persistence.typeHandling.PersistedDataMap)2 TDoubleIterator (gnu.trove.iterator.TDoubleIterator)1 TIntIterator (gnu.trove.iterator.TIntIterator)1