Skip to content

Commit 288621f

Browse files
committed
Fix debug visualizations with cropped 2D view
1 parent 9561593 commit 288621f

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

src/rendering/vulkan_imgui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void RND_Renderer::ImGuiOverlay::Render(long frameIdx, bool renderBackground) {
415415
// When cropped to 16:9, the 3D image fills the full window; otherwise
416416
// it is centered with the VR headset's aspect ratio.
417417
if (shouldCrop3DTo16_9) {
418-
DebugDraw::instance().Render(glm::vec2(0.0f, 0.0f), glm::vec2(windowSize.x, windowSize.y));
418+
DebugDraw::instance().Render(glm::vec2(0.0f, 0.0f), glm::vec2(windowSize.x, windowSize.y), glm::vec2(croppedUv0.x, croppedUv0.y), glm::vec2(croppedUv1.x, croppedUv1.y));
419419
}
420420
else {
421421
DebugDraw::instance().Render(glm::vec2(centerPos.x, centerPos.y), glm::vec2(squishedWindowSize.x, squishedWindowSize.y));

src/utils/debug_draw.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void DebugDraw::Frustum(const glm::mat4& viewProjection, uint32_t color, float t
3232

3333
static constexpr float NEAR_CLIP_W = 0.001f;
3434

35-
bool DebugDraw::ProjectPoint(const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec3& worldPos, glm::vec4& clipOut, ImVec2& screenOut) {
35+
bool DebugDraw::ProjectPoint(const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec2& uvMin, const glm::vec2& uvMax, const glm::vec3& worldPos, glm::vec4& clipOut, ImVec2& screenOut) {
3636
clipOut = vp * glm::vec4(worldPos, 1.0f);
3737

3838
if (clipOut.w <= NEAR_CLIP_W) {
@@ -43,14 +43,18 @@ bool DebugDraw::ProjectPoint(const glm::mat4& vp, const glm::vec2& viewportPos,
4343
float ndcX = clipOut.x * invW;
4444
float ndcY = clipOut.y * invW;
4545

46-
// NDC [-1,1] -> viewport sub-region in ImGui logical coords
47-
screenOut.x = viewportPos.x + (ndcX * 0.5f + 0.5f) * viewportSize.x;
48-
screenOut.y = viewportPos.y + (-ndcY * 0.5f + 0.5f) * viewportSize.y; // Y flipped (screen Y goes down)
46+
glm::vec2 uvRange = glm::max(uvMax - uvMin, glm::vec2(0.0001f));
47+
float u = ndcX * 0.5f + 0.5f;
48+
float v = -ndcY * 0.5f + 0.5f;
49+
50+
// NDC [-1,1] -> cropped UV range -> viewport sub-region in ImGui logical coords
51+
screenOut.x = viewportPos.x + ((u - uvMin.x) / uvRange.x) * viewportSize.x;
52+
screenOut.y = viewportPos.y + ((v - uvMin.y) / uvRange.y) * viewportSize.y; // Y flipped (screen Y goes down)
4953

5054
return true;
5155
}
5256

53-
void DebugDraw::DrawClippedLine(ImDrawList* drawList, const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec3& a, const glm::vec3& b, uint32_t color, float thickness) {
57+
void DebugDraw::DrawClippedLine(ImDrawList* drawList, const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec2& uvMin, const glm::vec2& uvMax, const glm::vec3& a, const glm::vec3& b, uint32_t color, float thickness) {
5458
glm::vec4 clipA = vp * glm::vec4(a, 1.0f);
5559
glm::vec4 clipB = vp * glm::vec4(b, 1.0f);
5660

@@ -75,22 +79,25 @@ void DebugDraw::DrawClippedLine(ImDrawList* drawList, const glm::mat4& vp, const
7579

7680
// Perspective divide and viewport mapping
7781
// NDC [-1,1] maps to the viewport sub-region, not the full screen
82+
glm::vec2 uvRange = glm::max(uvMax - uvMin, glm::vec2(0.0001f));
7883
auto toScreen = [&](const glm::vec4& clip) -> ImVec2 {
7984
float invW = 1.0f / clip.w;
8085
float ndcX = clip.x * invW;
8186
float ndcY = clip.y * invW;
87+
float u = ndcX * 0.5f + 0.5f;
88+
float v = -ndcY * 0.5f + 0.5f;
8289
return ImVec2(
83-
viewportPos.x + (ndcX * 0.5f + 0.5f) * viewportSize.x,
84-
viewportPos.y + (-ndcY * 0.5f + 0.5f) * viewportSize.y
90+
viewportPos.x + ((u - uvMin.x) / uvRange.x) * viewportSize.x,
91+
viewportPos.y + ((v - uvMin.y) / uvRange.y) * viewportSize.y
8592
);
8693
};
8794

8895
drawList->AddLine(toScreen(clipA), toScreen(clipB), color, thickness);
8996
}
9097

91-
void DebugDraw::DrawEdges(ImDrawList* drawList, const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec3* corners, const int (*edges)[2], int edgeCount, uint32_t color, float thickness) {
98+
void DebugDraw::DrawEdges(ImDrawList* drawList, const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec2& uvMin, const glm::vec2& uvMax, const glm::vec3* corners, const int (*edges)[2], int edgeCount, uint32_t color, float thickness) {
9299
for (int i = 0; i < edgeCount; ++i) {
93-
DrawClippedLine(drawList, vp, viewportPos, viewportSize, corners[edges[i][0]], corners[edges[i][1]], color, thickness);
100+
DrawClippedLine(drawList, vp, viewportPos, viewportSize, uvMin, uvMax, corners[edges[i][0]], corners[edges[i][1]], color, thickness);
94101
}
95102
}
96103

@@ -128,7 +135,7 @@ void DebugDraw::SetViewProjection(const glm::mat4& vp) {
128135
// Render
129136
// -----------------------------------------------------------------------
130137

131-
void DebugDraw::Render(const glm::vec2& viewportPos, const glm::vec2& viewportSize) {
138+
void DebugDraw::Render(const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec2& uvMin, const glm::vec2& uvMax) {
132139
std::lock_guard lk(m_mutex);
133140

134141
if (m_primitives.empty() || !m_hasVP) {
@@ -147,7 +154,7 @@ void DebugDraw::Render(const glm::vec2& viewportPos, const glm::vec2& viewportSi
147154
for (const auto& prim : m_primitives) {
148155
switch (prim.type) {
149156
case PrimitiveType::LINE: {
150-
DrawClippedLine(drawList, viewProjection, viewportPos, viewportSize, prim.a, prim.b, prim.color, prim.thickness);
157+
DrawClippedLine(drawList, viewProjection, viewportPos, viewportSize, uvMin, uvMax, prim.a, prim.b, prim.color, prim.thickness);
151158
break;
152159
}
153160

@@ -166,7 +173,7 @@ void DebugDraw::Render(const glm::vec2& viewportPos, const glm::vec2& viewportSi
166173
{ mn.x, mx.y, mx.z },
167174
};
168175

169-
DrawEdges(drawList, viewProjection, viewportPos, viewportSize, corners, BOX_EDGES, 12, prim.color, prim.thickness);
176+
DrawEdges(drawList, viewProjection, viewportPos, viewportSize, uvMin, uvMax, corners, BOX_EDGES, 12, prim.color, prim.thickness);
170177
break;
171178
}
172179

@@ -192,7 +199,7 @@ void DebugDraw::Render(const glm::vec2& viewportPos, const glm::vec2& viewportSi
192199
corners[i] = center + rot * localCorners[i];
193200
}
194201

195-
DrawEdges(drawList, viewProjection, viewportPos, viewportSize, corners, BOX_EDGES, 12, prim.color, prim.thickness);
202+
DrawEdges(drawList, viewProjection, viewportPos, viewportSize, uvMin, uvMax, corners, BOX_EDGES, 12, prim.color, prim.thickness);
196203
break;
197204
}
198205

@@ -219,7 +226,7 @@ void DebugDraw::Render(const glm::vec2& viewportPos, const glm::vec2& viewportSi
219226
corners[i] = glm::vec3(world) / world.w;
220227
}
221228

222-
DrawEdges(drawList, viewProjection, viewportPos, viewportSize, corners, BOX_EDGES, 12, prim.color, prim.thickness);
229+
DrawEdges(drawList, viewProjection, viewportPos, viewportSize, uvMin, uvMax, corners, BOX_EDGES, 12, prim.color, prim.thickness);
223230
break;
224231
}
225232
}

src/utils/debug_draw.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class DebugDraw {
3131
// Projects all submitted primitives using the stored VP matrix and draws
3232
// them onto ImGui::GetForegroundDrawList(). Does NOT clear the buffer,
3333
// so the same primitives can be rendered for multiple eyes/passes.
34-
void Render(const glm::vec2& viewportPos, const glm::vec2& viewportSize);
34+
void Render(const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec2& uvMin = glm::vec2(0.0f), const glm::vec2& uvMax = glm::vec2(1.0f));
3535

3636
/// Clears all submitted primitives. Call once per game frame, after all
3737
/// Render() calls for that frame are complete.
@@ -68,7 +68,7 @@ class DebugDraw {
6868
bool m_hasVP = false;
6969

7070
// Internal helpers
71-
static bool ProjectPoint(const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec3& worldPos, glm::vec4& clipOut, ImVec2& screenOut);
72-
static void DrawClippedLine(ImDrawList* drawList, const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec3& a, const glm::vec3& b, uint32_t color, float thickness);
73-
static void DrawEdges(ImDrawList* drawList, const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec3* corners, const int (*edges)[2], int edgeCount, uint32_t color, float thickness);
71+
static bool ProjectPoint(const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec2& uvMin, const glm::vec2& uvMax, const glm::vec3& worldPos, glm::vec4& clipOut, ImVec2& screenOut);
72+
static void DrawClippedLine(ImDrawList* drawList, const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec2& uvMin, const glm::vec2& uvMax, const glm::vec3& a, const glm::vec3& b, uint32_t color, float thickness);
73+
static void DrawEdges(ImDrawList* drawList, const glm::mat4& vp, const glm::vec2& viewportPos, const glm::vec2& viewportSize, const glm::vec2& uvMin, const glm::vec2& uvMax, const glm::vec3* corners, const int (*edges)[2], int edgeCount, uint32_t color, float thickness);
7474
};

0 commit comments

Comments
 (0)