/*
 * vertex shader for heightmap rendering
 *
 * renders the given mesh and sets each y-coordinate
 * by using information from the given texture
 *
 * written by:
 * christian moellinger <ch.moellinger@gmail.com>
 * 01/2011
 */

#version 330 core

uniform mat4 Camera_ProjectionMatrix;
uniform mat4 Camera_TransformMatrix;
uniform mat4 Camera_TempTransformMatrix;
uniform mat4 Model_TransformMatrix;
uniform mat3 NormalMatrix;

uniform sampler2D heightmap;

in vec3 in_Position;

out vec2 texcoords;
out vec3 my_ObjPosition;
out vec4 finalPosition;

/*out mat4 NormalMatrix2;*/

void main(void)
{
  const float terrain_resolution = 256;

  mat4 ProjectionMatrix = Camera_ProjectionMatrix;
  mat4 ModelViewMatrix = Camera_TransformMatrix * Model_TransformMatrix;

  vec3 real_in_Position = in_Position;
  real_in_Position.x += gl_InstanceID / terrain_resolution;

  texcoords = vec2(real_in_Position.x, real_in_Position.z);

  my_ObjPosition = real_in_Position;

  my_ObjPosition.y = (texture2D(heightmap, texcoords).r - 0.5) * 0.1;

  finalPosition = ProjectionMatrix * ModelViewMatrix * vec4(my_ObjPosition, 1.0);

  gl_Position = finalPosition;
}
