Guest User

Untitled

a guest
Oct 8th, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. bool should_draw_chunk(glm::vec2 chunk_world_offset, glm::vec2 chunk_size, const Camera3D::Frustum& view_frustum) {
  2.     glm::vec2 min = chunk_world_offset;
  3.     glm::vec2 max = chunk_world_offset + chunk_size;
  4.  
  5.     std::array<glm::vec3, 4> corners = {
  6.         glm::vec3(min.x, 0.0f, min.y),
  7.         glm::vec3(min.x, 0.0f, max.y),
  8.         glm::vec3(max.x, 0.0f, min.y),
  9.         glm::vec3(max.x, 0.0f, max.y),
  10.         /* glm::vec3(min.x, -10.0f, min.y),
  11.         glm::vec3(min.x, -10.0f, max.y),
  12.         glm::vec3(max.x, -10.0f, min.y),
  13.         glm::vec3(max.x, -10.0f, max.y) */
  14.     };
  15.  
  16.     auto plane_test = [&](const Camera3D::Plane& plane) {
  17.         // If all corners are outside this plane, the chunk is culled
  18.         for (auto& c : corners) {
  19.             float dist = glm::dot(plane.normal, c) - plane.distance;
  20.             if (dist >= 0.0f) {
  21.                 return true; // at least one corner inside
  22.             }
  23.         }
  24.         return false; // all outside
  25.     };
  26.  
  27.     if (!plane_test(view_frustum.left_face))  return false;
  28.     if (!plane_test(view_frustum.right_face)) return false;
  29.     if (!plane_test(view_frustum.near_face))  return false;
  30.     if (!plane_test(view_frustum.far_face))   return false;
  31.  
  32.     return true;
  33. }
  34.  
  35.  
  36.  
  37.  
  38. Camera3D::Camera3D(u32 width, u32 height, glm::vec3 position, glm::mat4 projection, float fov_y, float near, float far)
  39. : projection(projection), width(width), height(height), position(position), yaw(glm::radians(-90.0f)), pitch(0.0f), fov_y(fov_y), near(near), far(far) {}
  40.  
  41. Camera3D::Frustum Camera3D::create_frustum() const {
  42.     Frustum frustum;
  43.     const float halfVSide = far * std::tanf(fov_y * 0.5f);
  44.     const float halfHSide = halfVSide * (float(width) / float(height));
  45.     const glm::vec3 forward = get_forward();
  46.     const glm::vec3 right = glm::cross(forward, up);
  47.     const glm::vec3 frontMultFar = far * forward;
  48.  
  49.     frustum.near_face = { position + near * forward, forward };
  50.     frustum.far_face = { position + frontMultFar, -forward };
  51.     frustum.right_face = { position,
  52.                             glm::cross(frontMultFar - right * halfHSide, up) };
  53.     frustum.left_face = { position,
  54.                             glm::cross(up,frontMultFar + right * halfHSide) };
  55.     frustum.top_face = { position,
  56.                             glm::cross(right, frontMultFar - up * halfVSide) };
  57.     frustum.bottom_face = { position,
  58.                             glm::cross(frontMultFar + up * halfVSide, right) };
  59.  
  60.     return frustum;
  61. }
  62.  
  63.  
  64.  
  65.  
  66. struct Plane {
  67.     glm::vec3 normal = { 0.0f, 1.0f, 0.0f };
  68.     float distance = 0.0f;
  69.  
  70.     Plane() {}
  71.  
  72.     Plane(const glm::vec3& point, const glm::vec3& normal)
  73.     : normal(glm::normalize(normal)), distance(glm::dot(this->normal, point)) {}
  74. };
  75.  
  76. struct Frustum {
  77.     Plane top_face;
  78.     Plane bottom_face;
  79.  
  80.     Plane right_face;q
  81.     Plane left_face;
  82.  
  83.     Plane far_face;
  84.     Plane near_face;
  85. };
  86.    
Advertisement
Add Comment
Please, Sign In to add comment