FLTK logo

STR #3440

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 Home  |  Articles & FAQs  |  Bugs & Features  |  Documentation  |  Download  |  Screenshots  ]
 

Return to Bugs & Features | SVN ⇄ GIT ]

STR #3440

Application:FLTK Library
Status:1 - Closed w/Resolution
Priority:1 - Request for Enhancement, e.g. asking for a feature
Scope:2 - Specific to an operating system
Subsystem:OpenGL
Summary:Drawing bitmap text in Fl_Gl_Window is impossible in macOS when initialized with FL_OPENGL3
Version:1.4-feature
Created By:alpopa
Assigned To:manolo
Fix Version:1.4-current
Update Notification:

Receive EMails Don't Receive EMails

Trouble Report Files:


Name/Time/Date Filename/Size  
 
#1 alpopa
10:18 Feb 27, 2018
TextShader-draft.tar.gz
5k
 
     

Trouble Report Comments:


Name/Time/Date Text  
 
#1 alpopa
01:51 Dec 09, 2017
The functions to display bitmapped text in Fl_Gl_Window (all gl_draw, glutBitmapString, glutBitMapCharacter) are closely related to OpenGL prior to version 3.0. In Windows and Linux a possible work-around is:

#include <GL/glew.h>
#include <FL/gl.h>
#include <FL/glut.H>
...
glUseProgram(0);
glRasterPos4dv(...);            // imported from GL/glew.h
gl_draw("some text");           // from FLTK library, or
glutBitmapString("other text"); // from FLTK library
// gl_draw("text with coordinated", x, y); - this doesn't work in Windows / Linux for me
glUseProgram(program);
...


The above code dowsn't wirk in macOS (using GLEW library and not including OpenGL/gl.h). No compilation error, just text is not drawn. The same code (except glUseProgram) works in macOS when Fl_Gl_Window is initialized without FL_OPENGL3 flag.

My project is rather large to attach the code, but I probably can create a minimal code sample if necessary.
 
 
#2 alpopa
06:05 Jan 26, 2018
The implementation is possible through representing the font as texture rather than bitmap. More info can be found here:
https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01
https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_02

The problem is, on MacOS core context only is available (?), which doesn't work if some legacy deprecated OpenGL code is used somewhere else.

I will try to prepare the patch.
 
 
#3 AlbrechtS
11:33 Jan 26, 2018
Thanks for the offer to develop a patch.

Please take a look at STR #3450 first which may be a possible solution:
http://www.fltk.org/str.php?L3450

Disclaimer: I'm not an OpenGL user so my statement may be wrong.

If you know OpenGL better, then all comments (@ STR #3450) would be appreciated by the FLTK team. Particularly if STR #3450 resolves the issue reported here (STR #3440) so we can close this STR as well.
TIA.
 
 
#4 alpopa
12:08 Jan 27, 2018
Did not try the patch attached to STR #3450, however I carefully read the code. It contains much more than I ever imagined. Unfortunately, this is different issue.

Firstly, FLTK 1.4 branch uses graphics drivers architecture, very different from #if #else #endif in FLTK 1.3, so the patch is at least difficult to port to FLTK 1.3 (if it is desired).

Secondly, the GLtextureV3.patch introduces new way of drawing text in Fl_Gl_Window (texture rather than display list), but the code uses many legacy and deprecated OpenGL calls (glBegin(...), glEnd(...) glPopAttribute(...), glPushAttribute(...), glRasterPos..(...) and so on). All these function pointers are invalid in OpenGL core profile under MacOS (which is the case when FL_OPENGL3 flag is used) and I am not sure it will work this way. Still the patch contains many really interesting ideas about the data organization in order to produce output.

I am not too experienced OpenGL programmer, I just made my program (which uses FLTK) to draw the text using textures (I have more exotic needs, so antialiasing and unicode is my last concern), but it doesn't work under MacOS. I am not sure, but this may be because it is not completely free from legacy and deprecated OpenGL code.
 
 
#5 alpopa
01:28 Feb 07, 2018
I finally mastered texture text drawing with shaders, so I am prepared to implement a patch for OpenGL 3.0+. Unfortunately, there are 2 different approaches, each with its advantages and disadvantages.

Approach 1.
Drawing whole text to offscreen using fl_draw(...) function, then capture the image with fl_read_image(...) and use it as a texture. The advantage of this approach is the ability to draw whatever text with whatever font, size, color, alignment, and it will be painted perfectly with anti-aliasing, full utf8 support, kerning etc. The disadvantage is, most of GPUs have a severe limit of the pixel size one texture may handle. Maybe this can be adjusted by reducing captured image to fit into one texture and then enlarging it again during painting, but this will reduce the output quality. The interface will be similar to the following:

class TextShaderStatic
{
  GLuint program;
  GLuint vertexObjectArray;
  void color(Fl_Color color);
  Fl_Color color();
  void position(int x, int y);
  int x();
  int y();
  void cleanup();
};

This can be used as following:
TextShaderStatic text = fl_draw_shader_static(...); // The arguments for this function are similar to fl_draw(...) with alignment and images
...
glUseProgram(text.program);
glDrawArrays(..., text.vertexObjectArray, ...);
...
text.cleanup(); // Releases the allocated GPU resources

The same text without re-initialization can be drawn at different positions and with different colors.

Approach 2.
Collect all the characters from some font that user ever want to use into alphabet texture and use this alphabet to draw whatever text at whatever position with whatever color. The only condition is to use exclusively the characters from the alphabet. The advantage of this approach is drawing arbitrary text with minimum GPU allocation, which is drawn with anti-aliased font. The disadvantage is lack of kerning, alignment and other cool stuff fl_draw(...) allows, but for some national language and a font which doesn't contain kerning this is probably more flexible approach. The interface will be similar to the following:

class TextureFont
{
  GLuint program;
  void cleanup();
};

class TextShaderDynamic
{
  TextureFont font;
  GLuint vertexObjectArray;
  GLuint start;
  GLuint size;
  void color(Fl_Color color);
  Fl_Color color();
  void position(int x, int y);
  int x();
  int y();
  void cleanup();
};

This can be used as following:
TextureFont font(...); // Pass here font name and font size
TextShaderDynamic text1(font, "String1", ...); // Pass here color and position
TextShaderDynamic text2(font, "String2", ...); // They can be modified without re-initializing the object
...
glUseProgram(font.program);
glDrawArrays(..., text1.vertexObjectArray, text1.start, text1.size);
glDrawArrays(..., text2.vertexObjectArray, text2.start, text2.size);
...
text2.cleanup();
text1.cleanup();
font.cleanup();

Maybe some more user-friendly interface will be nice, but the interface to directly interact with OpenGL is a must.
 
 
#6 alpopa
10:25 Feb 27, 2018
The archive TextShader-draft.tar.gz contains basic implementation of OpenGL3 enabled text drawing similar to gl_draw(...) together with test program showing how to use it. It can be used (inefficiently) just as gl_draw_shader(...) inside Fl_Gl_Window::draw() or better as new class Fl_Gl_Text_Static.

Bugs:
1. Does not working under X Window System (Windows and MacOS are OK). The call to fl_rectf(...) prior to showing the window leads to crash.
2. For some reason it only works for GL_TEXTURE0 and draws nothing for any other texture.
 
 
#7 AlbrechtS
14:50 Jan 02, 2019
Changed priority to 1 (RFE).

Although this is - strictly speaking - not a feature request because "something doesn't work" I classified it down to RFE because, OTOH, this STR requests a new feature: OpenGL3+ handling in macOS which is currently not supported by FLTK.

Note also that it is very unlikely that such large code changes (that seem to be necessary) will be implemented in FLTK 1.3.x since this branch is closed for new features. We may eventually move this STR to 1.4.x in the future.
 
 
#8 manolo
07:52 Feb 06, 2023
Drawing text above an OpenGL3 scene in macOS is now possible with FLTK 1.4. See examples/OpenGL3test.cxx therein as an example.

The current implementation is based on
https://fltk.gitlab.io/fltk/opengl.html#opengl_with_fltk_widgets
that is, FLTK widgets are added to the GL window as child objects, and these can contain text which gets drawn above the GL3 scene.

The OP is invited to use FLTK 1.4
 
 
#9 manolo
07:53 Feb 06, 2023
Fixed in Git repository.  
     

Return to Bugs & Features ]

 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.