depth.vert 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* depth.vert -- Vertex shader used for dir/spot-lights shadow mapping
  2. *
  3. * Copyright (c) 2025 Le Juez Victor
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * For conditions of distribution and use, see accompanying LICENSE file.
  7. */
  8. #version 330 core
  9. /* === Includes === */
  10. #include "../include/billboard.glsl"
  11. /* === Attributes === */
  12. layout(location = 0) in vec3 aPosition;
  13. layout(location = 1) in vec2 aTexCoord;
  14. layout(location = 3) in vec4 aColor;
  15. layout(location = 5) in ivec4 aBoneIDs;
  16. layout(location = 6) in vec4 aWeights;
  17. /* === Instances Attributes === */
  18. layout(location = 10) in mat4 iMatModel;
  19. layout(location = 14) in vec4 iColor;
  20. /* === Uniforms === */
  21. uniform sampler1D uTexBoneMatrices;
  22. uniform mat4 uMatInvView; ///< Only for billboard modes
  23. uniform mat4 uMatModel;
  24. uniform mat4 uMatVP;
  25. uniform vec2 uTexCoordOffset;
  26. uniform vec2 uTexCoordScale;
  27. uniform float uAlpha;
  28. uniform bool uInstancing;
  29. uniform bool uSkinning;
  30. uniform int uBillboard;
  31. /* === Varyings === */
  32. out vec2 vTexCoord;
  33. out float vAlpha;
  34. /* === Helper functions === */
  35. mat4 BoneMatrix(int boneID)
  36. {
  37. int baseIndex = 4 * boneID;
  38. vec4 row0 = texelFetch(uTexBoneMatrices, baseIndex + 0, 0);
  39. vec4 row1 = texelFetch(uTexBoneMatrices, baseIndex + 1, 0);
  40. vec4 row2 = texelFetch(uTexBoneMatrices, baseIndex + 2, 0);
  41. vec4 row3 = texelFetch(uTexBoneMatrices, baseIndex + 3, 0);
  42. return transpose(mat4(row0, row1, row2, row3));
  43. }
  44. mat4 SkinMatrix(ivec4 boneIDs, vec4 weights)
  45. {
  46. return weights.x * BoneMatrix(boneIDs.x) +
  47. weights.y * BoneMatrix(boneIDs.y) +
  48. weights.z * BoneMatrix(boneIDs.z) +
  49. weights.w * BoneMatrix(boneIDs.w);
  50. }
  51. /* === Main function === */
  52. void main()
  53. {
  54. mat4 matModel = uMatModel;
  55. if (uSkinning) {
  56. mat4 sMatModel = SkinMatrix(aBoneIDs, aWeights);
  57. matModel = matModel * sMatModel;
  58. }
  59. if (uInstancing) {
  60. matModel = transpose(iMatModel) * matModel;
  61. }
  62. switch(uBillboard) {
  63. case BILLBOARD_NONE:
  64. break;
  65. case BILLBOARD_FRONT:
  66. BillboardFront(matModel, uMatInvView);
  67. break;
  68. case BILLBOARD_Y_AXIS:
  69. BillboardYAxis(matModel, uMatInvView);
  70. break;
  71. }
  72. vTexCoord = uTexCoordOffset + aTexCoord * uTexCoordScale;
  73. vAlpha = uAlpha * iColor.a * aColor.a;
  74. gl_Position = uMatVP * (matModel * vec4(aPosition, 1.0));
  75. }