package speiger.src.coreengine.platform.graphics.opengl.utils.states;

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

import speiger.src.coreengine.platform.graphics.api.shader.states.BlendFunction;
import speiger.src.coreengine.platform.graphics.opengl.utils.GLUtils;

public class BlendState extends GLState {
	BlendFunction function = BlendFunction.DEFAULT;
	
	public BlendState() {
		super(GL11.GL_BLEND);
	}
	
	public BlendState enableSet(BlendFunction function) {
		enable();
		return set(function);
	}
	
	public BlendState set(BlendFunction function) {
		if(this.function.equals(function)) return this;
		this.function = function;
		GL15.glBlendFuncSeparate(GLUtils.toGL(function.colorSource()), GLUtils.toGL(function.colorTarget()), GLUtils.toGL(function.alphaSource()), GLUtils.toGL(function.alphaTarget()));
		return this;
	}
	
	@Override
	public void reapply() {
		super.reapply();
		GL15.glBlendFuncSeparate(GLUtils.toGL(function.colorSource()), GLUtils.toGL(function.colorTarget()), GLUtils.toGL(function.alphaSource()), GLUtils.toGL(function.alphaTarget()));
	}
	
	@Override
	public void setDefault() {
		super.setDefault();
		set(BlendFunction.DEFAULT);
	}
}
