Search in sources :

Example 16 with Vector2f

use of org.joml.Vector2f in project Terasology by MovingBlocks.

the class BlockSelectionRenderer method buildBlockMesh.

private static void buildBlockMesh(StandardMeshData mesh, float size, Rectanglef texRect, Colorc c) {
    Vector3f pos = new Vector3f();
    Vector3f norm = new Vector3f();
    Vector2f texCoord = new Vector2f();
    final float sizeHalf = size / 2;
    int firstIndex = mesh.position.getPosition();
    // top
    mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, -sizeHalf));
    mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, -sizeHalf));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
    for (int i = 0; i < 4; i++) {
        mesh.normal.put(norm.set(0, 1.0f, 0));
        mesh.color0.put(c);
    }
    // left
    mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, -sizeHalf));
    mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, -sizeHalf));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.minY, texRect.minY));
    for (int i = 0; i < 4; i++) {
        mesh.normal.put(norm.set(-1.0f, 0, 0));
        mesh.color0.put(c);
    }
    // right
    mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, -sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, -sizeHalf));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
    for (int i = 0; i < 4; i++) {
        mesh.normal.put(norm.set(1.0f, 0, 0));
        mesh.color0.put(c);
    }
    // back
    mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, -sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, -sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, -sizeHalf));
    mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, -sizeHalf));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
    for (int i = 0; i < 4; i++) {
        mesh.normal.put(norm.set(0, 0, -1.0f));
        mesh.color0.put(c);
    }
    // front
    mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, sizeHalf));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.minY, texRect.minY));
    for (int i = 0; i < 4; i++) {
        mesh.normal.put(norm.set(0, 0, 1.0f));
        mesh.color0.put(c);
    }
    // bottom
    mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, -sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, -sizeHalf));
    mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, sizeHalf));
    mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, sizeHalf));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
    mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
    mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
    for (int i = 0; i < 4; i++) {
        mesh.normal.put(norm.set(0, -1, 0f));
        mesh.color0.put(c);
    }
    int lastIndex = mesh.position.getPosition();
    for (int i = firstIndex; i < lastIndex - 2; i += 4) {
        mesh.indices.put(i);
        mesh.indices.put(i + 1);
        mesh.indices.put(i + 2);
        mesh.indices.put(i + 2);
        mesh.indices.put(i + 3);
        mesh.indices.put(i);
    }
}
Also used : Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f)

Example 17 with Vector2f

use of org.joml.Vector2f in project Terasology by MovingBlocks.

the class OpenGLSkeletalMesh method doReload.

@Override
protected void doReload(SkeletalMeshData newData) {
    try {
        GameThread.synch(() -> {
            this.data = newData;
            if (this.disposalAction.vao == 0) {
                this.disposalAction.vao = GL30.glGenVertexArrays();
                this.disposalAction.vbo = GL30.glGenBuffers();
                this.disposalAction.ebo = GL30.glGenBuffers();
            }
            // bind vertex array and buffer
            GL30.glBindVertexArray(this.disposalAction.vao);
            GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, this.disposalAction.vbo);
            GL30.glEnableVertexAttribArray(StandardMeshData.VERTEX_INDEX);
            GL30.glVertexAttribPointer(StandardMeshData.VERTEX_INDEX, 3, GL30.GL_FLOAT, false, VERTEX_NORMAL_SIZE, 0);
            GL30.glEnableVertexAttribArray(StandardMeshData.NORMAL_INDEX);
            GL30.glVertexAttribPointer(StandardMeshData.NORMAL_INDEX, 3, GL30.GL_FLOAT, false, VERTEX_NORMAL_SIZE, VERTEX_SIZE);
            GL30.glEnableVertexAttribArray(StandardMeshData.UV0_INDEX);
            GL30.glVertexAttribPointer(StandardMeshData.UV0_INDEX, 2, GL30.GL_FLOAT, false, UV_SIZE, (long) VERTEX_NORMAL_SIZE * newData.getVertexCount());
            int payloadSize = (UV_SIZE + VERTEX_SIZE + NORMAL_SIZE) * newData.getVertexCount();
            ByteBuffer buffer = BufferUtils.createByteBuffer(payloadSize);
            buffer.position(newData.getVertexCount() * VERTEX_NORMAL_SIZE);
            for (Vector2f uv : newData.getUVs()) {
                buffer.putFloat(uv.x);
                buffer.putFloat(uv.y);
            }
            buffer.flip();
            GL30.glBufferData(GL30.GL_ARRAY_BUFFER, buffer, GL30.GL_DYNAMIC_DRAW);
            IntBuffer indexBuffer = BufferUtils.createIntBuffer(newData.getIndices().size());
            indexBuffer.put(newData.getIndices().toArray());
            indexBuffer.flip();
            GL30.glBindBuffer(GL30.GL_ELEMENT_ARRAY_BUFFER, this.disposalAction.ebo);
            GL30.glBufferData(GL30.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL30.GL_STATIC_DRAW);
            GL30.glBindVertexArray(0);
        });
    } catch (InterruptedException e) {
        logger.error("Failed to reload {}", getUrn(), e);
    }
}
Also used : Vector2f(org.joml.Vector2f) IntBuffer(java.nio.IntBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 18 with Vector2f

use of org.joml.Vector2f in project Terasology by MovingBlocks.

the class TessellatorHelper method addGUIQuadMesh.

public static void addGUIQuadMesh(Tessellator tessellator, Vector4f color, float sizeX, float sizeY) {
    tessellator.resetParams();
    tessellator.setColor(new Vector4f(color.x, color.y, color.z, color.w));
    tessellator.setUseLighting(false);
    tessellator.setUseNormals(false);
    tessellator.addPoly(new Vector3f[] { new Vector3f(0, 0, 0), new Vector3f(sizeX, 0, 0), new Vector3f(sizeX, sizeY, 0), new Vector3f(0, sizeY, 0) }, new Vector2f[] { new Vector2f(0, 0), new Vector2f(1, 0), new Vector2f(1, 1), new Vector2f(0, 1) });
    tessellator.setUseLighting(true);
    tessellator.setUseNormals(true);
}
Also used : Vector4f(org.joml.Vector4f) Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f)

Example 19 with Vector2f

use of org.joml.Vector2f in project Terasology by MovingBlocks.

the class Voronoi method processCell.

private void processCell(int cellX, int cellY, Vector2f at, VoronoiResult[] results) {
    long seed = (702395077 * (long) cellX + 915488749 * (long) cellY);
    // Number of features
    int count = poissonCount[(int) (seed >> 24)];
    seed = incrementSeed(seed);
    for (int point = 0; point < count; point++) {
        long id = seed;
        seed = incrementSeed(seed);
        float x = (seed + 0.5f) / 4294967296.0f;
        seed = incrementSeed(seed);
        float y = (seed + 0.5f) / 4294967296.0f;
        seed = incrementSeed(seed);
        Vector2f innerPos = new Vector2f(x, y);
        Vector2f delta = new Vector2f(cellX + innerPos.x - at.x, cellY + innerPos.y - at.y);
        float dist = standardDistanceFunction(delta);
        if (dist < results[results.length - 1].distance) {
            int index = results.length - 1;
            while (index > 0 && dist < results[index - 1].distance) {
                index--;
            }
            for (int i = results.length - 1; i > index; i--) {
                results[i] = results[i - 1];
            }
            results[index].distance = dist;
            results[index].delta = new Vector2f(delta);
            results[index].id = (int) id;
        }
    }
}
Also used : Vector2f(org.joml.Vector2f)

Example 20 with Vector2f

use of org.joml.Vector2f in project Terasology by MovingBlocks.

the class Voronoi method getClosestPoints.

/**
 * @param at
 * @param numPoints Should be &le; 5. The number of points to return
 * @return
 */
public VoronoiResult[] getClosestPoints(Vector2f at, int numPoints) {
    VoronoiResult[] results = new VoronoiResult[numPoints];
    for (VoronoiResult result : results) {
        result.distance = Float.MAX_VALUE;
    }
    at.mul(DENSITY_ADJUSTMENT);
    at.add(offset);
    int cellX = Math.roundUsing(at.x, RoundingMode.FLOOR);
    int cellY = Math.roundUsing(at.y, RoundingMode.FLOOR);
    processCell(cellX, cellY, at, results);
    Vector2f cellPos = new Vector2f(at);
    cellPos.x -= cellX;
    cellPos.y -= cellY;
    Vector2f distMax = new Vector2f(standardDistanceFunction(new Vector2f(1 - cellPos.x, 0)), standardDistanceFunction(new Vector2f(0, 1 - cellPos.y)));
    Vector2f distMin = new Vector2f(standardDistanceFunction(new Vector2f(cellPos.x, 0)), standardDistanceFunction(new Vector2f(0, cellPos.y)));
    // Test near cells
    if (distMin.x < results[results.length - 1].distance) {
        processCell(cellX - 1, cellY, at, results);
    }
    if (distMin.y < results[results.length - 1].distance) {
        processCell(cellX, cellY - 1, at, results);
    }
    if (distMax.x < results[results.length - 1].distance) {
        processCell(cellX + 1, cellY, at, results);
    }
    if (distMax.y < results[results.length - 1].distance) {
        processCell(cellX, cellY + 1, at, results);
    }
    // Test further cells
    if (distMin.x + distMin.y < results[results.length - 1].distance) {
        processCell(cellX - 1, cellY - 1, at, results);
    }
    if (distMax.x + distMax.y < results[results.length - 1].distance) {
        processCell(cellX + 1, cellY + 1, at, results);
    }
    if (distMin.x + distMax.y < results[results.length - 1].distance) {
        processCell(cellX - 1, cellY + 1, at, results);
    }
    if (distMax.x + distMin.y < results[results.length - 1].distance) {
        processCell(cellX + 1, cellY - 1, at, results);
    }
    for (VoronoiResult result : results) {
        result.delta.mul(INVERSE_DENSITY_ADJUSTMENT);
        result.distance *= INVERSE_DENSITY_ADJUSTMENT * INVERSE_DENSITY_ADJUSTMENT;
    }
    return results;
}
Also used : Vector2f(org.joml.Vector2f)

Aggregations

Vector2f (org.joml.Vector2f)47 Vector3f (org.joml.Vector3f)13 Test (org.junit.jupiter.api.Test)6 Vector2i (org.joml.Vector2i)5 Vector4f (org.joml.Vector4f)5 Rectanglei (org.terasology.joml.geom.Rectanglei)5 Rectanglef (org.terasology.joml.geom.Rectanglef)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Vector3i (org.joml.Vector3i)3 SubtextureData (org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData)3 Name (org.terasology.gestalt.naming.Name)3 Vector2fc (org.joml.Vector2fc)2 Vector3d (org.joml.Vector3d)2 MeshBuilder (org.terasology.engine.rendering.assets.mesh.MeshBuilder)2 TFloatList (gnu.trove.list.TFloatList)1 TObjectIntMap (gnu.trove.map.TObjectIntMap)1 TObjectIntHashMap (gnu.trove.map.hash.TObjectIntHashMap)1 Location (io.xol.chunkstories.api.Location)1 Controller (io.xol.chunkstories.api.entity.Controller)1