Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <spu_intrinsics.h>
  3.  
  4. struct vector3_t
  5. {
  6. float x, y, z;
  7. };
  8.  
  9. struct matrix43_t
  10. {
  11. struct vector3_t row0;
  12. struct vector3_t row1;
  13. struct vector3_t row2;
  14. struct vector3_t row3;
  15. };
  16.  
  17. struct aabb_t
  18. {
  19. struct vector3_t min;
  20. struct vector3_t max;
  21. };
  22.  
  23. struct plane_t
  24. {
  25. float x, y, z, w;
  26. };
  27.  
  28. struct frustum_t
  29. {
  30. struct plane_t planes[6];
  31. };
  32.  
  33. inline void transform_point(struct vector3_t* p, const struct matrix43_t* mat)
  34. {
  35. struct vector3_t op = *p;
  36.  
  37. #define COMP(c) p->c = op.x * mat->row0.c + op.y * mat->row1.c + op.z * mat->row2.c + mat->row3.c
  38.  
  39. COMP(x); COMP(y); COMP(z);
  40.  
  41. #undef COMP
  42. }
  43.  
  44. inline float dot(const struct vector3_t* v, const struct plane_t* p)
  45. {
  46. return v->x * p->x + v->y * p->y + v->z * p->z + p->w;
  47. }
  48.  
  49. __attribute__((noinline)) bool is_visible(struct matrix43_t* transform, struct aabb_t* aabb, struct frustum_t* frustum)
  50. {
  51. // get aabb points
  52. struct vector3_t points[] =
  53. {
  54. { aabb->min.x, aabb->min.y, aabb->min.z },
  55. { aabb->max.x, aabb->min.y, aabb->min.z },
  56. { aabb->max.x, aabb->max.y, aabb->min.z },
  57. { aabb->min.x, aabb->max.y, aabb->min.z },
  58.  
  59. { aabb->min.x, aabb->min.y, aabb->max.z },
  60. { aabb->max.x, aabb->min.y, aabb->max.z },
  61. { aabb->max.x, aabb->max.y, aabb->max.z },
  62. { aabb->min.x, aabb->max.y, aabb->max.z }
  63. };
  64.  
  65. // transform points to world space
  66. for (int i = 0; i < 8; ++i)
  67. {
  68. transform_point(points + i, transform);
  69. }
  70.  
  71. // for each plane...
  72. for (int i = 0; i < 6; ++i)
  73. {
  74. bool inside = false;
  75.  
  76. for (int j = 0; j < 8; ++j)
  77. {
  78. if (dot(points + j, frustum->planes + i) > 0)
  79. {
  80. inside = true;
  81. break;
  82. }
  83. }
  84.  
  85. if (!inside)
  86. {
  87. return false;
  88. }
  89. }
  90.  
  91. return true;
  92. }
  93.  
  94. // simple ortho frustum
  95. struct frustum_t frustum =
  96. {
  97. {
  98. { 1, 0, 0, 10 },
  99. { -1, 0, 0, 10 },
  100. { 0, 1, 0, 10 },
  101. { 0, -1, 0, 10 },
  102. { 0, 0, 1, 10 },
  103. { 0, 0, -1, 10 }
  104. }
  105. };
  106.  
  107. // small box
  108. struct aabb_t aabb =
  109. {
  110. { -1, -2, -3 },
  111. { 1, 2, 3 }
  112. };
  113.  
  114. // and some weird matrix
  115. struct matrix43_t transform =
  116. {
  117. { 0.123f, 0.456f, 0.789f, },
  118. { 0.456f, 0.123f, 0.789f, },
  119. { 0.789f, 0.123f, 0.456f, },
  120. { 1, -1, 1 }
  121. };
  122.  
  123. void _start()
  124. {
  125. is_visible(&transform, &aabb, &frustum);
  126. si_stop(0);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement