#version 330

layout( location = 0 ) in vec3 InVertex;

out vec3 vNormal;
out vec3 vEye;
out vec2 vTexCoord;

uniform sampler2D uNoiseTexSampler;

uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix;
uniform mat4 uModelMatrix;

uniform float uTime;

out vec2 vTimeOffset;
out float vHeight;

void main()
{
	vec2 scaledCoords = ( InVertex.xz / (3000.0 - 0.1) ) * 0.5;
	vTexCoord = vec2( 0.5 ) + ( scaledCoords ) + vec2( uTime / 64.0, uTime / 16.0 );
	vTimeOffset = vec2( ( uTime * 32.0 ) * ( 1.0 / 1024.0 ) );
	float texHeight1 = texture( uNoiseTexSampler, vTexCoord - vec2( vTimeOffset.x * 4.0, 0.0 ) ).x;
	float texHeight1A = texture( uNoiseTexSampler, vTexCoord - vec2( 0.0, vTimeOffset.x * 8.0 ) ).x;
	float texHeight2 = texture( uNoiseTexSampler, vTexCoord + vec2( 0.0, vTimeOffset.x * 2.0 ) ).y;
	float texHeight3 = texture( uNoiseTexSampler, vTexCoord - vec2( vTimeOffset.x / 4.0, 0.0 ) ).z;
	float texHeight4 = texture( uNoiseTexSampler, vTexCoord + vec2( 0.0, vTimeOffset.x / 8.0 ) ).w;
	vec3 pos = InVertex;
	float height = texHeight1 * 256.0;//( texHeight1 + texHeight1A * 0.5 + texHeight2 * 0.75 + texHeight3 * 0.5 + texHeight4 * 0.125 ) * 50.0;
	vHeight = pow( height, 1.0 );
	pos.y += vHeight;
	//pos.x += sin( vHeight / 200.0 ) * 256.0;
	//pos.z -= cos( vHeight / 200.0 ) * 128.0;
	/*
	vec2 waveK = vec2( 4.0, 0.0 );
	float waveLength = 8.0;
	float kLambda = ( 2.0 * 3.14 ) / waveLength;
	float waveAmp = 64.0;
	float waveDot = dot( waveK, pos.xz );
	float waveFreq = sqrt( kLambda * 9.8 );
	pos.xz = pos.xz - ( waveK / kLambda ) * waveAmp * sin( waveDot - waveFreq * uTime );
	pos.y = waveAmp * cos( waveDot - waveFreq * uTime );
	*/
	vEye = vec3( ( uViewMatrix * uModelMatrix ) * vec4( pos, 1.0 ) );
	vNormal = normalize( transpose( inverse( mat3( uViewMatrix * uModelMatrix ) ) ) * vec3( 0.0, 1.0, 0.0 ) );
	gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4( pos, 1.0 );
}