Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. //Plane.h
  2. #pragma once
  3. #include "glm/glm.hpp"
  4. class Plane
  5. {
  6. public:
  7. float d;
  8. glm::vec3 normal;
  9. Plane() {
  10. this->normal = glm::vec3(0);
  11. this->d = 0;
  12. };
  13. void set3Points(glm::vec3 p0, glm::vec3 p1, glm::vec3 p2) {
  14. this->normal = glm::normalize(glm::cross(p1 - p0, p2 - p0));
  15. this->d = glm::dot(-this->normal, p0);
  16. };
  17. void setNormalAndPoint(glm::vec3 normal, glm::vec3 point) {
  18. this->normal = normal;
  19. this->d = glm::dot(-this->normal, point);
  20. };
  21. };
  22.  
  23. //Frustum.h
  24. #include "glm/glm.hpp"
  25. #include "Plane.h"
  26. class Frustum
  27. {
  28. private:
  29. enum {
  30. TOP = 0, BOTTOM, LEFT,
  31. RIGHT, NEARPLANE, FARPLANE
  32. };
  33. static enum { OUTSIDE, INTERSECT, INSIDE };
  34. Plane plane[6];
  35. glm::vec3 nearTopLeft, nearTopRight, nearBottomLeft, nearBottomRight, farTopLeft, farTopRight, farBottomLeft, farBottomRight;
  36. float nearDistance, farDistance, ratio, viewAngle;
  37. float nearWidth, nearHeight, farWidth, farHeight;
  38. public:
  39. Frustum(float viewAngle, float ratio, float nearDistance, float farDistance);
  40. void setCamDef(glm::vec3 pos, glm::vec3 dir, glm::vec3 up, glm::vec3 right);
  41. bool pointInFrustum(glm::vec3 point);
  42. bool sphereInFrustum(glm::vec3 center, float radius);
  43. //int boxInFrustum(AABox &b);
  44. };
  45.  
  46. //Frustum.cpp
  47. #include "Frustum.h"
  48.  
  49. Frustum::Frustum(float viewAngle, float ratio, float nearDistance, float farDistance)
  50. {
  51. // store the information
  52. this->ratio = ratio;
  53. this->viewAngle = viewAngle;
  54. this->nearDistance = nearDistance;
  55. this->farDistance = farDistance;
  56.  
  57. // compute width and height of the near and far plane sections
  58. float tangHolder = glm::tan(glm::radians(this->viewAngle) / 2);
  59. this->nearHeight = tangHolder * this->nearDistance;
  60. this->nearWidth = this->nearHeight * this->ratio;
  61. this->farHeight = tangHolder * this->farDistance;
  62. this->farWidth = this->farHeight * this->ratio;
  63. }
  64.  
  65. void Frustum::setCamDef(glm::vec3 pos, glm::vec3 dir, glm::vec3 up, glm::vec3 right)
  66. {
  67. glm::vec3 vectorHolder, normalHolder;
  68. glm::vec3 nearCenter = pos + dir * this->nearDistance;
  69. glm::vec3 farCenter = pos + dir * this->farDistance;
  70.  
  71. this->plane[NEARPLANE].setNormalAndPoint(-dir, nearCenter);
  72. this->plane[FARPLANE].setNormalAndPoint(dir, farCenter);
  73.  
  74. vectorHolder = glm::normalize((nearCenter + up * this->nearHeight) - pos);
  75. normalHolder = glm::cross(right, vectorHolder);
  76. this->plane[TOP].setNormalAndPoint(normalHolder, pos);
  77.  
  78. vectorHolder = glm::normalize((nearCenter - up * this->nearHeight) - pos);
  79. normalHolder = glm::cross(vectorHolder, right);
  80. this->plane[BOTTOM].setNormalAndPoint(normalHolder, pos);
  81.  
  82. vectorHolder = glm::normalize((nearCenter - right * this->nearWidth) - pos);
  83. normalHolder = glm::cross(up, vectorHolder);
  84. this->plane[LEFT].setNormalAndPoint(normalHolder, pos);
  85.  
  86. vectorHolder = glm::normalize((nearCenter + right * this->nearWidth) - pos);
  87. normalHolder = glm::cross(vectorHolder, up);
  88. this->plane[RIGHT].setNormalAndPoint(normalHolder, pos);
  89. }
  90.  
  91. bool Frustum::pointInFrustum(glm::vec3 point)
  92. {
  93. if (0 >= glm::dot(this->plane[FARPLANE].normal, point) + this->plane[FARPLANE].d
  94. && 0 >= glm::dot(this->plane[NEARPLANE].normal, point) + this->plane[NEARPLANE].d
  95. && 0 >= glm::dot(this->plane[TOP].normal, point) + this->plane[TOP].d
  96. && 0 >= glm::dot(this->plane[BOTTOM].normal, point) + this->plane[BOTTOM].d
  97. && 0 >= glm::dot(this->plane[RIGHT].normal, point) + this->plane[RIGHT].d
  98. && 0 >= glm::dot(this->plane[LEFT].normal, point) + this->plane[LEFT].d)
  99. {
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107.  
  108. bool Frustum::sphereInFrustum(glm::vec3 center, float radius)
  109. {
  110. if (0 >= glm::dot(this->plane[FARPLANE].normal, center - this->plane[FARPLANE].normal * 0.5f) + this->plane[FARPLANE].d
  111. && 0 >= glm::dot(this->plane[NEARPLANE].normal, center) + this->plane[NEARPLANE].d
  112. && 0 >= glm::dot(this->plane[TOP].normal, center - this->plane[TOP].normal * radius * 1.5f) + this->plane[TOP].d
  113. && 0 >= glm::dot(this->plane[BOTTOM].normal, center) + this->plane[BOTTOM].d
  114. && 0 >= glm::dot(this->plane[RIGHT].normal, center) + this->plane[RIGHT].d
  115. && 0 >= glm::dot(this->plane[LEFT].normal, center) + this->plane[LEFT].d)
  116. {
  117. return true;
  118. }
  119. else
  120. {
  121. return false;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement