shadowmatrixgeneration.comp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. uniform mat4 SunCamMatrix;
  2. layout (local_size_x = 4) in;
  3. struct CascadeBoundingBox
  4. {
  5. int xmin;
  6. int xmax;
  7. int ymin;
  8. int ymax;
  9. int zmin;
  10. int zmax;
  11. };
  12. layout (std430) buffer BoundingBoxes
  13. {
  14. CascadeBoundingBox BB[4];
  15. };
  16. layout (std140) buffer NewMatrixData
  17. {
  18. mat4 ShadowMatrixes[4];
  19. };
  20. mat4 buildProjectionMatrixOrthoLH(float left, float right, float up, float down, float zNear, float zFar)
  21. {
  22. mat4 M;
  23. M[0] = vec4(2. / (right - left), 0., 0., 0.);
  24. M[1] = vec4(0., 2. / (up - down), 0., 0.);
  25. M[2] = vec4(0., 0., 1. / (zFar - zNear), 0.);
  26. M[3].x = - (right + left) / (right - left);
  27. M[3].y = - (up + down) / (up - down);
  28. M[3].z = zNear / (zNear - zFar);
  29. M[3].w = 1.;
  30. return M;
  31. }
  32. void main()
  33. {
  34. if (gl_LocalInvocationIndex > 3)
  35. return;
  36. ShadowMatrixes[gl_LocalInvocationIndex] = buildProjectionMatrixOrthoLH(
  37. BB[gl_LocalInvocationIndex].xmin / 4. - 1., BB[gl_LocalInvocationIndex].xmax / 4. + 1.,
  38. BB[gl_LocalInvocationIndex].ymax / 4. + 1., BB[gl_LocalInvocationIndex].ymin / 4. - 1.,
  39. BB[gl_LocalInvocationIndex].zmin / 4. - 100., BB[gl_LocalInvocationIndex].zmax / 4. + 1.) * SunCamMatrix;
  40. }