Skip to content

Commit 08c90f7

Browse files
fredroyhugtalbot
andauthored
[Sofa.GL] DrawToolGL: improve indices text rendering quality (#6093)
* auto-scale indices display based on camera distance The indices text now maintains a constant screen-space size regardless of zoom level and scene dimensions, removing the need to manually adjust showIndicesScale per scene. - Compute per-vertex scale from the projection matrix and camera depth in GlText::textureDraw_Indices - Handle both perspective and orthographic projections - Add alpha blending for smoother text rendering - Update all callers to pass a dimensionless multiplier (default 1.0) instead of bbox-dependent world-space size * improve indices text rendering quality - Add drop shadow (black, 70% opacity, 1.5px offset) behind each label for readability against any background - Disable depth test so indices always render on top of geometry - Enable mipmaps on the ASCII texture atlas for cleaner downsampling * set an option to render with depth test (true by default) --------- Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
1 parent 0cf5f5e commit 08c90f7

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

Sofa/GL/src/sofa/gl/glText.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void GlText::initTexture()
3838
}
3939
if (s_asciiTexture == nullptr && s_asciiImage != nullptr)
4040
{
41-
s_asciiTexture = new sofa::gl::Texture(s_asciiImage, false, true, false );
41+
s_asciiTexture = new sofa::gl::Texture(s_asciiImage, false, true, true );
4242
}
4343
}
4444

@@ -170,7 +170,7 @@ void GlText::textureDraw_Overlay(const char* text, const double scale)
170170

171171
}
172172

173-
void GlText::textureDraw_Indices(const type::vector<type::Vec3>& positions, const float& scale)
173+
void GlText::textureDraw_Indices(const type::vector<type::Vec3>& positions, const float& scale, bool enableDepthTest)
174174
{
175175
if (!s_asciiTexture)
176176
{
@@ -211,8 +211,17 @@ void GlText::textureDraw_Indices(const type::vector<type::Vec3>& positions, cons
211211
glEnable(GL_BLEND);
212212
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
213213

214+
if(!enableDepthTest)
215+
glDisable(GL_DEPTH_TEST);
216+
217+
glDisable(GL_LIGHTING);
218+
214219
s_asciiTexture->bind();
215220

221+
// Save the caller-set color for the main text pass
222+
GLfloat textColor[4];
223+
glGetFloatv(GL_CURRENT_COLOR, textColor);
224+
216225
for (std::size_t i = 0; i < positions.size(); i++)
217226
{
218227
std::ostringstream oss;
@@ -223,8 +232,6 @@ void GlText::textureDraw_Indices(const type::vector<type::Vec3>& positions, cons
223232
std::vector<Vec3f> vertices;
224233
std::vector<Vec2f> UVs;
225234

226-
glDisable(GL_LIGHTING);
227-
228235
glPushMatrix();
229236

230237
// Makes text always face the viewer by removing the scene rotation
@@ -248,11 +255,7 @@ void GlText::textureDraw_Indices(const type::vector<type::Vec3>& positions, cons
248255
autoScale = baseFontPixelHeight * scale * 2.0f / (p11 * viewportHeight);
249256
}
250257

251-
glLoadIdentity();
252-
//translate a little bit to center the text on the position (instead of starting from a top-left position)
253-
glTranslatef(temp[0] - (worldWidth*length*autoScale)*0.5f, temp[1] + worldHeight*autoScale*0.5f, temp[2]);
254-
glScalef(autoScale, autoScale, autoScale);
255-
glRotatef(180.0, 1, 0, 0);
258+
// Build quads for this label
256259
for (std::size_t j = 0; j < length; j++)
257260
{
258261
Vec3f vertex_up_left = Vec3f(j*worldWidth, worldHeight, 0.0f);
@@ -281,6 +284,31 @@ void GlText::textureDraw_Indices(const type::vector<type::Vec3>& positions, cons
281284
UVs.push_back(uv_up_right);
282285
}
283286

287+
// Shadow offset: 1.5 pixels in view space
288+
const float shadowOffset = 1.5f * autoScale / baseFontPixelHeight;
289+
290+
// Drop shadow pass (dark, offset down-right)
291+
glColor4f(0.0f, 0.0f, 0.0f, textColor[3] * 0.7f);
292+
glLoadIdentity();
293+
glTranslatef(temp[0] - (worldWidth*length*autoScale)*0.5f + shadowOffset,
294+
temp[1] + worldHeight*autoScale*0.5f - shadowOffset,
295+
temp[2]);
296+
glScalef(autoScale, autoScale, autoScale);
297+
glRotatef(180.0, 1, 0, 0);
298+
glBegin(GL_QUADS);
299+
for (std::size_t j = 0; j < vertices.size(); j++)
300+
{
301+
glTexCoord2fv(UVs[j].data());
302+
glVertex3fv(vertices[j].data());
303+
}
304+
glEnd();
305+
306+
// Main text pass
307+
glColor4fv(textColor);
308+
glLoadIdentity();
309+
glTranslatef(temp[0] - (worldWidth*length*autoScale)*0.5f, temp[1] + worldHeight*autoScale*0.5f, temp[2]);
310+
glScalef(autoScale, autoScale, autoScale);
311+
glRotatef(180.0, 1, 0, 0);
284312
glBegin(GL_QUADS);
285313
for (std::size_t j = 0; j < vertices.size(); j++)
286314
{
@@ -295,9 +323,9 @@ void GlText::textureDraw_Indices(const type::vector<type::Vec3>& positions, cons
295323
s_asciiTexture->unbind();
296324
glDisable(GL_ALPHA_TEST);
297325
glDisable(GL_BLEND);
326+
glEnable(GL_DEPTH_TEST);
298327
glPopAttrib();
299328

300-
301329
glEnable(GL_LIGHTING);
302330
}
303331

Sofa/GL/src/sofa/gl/glText.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class SOFA_GL_API GlText
7575
static void draw ( const T& text, const type::Vec3& position = type::Vec3(0.0,0.0,0.0), const double& scale = 1.0);
7676

7777
static void textureDraw_Overlay(const char* text, const double scale = 1.0);
78-
static void textureDraw_Indices(const type::vector<type::Vec3>& positions, const float& scale);
78+
static void textureDraw_Indices(const type::vector<type::Vec3>& positions, const float& scale, bool enableDepthTest = true);
7979

8080
private:
8181
static void initTexture();

0 commit comments

Comments
 (0)