
>> vertex << //////////////////////////////

vec3 IN_position;
uniform vec2 viewport;
varying	vec4 OUT_position;
varying	vec2 OUT_texCoords;


void main()
{
	IN_position = gl_Vertex.xyz;
    float ar = viewport.y / viewport.x;
    float targetAr = 400.0/600.0;
    OUT_texCoords = (IN_position.xy + 1.0) / 2;
    OUT_position = vec4(IN_position, 1.0);
    if (ar/targetAr < 1)
        gl_Position = OUT_position * vec4(ar/targetAr,1,0,1);
    else
        gl_Position = OUT_position * vec4(1,targetAr/ar,0,1);
}

>> vertex-water << //////////////////////////////

vec3 IN_position;
uniform vec2 viewport;
varying	vec4 OUT_position;
varying	vec2 OUT_texCoords;

void main()
{
	IN_position = gl_Vertex.xyz;

		
	OUT_texCoords = (IN_position.xy + 1.0) / 2;
    OUT_position = vec4(IN_position, 1.0);
    gl_Position = OUT_position * vec4(1,1,0,1);
}

>> fragment-water << //////////////////////////////
// #extension GL_EXT_gpu_shader4 : enable //Enabled for texelFetch

uniform sampler2D colorTexture;
uniform vec2 viewport;
uniform vec4 cameraPosition;
uniform vec4 healthRadius;
uniform vec4 playerPosition;
uniform vec4 cameraWorldPosition;
uniform vec4 delta;
uniform vec4 uniform_colors[8];
uniform vec4 uniform_backcol;

varying	vec4 OUT_position;
varying	vec2 OUT_texCoords;

#define NEW_REFLECTION 1

void main()
{
	vec2 pixelPosition = (cameraWorldPosition.xy - OUT_position.xy*vec2(-300,200)); // correct AR
	vec2 playerToPixelPosition = playerPosition.xy-pixelPosition;

	// do the pixelation effect of the health
	playerToPixelPosition /= 2;
	playerToPixelPosition -= fract(playerToPixelPosition);
	playerToPixelPosition *= 2;

	float distanceToPlayer = length(playerToPixelPosition);
	distanceToPlayer /= 350;

	float healthAmount = healthRadius.x/healthRadius.y;
	if (healthAmount > 0.99)
		healthAmount = 1000;
		
    vec2 texCoords = OUT_texCoords;
	texCoords.y = 1.0-texCoords.y;

	float cameraYViewSpace = cameraPosition.x;
	float wobbleWave = cameraPosition.y;

	float seaGradient = (texCoords.y) + (cameraYViewSpace-0.5); // below 0 means above sea level, above 0 means deeper
	if (seaGradient < 0) 
		seaGradient = 0;
#if NEW_REFLECTION
	seaGradient = 0;
#endif		
	float wobblingGradient = ((texCoords.y+cameraYViewSpace)*350+wobbleWave*7.0);
	float wobbleWidth = 1.0/2.5; // screen scale
	float maxWobbleWidth = 1;

	float xWobbleOffset = clamp((sin(wobblingGradient)*wobbleWidth),-maxWobbleWidth,maxWobbleWidth) * clamp(seaGradient, 0,0.01);
#if !NEW_REFLECTION
	vec2 wobblingTexCoords = texCoords + vec2(xWobbleOffset, 0);
#else
	vec2 wobblingTexCoords  = texCoords;
#endif
	float fader = clamp(seaGradient * 5.0, 0, 1);

	vec4 finalColor;

	vec2 requiredTexCoords;
	if (seaGradient > 0)
		requiredTexCoords = wobblingTexCoords;
	else
		requiredTexCoords = texCoords;

	finalColor = texture2D(colorTexture, requiredTexCoords);
	vec4 backCol = uniform_backcol;
#if NEW_REFLECTION
	if(((texCoords.y) + (cameraYViewSpace-0.5)) > 0)
	{
		float delta = 0.01;
		if (length(finalColor.xyz-uniform_colors[1].xyz) < delta)
			finalColor = uniform_colors[3];
		else if (length(finalColor.xyz-uniform_colors[2].xyz) < delta)
			finalColor = uniform_colors[3];
		else if (length(finalColor.xyz-uniform_colors[4].xyz) < delta)
			finalColor = uniform_backcol; //no silly bg cloud, looks weird
		else if (length(finalColor.xyz-uniform_colors[5].xyz) < delta)
			finalColor = uniform_colors[6];
		else if (length(finalColor.xyz-uniform_colors[6].xyz) < delta)
			finalColor = uniform_backcol; //no silly bg cloud, looks weird
		if (length(finalColor.xyz-uniform_colors[7].xyz) < delta)
			finalColor = uniform_colors[6];
	}
#endif
	if (finalColor.a == 0)
	{
#if !NEW_REFLECTION
		if (healthAmount < distanceToPlayer && seaGradient == 0)
#else
		if (healthAmount < distanceToPlayer && ((texCoords.y) + (cameraYViewSpace-0.5)) < 0)
#endif
		{
			if(healthRadius.z  < 1 ) gl_FragColor = uniform_colors[7];
			else gl_FragColor = uniform_colors[6];
		}
		else
		{
			gl_FragColor = backCol;
		}
		return;
	}
	gl_FragColor = (1.0 - fader) * finalColor + fader * backCol;
}

>> fragment << //////////////////////////////
// #extension GL_EXT_gpu_shader4 : enable //Enabled for texelFetch

uniform sampler2D colorTexture;
uniform sampler2D diffuseTex;
varying	vec4 OUT_position;
varying	vec2 OUT_texCoords;

uniform vec4 uniform_colors[8];
uniform vec4 cameraPosition;

void main()
{
    vec2 tc = OUT_texCoords;

	// vec4 cta = texelFetch(colorTexture,ivec2(tc * textureSize2D(colorTexture, 0)), 0);
	vec4 cta = texture2D(colorTexture,tc);
	
	if (cta.rgb == vec4(1).rgb) // TODO: Why?
		cta = uniform_colors[7];
		
	

	gl_FragColor = cta;
	//gl_FragColor.rgb = thergb;
}

>> vertex/CG << //////////////////////////////
	
struct input
{
float3 position 	: POSITION;
};

struct output
{
float4 position;
float4 gl_Position : POSITION;
float2 texCoords;
};

output main(input IN, uniform float2 viewport)
{
float ar = viewport.y / viewport.x;
float targetAr = 400.0/600.0;
output OUT;
OUT.texCoords = (IN.position.xy + 1.0) / 2.0;
OUT.position = float4(IN.position, 1.0);
if (ar/targetAr < 1.0)
{
OUT.gl_Position = OUT.position * float4(ar/targetAr,1,0,1);
}
else
{
OUT.gl_Position = OUT.position * float4(1,targetAr/ar,0,1);
}
return OUT;
}


>> vertex-water/CG << //////////////////////////////
	
struct input
{
float3 position 	: POSITION;
};

struct output
{
float4 position;
float4 gl_Position : POSITION;
float2 texCoords;
};

output main(input IN, uniform float2 viewport)
{
output OUT;
OUT.texCoords = (IN.position.xy + 1.0) / 2.0;
OUT.position = float4(IN.position, 1.0);
OUT.gl_Position = OUT.position * float4(1,1,0,1);
return OUT;
}


>> fragment-water/CG << //////////////////////////////
	
struct input
{
float4 position;
float4 gl_Position : POSITION;
float2 texCoords;
};

struct output
{
float4 color 	: COLOR;
};

#define NEW_REFLECTION 1

output main(input IN,
uniform sampler2D colorTexture : TEXUNIT0,
uniform float2 viewport,
uniform float4 cameraPosition,
uniform float4 healthRadius,
uniform float4 playerPosition,
uniform float4 cameraWorldPosition,
uniform float4 delta,
uniform float4 uniform_backcol,
uniform float4 uniform_colors[8])
{
	float2 pixelPosition = (cameraWorldPosition.xy - IN.position.xy*float2(-300,200));
	float2 playerToPixelPosition = playerPosition.xy-pixelPosition;
	float distanceToPlayer = length(playerToPixelPosition);
	distanceToPlayer /= 350;
	float healthAmount = healthRadius.x/healthRadius.y;
	if (healthAmount > 0.99)
		healthAmount = 1000;
	float2 texCoords = IN.texCoords;
	texCoords.y = 1.0-texCoords.y;
	float cameraYViewSpace = cameraPosition.x;
	float wobbleWave = cameraPosition.y;
	float seaGradient = (texCoords.y) + (cameraYViewSpace-0.5); // below 0 means above sea level, above 0 means deeper
	if (seaGradient < 0) 
		seaGradient = 0;
#if NEW_REFLECTION
	seaGradient = 0;
#endif
	float wobblingGradient = ((texCoords.y+cameraYViewSpace)*350+wobbleWave*7.0);
	float wobbleWidth = 1.0/2.5; // screen scale
	float maxWobbleWidth = 1;
	float xWobbleOffset = clamp((sin(wobblingGradient)*wobbleWidth),-maxWobbleWidth,maxWobbleWidth) * clamp(seaGradient, 0,0.01);
#if !NEW_REFLECTION
	float2 wobblingTexCoords = texCoords + float2(xWobbleOffset, 0);
#else
	float2 wobblingTexCoords = texCoords;
#endif
	float fader = clamp(seaGradient * 5.0, 0, 1);
	float4 finalColor;
	float2 requiredTexCoords;
	if (seaGradient > 0)
		requiredTexCoords = wobblingTexCoords;
	else
		requiredTexCoords = texCoords;
	finalColor = tex2D(colorTexture, requiredTexCoords);
	float4 backCol = uniform_backcol;
#if NEW_REFLECTION
	if(((texCoords.y) + (cameraYViewSpace-0.5)) > 0)
	{
		float delta = 0.01;
		if (length(finalColor.xyz-uniform_colors[1].xyz) < delta)
			finalColor = uniform_colors[3];
		else if (length(finalColor.xyz-uniform_colors[2].xyz) < delta)
			finalColor = uniform_colors[3];
		else if (length(finalColor.xyz-uniform_colors[4].xyz) < delta)
			finalColor = uniform_backcol; //no silly bg cloud, looks weird
		else if (length(finalColor.xyz-uniform_colors[5].xyz) < delta)
			finalColor = uniform_colors[6];			
		else if (length(finalColor.xyz-uniform_colors[6].xyz) < delta)
			finalColor = uniform_backcol; //no silly bg cloud, looks weird
		else if (length(finalColor.xyz-uniform_colors[7].xyz) < delta)
			finalColor = uniform_colors[6];
	}
#endif

	output OUT;

	if (finalColor.a == 0)
	{
#if !NEW_REFLECTION
		if (healthAmount < distanceToPlayer && seaGradient == 0)
#else
		if (healthAmount < distanceToPlayer && ((texCoords.y) + (cameraYViewSpace-0.5)) < 0)
#endif
		{
			if(healthRadius.z  < 1 ) OUT.color = uniform_colors[7];
			else OUT.color = uniform_colors[6];
		}
		else
		{
			OUT.color = backCol;
		}
		return OUT;
	}
	OUT.color = (1.0 - fader) * finalColor + fader * backCol;
	return OUT;
}


>> fragment/CG << //////////////////////////////
	
struct input
{
float4 position;
float4 gl_Position : POSITION;
float2 texCoords;
};

struct output
{
float4 color 	: COLOR;
};

uniform float4 uniform_colors[8];
uniform float4 cameraPosition;

output main(input IN,
uniform sampler2D colorTexture: TEXUNIT0,
uniform float4 uniform_colors[8],
uniform float4 cameraPosition ) : COLOR
{
float2 tc = (IN.texCoords + 1.0) / 2.0;
float4 cta = tex2D(colorTexture,tc);

//if (cta.rgb == float3(1))
if (cta.r == 1.0 && cta.g == 1.0 && cta.b == 1.0)
cta = uniform_colors[7];

output OUT;

OUT.color = cta;
OUT.color.a = 1.0;
return OUT;
}

