Returns 'false' if the bounding box is off the frustum. Returning 'true' does not guarantee that the object intersects the frustum or is inside it.
| 110 | |
| 111 | // Returns 'false' if the bounding box is off the frustum. Returning 'true' does not guarantee that the object intersects the frustum or is inside it. |
| 112 | bool TestFrustumOBB(const Frustum& frustum, const dmVMath::Matrix4& world, dmVMath::Vector3& aabb_min, dmVMath::Vector3& aabb_max) |
| 113 | { |
| 114 | // calculate coordinates of all 8 corners of the bounding cube. |
| 115 | // To find them we'll take all points P(Xi,Yj,Zk) i=aabb_min.x|aabb_max.x, j=aabb_min.y|aabb_max.y, k=aabb_min.z|aabb_max.z |
| 116 | dmVMath::Point3 point0 = dmVMath::Point3(aabb_min.getX(), aabb_min.getY(), aabb_min.getZ()); |
| 117 | dmVMath::Point3 point1 = dmVMath::Point3(aabb_min.getX(), aabb_min.getY(), aabb_max.getZ()); |
| 118 | dmVMath::Point3 point2 = dmVMath::Point3(aabb_min.getX(), aabb_max.getY(), aabb_min.getZ()); |
| 119 | dmVMath::Point3 point3 = dmVMath::Point3(aabb_min.getX(), aabb_max.getY(), aabb_max.getZ()); |
| 120 | dmVMath::Point3 point4 = dmVMath::Point3(aabb_max.getX(), aabb_min.getY(), aabb_min.getZ()); |
| 121 | dmVMath::Point3 point5 = dmVMath::Point3(aabb_max.getX(), aabb_min.getY(), aabb_max.getZ()); |
| 122 | dmVMath::Point3 point6 = dmVMath::Point3(aabb_max.getX(), aabb_max.getY(), aabb_min.getZ()); |
| 123 | dmVMath::Point3 point7 = dmVMath::Point3(aabb_max.getX(), aabb_max.getY(), aabb_max.getZ()); |
| 124 | |
| 125 | dmVMath::Vector4 corner_points[8]; // corner points in world coords |
| 126 | |
| 127 | corner_points[0] = world * point0; |
| 128 | corner_points[1] = world * point1; |
| 129 | corner_points[2] = world * point2; |
| 130 | corner_points[3] = world * point3; |
| 131 | corner_points[4] = world * point4; |
| 132 | corner_points[5] = world * point5; |
| 133 | corner_points[6] = world * point6; |
| 134 | corner_points[7] = world * point7; |
| 135 | |
| 136 | // for any of the six frustum planes if the all corner points lie in the negative halfspace, do cull the object |
| 137 | int num_planes = frustum.m_NumPlanes; |
| 138 | for (int plane_i = 0; plane_i < num_planes; ++plane_i) |
| 139 | { |
| 140 | // get plane normal/equation |
| 141 | bool positive_found = false; |
| 142 | for (int corner_i = 0; corner_i < 8; ++corner_i) |
| 143 | { |
| 144 | float distance = DistanceToPlane(frustum.m_Planes[plane_i], corner_points[corner_i]); |
| 145 | if (distance >= 0) |
| 146 | { |
| 147 | positive_found = true; |
| 148 | break; // no need to check for the rest of the points |
| 149 | } |
| 150 | } |
| 151 | if (!positive_found) // if all corners are in the negative halfspace for the plane, we're done |
| 152 | { |
| 153 | return false; // no intersection, do cull |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | return true; // inside the frustum but false positives may also happen. They are ok when used for frustum culling where the object will be hidden later in the rendering pipeline. |
| 158 | } |
| 159 | |
| 160 | } // dmIntersection |
no test coverage detected