Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. bool Frustum::checkSphere(const PrimitiveSphere& sphere, const WorldPoint& were) const noexcept {
  2.  
  3. const auto pos = were.position + sphere.center;
  4. for (const auto& plane : planes) {
  5.  
  6. const float radius = sphere.radius * were.scale.unitMagnitude(); //unit magnitude is just x + y + z / 3
  7. const float dist =
  8. plane.x * pos.x +
  9. plane.y * pos.y +
  10. plane.z * pos.z + plane.w;
  11. if (dist < -radius)
  12. return false;
  13. }
  14. return true;
  15. }
  16.  
  17. void Frustum::fromViewProj(const float* const vp) noexcept {
  18.  
  19. const float xw = vp[3];
  20. const float yw = vp[7];
  21. const float zw = vp[11];
  22. const float ww = vp[15];
  23.  
  24. const float xz = vp[2];
  25. const float yz = vp[6];
  26. const float zz = vp[10];
  27. const float wz = vp[14];
  28.  
  29. PrimitivePlane& near = planes[0];
  30. PrimitivePlane& far = planes[1];
  31. PrimitivePlane& left = planes[2];
  32. PrimitivePlane& right = planes[3];
  33. PrimitivePlane& top = planes[4];
  34. PrimitivePlane& bottom = planes[5];
  35.  
  36. near.x = xw - xz;
  37. near.y = yw - yz;
  38. near.z = zw - zz;
  39. near.w = ww - wz;
  40.  
  41. far.x = xw + xz;
  42. far.y = yw + yz;
  43. far.z = zw + zz;
  44. far.w = ww + wz;
  45.  
  46. const float xx = vp[0];
  47. const float yx = vp[4];
  48. const float zx = vp[8];
  49. const float wx = vp[12];
  50.  
  51. left.x = xw - xx;
  52. left.y = yw - yx;
  53. left.z = zw - zx;
  54. left.w = ww - wx;
  55.  
  56. right.x = xw + xx;
  57. right.y = yw + yx;
  58. right.z = zw + zx;
  59. right.w = ww + wx;
  60.  
  61. const float xy = vp[1];
  62. const float yy = vp[5];
  63. const float zy = vp[9];
  64. const float wy = vp[13];
  65.  
  66. top.x = xw + xy;
  67. top.y = yw + yy;
  68. top.z = zw + zy;
  69. top.w = ww + wy;
  70.  
  71. bottom.x = xw - xy;
  72. bottom.y = yw - yy;
  73. bottom.z = zw - zy;
  74. bottom.w = ww - wy;
  75.  
  76. for (auto& plane : planes)
  77. plane.normalize();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement