/*#version 140*/
#extension GL_EXT_gpu_shader4 : enable          
			
/*#ifdef _VERSION_CHECK_
	#ifndef MSET

		#ifndef _NOT_INSTANCED_
		
			
			
		#endif
		
	#endif
#endif
*/

uniform sampler2D 	waveHeights0;
uniform sampler2D 	waveHeights1;
/*uniform sampler2D 	waveHeights2;
uniform sampler2D 	waveHeights3;
uniform sampler2D 	waveHeights4;
uniform sampler2D 	waveHeights5;
*/
uniform float 		tiling0;
uniform float 		tiling1;
uniform float 		tiling2;
uniform float 		tiling3;

uniform vec3		campos;
uniform vec3		camdir;

//uniform vec2		ofs;
uniform float		waterHeight;
uniform float		heightScale;
uniform float		heightScale0;
uniform float		heightScale1;
uniform float 		choppiness0;
uniform float 		choppiness1;

varying vec3		vY;
varying vec2 		texcoord0;
varying vec2 		texcoord1;
varying vec2 		texcoord2;
varying vec2 		texcoord3;
varying vec4		pos;
varying vec3		vdir;

// vertex attributes
attribute vec3 			s2_vertex;
attribute vec4			s2_texcoords; 

void main()
{
	// snap camera pos
	vec3 cd=normalize(vec3(camdir.x,0.0,camdir.z));
	vec3 c=floor((campos+cd*6400.0)/500.0)*vec3(500.0);
	vec4 vertex=vec4(s2_vertex,1.0);
	vertex.xyz+=vec3(c.x,0.0,c.z);	
	float fade2dist=(1.0-clamp((length(vec3(campos.x,0.0,campos.z)-vec3(vertex.x,0.0,vertex.z))-20000.0)/1500.0,0.0,1.0));
	//float h=heightScale*fade2dist;
	float h0=heightScale0*fade2dist;
	float h1=heightScale1*fade2dist;
	vY=vec3(0.0);
	
	vec2 cTexcoords=s2_texcoords.st+c.xz/vec2(25600.0);
	texcoord0 = (cTexcoords*tiling0/* + ofs*/);
	texcoord2 = (cTexcoords*tiling2/* - ofs*/);
	texcoord1 = (cTexcoords*-tiling1/* - ofs*/);
	texcoord3 = (cTexcoords*tiling3);
	
	/*vY=(	texture2D(waveHeights0,(texcoord0.st)).xzy+
			texture2D(waveHeights1,(texcoord0.st*0.1)).xzy+
			texture2D(waveHeights2,(texcoord0.st*0.01)).xzy+
			texture2D(waveHeights3,(texcoord0.st*0.001)).xzy+
			texture2D(waveHeights4,(texcoord0.st*0.00025)).xzy+
			texture2D(waveHeights5,(texcoord0.st*0.0000625)).xzy			
		)*vec3(choppiness,1000.0*h,-choppiness);
		
	vertex.xyz+=vY;*/
	
	vec3 waves=	texture2D(waveHeights0,(texcoord0.st)).xzy*vec3(choppiness0,1000.0*h0,-choppiness0) + 
				texture2D(waveHeights1,(texcoord2.st*0.125)).xzy*vec3(choppiness1,1000.0*h1,-choppiness1);
	vertex.xyz+=waves;
	
	vdir=campos-(vertex.xyz+vec3(0.0,waterHeight,0.0));
	gl_Position = gl_ModelViewProjectionMatrix*vec4(vertex.xyz,1.0);
	pos=gl_Position;
}