TazGraph Project v0.1.0
Loading...
Searching...
No Matches
SelectionFrustum.h
1#pragma once
2
3#include "../../pch.h"
4
6 glm::vec3 corners[8];
7 glm::vec4 planes[6]; // left, right, bottom, top, near, far
8
9 // Create frustum from screen-space selection box
10 bool createFromSelectionBox(const glm::vec2& startPos, const glm::vec2& endPos,
11 ICamera* camera, float nearZ = 100.0f, float farZ = 300000.0f) {
12 // Ensure min/max order
13 glm::vec2 minPos = glm::min(startPos, endPos);
14 glm::vec2 maxPos = glm::max(startPos, endPos);
15
16 // Convert screen coordinates to world coordinates at different Z depths
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);
21
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);
26
27 // Store corners
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;
36
37 // Calculate frustum planes (normal pointing inward)
38 // Left plane
39 planes[0] = calculatePlane(nearTopLeft, nearBottomLeft, farBottomLeft);
40 // Right plane
41 planes[1] = calculatePlane(nearBottomRight, nearTopRight, farTopRight);
42 // Bottom plane
43 planes[2] = calculatePlane(nearBottomLeft, nearBottomRight, farBottomRight);
44 // Top plane
45 planes[3] = calculatePlane(nearTopRight, nearTopLeft, farTopLeft);
46 // Near plane
47 planes[4] = calculatePlane(nearTopLeft, nearTopRight, nearBottomRight);
48 // Far plane
49 planes[5] = calculatePlane(farTopRight, farTopLeft, farBottomLeft);
50
51 bool isValid = true;
52 for (int i = 0; i < 6; ++i) {
53 if (glm::any(glm::isnan(glm::vec3(planes[i]))) || std::isnan(planes[i].w)) {
54 isValid = false;
55 break;
56 }
57 }
58
59 return isValid;
60 }
61
62private:
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);
69 }
70};
Definition ICamera.h:10
Definition SelectionFrustum.h:5