You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * rPBR [shader] - Physically based rendering vertex shader
  4. *
  5. * Copyright (c) 2017 Victor Fisac
  6. *
  7. **********************************************************************************************/
  8. #version 330
  9. // Input vertex attributes
  10. in vec3 vertexPosition;
  11. in vec2 vertexTexCoord;
  12. in vec3 vertexNormal;
  13. in vec4 vertexTangent;
  14. // Input uniform values
  15. uniform mat4 mvp;
  16. uniform mat4 mMatrix;
  17. // Output vertex attributes (to fragment shader)
  18. out vec3 fragPosition;
  19. out vec2 fragTexCoord;
  20. out vec3 fragNormal;
  21. out vec3 fragTangent;
  22. out vec3 fragBinormal;
  23. void main()
  24. {
  25. // Calculate binormal from vertex normal and tangent
  26. vec3 vertexBinormal = cross(vertexNormal, vec3(vertexTangent));
  27. // Calculate fragment normal based on normal transformations
  28. mat3 normalMatrix = transpose(inverse(mat3(mMatrix)));
  29. // Calculate fragment position based on model transformations
  30. fragPosition = vec3(mMatrix*vec4(vertexPosition, 1.0f));
  31. // Send vertex attributes to fragment shader
  32. fragTexCoord = vertexTexCoord;
  33. fragNormal = normalize(normalMatrix*vertexNormal);
  34. fragTangent = normalize(normalMatrix*vec3(vertexTangent));
  35. fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal)*fragNormal);
  36. fragBinormal = normalize(normalMatrix*vertexBinormal);
  37. fragBinormal = cross(fragNormal, fragTangent);
  38. // Calculate final vertex position
  39. gl_Position = mvp*vec4(vertexPosition, 1.0);
  40. }