FunctionsMisc.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Collision/Functions.h>
  6. namespace anki
  7. {
  8. void extractClipPlane(const Mat4& mvp, FrustumPlaneType id, Plane& plane)
  9. {
  10. #define ANKI_CASE(i, a, op, b) \
  11. case i: \
  12. { \
  13. const Vec4 planeEqationCoefs = mvp.getRow(a) op mvp.getRow(b); \
  14. const Vec4 n = planeEqationCoefs.xyz0(); \
  15. const F32 len = n.getLength(); \
  16. plane = Plane(n / len, -planeEqationCoefs.w() / len); \
  17. break; \
  18. }
  19. switch(id)
  20. {
  21. ANKI_CASE(FrustumPlaneType::NEAR, 3, +, 2)
  22. ANKI_CASE(FrustumPlaneType::FAR, 3, -, 2)
  23. ANKI_CASE(FrustumPlaneType::LEFT, 3, +, 0)
  24. ANKI_CASE(FrustumPlaneType::RIGHT, 3, -, 0)
  25. ANKI_CASE(FrustumPlaneType::TOP, 3, -, 1)
  26. ANKI_CASE(FrustumPlaneType::BOTTOM, 3, +, 1)
  27. default:
  28. ANKI_ASSERT(0);
  29. }
  30. #undef ANKI_CASE
  31. }
  32. void extractClipPlanes(const Mat4& mvp, Array<Plane, 6>& planes)
  33. {
  34. for(U i = 0; i < 6; ++i)
  35. {
  36. extractClipPlane(mvp, static_cast<FrustumPlaneType>(i), planes[i]);
  37. }
  38. }
  39. void computeEdgesOfFrustum(F32 far, F32 fovX, F32 fovY, Vec4 points[4])
  40. {
  41. // This came from unprojecting. It works, don't touch it
  42. const F32 x = far * tan(fovY / 2.0f) * fovX / fovY;
  43. const F32 y = tan(fovY / 2.0f) * far;
  44. const F32 z = -far;
  45. points[0] = Vec4(x, y, z, 0.0f); // top right
  46. points[1] = Vec4(-x, y, z, 0.0f); // top left
  47. points[2] = Vec4(-x, -y, z, 0.0f); // bot left
  48. points[3] = Vec4(x, -y, z, 0.0f); // bot right
  49. }
  50. } // end namespace anki