forward.vert 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. const int MAX_BONES = 128;
  23. /* === Attributes === */
  24. layout(location = 0) in vec3 aPosition;
  25. layout(location = 1) in vec2 aTexCoord;
  26. layout(location = 2) in vec3 aNormal;
  27. layout(location = 3) in vec4 aColor;
  28. layout(location = 4) in vec4 aTangent;
  29. layout(location = 5) in ivec4 aBoneIDs;
  30. layout(location = 6) in vec4 aWeights;
  31. /* === Uniforms === */
  32. uniform mat4 uMatNormal;
  33. uniform mat4 uMatModel;
  34. uniform mat4 uMatMVP;
  35. uniform mat4 uMatLightVP[NUM_LIGHTS];
  36. uniform vec4 uAlbedoColor;
  37. uniform vec2 uTexCoordOffset;
  38. uniform vec2 uTexCoordScale;
  39. uniform mat4 uBoneMatrices[MAX_BONES];
  40. uniform bool uUseSkinning;
  41. /* === Varyings === */
  42. out vec3 vPosition;
  43. out vec2 vTexCoord;
  44. out vec4 vColor;
  45. out mat3 vTBN;
  46. out vec4 vPosLightSpace[NUM_LIGHTS];
  47. /* === Main program === */
  48. void main()
  49. {
  50. vec3 skinnedPosition = aPosition;
  51. vec3 skinnedNormal = aNormal;
  52. vec3 skinnedTangent = aTangent.xyz;
  53. if (uUseSkinning)
  54. {
  55. mat4 skinMatrix =
  56. aWeights.x * uBoneMatrices[aBoneIDs.x] +
  57. aWeights.y * uBoneMatrices[aBoneIDs.y] +
  58. aWeights.z * uBoneMatrices[aBoneIDs.z] +
  59. aWeights.w * uBoneMatrices[aBoneIDs.w];
  60. skinnedPosition = vec3(skinMatrix * vec4(aPosition, 1.0));
  61. skinnedNormal = mat3(skinMatrix) * aNormal;
  62. skinnedTangent = mat3(skinMatrix) * aTangent.xyz;
  63. }
  64. vec4 worldPosition = uMatModel * vec4(skinnedPosition, 1.0);
  65. vPosition = worldPosition.xyz;
  66. vTexCoord = uTexCoordOffset + aTexCoord * uTexCoordScale;
  67. vColor = aColor * uAlbedoColor;
  68. vec3 T = normalize(vec3(uMatModel * vec4(skinnedTangent, 0.0)));
  69. vec3 N = normalize(vec3(uMatNormal * vec4(skinnedNormal, 1.0)));
  70. vec3 B = normalize(cross(N, T)) * aTangent.w;
  71. vTBN = mat3(T, B, N);
  72. for (int i = 0; i < NUM_LIGHTS; i++)
  73. {
  74. vPosLightSpace[i] = uMatLightVP[i] * worldPosition;
  75. }
  76. gl_Position = uMatMVP * vec4(skinnedPosition, 1.0);
  77. }