From 3fe88d9e736b93bb41c25a3068101df544bf3bd3 Mon Sep 17 00:00:00 2001 From: Graham Pentheny Date: Sun, 24 Aug 2025 15:08:23 -0400 Subject: [PATCH] Use DrawWorldspaceText rather than gluProject and DrawScreenspaceText --- RecastDemo/Source/Sample_TempObstacles.cpp | 27 ++---- RecastDemo/Source/Sample_TileMesh.cpp | 28 ++---- RecastDemo/Source/TestCase.cpp | 14 +-- RecastDemo/Source/Tool_Crowd.cpp | 98 ++++++++------------ RecastDemo/Source/Tool_NavMeshTester.cpp | 14 ++- RecastDemo/Source/Tool_OffMeshConnection.cpp | 11 +-- 6 files changed, 69 insertions(+), 123 deletions(-) diff --git a/RecastDemo/Source/Sample_TempObstacles.cpp b/RecastDemo/Source/Sample_TempObstacles.cpp index c785bc65..5472db76 100644 --- a/RecastDemo/Source/Sample_TempObstacles.cpp +++ b/RecastDemo/Source/Sample_TempObstacles.cpp @@ -284,26 +284,13 @@ void drawDetailOverlay(const dtTileCache* tileCache, const int tileX, const int pos[1] = tile->header->bmin[1]; pos[2] = (tile->header->bmin[2] + tile->header->bmax[2]) / 2.0f; - GLdouble x, y, z; - if (gluProject( - static_cast(pos[0]), - static_cast(pos[1]), - static_cast(pos[2]), - model, - proj, - view, - &x, - &y, - &z)) - { - char text[128]; - snprintf(text, 128, "(%d,%d)/%d", tile->header->tx, tile->header->ty, tile->header->tlayer); - DrawScreenspaceText(static_cast(x), static_cast(y) - 25, IM_COL32(0, 0, 0, 220), text, true); - snprintf(text, 128, "Compressed: %.1f kB", static_cast(tile->dataSize) / 1024.0f); - DrawScreenspaceText(static_cast(x), static_cast(y) - 45, IM_COL32(0, 0, 0, 128), text, true); - snprintf(text, 128, "Raw:%.1fkB", static_cast(rawSize) / 1024.0f); - DrawScreenspaceText(static_cast(x), static_cast(y) - 65, IM_COL32(0, 0, 0, 128), text, true); - } + char text[128]; + snprintf(text, 128, "(%d,%d)/%d", tile->header->tx, tile->header->ty, tile->header->tlayer); + DrawWorldspaceText(pos[0], pos[1], pos[2], IM_COL32(0, 0, 0, 220), text, true, 25); + snprintf(text, 128, "Compressed: %.1f kB", static_cast(tile->dataSize) / 1024.0f); + DrawWorldspaceText(pos[0], pos[1], pos[2], IM_COL32(0, 0, 0, 128), text, true, 45); + snprintf(text, 128, "Raw:%.1fkB", static_cast(rawSize) / 1024.0f); + DrawWorldspaceText(pos[0], pos[1], pos[2], IM_COL32(0, 0, 0, 128), text, true, 65); } } diff --git a/RecastDemo/Source/Sample_TileMesh.cpp b/RecastDemo/Source/Sample_TileMesh.cpp index d8fbb544..175de097 100644 --- a/RecastDemo/Source/Sample_TileMesh.cpp +++ b/RecastDemo/Source/Sample_TileMesh.cpp @@ -179,19 +179,18 @@ public: void drawOverlayUI(double* proj, double* model, int* view) override { - GLdouble x, y, z; - if (m_hitPosSet && gluProject(m_hitPos[0], m_hitPos[1], m_hitPos[2], model, proj, view, &x, &y, &z)) + if (m_hitPosSet) { int tx = 0; int ty = 0; m_sample->getTilePos(m_hitPos, tx, ty); char text[32]; snprintf(text, 32, "(%d,%d)", tx, ty); - DrawScreenspaceText(static_cast(x), static_cast(y), IM_COL32(0, 0, 0, 220), text, true); + DrawWorldspaceText(m_hitPos[0], m_hitPos[1], m_hitPos[2], IM_COL32(0, 0, 0, 220), text); } // Tool help - DrawScreenspaceText(280, 40, IM_COL32(255, 255, 255, 192), "LMB: Rebuild hit tile. Shift+LMB: Clear hit tile."); + DrawScreenspaceText(280, 40, IM_COL32(255, 255, 255, 192), "LMB: Rebuild selected tile. Shift+LMB: Clear tile selection."); } }; @@ -535,24 +534,17 @@ void Sample_TileMesh::render() void Sample_TileMesh::renderOverlay(double* proj, double* model, int* view) { - GLdouble x, y, z; - // Draw start and end point labels - const int projectResult = gluProject( - static_cast(lastBuiltTileBoundsMin[0] + lastBuiltTileBoundsMax[0]) / 2, - static_cast(lastBuiltTileBoundsMin[1] + lastBuiltTileBoundsMax[1]) / 2, - static_cast(lastBuiltTileBoundsMin[2] + lastBuiltTileBoundsMax[2]) / 2, - model, - proj, - view, - &x, - &y, - &z); - if (tileBuildTime > 0.0f && projectResult == GL_TRUE) + if (tileBuildTime > 0.0f) { char text[64]; snprintf(text, 64, "%.3fms / %dTris / %.1fkB", tileBuildTime, tileTriCount, tileMemUsage); - DrawScreenspaceText(static_cast(x), static_cast(y), IM_COL32(0, 0, 0, 220), text); + DrawWorldspaceText( + (lastBuiltTileBoundsMin[0] + lastBuiltTileBoundsMax[0]) / 2, + (lastBuiltTileBoundsMin[1] + lastBuiltTileBoundsMax[1]) / 2, + (lastBuiltTileBoundsMin[2] + lastBuiltTileBoundsMax[2]) / 2, + IM_COL32(0, 0, 0, 220), + text); } if (tool) diff --git a/RecastDemo/Source/TestCase.cpp b/RecastDemo/Source/TestCase.cpp index a7ceab81..3c0c118a 100644 --- a/RecastDemo/Source/TestCase.cpp +++ b/RecastDemo/Source/TestCase.cpp @@ -393,7 +393,6 @@ void TestCase::handleRender() bool TestCase::handleRenderOverlay(double* proj, double* model, int* view) { - GLdouble x, y, z; char text[64]; int n = 0; @@ -422,17 +421,10 @@ bool TestCase::handleRenderOverlay(double* proj, double* model, int* view) pt[1] += 0.5f; } - if (gluProject(pt[0], pt[1], pt[2], model, proj, view, &x, &y, &z)) - { - snprintf(text, 64, "Path %d\n", n); - unsigned int col = IM_COL32(0, 0, 0, 128); - if (test.expand) - { - col = IM_COL32(255, 192, 0, 220); - } - DrawScreenspaceText(static_cast(x), static_cast(y) - 25, col, text, true); - } + snprintf(text, 64, "Path %d\n", n); + unsigned int color = test.expand ? IM_COL32(255, 192, 0, 220) : IM_COL32(0, 0, 0, 128); + DrawWorldspaceText(pt[0], pt[1], pt[2], color, text, true, 25); n++; } diff --git a/RecastDemo/Source/Tool_Crowd.cpp b/RecastDemo/Source/Tool_Crowd.cpp index f097cd19..724f26b3 100644 --- a/RecastDemo/Source/Tool_Crowd.cpp +++ b/RecastDemo/Source/Tool_Crowd.cpp @@ -601,12 +601,10 @@ void CrowdToolState::handleRender() void CrowdToolState::handleRenderOverlay(double* proj, double* model, int* view) { - GLdouble x, y, z; - // Draw start and end point labels - if (targetPolyRef && gluProject(targetPosition[0], targetPosition[1], targetPosition[2], model, proj, view, &x, &y, &z)) + if (targetPolyRef) { - DrawScreenspaceText(static_cast(x), static_cast(y), IM_COL32(0, 0, 0, 220), "TARGET", true); + DrawWorldspaceText(targetPosition[0], targetPosition[1], targetPosition[2], IM_COL32(0, 0, 0, 220), "TARGET", true); } char label[32]; @@ -620,7 +618,6 @@ void CrowdToolState::handleRenderOverlay(double* proj, double* model, int* view) const dtNodePool* pool = navquery->getNodePool(); if (pool) { - const float off = 0.5f; for (int i = 0; i < pool->getHashSize(); ++i) { for (dtNodeIndex j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j)) @@ -631,26 +628,15 @@ void CrowdToolState::handleRenderOverlay(double* proj, double* model, int* view) continue; } - if (gluProject( - (GLdouble)node->pos[0], - (GLdouble)node->pos[1] + off, - (GLdouble)node->pos[2], - model, - proj, - view, - &x, - &y, - &z)) - { - const float heuristic = node->total; // - node->cost; - snprintf(label, 32, "%.2f", heuristic); - DrawScreenspaceText( - static_cast(x), - static_cast(y) + 15, - IM_COL32(0, 0, 0, 220), - label, - true); - } + snprintf(label, 32, "%.2f", node->total); + DrawWorldspaceText( + node->pos[0], + node->pos[1] + 0.5f, + node->pos[2], + IM_COL32(0, 0, 0, 220), + label, + true, + 15); } } } @@ -664,18 +650,21 @@ void CrowdToolState::handleRenderOverlay(double* proj, double* model, int* view) { for (int i = 0; i < crowd->getAgentCount(); ++i) { - const dtCrowdAgent* ag = crowd->getAgent(i); - if (!ag->active) + const dtCrowdAgent* agent = crowd->getAgent(i); + if (!agent->active) { continue; } - const float* pos = ag->npos; - const float h = ag->params.height; - if (gluProject(pos[0], static_cast(pos[1]) + h, pos[2], model, proj, view, &x, &y, &z)) - { - snprintf(label, 32, "%d", i); - DrawScreenspaceText(static_cast(x), static_cast(y) + 15, IM_COL32(0, 0, 0, 220), label, true); - } + + snprintf(label, 32, "%d", i); + DrawWorldspaceText( + agent->npos[0], + agent->npos[1] + agent->params.height, + agent->npos[2], + IM_COL32(0, 0, 0, 220), + label, + true, + 15); } } } @@ -690,41 +679,32 @@ void CrowdToolState::handleRenderOverlay(double* proj, double* model, int* view) { continue; } - const dtCrowdAgent* ag = crowd->getAgent(i); - if (!ag->active) + const dtCrowdAgent* agent = crowd->getAgent(i); + if (!agent->active) { continue; } - const float radius = ag->params.radius; + if (toolParams.showNeighbors) { - for (int j = 0; j < ag->nneis; ++j) + for (int neighborIndex = 0; neighborIndex < agent->nneis; ++neighborIndex) { - const dtCrowdAgent* nei = crowd->getAgent(ag->neis[j].idx); - if (!nei->active) + const dtCrowdNeighbour& neighborData = agent->neis[neighborIndex]; + const dtCrowdAgent* neighbor = crowd->getAgent(neighborData.idx); + if (!neighbor->active) { continue; } - if (gluProject( - nei->npos[0], - static_cast(nei->npos[1]) + radius, - nei->npos[2], - model, - proj, - view, - &x, - &y, - &z)) - { - snprintf(label, 32, "%.3f", ag->neis[j].dist); - DrawScreenspaceText( - static_cast(x), - static_cast(y) + 15, - IM_COL32(255, 255, 255, 220), - label, - true); - } + snprintf(label, 32, "%.3f", neighborData.dist); + DrawWorldspaceText( + neighbor->npos[0], + neighbor->npos[1] + agent->params.radius, + neighbor->npos[2], + IM_COL32(255, 255, 255, 220), + label, + true, + 15); } } } diff --git a/RecastDemo/Source/Tool_NavMeshTester.cpp b/RecastDemo/Source/Tool_NavMeshTester.cpp index d49afba1..33be21e0 100644 --- a/RecastDemo/Source/Tool_NavMeshTester.cpp +++ b/RecastDemo/Source/Tool_NavMeshTester.cpp @@ -1497,20 +1497,18 @@ void NavMeshTesterTool::render() void NavMeshTesterTool::drawOverlayUI(double* proj, double* model, int* view) { - GLdouble x, y, z; - - // Draw start and end point labels - if (sposSet && gluProject(spos[0], spos[1], spos[2], model, proj, view, &x, &y, &z)) + if (sposSet) { - DrawScreenspaceText(static_cast(x), static_cast(y) - 25, IM_COL32(0, 0, 0, 220), "Start", true); + DrawWorldspaceText(spos[0], spos[1], spos[2], IM_COL32(0, 0, 0, 220), "Start", true); } - if (eposSet && gluProject(epos[0], epos[1], epos[2], model, proj, view, &x, &y, &z)) + + if (eposSet) { - DrawScreenspaceText(static_cast(x), static_cast(y) - 25, IM_COL32(0, 0, 0, 220), "End", true); + DrawWorldspaceText(epos[0], epos[1], epos[2], IM_COL32(0, 0, 0, 220), "End", true); } // Tool help - DrawScreenspaceText( 280, 40, IM_COL32(255, 255, 255, 192), "LMB+SHIFT: Set start location LMB: Set end location"); + DrawScreenspaceText(280, 40, IM_COL32(255, 255, 255, 192), "LMB+SHIFT: Set start location LMB: Set end location"); } void NavMeshTesterTool::drawAgent(const float* pos, float r, float h, float c, const unsigned int col) const diff --git a/RecastDemo/Source/Tool_OffMeshConnection.cpp b/RecastDemo/Source/Tool_OffMeshConnection.cpp index 732c027e..3e47f461 100644 --- a/RecastDemo/Source/Tool_OffMeshConnection.cpp +++ b/RecastDemo/Source/Tool_OffMeshConnection.cpp @@ -133,16 +133,13 @@ void OffMeshConnectionTool::render() void OffMeshConnectionTool::drawOverlayUI(double* proj, double* model, int* view) { - GLdouble x, y, z; - - // Draw start and end point labels - if (hitPosSet && gluProject((GLdouble)hitPos[0], (GLdouble)hitPos[1], (GLdouble)hitPos[2], model, proj, view, &x, &y, &z)) + if (hitPosSet) { - DrawScreenspaceText(static_cast(x), static_cast(y) - 25, IM_COL32(0, 0, 0, 220), "Start", true); + DrawWorldspaceText(hitPos[0], hitPos[1], hitPos[2], IM_COL32(0, 0, 0, 220), "Start", true, 25); } // Tool help - const auto setMessage = "LMB: Set connection end point and finish."; - const auto connectMessage = "LMB: Create new connection. SHIFT+LMB: Delete existing connection, click near start or end point."; + const char* setMessage = "LMB: Set connection end point and finish."; + const char* connectMessage = "LMB: Create new connection. SHIFT+LMB: Delete existing connection, click near start or end point."; DrawScreenspaceText(280, 40, IM_COL32(255, 255, 255, 192), hitPosSet ? setMessage : connectMessage); }