#version 330 core

// Instanced billboard render: one quad, drawn maxParticles times, each instance reading its particle
// straight from the simulation buffer. The quad is oriented to face the camera using the view matrix.
layout(location = 0) in vec2 quad;  // -0.5..0.5 corner (per vertex)
layout(location = 1) in vec3 iPos;  // per-instance particle position
layout(location = 2) in float iLife;
layout(location = 3) in float iSeed;

uniform mat4 view;
uniform mat4 projection;
uniform float uSizeStart;
uniform float uSizeEnd;
uniform float uLifeMax;

out vec2 ex_Uv;
out float ex_LifeFrac;

void main() {
	float lifeFrac = clamp(iLife / max(uLifeMax, 0.0001), 0.0, 1.0);
	float size = mix(uSizeEnd, uSizeStart, lifeFrac);

	// Camera right/up are the first two rows of the view matrix.
	vec3 right = vec3(view[0][0], view[1][0], view[2][0]);
	vec3 up = vec3(view[0][1], view[1][1], view[2][1]);
	vec3 world = iPos + (right * quad.x + up * quad.y) * size;

	ex_Uv = quad + 0.5;
	ex_LifeFrac = lifeFrac;

	if (iLife <= 0.0) {
		gl_Position = vec4(2.0, 2.0, 2.0, 1.0); // dead: push offscreen
	} else {
		gl_Position = projection * view * vec4(world, 1.0);
	}
}
