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.Vec3f;
import speiger.src.coreengine.rendering.utils.AllocationTracker;

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

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

	public void store(int index, Vec3f data) {
		if (hasChanged(index, data)) {
			GL20.glUniform3f(getPosition(index), data.x(), data.y(), data.z());
			AllocationTracker.INSTANCE.addGPUBytes(12L);
		}
	}

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

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

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