FrustumTests.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(
  16. &f, &V(0, 0, 0), &V(1, 0, 0), &V(0, 1, 0), &V(0, 0, 1), &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(
  30. &f, &V(0, 0, 0), &V(1, 0, 0), &V(0, 1, 0), &V(0, 0, 1), &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(
  53. "[[1.299, 0.000, 0.000, 0.000], "
  54. "[0.000, 1.732, 0.000, 0.000], "
  55. "[0.000, 0.000, -1.000, -0.200], "
  56. "[0.000, 0.000, -1.000, 0.000]]",
  57. buffer);
  58. }
  59. void testFrustum() {
  60. testToString();
  61. testPointIsInside();
  62. testSphereIsInside();
  63. testUpdateProjection();
  64. }