/*
 * vertex shader for bump lighting
 *
 * 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[128];
uniform mat3 NormalMatrix;
uniform vec4 Light0Position;

//uniform int instance;

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

out vec3 my_Texcoord;
out vec3 my_LightDir;
out vec3 my_EyeDir;
out vec3 my_ObjPosition;
out mat4 my_ModelviewMatrix;

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

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

  vec3 vVertex = (ModelViewMatrix * vec4(in_Position, 1.0)).xyz;
  vec3 my_Lightpos = (ModelViewMatrix * Light0Position).xyz;

  vec3 tempVec = my_Lightpos - vVertex;

  my_Texcoord = in_Texcoord;
  my_EyeDir = vVertex;

  vec3 n = normalize(NormalMatrix * in_Normal);
  vec3 t = normalize(NormalMatrix * in_Tangent);
  vec3 b = cross(n, t);

  vec3 v;
  v.x = dot(tempVec, t);
  v.y = dot(tempVec, b);
  v.z = dot(tempVec, n);
  my_LightDir = normalize(v);

  tempVec = -vVertex;
  v.x = dot(tempVec, t);
  v.y = dot(tempVec, b);
  v.z = dot(tempVec, n);

  my_EyeDir = normalize(v);
}
