<?xml version="1.0" encoding="UTF-8" ?>
<shader>
	<fragment><![CDATA[
		#version 150

		uniform float terrain_Height;
		uniform vec2 terrain_Size;

		uniform sampler2D terrain_Heightmap;

		smooth in vec2 frag_Position;

		out vec4 output_Colors;
		out vec4 output_Normals;

		vec3 CalculateNormal()
		{
			vec2 position = frag_Position;
			
			float s1 = textureOffset(terrain_Heightmap, position, ivec2(-1, 0)).x;
			float s2 = textureOffset(terrain_Heightmap, position, ivec2(1, 0)).x;
			float s3 = textureOffset(terrain_Heightmap, position, ivec2(0, -1)).x;
			float s4 = textureOffset(terrain_Heightmap, position, ivec2(0, 1)).x;
			vec3 v1 = normalize(vec3(-1.0, (s2 - s1), 0.0));
			vec3 v2 = normalize(vec3(0.0, (s4 - s3), -1.0));
			
			return normalize(cross(v1, v2));
		}

		void main()
		{
			output_Colors = vec4(0.0, 1.0, 0.0, 1.0);
			output_Normals = vec4(CalculateNormal(), 1.0);
		}
	]]></fragment>
	<vertex><![CDATA[
		#version 150

		uniform mat4 terrain_Projection;
		uniform mat4 terrain_View;

		uniform float terrain_Height;
		uniform vec2 terrain_Size;

		uniform sampler2D terrain_Heightmap;

		in vec2 terrain_Position;

		smooth out vec2 frag_Position;

		void main()
		{
			float h = texture(terrain_Heightmap, terrain_Position / terrain_Size).x * terrain_Height;
			gl_Position = terrain_Projection * terrain_View * vec4(terrain_Position.x, h, terrain_Position.y, 1.0);
			
			frag_Position = terrain_Position / terrain_Size;
		}
	]]></vertex>
</shader>
