1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| void Frustum::updatePlanes(const glm::vec3 cameraPos, const glm::quat rotation, float fovy, float AR, float near, float far) { float tanHalfFOVy = tan(fovy / 2.0f); float near_height = near * tanHalfFOVy; float near_width = near_height * AR;
glm::vec3 right = rotation * glm::vec3(1.0f, 0.0f, 0.0f); glm::vec3 forward = rotation * glm::vec3(0.0f, 0.0f, 1.0f); glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 nearCenter = cameraPos + forward * near; glm::vec3 farCenter = cameraPos + forward * far;
glm::vec3 point; glm::vec3 normal;
pl[NEARP].setNormalAndPoint(forward, nearCenter);
pl[FARP].setNormalAndPoint(-forward, farCenter);
point = nearCenter + up * near_height; normal = glm::normalize(point - cameraPos); normal = glm::cross(right, normal); pl[TOP].setNormalAndPoint(normal, point);
point = nearCenter - up * near_height; normal = glm::normalize(point - cameraPos); normal = glm::cross(normal, right); pl[BOTTOM].setNormalAndPoint(normal, point);
point = nearCenter - right * near_width; normal = glm::normalize(point - cameraPos); normal = glm::cross(up, normal); pl[LEFT].setNormalAndPoint(normal, point);
point = nearCenter + right * near_width; normal = glm::normalize(point - cameraPos); normal = glm::cross(normal, up); pl[RIGHT].setNormalAndPoint(normal, point); }
bool Frustum::checkIfInside(BoundingBox* box) { for (int i = 0; i < 6; ++i) { int out = 0; out += ((pl[i].distance(glm::vec3(box->min.x, box->min.y, box->min.z)) < 0.0) ? 1 : 0); out += ((pl[i].distance(glm::vec3(box->max.x, box->min.y, box->min.z)) < 0.0) ? 1 : 0); out += ((pl[i].distance(glm::vec3(box->min.x, box->max.y, box->min.z)) < 0.0) ? 1 : 0); out += ((pl[i].distance(glm::vec3(box->max.x, box->max.y, box->min.z)) < 0.0) ? 1 : 0); out += ((pl[i].distance(glm::vec3(box->min.x, box->min.y, box->max.z)) < 0.0) ? 1 : 0); out += ((pl[i].distance(glm::vec3(box->max.x, box->min.y, box->max.z)) < 0.0) ? 1 : 0); out += ((pl[i].distance(glm::vec3(box->min.x, box->max.y, box->max.z)) < 0.0) ? 1 : 0); out += ((pl[i].distance(glm::vec3(box->max.x, box->max.y, box->max.z)) < 0.0) ? 1 : 0);
if (out == 8) return false; } return true; }
|