10 bool createFromSelectionBox(
const glm::vec2& startPos,
const glm::vec2& endPos,
11 ICamera* camera,
float nearZ = 100.0f,
float farZ = 300000.0f) {
13 glm::vec2 minPos = glm::min(startPos, endPos);
14 glm::vec2 maxPos = glm::max(startPos, endPos);
17 glm::vec3 nearBottomLeft = camera->convertScreenToWorldDistance(glm::vec2(minPos.x, maxPos.y), nearZ);
18 glm::vec3 nearBottomRight = camera->convertScreenToWorldDistance(glm::vec2(maxPos.x, maxPos.y), nearZ);
19 glm::vec3 nearTopLeft = camera->convertScreenToWorldDistance(glm::vec2(minPos.x, minPos.y), nearZ);
20 glm::vec3 nearTopRight = camera->convertScreenToWorldDistance(glm::vec2(maxPos.x, minPos.y), nearZ);
22 glm::vec3 farBottomLeft = camera->convertScreenToWorldDistance(glm::vec2(minPos.x, maxPos.y), farZ);
23 glm::vec3 farBottomRight = camera->convertScreenToWorldDistance(glm::vec2(maxPos.x, maxPos.y), farZ);
24 glm::vec3 farTopLeft = camera->convertScreenToWorldDistance(glm::vec2(minPos.x, minPos.y), farZ);
25 glm::vec3 farTopRight = camera->convertScreenToWorldDistance(glm::vec2(maxPos.x, minPos.y), farZ);
28 corners[0] = nearBottomLeft;
29 corners[1] = nearBottomRight;
30 corners[2] = nearTopRight;
31 corners[3] = nearTopLeft;
32 corners[4] = farBottomLeft;
33 corners[5] = farBottomRight;
34 corners[6] = farTopRight;
35 corners[7] = farTopLeft;
39 planes[0] = calculatePlane(nearTopLeft, nearBottomLeft, farBottomLeft);
41 planes[1] = calculatePlane(nearBottomRight, nearTopRight, farTopRight);
43 planes[2] = calculatePlane(nearBottomLeft, nearBottomRight, farBottomRight);
45 planes[3] = calculatePlane(nearTopRight, nearTopLeft, farTopLeft);
47 planes[4] = calculatePlane(nearTopLeft, nearTopRight, nearBottomRight);
49 planes[5] = calculatePlane(farTopRight, farTopLeft, farBottomLeft);
52 for (
int i = 0; i < 6; ++i) {
53 if (glm::any(glm::isnan(glm::vec3(planes[i]))) || std::isnan(planes[i].w)) {
63 glm::vec4 calculatePlane(
const glm::vec3& p1,
const glm::vec3& p2,
const glm::vec3& p3) {
64 glm::vec3 v1 = p2 - p1;
65 glm::vec3 v2 = p3 - p1;
66 glm::vec3 normal = glm::normalize(glm::cross(v1, v2));
67 float d = -glm::dot(normal, p1);
68 return glm::vec4(normal, d);