FrustumTests.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "../Tests.h"
  2. #include "core/Frustum.h"
  3. #include "core/ToString.h"
  4. static void testToString() {
  5. Frustum f;
  6. initFrustum(&f, 60.0f, 0.1f, 1000.0f);
  7. TEST_FLOAT(0.577f, f.tan, 0.01f);
  8. TEST_FLOAT(0.100f, f.nearClip, 0.01f);
  9. TEST_FLOAT(1000.0f, f.farClip, 0.01f);
  10. }
  11. static void testPointIsInside() {
  12. Frustum f;
  13. initFrustum(&f, 60.0f, 0.1f, 1000.0f);
  14. updateFrustumPlanes(&f, &V(0, 0, 0), &V(1, 0, 0), &V(0, 1, 0), &V(0, 0, 1),
  15. &IV(200, 100));
  16. TEST_TRUE(isInsideFrustum(&f, &V(0, 0, 5)));
  17. TEST_FALSE(isInsideFrustum(&f, &V(0, 0, 1004)));
  18. TEST_FALSE(isInsideFrustum(&f, &V(0, 0, -5)));
  19. TEST_FALSE(isInsideFrustum(&f, &V(0, 50, 5)));
  20. TEST_FALSE(isInsideFrustum(&f, &V(0, -50, 5)));
  21. TEST_FALSE(isInsideFrustum(&f, &V(50, 0, 5)));
  22. TEST_FALSE(isInsideFrustum(&f, &V(-50, 0, 5)));
  23. }
  24. static void testSphereIsInside() {
  25. IntVector2 size = {{200, 100}};
  26. Frustum f;
  27. initFrustum(&f, 60.0f, 0.1f, 1000.0f);
  28. updateFrustumPlanes(&f, &V(0, 0, 0), &V(1, 0, 0), &V(0, 1, 0), &V(0, 0, 1),
  29. &size);
  30. TEST_TRUE(isInsideFrustumRadius(&f, &V(0, 0, 5), 3));
  31. TEST_FALSE(isInsideFrustumRadius(&f, &V(0, 0, 1004), 3));
  32. TEST_FALSE(isInsideFrustumRadius(&f, &V(0, 0, -5), 3));
  33. TEST_FALSE(isInsideFrustumRadius(&f, &V(0, 50, 5), 3));
  34. TEST_FALSE(isInsideFrustumRadius(&f, &V(0, -50, 5), 3));
  35. TEST_FALSE(isInsideFrustumRadius(&f, &V(50, 0, 5), 3));
  36. TEST_FALSE(isInsideFrustumRadius(&f, &V(-50, 0, 5), 3));
  37. TEST_TRUE(isInsideFrustumRadius(&f, &V(0, 0, 5), 3));
  38. TEST_TRUE(isInsideFrustumRadius(&f, &V(0, 0, 1004), 50));
  39. TEST_TRUE(isInsideFrustumRadius(&f, &V(0, 0, -5), 50));
  40. TEST_TRUE(isInsideFrustumRadius(&f, &V(0, 50, 5), 50));
  41. TEST_TRUE(isInsideFrustumRadius(&f, &V(0, -50, 5), 50));
  42. TEST_TRUE(isInsideFrustumRadius(&f, &V(50, 0, 5), 50));
  43. TEST_TRUE(isInsideFrustumRadius(&f, &V(-50, 0, 5), 50));
  44. }
  45. static void testUpdateProjection() {
  46. Frustum f;
  47. initFrustum(&f, 60.0f, 0.1f, 1000.0f);
  48. const Matrix* m = updateProjection(&f, &IV(400, 300));
  49. char buffer[128];
  50. toString(m, buffer, sizeof(buffer));
  51. TEST_STRING("[[1.299, 0.000, 0.000, 0.000], "
  52. "[0.000, 1.732, 0.000, 0.000], "
  53. "[0.000, 0.000, -1.000, -0.200], "
  54. "[0.000, 0.000, -1.000, 0.000]]",
  55. buffer);
  56. }
  57. void testFrustum() {
  58. testToString();
  59. testPointIsInside();
  60. testSphereIsInside();
  61. testUpdateProjection();
  62. }