package speiger.src.coreengine.rendering.tesselation.buffer; import java.nio.ByteBuffer; import speiger.src.coreengine.rendering.tesselation.format.VertexElement; import speiger.src.coreengine.rendering.tesselation.format.VertexFormat; import speiger.src.coreengine.rendering.utils.values.GLMode; public class VertexBuilder implements IVertexBuilder { GLMode mode; ByteBuffer buffer; VertexFormat format; int index = 0; int vertecies; int totalStoredBytes; int vertexBytes; int elementBytes; public VertexBuilder(int size) { this(ByteBuffer.allocate(size)); } public VertexBuilder(ByteBuffer buffer) { this.buffer = buffer; } public VertexBuilder start(GLMode mode, VertexFormat format) { this.mode = mode; this.format = format; return this; } @Override public IVertexBuffer endVertex() { if(index != 0) throw new IllegalArgumentException("Format is not completed"); vertecies++; if(mode == GLMode.LINES || mode == GLMode.LINE_STRIP) { buffer.put(totalStoredBytes, buffer, totalStoredBytes - vertexBytes, vertexBytes); vertecies++; } vertexBytes = 0; return this; } @Override public VertexElement current() { return format.get(index); } @Override public IVertexBuilder nextElement() { index = (index + 1) % format.size(); totalStoredBytes += elementBytes; vertexBytes += elementBytes; elementBytes = 0; return this; } @Override public int size() { return vertecies; } @Override public IVertexBuilder put(int offset, byte value) { buffer.put(totalStoredBytes + offset, value); elementBytes+=1; return this; } @Override public IVertexBuilder putShort(int offset, short value) { buffer.putShort(totalStoredBytes + offset, value); elementBytes+=2; return this; } @Override public IVertexBuilder putInt(int offset, int value) { buffer.putInt(totalStoredBytes + offset, value); elementBytes+=4; return this; } @Override public IVertexBuilder putFloat(int offset, float value) { buffer.putFloat(totalStoredBytes + offset, value); elementBytes+=4; return this; } public byte[] getBytes() { byte[] data = new byte[totalStoredBytes]; buffer.get(0, data); return data; } }