Created
April 17, 2021 03:12
-
-
Save pollend/946a71c7f5a7db962f449372621abec7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright 2021 The Terasology Foundation | |
| // SPDX-License-Identifier: Apache-2.0 | |
| package org.terasology.engine.rendering.assets.mesh.vertex; | |
| import org.joml.Vector3f; | |
| import org.joml.Vector3i; | |
| import org.lwjgl.opengl.GL30; | |
| import java.nio.ByteBuffer; | |
| public class VertexAttribute<T> { | |
| public static final VertexAttribute<Vector3i> VECTOR_3_I_VERTEX_ATTRIBUTE = new VertexAttribute<>(Vector3i.class, (index, buffer, value) -> { | |
| buffer.putInt(index, value.x); | |
| buffer.putInt(index + Integer.BYTES, value.y); | |
| buffer.putInt(index + Integer.BYTES * 2, value.z); | |
| }, TypeMapping.ATTR_INT, 3); | |
| public static final VertexAttribute<Vector3f> VECTOR_3_F_VERTEX_ATTRIBUTE = new VertexAttribute<>( | |
| Vector3f.class, (index, buffer, value) -> { | |
| buffer.putFloat(index, value.x); | |
| buffer.putFloat(index + Float.BYTES, value.y); | |
| buffer.putFloat(index + Float.BYTES * 2, value.z); | |
| }, TypeMapping.ATTR_FLOAT, 3); | |
| public enum TypeMapping { | |
| ATTR_FLOAT(Float.BYTES, GL30.GL_FLOAT), | |
| ATTR_SHORT(Short.BYTES, GL30.GL_SHORT), | |
| ATTR_BYTE(Byte.BYTES, GL30.GL_BYTE), | |
| ATTR_INT(Integer.BYTES, GL30.GL_INT); | |
| public final int size; | |
| public final int glType; | |
| TypeMapping(int size, int glType) { | |
| this.size = size; | |
| this.glType = glType; | |
| } | |
| } | |
| interface Mapper<T> { | |
| void map(int index, ByteBuffer buffer, T value); | |
| } | |
| public final Mapper<T> mapper; | |
| VertexAttribute(Class target, Mapper<T> mapper, TypeMapping mapping, int count) { | |
| this.mapper = mapper; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright 2021 The Terasology Foundation | |
| // SPDX-License-Identifier: Apache-2.0 | |
| package org.terasology.engine.rendering.assets.mesh.vertex; | |
| import org.lwjgl.BufferUtils; | |
| import java.nio.ByteBuffer; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class VertexResource { | |
| public final int inStride; | |
| public final int inSize; | |
| public final ByteBuffer buffer; | |
| public VertexAttribute[] attributes; | |
| protected VertexResource(int inStride, int inSize, VertexAttribute[] attributes) { | |
| this.inSize = inSize; | |
| this.inStride = inStride; | |
| this.buffer = BufferUtils.createByteBuffer(inSize); | |
| } | |
| public static class VertexResourceBuilder { | |
| List<VertexAttribute> attributes = new ArrayList<>(); | |
| } | |
| public static class VertexAttributeBinding<Y,V extends VertexAttribute<Y>> { | |
| private V attributes; | |
| private VertexResource resource; | |
| private int position = 0; | |
| private int offset = 0; | |
| public void put(Y value) { | |
| attributes.mapper.map(position * resource.inStride + offset, resource.buffer, value); | |
| } | |
| public void put(int vertexIndex, Y value) { | |
| attributes.mapper.map(vertexIndex * resource.inStride + offset, resource.buffer, value); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment