normalmap.vs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #version 400
  2. layout (location = 0) in vec3 VertexPosition;
  3. layout (location = 1) in vec3 VertexNormal;
  4. layout (location = 2) in vec2 VertexTexCoord;
  5. layout (location = 3) in vec4 VertexTangent;
  6. struct LightInfo {
  7. vec4 Position; // Light position in eye coords.
  8. vec3 Intensity; // A,D,S intensity
  9. };
  10. uniform LightInfo Light;
  11. out vec3 LightDir;
  12. out vec2 TexCoord;
  13. out vec3 ViewDir;
  14. uniform mat4 ModelViewMatrix;
  15. uniform mat3 NormalMatrix;
  16. uniform mat4 ProjectionMatrix;
  17. uniform mat4 MVP;
  18. void main()
  19. {
  20. // Transform normal and tangent to eye space
  21. vec3 norm = normalize( NormalMatrix * VertexNormal );
  22. vec3 tang = normalize( NormalMatrix * vec3(VertexTangent) );
  23. // Compute the binormal
  24. vec3 binormal = normalize( cross( norm, tang ) ) * VertexTangent.w;
  25. // Matrix for transformation to tangent space
  26. mat3 toObjectLocal = mat3(
  27. tang.x, binormal.x, norm.x,
  28. tang.y, binormal.y, norm.y,
  29. tang.z, binormal.z, norm.z ) ;
  30. // Transform light direction and view direction to tangent space
  31. vec3 pos = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) );
  32. LightDir = normalize( toObjectLocal * (Light.Position.xyz - pos) );
  33. ViewDir = toObjectLocal * normalize(-pos);
  34. TexCoord = VertexTexCoord;
  35. gl_Position = MVP * vec4(VertexPosition,1.0);
  36. }