TazGraph Project v0.1.0
Loading...
Searching...
No Matches
ImGuiText.h
1#pragma once
2
3#include <imgui/imgui.h>
4#include <glm/glm.hpp>
5
6#include "./pch.h"
7
8namespace TazGraphEngine {
9
10
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();
15
16 ImVec2 pos = ImGui::GetWindowPos();
17 ImVec2 viewportSize = ImGui::GetWindowSize();
18
19
20 // Convert world to clip space
21 glm::vec4 clipPos = camera->getCameraMatrix() * glm::vec4(worldPos, 1.0f);
22
23 // Perspective division
24 if (clipPos.w > 0.0f) {
25 clipPos.x /= clipPos.w;
26 clipPos.y /= clipPos.w;
27
28 if (clipPos.x < -1.0f || clipPos.x > 1.0f ||
29 clipPos.y < -1.0f || clipPos.y > 1.0f) {
30 return; // Point is outside view frustum
31 }
32
33 // Convert to screen coordinates
34 glm::vec2 screenPos;
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);
38 // Use clipPos.w (distance in clip space) for scaling
39 float distance = -viewPos.z; // view space forward is -Z
40 float baseFontSize = 16.0f;
41 float scale = 1000.0f / distance; // Adjust multiplier as needed
42 float fontSize = glm::clamp(baseFontSize * scale, 6.0f, baseFontSize);
43
44 ImDrawList* drawList = ImGui::GetForegroundDrawList();
45
46 // Push scaled font
47 ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[0]);
48 ImGui::SetWindowFontScale(fontSize / baseFontSize);
49
50 ImFont* font = ImGui::GetIO().Fonts->Fonts[0];
51 ImVec2 textSize = ImGui::CalcTextSize(text);
52 drawList->AddText(
53 font, fontSize,
54 ImVec2(screenPos.x - textSize.x * 0.5f, screenPos.y - textSize.y * 0.5f),
55 ImColor(color.r, color.g, color.b, color.a),
56 text);
57
58 ImGui::SetWindowFontScale(1.0f);
59 ImGui::PopFont();
60 }
61 }
62}
Definition ICamera.h:10