FunctionsMisc.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. void extractClipPlane(const Mat4& mvp, FrustumPlaneType id, Plane& plane)
  8. {
  9. #define ANKI_CASE(i, a, op, b) \
  10. case i: \
  11. { \
  12. const Vec4 planeEqationCoefs = mvp.getRow(a) op mvp.getRow(b); \
  13. const Vec4 n = planeEqationCoefs.xyz0(); \
  14. const F32 len = n.getLength(); \
  15. plane = Plane(n / len, -planeEqationCoefs.w() / len); \
  16. break; \
  17. }
  18. switch(id)
  19. {
  20. ANKI_CASE(FrustumPlaneType::NEAR, 3, +, 2)
  21. ANKI_CASE(FrustumPlaneType::FAR, 3, -, 2)
  22. ANKI_CASE(FrustumPlaneType::LEFT, 3, +, 0)
  23. ANKI_CASE(FrustumPlaneType::RIGHT, 3, -, 0)
  24. ANKI_CASE(FrustumPlaneType::TOP, 3, -, 1)
  25. ANKI_CASE(FrustumPlaneType::BOTTOM, 3, +, 1)
  26. default:
  27. ANKI_ASSERT(0);
  28. }
  29. #undef ANKI_CASE
  30. }
  31. void extractClipPlanes(const Mat4& mvp, Array<Plane, 6>& planes)
  32. {
  33. for(U i = 0; i < 6; ++i)
  34. {
  35. extractClipPlane(mvp, static_cast<FrustumPlaneType>(i), planes[i]);
  36. }
  37. }
  38. void computeEdgesOfFrustum(F32 far, F32 fovX, F32 fovY, Vec4 points[4])
  39. {
  40. // This came from unprojecting. It works, don't touch it
  41. const F32 x = far * tan(fovY / 2.0f) * fovX / fovY;
  42. const F32 y = tan(fovY / 2.0f) * far;
  43. const F32 z = -far;
  44. points[0] = Vec4(x, y, z, 0.0f); // top right
  45. points[1] = Vec4(-x, y, z, 0.0f); // top left
  46. points[2] = Vec4(-x, -y, z, 0.0f); // bot left
  47. points[3] = Vec4(x, -y, z, 0.0f); // bot right
  48. }
  49. } // end namespace anki