FrustumTests.c 2.5 KB

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