#include "../shared.h"
#include "randomnumbers.h"
#include "program.h"

#define BALLCOUNT		128

__kernel void clear( write_only image2d_t outimg )
{
	int column = get_global_id( 0 );
	int line = get_global_id( 1 );
	if ((column >= 800) || (line >= 480)) return;
	write_imagef( outimg, (int2)(column, line), 0 );
}

__kernel void update( global float* balls )
{
	int idx = get_global_id( 0 );
	balls[idx * 6 + 0] += balls[idx * 6 + 2];
	balls[idx * 6 + 1] += balls[idx * 6 + 3];
}

__kernel void render( write_only image2d_t outimg, global float* balls )
{
	int column = get_global_id( 0 );
	int line = get_global_id( 1 );
	if ((column >= 800) || (line >= 480)) return;
	float2 uv = { (float)column / 800.0, (float)line / 480.0 };
	// draw balls
	for( int i = 0; i < BALLCOUNT; i++ )
	{
		float2 pos = { balls[i * 6], balls[i * 6 + 1] };
		float dist = length( pos - uv );
		if (dist < 0.02f)
		{
			write_imagef( outimg, (int2)(column, 479 - line), (float4)(1,0,0,1) );
			break;
		}
	}
}