forward.vert 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2025 Le Juez Victor
  3. *
  4. * This software is provided "as-is", without any express or implied warranty. In no event
  5. * will the authors be held liable for any damages arising from the use of this software.
  6. *
  7. * Permission is granted to anyone to use this software for any purpose, including commercial
  8. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  9. *
  10. * 1. The origin of this software must not be misrepresented; you must not claim that you
  11. * wrote the original software. If you use this software in a product, an acknowledgment
  12. * in the product documentation would be appreciated but is not required.
  13. *
  14. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  15. * as being the original software.
  16. *
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #version 330 core
  20. /* === Defines === */
  21. #define NUM_LIGHTS 8
  22. /* === Constants === */
  23. const int MAX_BONES = 128;
  24. /* === Attributes === */
  25. layout(location = 0) in vec3 aPosition;
  26. layout(location = 1) in vec2 aTexCoord;
  27. layout(location = 2) in vec3 aNormal;
  28. layout(location = 3) in vec4 aColor;
  29. layout(location = 4) in vec4 aTangent;
  30. layout(location = 5) in ivec4 aBoneIDs;
  31. layout(location = 6) in vec4 aWeights;
  32. /* === Uniforms === */
  33. uniform mat4 uMatNormal;
  34. uniform mat4 uMatModel;
  35. uniform mat4 uMatMVP;
  36. uniform mat4 uMatLightVP[NUM_LIGHTS];
  37. uniform vec4 uAlbedoColor;
  38. uniform vec2 uTexCoordOffset;
  39. uniform vec2 uTexCoordScale;
  40. uniform mat4 uBoneMatrices[MAX_BONES];
  41. uniform bool uUseSkinning;
  42. /* === Varyings === */
  43. out vec3 vPosition;
  44. out vec2 vTexCoord;
  45. out vec4 vColor;
  46. out mat3 vTBN;
  47. out vec4 vPosLightSpace[NUM_LIGHTS];
  48. /* === Main program === */
  49. void main()
  50. {
  51. vec3 skinnedPosition = aPosition;
  52. vec3 skinnedNormal = aNormal;
  53. vec3 skinnedTangent = aTangent.xyz;
  54. if (uUseSkinning)
  55. {
  56. mat4 skinMatrix =
  57. aWeights.x * uBoneMatrices[aBoneIDs.x] +
  58. aWeights.y * uBoneMatrices[aBoneIDs.y] +
  59. aWeights.z * uBoneMatrices[aBoneIDs.z] +
  60. aWeights.w * uBoneMatrices[aBoneIDs.w];
  61. skinnedPosition = vec3(skinMatrix * vec4(aPosition, 1.0));
  62. skinnedNormal = mat3(skinMatrix) * aNormal;
  63. skinnedTangent = mat3(skinMatrix) * aTangent.xyz;
  64. }
  65. vec4 worldPosition = uMatModel * vec4(skinnedPosition, 1.0);
  66. vPosition = worldPosition.xyz;
  67. vTexCoord = uTexCoordOffset + aTexCoord * uTexCoordScale;
  68. vColor = aColor * uAlbedoColor;
  69. vec3 T = normalize(vec3(uMatModel * vec4(skinnedTangent, 0.0)));
  70. vec3 N = normalize(vec3(uMatNormal * vec4(skinnedNormal, 1.0)));
  71. vec3 B = normalize(cross(N, T)) * aTangent.w;
  72. vTBN = mat3(T, B, N);
  73. for (int i = 0; i < NUM_LIGHTS; i++)
  74. {
  75. vPosLightSpace[i] = uMatLightVP[i] * worldPosition;
  76. }
  77. gl_Position = uMatMVP * vec4(skinnedPosition, 1.0);
  78. }