3#include <imgui/imgui.h>
8namespace TazGraphEngine {
11 inline void drawTextAtWorldPositionPerspective(
const glm::vec3& worldPos,
const char* text,
12 const glm::vec4& color,
ICamera* camera) {
13 glm::mat4 view = camera->getViewMatrix();
14 glm::mat4 proj = camera->getProjMatrix();
16 ImVec2 pos = ImGui::GetWindowPos();
17 ImVec2 viewportSize = ImGui::GetWindowSize();
21 glm::vec4 clipPos = camera->getCameraMatrix() * glm::vec4(worldPos, 1.0f);
24 if (clipPos.w > 0.0f) {
25 clipPos.x /= clipPos.w;
26 clipPos.y /= clipPos.w;
28 if (clipPos.x < -1.0f || clipPos.x > 1.0f ||
29 clipPos.y < -1.0f || clipPos.y > 1.0f) {
35 screenPos.x = pos.x + (clipPos.x * 0.5f + 0.5f) * viewportSize.x;
36 screenPos.y = pos.y + (-clipPos.y * 0.5f + 0.5f) * viewportSize.y;
37 glm::vec4 viewPos = view * glm::vec4(worldPos, 1.0f);
39 float distance = -viewPos.z;
40 float baseFontSize = 16.0f;
41 float scale = 1000.0f / distance;
42 float fontSize = glm::clamp(baseFontSize * scale, 6.0f, baseFontSize);
44 ImDrawList* drawList = ImGui::GetForegroundDrawList();
47 ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[0]);
48 ImGui::SetWindowFontScale(fontSize / baseFontSize);
50 ImFont* font = ImGui::GetIO().Fonts->Fonts[0];
51 ImVec2 textSize = ImGui::CalcTextSize(text);
54 ImVec2(screenPos.x - textSize.x * 0.5f, screenPos.y - textSize.y * 0.5f),
55 ImColor(color.r, color.g, color.b, color.a),
58 ImGui::SetWindowFontScale(1.0f);