Skip to content

Commit 321d865

Browse files
[Engine] Is in frustum function for camera
1 parent 7bd4444 commit 321d865

3 files changed

Lines changed: 35 additions & 12 deletions

File tree

Engine/MauEng/Private/GUI/ImGUILayer.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,8 @@ namespace MauEng
8282
auto v{ scene->GetECSWorld().View<CDebugText, CTransform>() };
8383
v.Each([drawList, pos, &viewProj, pWindow, defaultFont, &cameraPos = camera->GetPosition()](CDebugText const& d, CTransform const& t)
8484
{
85-
glm::vec4 clipSpacePos{ viewProj * glm::vec4{ t.translation, 1.0f } };
86-
87-
if (clipSpacePos.w != 0.0f)
88-
{
89-
clipSpacePos /= clipSpacePos.w;
90-
}
91-
92-
float const x{ (clipSpacePos.x * 0.5f + 0.5f) * pWindow->width };
93-
float const y{ (clipSpacePos.y * 0.5f + 0.5f) * pWindow->height };
94-
95-
if (clipSpacePos.z > 0.0f && clipSpacePos.z < 1.0f)
85+
auto const res{ Camera::IsInFrustum(t.translation, viewProj, pWindow->width, pWindow->height) };
86+
if (res.first)
9687
{
9788
float const distance{ glm::distance(cameraPos, t.translation) };
9889
float const fontSize{ d.scaleWithDistance ? glm::clamp(d.baseFontSize / distance, d.minFontSize, d.maxFontSize)
@@ -116,7 +107,7 @@ namespace MauEng
116107
case VerticalAlignment::Bottom: offsetY = -textSize.y; break;
117108
}
118109

119-
drawList->AddText(defaultFont, fontSize,ImVec2{ pos.x + x + offsetX, pos.y + y + offsetY }, IM_COL32(d.colour.r * 255.f, d.colour.g * 255.f, d.colour.b * 255.f, d.colour.a * 255.f), d.text.c_str());
110+
drawList->AddText(defaultFont, fontSize,ImVec2{ pos.x + res.second.x + offsetX, pos.y + res.second.y + offsetY }, IM_COL32(d.colour.r * 255.f, d.colour.g * 255.f, d.colour.b * 255.f, d.colour.a * 255.f), d.text.c_str());
120111
}
121112
});
122113
}

Engine/MauEng/Private/Scene/Camera.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,31 @@ namespace MauEng
153153

154154
m_EnableExposure = true;
155155
}
156+
157+
std::pair<bool, glm::vec2> Camera::IsInFrustum(glm::vec3 const& pos, glm::mat4 const& viewProj,
158+
uint32_t windowWidth, uint32_t windowHeight) noexcept
159+
{
160+
glm::vec4 clipSpacePos{ viewProj * glm::vec4{ pos, 1.0f } };
161+
162+
if (clipSpacePos.w != 0.0f)
163+
{
164+
clipSpacePos /= clipSpacePos.w;
165+
}
166+
167+
float const x{ (clipSpacePos.x * 0.5f + 0.5f) * windowWidth };
168+
float const y{ (clipSpacePos.y * 0.5f + 0.5f) * windowHeight };
169+
170+
if (clipSpacePos.z > 0.0f && clipSpacePos.z < 1.0f)
171+
{
172+
return { true, glm::vec2{ x, y } };
173+
}
174+
175+
return { false, glm::vec2{ x, y } };
176+
}
177+
178+
std::pair<bool, glm::vec2> Camera::IsInFrustum(glm::vec3 const& pos, uint32_t windowWidth,
179+
uint32_t windowHeight) noexcept
180+
{
181+
return IsInFrustum(pos, m_ViewMatrix * m_ProjectionMatrix, windowWidth, windowHeight);
182+
}
156183
}

Engine/MauEng/Public/Scene/Camera.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ namespace MauEng
6363
void SetCamSettingsSunny16() noexcept;
6464
void SetCamSettingsIndoor() noexcept;
6565

66+
// Returns true if the position is in the frustum, and returns the screen position if it is
67+
[[nodiscard]] static std::pair<bool, glm::vec2> IsInFrustum(glm::vec3 const& pos, glm::mat4 const& viewProj, uint32_t windowWidth, uint32_t windowHeight) noexcept;
68+
// Returns true if the position is in the frustum, and returns the screen position if it is
69+
[[nodiscard]] std::pair<bool, glm::vec2> IsInFrustum(glm::vec3 const& pos, uint32_t windowWidth, uint32_t windowHeight) noexcept;
70+
6671
Camera(Camera const&) = default;
6772
Camera(Camera&&) = default;
6873
Camera& operator=(Camera const&) = default;

0 commit comments

Comments
 (0)