Modern OpenGL does not support drawing text so it needs to be handled independently. There many popular libraries available such as freetype.
Instead, using GDI Plus APIs text can be drawn into a memory based bitmap. Later the bitmap is loaded as a texture.
Implementation
A new mesh class - TextMesh is defined for loading bitmap as texture. It's used by DrawTextUtil object for loading vertex data and texture data to the vertex and fragment shaders.
DrawTextUtil uses GDIPlus to create ARGB in memory bitmap containing text to be drawn. The size of the bitmap should be multiples of 128. i.e., 128, 256, 512 etc.
First bitmap is filled with black color and then Text is drawn using the font and color. Note the text color needs to be non black. Font can be changed by passing LOGFONT structure, Also text color can be changed by passing COLLOREF of the color.
Fragment shader is modified to discard non text fragments for blending with the background.
Client
Client needs to first Startup() GDIPlus in the beginning of the application and Shutdown() at the end.
Instantiate DrawTextUtil object by Init() method supply unique texture id and size.
UpdateFontandColor() should be called to supply font details and text color.
DrawText() can be called to draw text.
The DrawTextUtil object can be translated, rotated etc. just like any 3D object.
No comments:
Post a Comment