/*
 * vertex shader for phong shading,
 * used when rendering reflected objects on water surface
 *
 * this shader does the same as the normal phong-shader,
 * but it takes care of the flipped y-axis and defines a
 * clipping plane on the water surface
 *
 * written by:
 * christian moellinger <ch.moellinger@gmail.com>
 * 11/2010
 */

#version 330 core

uniform mat4 Camera_ProjectionMatrix;
uniform mat4 Camera_TransformMatrix;
uniform mat4 Model_TransformMatrix[63];
uniform mat3 NormalMatrix;
uniform vec4 Light0Position;

in vec3 in_Position;
in vec3 in_Normal;
in vec3 in_Texcoord;

out mat3 NormalMatrix2;
out vec3 my_Normal;
out vec3 my_Texcoord;
out vec3 my_LightDir;
out vec3 my_EyeDir;
out vec4 my_WorldPosition;

void main(void)
{
  mat4 ProjectionMatrix = Camera_ProjectionMatrix;
  mat4 ModelViewMatrix = Camera_TransformMatrix * Model_TransformMatrix[gl_InstanceID];

  gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(in_Position, 1.0);

  vec3 vVertex = (ModelViewMatrix * vec4(in_Position, 1.0)).xyz;
  vec4 ReflectedLight0Position = vec4(Light0Position.x, -Light0Position.y, Light0Position.z, Light0Position.w);

  vec3 my_Lightpos = (Camera_TransformMatrix * ReflectedLight0Position).xyz;

  NormalMatrix2 = NormalMatrix;
  my_Normal = in_Normal;
  my_Texcoord = in_Texcoord;
  my_LightDir = my_Lightpos - vVertex;
  my_EyeDir = -vVertex;
  my_WorldPosition = Model_TransformMatrix[gl_InstanceID] * vec4(in_Position, 1.0);

  gl_ClipDistance[0] = dot((Model_TransformMatrix[gl_InstanceID] * vec4(in_Position, 1.0)).xyz, vec3(0, -1, 0));
}
