#version 440

layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec4 color;
layout(location = 3) in vec2 texCoord;
layout(location = 4) in int drawId;

uniform mat4 viewProjectionMatrix;
uniform mat4 viewMatrix;

out vec4 outColor;
out vec3 outNormal;
out vec4 outPosition;
out vec4 outEyePosition;
out vec2 outTextureCoordinate;
out int outDrawId;
out vec3 cameraDirection;

#include "vertexHelper.hglsl"

void main()
{
  mat4 modelMatrix = getModelMatrix(drawId);

  outColor = color;
  outPosition = viewProjectionMatrix * modelMatrix * vec4(pos, 1.0f);
  outEyePosition = viewMatrix * modelMatrix * vec4(pos, 1.0f);
  gl_Position = outPosition;
  outNormal = normalize(mul(viewMatrix * modelMatrix, normal).xyz);
  outTextureCoordinate = texCoord;
  outDrawId = drawId;
  cameraDirection = vec3(viewMatrix[2][0], viewMatrix[2][1], viewMatrix[2][2]);
}

#version 440

layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec4 color;
layout(location = 3) in vec2 texCoord;
layout(location = 4) in int drawId;

uniform mat4 viewProjectionMatrix;
uniform mat4 viewMatrix;

out vec4 outColor;
out vec3 outNormal;
out vec4 outPosition;
out vec4 outEyePosition;
out vec2 outTextureCoordinate;
out int outDrawId;
out vec3 cameraDirection;

#include "vertexHelper.hglsl"

void main()
{
  mat4 modelMatrix = getModelMatrix(drawId);

  outColor = color;
  outPosition = viewProjectionMatrix * modelMatrix * vec4(pos, 1.0f);
  outEyePosition = viewMatrix * modelMatrix * vec4(pos, 1.0f);
  gl_Position = outPosition;
  outNormal =
      normalize(mul(transpose(inverse(viewMatrix * modelMatrix)), normal).xyz);
  outTextureCoordinate = texCoord;
  outDrawId = drawId;
  cameraDirection = vec3(viewMatrix[2][0], viewMatrix[2][1], viewMatrix[2][2]);
}

Fix normal transformation by calculating the normal matrix.
Fix normal transformation by calculating the normal matrix.

This is quite an expensive operation, but passing the normal matrix
to every object is probably worse considering that only the phong shader
needs it.
