FrustumTests.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "../Tests.h"
  2. #include "core/Frustum.h"
  3. #define CV3(a, b, c) (&(CoreVector3){{a, b, c}})
  4. static void testToString() {
  5. CoreFrustum f;
  6. coreInitFrustum(&f, 60.0f, 0.1f, 1000.0f);
  7. char buffer[128];
  8. coreToStringFrustum(&f, buffer, sizeof(buffer));
  9. CORE_TEST_STRING("(tan = 0.577, nearClip = 0.100, farClip = 1000.000)",
  10. buffer);
  11. }
  12. static void testPointIsInside() {
  13. CoreIntVector2 size = {{200, 100}};
  14. CoreFrustum f;
  15. coreInitFrustum(&f, 60.0f, 0.1f, 1000.0f);
  16. coreUpdateFrustumPlanes(&f, CV3(0, 0, 0), CV3(1, 0, 0), CV3(0, 1, 0),
  17. CV3(0, 0, 1), &size);
  18. CORE_TEST_TRUE(coreIsInsideFrustum(&f, CV3(0, 0, 5)));
  19. CORE_TEST_FALSE(coreIsInsideFrustum(&f, CV3(0, 0, 1004)));
  20. CORE_TEST_FALSE(coreIsInsideFrustum(&f, CV3(0, 0, -5)));
  21. CORE_TEST_FALSE(coreIsInsideFrustum(&f, CV3(0, 50, 5)));
  22. CORE_TEST_FALSE(coreIsInsideFrustum(&f, CV3(0, -50, 5)));
  23. CORE_TEST_FALSE(coreIsInsideFrustum(&f, CV3(50, 0, 5)));
  24. CORE_TEST_FALSE(coreIsInsideFrustum(&f, CV3(-50, 0, 5)));
  25. }
  26. static void testSphereIsInside() {
  27. CoreIntVector2 size = {{200, 100}};
  28. CoreFrustum f;
  29. coreInitFrustum(&f, 60.0f, 0.1f, 1000.0f);
  30. coreUpdateFrustumPlanes(&f, CV3(0, 0, 0), CV3(1, 0, 0), CV3(0, 1, 0),
  31. CV3(0, 0, 1), &size);
  32. CORE_TEST_TRUE(coreIsInsideFrustumRadius(&f, CV3(0, 0, 5), 3));
  33. CORE_TEST_FALSE(coreIsInsideFrustumRadius(&f, CV3(0, 0, 1004), 3));
  34. CORE_TEST_FALSE(coreIsInsideFrustumRadius(&f, CV3(0, 0, -5), 3));
  35. CORE_TEST_FALSE(coreIsInsideFrustumRadius(&f, CV3(0, 50, 5), 3));
  36. CORE_TEST_FALSE(coreIsInsideFrustumRadius(&f, CV3(0, -50, 5), 3));
  37. CORE_TEST_FALSE(coreIsInsideFrustumRadius(&f, CV3(50, 0, 5), 3));
  38. CORE_TEST_FALSE(coreIsInsideFrustumRadius(&f, CV3(-50, 0, 5), 3));
  39. CORE_TEST_TRUE(coreIsInsideFrustumRadius(&f, CV3(0, 0, 5), 3));
  40. CORE_TEST_TRUE(coreIsInsideFrustumRadius(&f, CV3(0, 0, 1004), 50));
  41. CORE_TEST_TRUE(coreIsInsideFrustumRadius(&f, CV3(0, 0, -5), 50));
  42. CORE_TEST_TRUE(coreIsInsideFrustumRadius(&f, CV3(0, 50, 5), 50));
  43. CORE_TEST_TRUE(coreIsInsideFrustumRadius(&f, CV3(0, -50, 5), 50));
  44. CORE_TEST_TRUE(coreIsInsideFrustumRadius(&f, CV3(50, 0, 5), 50));
  45. CORE_TEST_TRUE(coreIsInsideFrustumRadius(&f, CV3(-50, 0, 5), 50));
  46. }
  47. static void testUpdateProjection() {
  48. CoreFrustum f;
  49. coreInitFrustum(&f, 60.0f, 0.1f, 1000.0f);
  50. const CoreMatrix* m =
  51. coreUpdateProjection(&f, &(CoreIntVector2){{400, 300}});
  52. char buffer[128];
  53. coreToStringMatrix(m, buffer, sizeof(buffer));
  54. CORE_TEST_STRING("[[1.299, 0.000, 0.000, 0.000], "
  55. "[0.000, 1.732, 0.000, 0.000], "
  56. "[0.000, 0.000, -1.000, -0.200], "
  57. "[0.000, 0.000, -1.000, 0.000]]",
  58. buffer);
  59. }
  60. void testFrustum() {
  61. testToString();
  62. testPointIsInside();
  63. testSphereIsInside();
  64. testUpdateProjection();
  65. }