Search in sources :

Example 26 with Vector2f

use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.

the class AtlasFormat method process.

private void process(GridDefinition grid, Texture texture, Vector2i size, Map<Name, SubtextureData> out) {
    if (grid.getTileSize() == null) {
        logger.error("Bad grid definition - missing mandatory property tileSize");
        return;
    }
    if (grid.getGridDimensions() == null) {
        logger.error("Bad grid definition - missing mandatory property gridDimensions");
        return;
    }
    Vector2f offset = new Vector2f(0, 0);
    if (grid.getGridOffset() != null) {
        offset.set((float) grid.getGridOffset().x / size.x, (float) grid.getGridOffset().y / size.y);
    }
    Vector2f tileSize = new Vector2f((float) grid.getTileSize().x / size.x, (float) grid.getTileSize().y / size.y);
    int tileX = 0;
    int tileY = 0;
    for (String name : grid.getTileNames()) {
        if (!name.isEmpty()) {
            Vector2f pos = new Vector2f(offset);
            pos.x += tileX * tileSize.x;
            pos.y += tileY * tileSize.y;
            Rect2f tileLocation = Rect2f.createFromMinAndSize(offset.x + tileX * tileSize.x, offset.y + tileY * tileSize.y, tileSize.x, tileSize.y);
            out.put(new Name(name), new SubtextureData(texture, tileLocation));
        }
        tileX++;
        if (tileX == grid.getGridDimensions().x) {
            tileX = 0;
            tileY++;
        }
    }
}
Also used : Rect2f(org.terasology.math.geom.Rect2f) Vector2f(org.terasology.math.geom.Vector2f) SubtextureData(org.terasology.rendering.assets.texture.subtexture.SubtextureData) Name(org.terasology.naming.Name)

Example 27 with Vector2f

use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.

the class AtlasFormat method process.

private void process(FreeformDefinition freeform, Texture texture, Vector2i size, Map<Name, SubtextureData> out) {
    if (freeform.getName() == null || freeform.getName().isEmpty()) {
        logger.error("Bad subimage definition - missing mandatory property name");
        return;
    }
    if (freeform.getMin() == null) {
        logger.error("Bad subimage definition '{}' - missing mandatory property min", freeform.getName());
        return;
    }
    if (freeform.getSize() == null && freeform.getMax() == null) {
        logger.error("Bad subimage definition '{}' - requires one of max or size", freeform.getName());
        return;
    }
    Vector2f min = new Vector2f((float) freeform.getMin().x / size.x, (float) freeform.getMin().y / size.y);
    if (freeform.getSize() != null) {
        Vector2f itemSize = new Vector2f((float) freeform.getSize().x / size.x, (float) freeform.getSize().y / size.y);
        out.put(new Name(freeform.getName()), new SubtextureData(texture, Rect2f.createFromMinAndSize(min, itemSize)));
    } else if (freeform.getMax() != null) {
        Vector2f max = new Vector2f((float) freeform.getMax().x / size.x, (float) freeform.getMax().y / size.y);
        out.put(new Name(freeform.getName()), new SubtextureData(texture, Rect2f.createFromMinAndMax(min, max)));
    }
}
Also used : Vector2f(org.terasology.math.geom.Vector2f) SubtextureData(org.terasology.rendering.assets.texture.subtexture.SubtextureData) Name(org.terasology.naming.Name)

Example 28 with Vector2f

use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.

the class ObjMeshFormat method load.

@Override
public MeshData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputs.get(0).openStream()))) {
        List<Vector3f> rawVertices = Lists.newArrayList();
        List<Vector3f> rawNormals = Lists.newArrayList();
        List<Vector2f> rawTexCoords = Lists.newArrayList();
        List<Vector3i[]> rawIndices = Lists.newArrayList();
        // Gather data
        readMeshData(reader, rawVertices, rawNormals, rawTexCoords, rawIndices);
        // Determine face format;
        if (rawIndices.size() == 0) {
            throw new IOException("No index data");
        }
        MeshData data = processData(rawVertices, rawNormals, rawTexCoords, rawIndices);
        if (data.getVertices() == null) {
            throw new IOException("No vertices define");
        }
        if (!data.getNormals().isEmpty() && data.getNormals().size() != data.getVertices().size()) {
            throw new IOException("The number of normals does not match the number of vertices.");
        }
        if (!data.getTexCoord0().isEmpty() && data.getTexCoord0().size() / 2 != data.getVertices().size() / 3) {
            throw new IOException("The number of tex coords does not match the number of vertices.");
        }
        return data;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Vector2f(org.terasology.math.geom.Vector2f) Vector3f(org.terasology.math.geom.Vector3f) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 29 with Vector2f

use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.

the class ObjMeshFormat method readMeshData.

private void readMeshData(BufferedReader reader, List<Vector3f> rawVertices, List<Vector3f> rawNormals, List<Vector2f> rawTexCoords, List<Vector3i[]> rawIndices) throws IOException {
    String line = null;
    int lineNum = 0;
    try {
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            lineNum++;
            if (line.isEmpty()) {
                continue;
            }
            String[] prefixSplit = line.trim().split("\\s+", 2);
            String prefix = prefixSplit[0];
            // Comment
            if ("#".equals(prefix)) {
                continue;
            }
            if (prefixSplit.length < 2) {
                throw new IOException(String.format("Incomplete statement"));
            }
            switch(prefix) {
                // Object name
                case "o":
                    // Just skip the name
                    break;
                // Vertex position
                case "v":
                    {
                        String[] floats = prefixSplit[1].trim().split("\\s+", 4);
                        if (floats.length != 3) {
                            throw new IOException("Bad statement");
                        }
                        rawVertices.add(new Vector3f(Float.parseFloat(floats[0]), Float.parseFloat(floats[1]), Float.parseFloat(floats[2])));
                        break;
                    }
                // Vertex texture coords
                case "vt":
                    {
                        String[] floats = prefixSplit[1].trim().split("\\s+", 4);
                        if (floats.length < 2 || floats.length > 3) {
                            throw new IOException("Bad statement");
                        }
                        // Need to flip v coord, apparently
                        rawTexCoords.add(new Vector2f(Float.parseFloat(floats[0]), Float.parseFloat(floats[1])));
                        break;
                    }
                // Vertex normal
                case "vn":
                    {
                        String[] floats = prefixSplit[1].trim().split("\\s+", 4);
                        if (floats.length != 3) {
                            throw new IOException("Bad statement");
                        }
                        rawNormals.add(new Vector3f(Float.parseFloat(floats[0]), Float.parseFloat(floats[1]), Float.parseFloat(floats[2])));
                        break;
                    }
                // Material name (ignored)
                case "usemtl":
                    break;
                // Smoothing group (not supported)
                case "s":
                    {
                        if (!"off".equals(prefixSplit[1]) && !"0".equals(prefixSplit[1])) {
                            logger.warn("Smoothing groups not supported in obj import yet");
                        }
                        break;
                    }
                // Face (polygon)
                case "f":
                    {
                        String[] elements = prefixSplit[1].trim().split("\\s+");
                        Vector3i[] result = new Vector3i[elements.length];
                        for (int i = 0; i < elements.length; ++i) {
                            String[] parts = elements[i].split("/", 4);
                            if (parts.length > 3) {
                                throw new IOException("Bad Statement");
                            }
                            result[i] = new Vector3i(Integer.parseInt(parts[0]), -1, -1);
                            if (parts.length > 1 && !parts[1].isEmpty()) {
                                result[i].y = Integer.parseInt(parts[1]);
                            }
                            if (parts.length > 2 && !parts[2].isEmpty()) {
                                result[i].z = Integer.parseInt(parts[2]);
                            }
                        }
                        rawIndices.add(result);
                        break;
                    }
                default:
                    logger.warn("Skipping unsupported obj statement on line {}:\"{}\"", lineNum, line);
            }
        }
    } catch (RuntimeException e) {
        throw new IOException(String.format("Failed to process line %d:\"%s\"", lineNum, line), e);
    }
}
Also used : Vector2f(org.terasology.math.geom.Vector2f) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) IOException(java.io.IOException)

Example 30 with Vector2f

use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.

the class Rect2fTypeHandler method deserialize.

@Override
public Rect2f deserialize(PersistedData data, DeserializationContext context) {
    if (!data.isNull() && data.isValueMap()) {
        PersistedDataMap map = data.getAsValueMap();
        Vector2f min = context.deserializeAs(map.get(MIN_FIELD), Vector2f.class);
        Vector2f size = context.deserializeAs(map.get(SIZE_FIELD), Vector2f.class);
        return Rect2f.createFromMinAndSize(min, size);
    }
    return null;
}
Also used : PersistedDataMap(org.terasology.persistence.typeHandling.PersistedDataMap) Vector2f(org.terasology.math.geom.Vector2f)

Aggregations

Vector2f (org.terasology.math.geom.Vector2f)41 Vector3f (org.terasology.math.geom.Vector3f)8 IOException (java.io.IOException)5 TIntList (gnu.trove.list.TIntList)4 Vector2i (org.terasology.math.geom.Vector2i)4 TFloatList (gnu.trove.list.TFloatList)3 Map (java.util.Map)3 Test (org.junit.Test)3 Name (org.terasology.naming.Name)3 SubtextureData (org.terasology.rendering.assets.texture.subtexture.SubtextureData)3 BrownianNoise (org.terasology.utilities.procedural.BrownianNoise)3 PerlinNoise (org.terasology.utilities.procedural.PerlinNoise)3 SubSampledNoise (org.terasology.utilities.procedural.SubSampledNoise)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 InputStream (java.io.InputStream)2 Vector4f (org.terasology.math.geom.Vector4f)2 Bone (org.terasology.rendering.assets.skeletalmesh.Bone)2 BoneWeight (org.terasology.rendering.assets.skeletalmesh.BoneWeight)2 SkeletalMeshDataBuilder (org.terasology.rendering.assets.skeletalmesh.SkeletalMeshDataBuilder)2 BlockAppearance (org.terasology.world.block.BlockAppearance)2