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

import java.util.Collection;
import java.util.Iterator;

import org.lwjgl.opengl.GL20;

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

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

	public UniformVec4fArray(String id, int length) {
		super(id, length);
	}

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

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

	public void store(Collection<Vec4f> store) {
		Iterator<Vec4f> values = store.iterator();
		for (int i = 0; i < limit && values.hasNext(); i++) {
			store(i, values.next());
		}
	}

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