forked from TrenchBroom/TrenchBroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoxInfoRenderer.cpp
206 lines (174 loc) · 11.4 KB
/
BoxInfoRenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Copyright (C) 2010-2012 Kristian Duske
This file is part of TrenchBroom.
TrenchBroom is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TrenchBroom is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BoxInfoRenderer.h"
#include "Renderer/Camera.h"
#include "Renderer/RenderContext.h"
#include "Renderer/Shader/Shader.h"
#include "Renderer/Shader/ShaderManager.h"
#include "Renderer/SharedResources.h"
#include "Renderer/Text/FontManager.h"
#include "Utility/Preferences.h"
#include "Utility/String.h"
#include <cassert>
namespace TrenchBroom {
namespace Renderer {
const Vec3f BoxInfoSizeTextAnchor::basePosition() const {
BBoxf::PointPosition camPos = m_bounds.pointPosition(m_camera.position());
Vec3f pos;
const Vec3f half = m_bounds.size() / 2.0f;
if (m_axis == Axis::AZ) {
if ((camPos.x == BBoxf::PointPosition::Less && camPos.y == BBoxf::PointPosition::Less) ||
(camPos.x == BBoxf::PointPosition::Less && camPos.y == BBoxf::PointPosition::Within)) {
pos[0] = m_bounds.min.x();
pos[1] = m_bounds.max.y();
} else if ((camPos.x == BBoxf::PointPosition::Less && camPos.y == BBoxf::PointPosition::Greater) ||
(camPos.x == BBoxf::PointPosition::Within && camPos.y == BBoxf::PointPosition::Greater)) {
pos[0] = m_bounds.max.x();
pos[1] = m_bounds.max.y();
} else if ((camPos.x == BBoxf::PointPosition::Greater && camPos.y == BBoxf::PointPosition::Greater) ||
(camPos.x == BBoxf::PointPosition::Greater && camPos.y == BBoxf::PointPosition::Within)) {
pos[0] = m_bounds.max.x();
pos[1] = m_bounds.min.y();
} else if ((camPos.x == BBoxf::PointPosition::Within && camPos.y == BBoxf::PointPosition::Less) ||
(camPos.x == BBoxf::PointPosition::Greater && camPos.y == BBoxf::PointPosition::Less)) {
pos[0] = m_bounds.min.x();
pos[1] = m_bounds.min.y();
}
pos[2] = m_bounds.min.z() + half.z();
} else {
if (m_axis == Axis::AX) {
pos[0] = m_bounds.min.x() + half.x();
if ( camPos.x == BBoxf::PointPosition::Less && camPos.y == BBoxf::PointPosition::Less) {
pos[1] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.min.y() : m_bounds.max.y();
} else if (camPos.x == BBoxf::PointPosition::Less && camPos.y == BBoxf::PointPosition::Within) {
pos[1] = m_bounds.max.y();
} else if (camPos.x == BBoxf::PointPosition::Less && camPos.y == BBoxf::PointPosition::Greater) {
pos[1] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.max.y() : m_bounds.min.y();
} else if (camPos.x == BBoxf::PointPosition::Within && camPos.y == BBoxf::PointPosition::Greater) {
pos[1] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.max.y() : m_bounds.min.y();
} else if (camPos.x == BBoxf::PointPosition::Greater && camPos.y == BBoxf::PointPosition::Greater) {
pos[1] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.max.y() : m_bounds.min.y();
} else if (camPos.x == BBoxf::PointPosition::Greater && camPos.y == BBoxf::PointPosition::Within) {
pos[1] = m_bounds.min.y();
} else if (camPos.x == BBoxf::PointPosition::Greater && camPos.y == BBoxf::PointPosition::Less) {
pos[1] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.min.y() : m_bounds.max.y();
} else if (camPos.x == BBoxf::PointPosition::Within && camPos.y == BBoxf::PointPosition::Less) {
pos[1] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.min.y() : m_bounds.max.y();
}
} else {
pos[1] = m_bounds.min.y() + half.y();
if ( camPos.x == BBoxf::PointPosition::Less && camPos.y == BBoxf::PointPosition::Less) {
pos[0] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.min.x() : m_bounds.max.x();
} else if (camPos.x == BBoxf::PointPosition::Less && camPos.y == BBoxf::PointPosition::Within) {
pos[0] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.min.x() : m_bounds.max.x();
} else if (camPos.x == BBoxf::PointPosition::Less && camPos.y == BBoxf::PointPosition::Greater) {
pos[0] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.min.x() : m_bounds.max.x();
} else if (camPos.x == BBoxf::PointPosition::Within && camPos.y == BBoxf::PointPosition::Greater) {
pos[0] = m_bounds.max.x();
} else if (camPos.x == BBoxf::PointPosition::Greater && camPos.y == BBoxf::PointPosition::Greater) {
pos[0] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.max.x() : m_bounds.min.x();
} else if (camPos.x == BBoxf::PointPosition::Greater && camPos.y == BBoxf::PointPosition::Within) {
pos[0] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.max.x() : m_bounds.min.x();
} else if (camPos.x == BBoxf::PointPosition::Greater && camPos.y == BBoxf::PointPosition::Less) {
pos[0] = camPos.z == BBoxf::PointPosition::Within ? m_bounds.max.x() : m_bounds.min.x();
} else if (camPos.x == BBoxf::PointPosition::Within && camPos.y == BBoxf::PointPosition::Less) {
pos[0] = m_bounds.min.x();
}
}
if (camPos.z == BBoxf::PointPosition::Less)
pos[2] = m_bounds.min.z();
else
pos[2] = m_bounds.max.z();
}
return pos;
}
const Text::Alignment::Type BoxInfoSizeTextAnchor::alignment() const {
if (m_axis == Axis::AZ)
return Text::Alignment::Right;
BBoxf::PointPosition camPos = m_bounds.pointPosition(m_camera.position());
if (camPos.z == BBoxf::PointPosition::Less)
return Text::Alignment::Top;
return Text::Alignment::Bottom;
}
BoxInfoSizeTextAnchor::BoxInfoSizeTextAnchor(const BBoxf& bounds, Axis::Type axis, Renderer::Camera& camera) :
m_bounds(bounds.expanded(1.0f)), // create a bit of a margin for the text label
m_axis(axis),
m_camera(camera) {}
const Vec3f BoxInfoMinMaxTextAnchor::basePosition() const {
if (m_minMax == BoxMin)
return m_bounds.min;
return m_bounds.max;
}
const Text::Alignment::Type BoxInfoMinMaxTextAnchor::alignment() const {
const BBoxf::PointPosition camPos = m_bounds.pointPosition(m_camera.position());
if (m_minMax == BoxMin) {
if (camPos.y == BBoxf::PointPosition::Less || (camPos.y == BBoxf::PointPosition::Within && camPos.x != BBoxf::PointPosition::Less))
return Text::Alignment::Top | Text::Alignment::Right;
return Text::Alignment::Top | Text::Alignment::Left;
}
if (camPos.y == BBoxf::PointPosition::Less || (camPos.y == BBoxf::PointPosition::Within && camPos.x != BBoxf::PointPosition::Less))
return Text::Alignment::Bottom | Text::Alignment::Left;
return Text::Alignment::Bottom | Text::Alignment::Right;
}
BoxInfoMinMaxTextAnchor::BoxInfoMinMaxTextAnchor(const BBoxf& bounds, EMinMax minMax, Renderer::Camera& camera) :
m_bounds(bounds.expanded(0.2f)),
m_minMax(minMax),
m_camera(camera) {}
BoxInfoRenderer::BoxInfoRenderer(const BBoxf& bounds, Text::FontManager& fontManager) :
m_bounds(bounds),
m_textRenderer(NULL),
m_initialized(false) {
Preferences::PreferenceManager& prefs = Preferences::PreferenceManager::preferences();
const String& fontName = prefs.getString(Preferences::RendererFontName);
int fontSize = prefs.getInt(Preferences::RendererFontSize);
Text::FontDescriptor fontDescriptor(fontName, static_cast<unsigned int>(fontSize));
Text::TexturedFont* font = fontManager.font(fontDescriptor);
assert(font != NULL);
m_textRenderer = new Text::TextRenderer<unsigned int>(*font);
m_textRenderer->setFadeDistance(2000.0f);
}
BoxInfoRenderer::~BoxInfoRenderer() {
delete m_textRenderer;
m_textRenderer = NULL;
}
void BoxInfoRenderer::render(Vbo& vbo, RenderContext& context) {
assert(m_textRenderer != NULL);
if (!m_initialized) {
const String labels[3] = {"X", "Y", "Z"};
const Vec3f size = m_bounds.size().corrected();
for (unsigned int i = 0; i < 3; i++) {
StringStream buffer;
buffer << labels[i] << ": " << size[i];
m_textRenderer->addString(i, buffer.str(), Text::TextAnchor::Ptr(new BoxInfoSizeTextAnchor(m_bounds, i, context.camera())));
}
StringStream minBuffer;
minBuffer << "Min: " << m_bounds.min.asString();
m_textRenderer->addString(3, minBuffer.str(), Text::TextAnchor::Ptr(new BoxInfoMinMaxTextAnchor(m_bounds, BoxInfoMinMaxTextAnchor::BoxMin, context.camera())));
StringStream maxBuffer;
maxBuffer << "Max: " << m_bounds.max.asString();
m_textRenderer->addString(4, maxBuffer.str(), Text::TextAnchor::Ptr(new BoxInfoMinMaxTextAnchor(m_bounds, BoxInfoMinMaxTextAnchor::BoxMax, context.camera())));
m_initialized = true;
}
Preferences::PreferenceManager& prefs = Preferences::PreferenceManager::preferences();
const Color& textColor = prefs.getColor(Preferences::InfoOverlayTextColor);
const Color& backgroundColor = prefs.getColor(Preferences::InfoOverlayBackgroundColor);
ShaderProgram& textShader = context.shaderManager().shaderProgram(Shaders::TextShader);
ShaderProgram& backgroundShader = context.shaderManager().shaderProgram(Shaders::TextBackgroundShader);
glDisable(GL_DEPTH_TEST);
m_textRenderer->render(context, m_textFilter, textShader, textColor, backgroundShader, backgroundColor);
glEnable(GL_DEPTH_TEST);
}
}
}