package speiger.src.coreengine.rendering.shader.uniform.base;

import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GL31;

import speiger.src.coreengine.rendering.models.buffers.UniformBuffer;
import speiger.src.coreengine.rendering.shader.uniform.Uniform;

public class BufferUniform extends Uniform {
	protected final int slot;
	protected int bufferId;
	
	public BufferUniform(String name, int slot) {
		super(name);
		this.slot = slot;
	}
	
	public BufferUniform set(UniformBuffer buffer) {
		if(bufferId != buffer.id()) {
			bufferId = buffer.id();
			GL30.glBindBufferBase(GL31.GL_UNIFORM_BUFFER, slot, bufferId);
		}
		return this;
	}
	
	@Override
	protected int getUniformLocation(int id, String name) {
		return GL31.glGetUniformBlockIndex(id, name);
	}
	
	@Override
	protected void processChanges(int programId, int location) {
		GL31.glUniformBlockBinding(programId, location, slot);		
	}
}
