Search in sources :

Example 16 with VertexResource

use of org.terasology.engine.rendering.assets.mesh.resource.VertexResource in project Terasology by MovingBlocks.

the class VertexGLAttributeTest method testByteBinding.

@Test
public void testByteBinding() {
    VertexResourceBuilder builder = new VertexResourceBuilder();
    VertexByteAttributeBinding a1 = builder.add(0, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
    VertexResource resource = builder.build();
    a1.put((byte) 10);
    a1.put((byte) 150);
    a1.put((byte) 100);
    a1.put((byte) 100);
    assertEquals(4, a1.getPosition());
    resource.writeBuffer(buffer -> {
        assertEquals(4 * Byte.BYTES, buffer.limit());
        assertEquals(10, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 0)));
        assertEquals(150, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 1)));
        assertEquals(100, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 2)));
        assertEquals(100, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 3)));
    });
}
Also used : VertexResource(org.terasology.engine.rendering.assets.mesh.resource.VertexResource) VertexByteAttributeBinding(org.terasology.engine.rendering.assets.mesh.resource.VertexByteAttributeBinding) VertexResourceBuilder(org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder) Test(org.junit.Test)

Example 17 with VertexResource

use of org.terasology.engine.rendering.assets.mesh.resource.VertexResource in project Terasology by MovingBlocks.

the class VertexGLAttributeTest method testVector4fBinding.

@Test
public void testVector4fBinding() {
    VertexResourceBuilder builder = new VertexResourceBuilder();
    VertexAttributeBinding<Vector4fc, Vector4f> a1 = builder.add(0, GLAttributes.VECTOR_4_F_VERTEX_ATTRIBUTE);
    VertexResource resource = builder.build();
    a1.put(new Vector4f(10, 150, -10, 12));
    a1.put(new Vector4f(15.1f, 15.04f, 10, 12));
    a1.put(new Vector4f(16f, 150, -10, 12));
    assertEquals(3, a1.getPosition());
    int stride = Float.BYTES * 4;
    resource.writeBuffer(buffer -> {
        assertEquals(3 * 4 * Float.BYTES, buffer.limit());
        assertEquals(10, buffer.getFloat(Float.BYTES * 0), 0.001f);
        assertEquals(150, buffer.getFloat(Float.BYTES * 1), 0.001f);
        assertEquals(-10, buffer.getFloat(Float.BYTES * 2), 0.001f);
        assertEquals(12, buffer.getFloat(Float.BYTES * 3), 0.001f);
        assertEquals(15.1f, buffer.getFloat((stride) + Float.BYTES * 0), 0.001f);
        assertEquals(15.04f, buffer.getFloat((stride) + Float.BYTES * 1), 0.001f);
        assertEquals(10, buffer.getFloat((stride) + Float.BYTES * 2), 0.001f);
        assertEquals(12, buffer.getFloat((stride) + Float.BYTES * 3), 0.001f);
        assertEquals(16f, buffer.getFloat((stride * 2) + Float.BYTES * 0), 0.001f);
        assertEquals(150f, buffer.getFloat((stride * 2) + Float.BYTES * 1), 0.001f);
        assertEquals(-10, buffer.getFloat((stride * 2) + Float.BYTES * 2), 0.001f);
        assertEquals(12, buffer.getFloat((stride * 2) + Float.BYTES * 3), 0.001f);
    });
}
Also used : Vector4fc(org.joml.Vector4fc) VertexResource(org.terasology.engine.rendering.assets.mesh.resource.VertexResource) VertexResourceBuilder(org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder) Vector4f(org.joml.Vector4f) Test(org.junit.Test)

Example 18 with VertexResource

use of org.terasology.engine.rendering.assets.mesh.resource.VertexResource in project Terasology by MovingBlocks.

the class OpenGLMeshBase method buildVBO.

default VBOContext buildVBO(int vbo, AllocationType allocation, VertexResource[] resources) {
    GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, vbo);
    int bufferSize = 0;
    for (VertexResource vertexResource : resources) {
        bufferSize += vertexResource.inSize();
    }
    GL30.glBufferData(GL30.GL_ARRAY_BUFFER, bufferSize, allocation.glCall);
    VBOContext state = new VBOContext();
    state.entries = new VBOContext.VBOSubBuffer[resources.length];
    int offset = 0;
    for (int i = 0; i < resources.length; i++) {
        VertexResource resource = resources[i];
        state.entries[i] = new VBOContext.VBOSubBuffer();
        state.entries[i].resource = resource;
        state.entries[i].version = resource.getVersion();
        state.entries[i].offset = offset;
        state.entries[i].size = resource.inSize();
        if (state.entries[i].size == 0) {
            continue;
        }
        int currentOffset = offset;
        resource.writeBuffer((buffer) -> {
            GL30.glBufferSubData(GL30.GL_ARRAY_BUFFER, currentOffset, buffer);
        });
        for (VertexResource.VertexDefinition attribute : resource.definitions()) {
            GL30.glEnableVertexAttribArray(attribute.location);
            GL30.glVertexAttribPointer(attribute.location, attribute.attribute.count, attribute.attribute.mapping.glType, false, resource.inStride(), offset + attribute.offset);
        }
        offset += resource.inSize();
    }
    state.vbo = vbo;
    GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, 0);
    return state;
}
Also used : VertexResource(org.terasology.engine.rendering.assets.mesh.resource.VertexResource)

Aggregations

VertexResource (org.terasology.engine.rendering.assets.mesh.resource.VertexResource)18 Test (org.junit.Test)16 VertexResourceBuilder (org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder)16 Vector3f (org.joml.Vector3f)7 Vector3fc (org.joml.Vector3fc)7 VertexByteAttributeBinding (org.terasology.engine.rendering.assets.mesh.resource.VertexByteAttributeBinding)3 VertexIntegerAttributeBinding (org.terasology.engine.rendering.assets.mesh.resource.VertexIntegerAttributeBinding)2 Color (org.terasology.nui.Color)2 Colorc (org.terasology.nui.Colorc)2 ByteBuffer (java.nio.ByteBuffer)1 Vector2f (org.joml.Vector2f)1 Vector2fc (org.joml.Vector2fc)1 Vector4f (org.joml.Vector4f)1 Vector4fc (org.joml.Vector4fc)1 VertexFloatAttributeBinding (org.terasology.engine.rendering.assets.mesh.resource.VertexFloatAttributeBinding)1 VertexShortAttributeBinding (org.terasology.engine.rendering.assets.mesh.resource.VertexShortAttributeBinding)1