package speiger.src.coreengine.rendering.shaderOld.uniforms;

import org.lwjgl.opengl.GL20;

import speiger.src.coreengine.math.color.ColorUtils;
import speiger.src.coreengine.math.vector.floats.Vec4f;
import speiger.src.coreengine.rendering.utils.AllocationTracker;

public class UniformVec4f extends UniformBase<Vec4f> {
	static final Vec4f STORAGE = Vec4f.mutable();

	public UniformVec4f(String id) {
		super(id);
	}

	public void storeData(Vec4f data) {
		if (hasChanged(data)) {
			GL20.glUniform4f(getPosition(), data.x(), data.y(), data.z(), data.w());
			AllocationTracker.INSTANCE.addGPUBytes(16L);
		}
	}

	public void storeData(int color) {
		storeData(STORAGE.set(ColorUtils.getRF(color), ColorUtils.getGF(color), ColorUtils.getBF(color),
				ColorUtils.getAF(color)));
	}

	public void storeData(float x, float y, float z, float w) {
		STORAGE.set(x, y, z, w);
		storeData(STORAGE);
	}

	@Override
	protected Vec4f setValue(Vec4f oldValue, Vec4f newValue) {
		if (oldValue != null) {
			oldValue.set(newValue);
			return oldValue;
		}
		return Vec4f.mutable(newValue);
	}
}
