diff --git a/libsrc/visualization/mvdraw.cpp b/libsrc/visualization/mvdraw.cpp index ffa497a3..89aa6cff 100644 --- a/libsrc/visualization/mvdraw.cpp +++ b/libsrc/visualization/mvdraw.cpp @@ -38,9 +38,11 @@ namespace netgen */ void (*opengl_text_function)(const char * text) = NULL; - void Set_OpenGLText_Callback ( void (*fun) (const char * text) ) + int opengl_text_width = 0; + void Set_OpenGLText_Callback ( void (*fun) (const char * text), int width ) { opengl_text_function = fun; + opengl_text_width = width; } void MyOpenGLText (const char * text) @@ -50,6 +52,11 @@ namespace netgen // cout << "MyOpenGLText: " << text << endl; } + int MyOpenGLTextWidth () + { + return opengl_text_width; + } + // texture for color decoding // GLubyte * VisualScene :: colortexture = NULL; @@ -602,20 +609,26 @@ namespace netgen glPushAttrib (GL_LIST_BIT); // glListBase (fontbase); - char buf[20]; + constexpr size_t buf_size = 20; + char buf[buf_size]; + GLint viewport[4]; + glGetIntegerv (GL_VIEWPORT, viewport); + double char_width = 2.0*MyOpenGLTextWidth()/(viewport[3]); for (int i = 0; i <= 4; i++) { - double x = minx + i * (maxx-minx) / 4; - glRasterPos3d (x, 0.7,-5); - double val; if (logscale) val = minval * pow (maxval / minval, i / 4.0); else val = minval + i * (maxval-minval) / 4; - sprintf (buf, "%8.3e", val); + snprintf (buf, buf_size, "%8.3e", val); + auto n = strlen(buf); // glCallLists (GLsizei(strlen (buf)), GL_UNSIGNED_BYTE, buf); + double x = minx + i * (maxx-minx) / 4; + x -= 0.5*char_width * n; // center text + glRasterPos3d (x, 0.7,-5); + MyOpenGLText (buf); } diff --git a/libsrc/visualization/mvdraw.hpp b/libsrc/visualization/mvdraw.hpp index d3581724..79830b95 100644 --- a/libsrc/visualization/mvdraw.hpp +++ b/libsrc/visualization/mvdraw.hpp @@ -84,7 +84,8 @@ namespace netgen NGGUI_API extern void MyOpenGLText (const char * text); - NGGUI_API extern void Set_OpenGLText_Callback ( void (*fun) (const char * text) ); + NGGUI_API extern int MyOpenGLTextWidth (); + NGGUI_API extern void Set_OpenGLText_Callback ( void (*fun) (const char * text), int width ); NGGUI_API extern VisualScene visual_scene_cross; NGGUI_API extern VisualScene *visual_scene; diff --git a/ng/fonts.hpp b/ng/fonts.hpp index 9443ae01..c4d47a2e 100644 --- a/ng/fonts.hpp +++ b/ng/fonts.hpp @@ -2312,6 +2312,8 @@ namespace netgen { return list_base; } + int Width() { return w; }; + int Height() { return h; }; }; // create Fonts statically and return pointer on selecting diff --git a/ng/ngpkg.cpp b/ng/ngpkg.cpp index 96ef4ea1..6cc9984e 100644 --- a/ng/ngpkg.cpp +++ b/ng/ngpkg.cpp @@ -1992,7 +1992,7 @@ namespace netgen SetVisualScene (Togl_Interp(togl)); visual_scene->DrawScene(); - Set_OpenGLText_Callback (&MyOpenGLText_GUI); + Set_OpenGLText_Callback (&MyOpenGLText_GUI, font->Width()); return TCL_OK; }