package client.renderer;

import java.nio.ByteBuffer;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;

public class VertexBuffer {
	private int id;
	private final VertexFormat format;
	private int count;

	public VertexBuffer(VertexFormat format) {
		this.format = format;
		this.id = GL15.glGenBuffers();
	}

	public void bindBuffer() {
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.id);
	}

	public void bufferData(ByteBuffer buffer) {
		this.bindBuffer();
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
		this.unbindBuffer();
		this.count = buffer.limit() / this.format.getNextOffset();
	}

	public void drawArrays(int mode) {
		GL11.glDrawArrays(mode, 0, this.count);
	}

	public void unbindBuffer() {
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
	}

	public void deleteGlBuffers() {
		if(this.id >= 0) {
			GL15.glDeleteBuffers(this.id);
			this.id = -1;
		}
	}
}
