SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLTexFont Class Reference

Texture Font class inherits SLGLTexture for alpha blended font rendering. More...

#include <SLTexFont.h>

Inheritance diagram for SLTexFont:
[legend]

Classes

struct  SLTexFontChar
 Single Character info struct w. min. and max. texcoords. More...
 

Public Member Functions

 SLTexFont (SLstring fontFilename, SLGLProgram *fontTexProgram)
 
 SLTexFont (SLstring fontFilename, SLstring shaderDir)
 
 ~SLTexFont ()
 
void create (SLstring fontFilename)
 
SLVec2f calcTextSize (const SLstring &text, SLfloat maxWidth=0.0f, SLfloat lineHeightFactor=1.5f)
 
SLVstring wrapTextToLines (SLstring text, SLfloat maxW)
 
void buildTextBuffers (SLGLVertexArray &vao, const SLstring &text, SLfloat maxWidth=0.0f, SLfloat lineHeight=1.5f)
 
SLGLProgramfontTexProgram ()
 
- Public Member Functions inherited from SLGLTexture
 SLGLTexture ()
 Default ctor for all stack instances (not created with new) More...
 
 SLGLTexture (SLAssetManager *assetMgr, const SLVCol4f &colors, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLint wrapS=GL_REPEAT, const SLstring &name="2D-Texture")
 ctor for 1D texture with internal image allocation More...
 
 SLGLTexture (SLAssetManager *assetMgr, SLint min_filter, SLint mag_filter, SLint wrapS, SLint wrapT, SLenum target=GL_TEXTURE_2D)
 ctor for empty 2D textures More...
 
 SLGLTexture (SLAssetManager *assetMgr, unsigned char *data, int width, int height, int cvtype, SLint min_filter, SLint mag_filter, SLTextureType type, SLint wrapS, SLint wrapT)
 ctor for 2D textures from byte pointer More...
 
 SLGLTexture (SLAssetManager *assetMgr, const SLstring &imageFilename, SLint min_filter=GL_LINEAR_MIPMAP_LINEAR, SLint mag_filter=GL_LINEAR, SLTextureType type=TT_unknown, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT)
 ctor for 2D textures with internal image allocation More...
 
 SLGLTexture (SLAssetManager *assetMgr, const SLVstring &imageFilenames, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT, const SLstring &name="3D-Texture", SLbool loadGrayscaleIntoAlpha=false)
 ctor for 3D texture with internal image allocation More...
 
 SLGLTexture (SLAssetManager *assetMgr, SLint depth, const SLstring &imageFilename, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT, const SLstring &name="3D-Texture", SLbool loadGrayscaleIntoAlpha=false)
 ctor for 3D texture from a single file with depth as 3rd dimension More...
 
 SLGLTexture (SLAssetManager *assetMgr, const SLstring &imageFilenameXPos, const SLstring &imageFilenameXNeg, const SLstring &imageFilenameYPos, const SLstring &imageFilenameYNeg, const SLstring &imageFilenameZPos, const SLstring &imageFilenameZNeg, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLTextureType type=TT_unknown)
 ctor for cube mapping with internal image allocation More...
 
 ~SLGLTexture () override
 
virtual void build (SLint texUnit)
 
void deleteData (SLbool deleteAlsoOnGPU)
 Delete all data (CVImages and GPU textures) More...
 
void deleteDataGpu ()
 Deletes the OpenGL texture objects and releases the memory on the GPU. More...
 
void deleteImages ()
 Deletes the CVImages in _images. No more texture mapping in ray tracing. More...
 
void bindActive (SLuint texUnit=0)
 
void fullUpdate ()
 
void drawSprite (SLbool doUpdate, SLfloat x, SLfloat y, SLfloat w, SLfloat h)
 Draws the texture as 2D sprite with OpenGL buffers. More...
 
void cubeUV2XYZ (SLint index, SLfloat u, SLfloat v, SLfloat &x, SLfloat &y, SLfloat &z)
 Computes the unnormalised vector x,y,z from tex. coords. uv with cubemap index. More...
 
void cubeXYZ2UV (SLfloat x, SLfloat y, SLfloat z, SLint &index, SLfloat &u, SLfloat &v)
 Computes the uv and cubemap image index from a unnormalised vector x,y,z. More...
 
SLstring filterString (SLint glFilter)
 Returns OpenGL texture filter as string. More...
 
void texType (SLTextureType bt)
 
void uvIndex (SLbyte i)
 
void bumpScale (SLfloat bs)
 
void minFiler (SLint minF)
 
void magFiler (SLint magF)
 
void needsUpdate (SLbool update)
 
void textureSize (int width, int height)
 
void deleteImageAfterBuild (SLbool delImg)
 If deleteImageAfterBuild is set to true you won't be able to ray trace the scene. More...
 
SLuint width ()
 
SLuint height ()
 
SLuint depth ()
 
SLbyte uvIndex ()
 
SLint bytesPerPixel ()
 
SLint bytesOnGPU ()
 
SLint bytesInFile ()
 
CVVImageimages ()
 
SLenum target () const
 
SLuint texID () const
 
SLTextureType texType ()
 
SLfloat bumpScale () const
 
SLCol4f getTexelf (SLfloat u, SLfloat v, SLuint imgIndex=0)
 SLGLTexture::getTexelf returns a pixel color from u & v texture coordinates. More...
 
SLCol4f getTexelf (const SLVec3f &cubemapDir)
 SLGLTexture::getTexelf returns a pixel color at the specified cubemap direction. More...
 
SLbool hasAlpha ()
 
SLMat4f tm ()
 
SLbool autoCalcTM3D () const
 
SLbool needsUpdate ()
 
SLstring typeName ()
 Returns the texture type as string. More...
 
SLstring typeShortName ()
 Returns the texture type short. More...
 
bool isTexture ()
 
SLstring minificationFilterName ()
 
SLstring magnificationFilterName ()
 
void build2DMipmaps (SLint target, SLuint index)
 
SLbool copyVideoImage (SLint camWidth, SLint camHeight, CVPixelFormatGL glFormat, SLuchar *data, SLbool isContinuous, SLbool isTopLeft)
 Copies the image data from a video camera into the current video image. More...
 
SLbool copyVideoImage (SLint camWidth, SLint camHeight, CVPixelFormatGL srcFormat, CVPixelFormatGL dstFormat, SLuchar *data, SLbool isContinuous, SLbool isTopLeft)
 
void calc3DGradients (SLint sampleRadius, const function< void(int)> &onUpdateProgress=nullptr)
 
void smooth3DGradients (SLint smoothRadius, function< void(int)> onUpdateProgress=nullptr)
 
SLVec2f dudv (SLfloat u, SLfloat v)
 
- Public Member Functions inherited from SLObject
 SLObject (const SLstring &Name="", const SLstring &url="")
 
virtual ~SLObject ()
 
void name (const SLstring &Name)
 
void url (const SLstring &url)
 
const SLstringname () const
 
const SLstringurl () const
 

Public Attributes

SLTexFontChar chars [256]
 
SLint charsHeight
 
SLGLProgram_fontTexProgram = nullptr
 
bool _deleteProgram = false
 

Additional Inherited Members

- Static Public Member Functions inherited from SLGLTexture
static SLTextureType detectType (const SLstring &filename)
 Detects the texture type from the filename appendix (See SLTexType def.) More...
 
static string internalFormatStr (int internalFormat)
 Returns the internal pixel format from OpenGL. More...
 
- Static Public Attributes inherited from SLGLTexture
static SLfloat maxAnisotropy = -1.0f
 Returns the derivation as [s,t]. More...
 
static SLuint totalNumBytesOnGPU = 0
 Total NO. of bytes used for textures on GPU. More...
 
- Protected Member Functions inherited from SLGLTexture
void load (const SLstring &filename, SLbool flipVertical=true, SLbool loadGrayscaleIntoAlpha=false)
 Loads the texture, converts color depth & applies vertical mirroring. More...
 
void load (const SLVCol4f &colors)
 Loads the 1D color data into an image of height 1. More...
 
- Protected Attributes inherited from SLGLTexture
CVVImage _images
 Vector of CVImage pointers. More...
 
SLuint _texID
 OpenGL texture ID. More...
 
SLTextureType _texType
 See SLTextureType. More...
 
SLint _width
 Texture image width in pixels (images exist either in _images or on the GPU or on both) More...
 
SLint _height
 Texture image height in pixels (images exist either in _images or on the GPU or on both) More...
 
SLint _depth
 3D Texture image depth (images exist either in _images or on the GPU or on both) More...
 
SLbyte _uvIndex
 Texture coordinate index in SLMesh (0 = default) More...
 
SLint _internalFormat
 Internal OpenGL format. More...
 
SLint _bytesPerPixel
 Bytes per texture image pixel (images exist either in _images or on the GPU or on both) More...
 
SLint _min_filter
 Minification filter. More...
 
SLint _mag_filter
 Magnification filter. More...
 
SLint _wrap_s
 Wrapping in s direction. More...
 
SLint _wrap_t
 Wrapping in t direction. More...
 
SLenum _target
 texture target More...
 
SLMat4f _tm
 texture matrix More...
 
SLuint _bytesOnGPU
 NO. of bytes on GPU. More...
 
SLuint _bytesInFile
 NO. of bytes in file. More...
 
SLbool _autoCalcTM3D
 Flag if texture matrix should be calculated from AABB for 3D mapping. More...
 
SLfloat _bumpScale
 Bump mapping scale factor. More...
 
SLbool _resizeToPow2
 Flag if image should be resized to n^2. More...
 
SLGLVertexArray _vaoSprite
 Vertex array object for sprite rendering. More...
 
std::atomic< bool > _needsUpdate {}
 Flag if image needs an single update. More...
 
std::mutex _mutex
 Mutex to protect parallel access (used in ray tracing) More...
 
SLbool _deleteImageAfterBuild
 Flag if images should be deleted after build on GPU. More...
 
SLbool _compressedTexture = false
 True for compressed texture format on GPU. More...
 
- Protected Attributes inherited from SLObject
SLstring _name
 name of an object More...
 
SLstring _url
 uniform resource locator More...
 

Detailed Description

Texture Font class inherits SLGLTexture for alpha blended font rendering.

This texture font class was originally inspired by the the font class of the AntTweakBar library (http://www.antisphere.com/Wiki/tools:anttweakbar) The source bitmap includes 224 characters starting from ascii char 32 (space) to ascii char 255:

 !"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_
`abcdefghijklmnopqrstuvwxyz{|}~
���������������������������������������������������������������
��������������������������������
��������������������������������

First column of a source bitmap is a delimiter with color=zero at the end of each line of characters. Last row of a line of characters is a delimiter with color=zero at the last pixel of each character. The source bitmaps for the fonts are in the folder _data/fonts. The where used for design only. Their data is directly included a binary array in the source file SLTexFont.cpp.

Definition at line 39 of file SLTexFont.h.

Constructor & Destructor Documentation

◆ SLTexFont() [1/2]

SLTexFont::SLTexFont ( SLstring  fontFilename,
SLGLProgram fontTexProgram 
)

Definition at line 19 of file SLTexFont.cpp.

20 {
21  assert(fontTexProgram);
23  _deleteProgram = false;
24 
25  // Init texture members
26  _texType = TT_font;
27  _wrap_s = GL_CLAMP_TO_EDGE;
28  _wrap_t = GL_CLAMP_TO_EDGE;
29  _min_filter = GL_NEAREST;
30  _mag_filter = GL_NEAREST;
31 
32  for (auto& i : chars)
33  {
34  i.width = 0;
35  i.tx1 = 0;
36  i.tx2 = 0;
37  i.ty1 = 0;
38  i.ty2 = 0;
39  }
40  charsHeight = 0;
41 
42  create(std::move(fontFilename));
43 }
@ TT_font
Definition: SLGLTexture.h:88
SLint _wrap_t
Wrapping in t direction.
Definition: SLGLTexture.h:314
SLint _min_filter
Minification filter.
Definition: SLGLTexture.h:311
SLint _wrap_s
Wrapping in s direction.
Definition: SLGLTexture.h:313
SLTextureType _texType
See SLTextureType.
Definition: SLGLTexture.h:304
SLint _mag_filter
Magnification filter.
Definition: SLGLTexture.h:312
bool _deleteProgram
Definition: SLTexFont.h:72
SLTexFontChar chars[256]
Definition: SLTexFont.h:68
SLint charsHeight
Definition: SLTexFont.h:69
void create(SLstring fontFilename)
Definition: SLTexFont.cpp:83
SLGLProgram * _fontTexProgram
Definition: SLTexFont.h:71
SLGLProgram * fontTexProgram()
Definition: SLTexFont.h:56

◆ SLTexFont() [2/2]

SLTexFont::SLTexFont ( SLstring  fontFilename,
SLstring  shaderDir 
)

Definition at line 45 of file SLTexFont.cpp.

46 {
47  _fontTexProgram = new SLGLProgramGeneric(nullptr, shaderDir + "FontTex.vert", shaderDir + "FontTex.frag");
48  _deleteProgram = true;
49 
50  // Init texture members
51  _texType = TT_font;
52  _wrap_s = GL_CLAMP_TO_EDGE;
53  _wrap_t = GL_CLAMP_TO_EDGE;
54  _min_filter = GL_NEAREST;
55  _mag_filter = GL_NEAREST;
56 
57  for (auto& i : chars)
58  {
59  i.width = 0;
60  i.tx1 = 0;
61  i.tx2 = 0;
62  i.ty1 = 0;
63  i.ty2 = 0;
64  }
65  charsHeight = 0;
66 
67  create(std::move(fontFilename));
68 }
Generic Shader Program class inherited from SLGLProgram.

◆ ~SLTexFont()

SLTexFont::~SLTexFont ( )

Definition at line 70 of file SLTexFont.cpp.

71 {
73  {
74  delete _fontTexProgram;
75  }
76 }

Member Function Documentation

◆ buildTextBuffers()

void SLTexFont::buildTextBuffers ( SLGLVertexArray vao,
const SLstring text,
SLfloat  maxWidth = 0.0f,
SLfloat  lineHeight = 1.5f 
)

Builds the vertex array object with 2 texture mapped triangles per character. The text width < maxWidth the text will be on one line. If it is wider it will be split into multiple lines with a height = font height * lineHeight.

Parameters
vaoexternal vertex array object
texttext to render
maxWidthmax. width for multi-line text
lineHeightline height factor

Definition at line 345 of file SLTexFont.cpp.

349 {
350  SLVstring lines; // Vector of text lines
351  SLVVec2f sizes; // Sizes of text lines
352  size_t numP = 0; // No. of vertices
353  size_t numI = 0; // No. of indices (3 per triangle)
354  SLfloat x; // current lower-left x position
355  SLfloat y; // current lower-left y position
356  SLuint iV; // current vertex index
357  SLuint iI; // current vertex index index
358 
359  // Calculate number of vertices & indices
360  if (maxWidth > 0.0f)
361  {
362  // multiple text lines
363  lines = wrapTextToLines(text, maxWidth);
364  for (auto& line : lines)
365  {
366  numP += line.length();
367  sizes.push_back(calcTextSize(line));
368  }
369  numP *= 4;
370  numI = numP * 2 * 3;
371  }
372  else
373  {
374  // single text line
375  lines.push_back(text);
376  numP = text.length() * 4;
377  numI = text.length() * 2 * 3;
378  }
379 
380  SLVVec3f P;
381  P.resize(numP); // Vertex positions
382  SLVVec2f T;
383  T.resize(numP); // Vertex texture coords.
384  SLVushort I;
385  I.resize(numI); // Indexes
386 
387  iV = iI = 0;
388  y = ((SLfloat)lines.size() - 1) * (SLfloat)charsHeight * lineHeight;
389 
390  for (auto& line : lines)
391  {
392  x = 0;
393 
394  // Loop through characters
395  for (char c : line)
396  {
397  // Get width and height
398  SLfloat w = chars[(SLuint)c].width;
400 
401  // Specify texture coordinates
402  T[iV].set(chars[(SLuint)c].tx1, chars[(SLuint)c].ty2);
403  T[iV + 1].set(chars[(SLuint)c].tx2, chars[(SLuint)c].ty2);
404  T[iV + 2].set(chars[(SLuint)c].tx2, chars[(SLuint)c].ty1);
405  T[iV + 3].set(chars[(SLuint)c].tx1, chars[(SLuint)c].ty1);
406 
407  // vertices of the character quad
408  P[iV].set(x, y);
409  P[iV + 1].set(x + w, y);
410  P[iV + 2].set(x + w, y + h);
411  P[iV + 3].set(x, y + h);
412 
413  // triangle indices of the character quad
414  I[iI++] = (SLushort)iV;
415  I[iI++] = (SLushort)iV + 1;
416  I[iI++] = (SLushort)iV + 3;
417  I[iI++] = (SLushort)iV + 1;
418  I[iI++] = (SLushort)iV + 2;
419  I[iI++] = (SLushort)iV + 3;
420 
421  // Move to next character
422  iV += 4;
423  x += w;
424  }
425 
426  y -= (SLfloat)charsHeight * lineHeight;
427  }
428 
429  // create buffers on GPU
431  sp->useProgram();
433  vao.setAttrib(AT_uv1, AT_uv1, &T);
434  vao.setIndices(&I);
435  vao.generate((SLuint)numP);
436 }
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
unsigned short SLushort
Definition: SL.h:169
vector< SLstring > SLVstring
Definition: SL.h:201
vector< SLushort > SLVushort
Definition: SL.h:195
@ AT_position
Vertex position as a 2, 3 or 4 component vectors.
Definition: SLGLEnums.h:58
@ AT_uv1
Vertex 1st texture coordinate as 2 component vector.
Definition: SLGLEnums.h:60
vector< SLVec2f > SLVVec2f
Definition: SLVec2.h:143
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
void useProgram()
SLuint width()
Definition: SLGLTexture.h:218
void setAttrib(SLGLAttributeType type, SLint elementSize, SLint location, void *dataPointer, SLGLBufferType dataType=BT_float)
Adds a vertex attribute with data pointer and an element size.
void setIndices(SLuint numIndicesElements, SLGLBufferType indexDataType, void *indexDataElements, SLuint numIndicesEdges=0, void *indexDataEdges=nullptr)
Adds the index array for indexed element drawing.
void generate(SLuint numVertices, SLGLBufferUsage usage=BU_static, SLbool outputInterleaved=true, SLuint divisor=0)
Generates the VA & VB objects for a NO. of vertices.
SLVstring wrapTextToLines(SLstring text, SLfloat maxW)
Definition: SLTexFont.cpp:277
SLVec2f calcTextSize(const SLstring &text, SLfloat maxWidth=0.0f, SLfloat lineHeightFactor=1.5f)
Definition: SLTexFont.cpp:243
SLfloat tx1
Min. Texture x-coord.
Definition: SLTexFont.h:62
SLfloat tx2
Max. Texture x-coord.
Definition: SLTexFont.h:64

◆ calcTextSize()

SLVec2f SLTexFont::calcTextSize ( const SLstring text,
SLfloat  maxWidth = 0.0f,
SLfloat  lineHeightFactor = 1.5f 
)

Returns the size (width & height) of the full text in float pixels. If a max. width is passed the text is first wrapped into multiple lines. For multiline text the line height is calculate as the font height * lineHeightFactor.

Definition at line 243 of file SLTexFont.cpp.

246 {
247  SLVec2f size(0, 0);
248 
249  if (maxWidth > 0.0f)
250  {
251  SLfloat maxX = FLT_MIN;
252  SLVstring lines = wrapTextToLines(text, maxWidth);
253  for (const auto& line : lines)
254  {
255  SLVec2f sizeLine = calcTextSize(line);
256  if (sizeLine.x > maxX) maxX = sizeLine.x;
257  }
258  size.x = maxX;
259  size.y = (SLfloat)(lines.size() - 1) * (SLfloat)charsHeight * lineHeightFactor;
260  size.y += (SLfloat)charsHeight;
261  }
262  else
263  { // Loop through each character of text
264  for (char c : text)
265  {
266  size.x += chars[(SLuint)c].width;
267  }
268  size.y = (SLfloat)charsHeight;
269  }
270  return size;
271 }
T x
Definition: SLVec2.h:30

◆ create()

void SLTexFont::create ( SLstring  fontFilename)

SLTexFont::create creates the inherited texture map with the passed image file. The bitmap image is parsed for all 224 character positions to create the according texture coordinate.

Definition at line 83 of file SLTexFont.cpp.

84 {
85  // Check the font filename with path
86  if (!SLFileStorage::exists(fontFilename, IOK_font))
87  {
88  SLstring msg = "SLTexFont::create: File not found: " + fontFilename;
89  SL_EXIT_MSG(msg.c_str());
90  }
91 
92  CVImage img;
93  img.load(fontFilename, false);
94 
95  // find height of the font
96  SLint x, y;
97  SLint bmpW = img.cvMat().cols;
98  SLint bmpH = img.cvMat().rows;
99  SLuchar* bmp = img.cvMat().data;
100  SLint h = 0, hh = 0;
101  SLint r, NbRow = 0;
102 
103  for (y = 0; y < bmpH; ++y)
104  {
105  if (bmp[y * bmpW] == 0)
106  {
107  if ((hh <= 0 && h <= 0) || (h != hh && h > 0 && hh > 0))
108  SL_EXIT_MSG("Cannot determine font height (check first pixel column)");
109  else if (h <= 0)
110  h = hh;
111  else if (hh <= 0)
112  break;
113  hh = 0;
114  ++NbRow;
115  }
116  else
117  ++hh;
118  }
119 
120  // find width and position of each character
121  SLint x0[224], y0[224], x1[224], y1[224];
122  SLint ch = 32;
123  SLint start;
124  for (r = 0; r < NbRow; ++r)
125  {
126  start = 1;
127  for (x = 1; x < bmpW; ++x)
128  {
129  if (bmp[(r * (h + 1) + h) * bmpW + x] == 0 || x == bmpW - 1)
130  {
131  if (x == start) break; // next row
132  if (ch < 256)
133  {
134  x0[ch - 32] = start;
135  x1[ch - 32] = x;
136  y0[ch - 32] = r * (h + 1);
137  y1[ch - 32] = r * (h + 1) + h - 1;
138  start = x + 1;
139  }
140  ++ch;
141  }
142  }
143  }
144 
145  for (x = ch - 32; x < 224; ++x)
146  {
147  x0[x] = 0;
148  y0[x] = 0;
149  x1[x] = 0;
150  y1[x] = 0;
151  }
152 
153  // Repack: build 14 rows of 16 characters. First, find the largest row
154  SLint l, lmax = 1;
155  for (r = 0; r < 14; ++r)
156  {
157  l = 0;
158  for (x = 0; x < 16; ++x)
159  l += x1[x + r * 16] - x0[x + r * 16] + 1;
160  if (l > lmax) lmax = l;
161  }
162 
163  // A little empty margin is added between chars to avoid artefact when anti aliasing is on
164  const SLint MARGIN_X = 2;
165  const SLint MARGIN_Y = 2;
166  lmax += 16 * MARGIN_X;
167 
168  // 2) build the texture
169  charsHeight = h;
170  SLuint texWidth = Utils::nextPowerOf2((SLuint)lmax);
171  SLuint texHeight = Utils::nextPowerOf2(14 * (SLuint)(h + MARGIN_Y));
172 
173  // Fill up with 0
174  SLuchar* bits = new SLuchar[texWidth * texHeight];
175  memset(bits, 0, texWidth * texHeight);
176 
177  SLfloat du = 0.0f;
178  SLfloat dv = 0.0f;
179 
180  for (r = 0; r < 14; ++r)
181  {
182  for (SLint xx = 0, ch = r * 16; ch < (r + 1) * 16; ++ch)
183  {
184  if (y1[ch] - y0[ch] == h - 1)
185  {
186  for (y = 0; y < h; ++y)
187  {
188  for (x = x0[ch]; x <= x1[ch]; ++x)
189  {
190  SLfloat alpha = ((SLfloat)(bmp[x + (y0[ch] + y) * bmpW])) / 256.0f;
191  // alpha = alpha*sqrtf(alpha); // powf(alpha, 1.5f); // some gamma correction
192  bits[(SLuint)(xx + x - x0[ch]) +
193  (SLuint)(r * (h + MARGIN_Y) + y) * texWidth] = (SLuchar)(alpha * 256.0f);
194  }
195  }
196  chars[ch + 32].tx1 = ((SLfloat)xx + du) / (SLfloat)texWidth;
197  xx += x1[ch] - x0[ch] + 1;
198  chars[ch + 32].tx2 = ((SLfloat)xx + du) / (SLfloat)texWidth;
199  chars[ch + 32].ty1 = ((SLfloat)(r * (h + MARGIN_Y)) + dv) / (SLfloat)texHeight;
200  chars[ch + 32].ty2 = ((SLfloat)(r * (h + MARGIN_Y) + h) + dv) / (SLfloat)texHeight;
201  chars[ch + 32].width = (SLfloat)(x1[ch] - x0[ch] + 1);
202  xx += MARGIN_X;
203  }
204  }
205  }
206 
207  // Allocate memory for image pixels using only the alpha channel
208  _images.clear();
209  SLGLState* stateGL = SLGLState::instance();
211  _images.push_back(new CVImage((SLint)texWidth,
212  (SLint)texHeight,
213  format,
214  fontFilename));
215  _images[0]->load((SLint)texWidth,
216  (SLint)texHeight,
217  format,
218  format,
219  bits,
220  true,
221  false);
222  delete[] bits;
223  _width = _images[0]->width();
224  _height = _images[0]->height();
225  _depth = (SLint)_images.size();
226 
227  // Set characters below 32 to default
228  const SLuchar Undef = 127; // default character used as for undifined ones (having ascii codes from 0 to 31)
229  for (ch = 0; ch < 32; ++ch)
230  {
231  chars[ch].tx1 = chars[Undef].tx1;
232  chars[ch].tx2 = chars[Undef].tx2;
233  chars[ch].ty1 = chars[Undef].ty1;
234  chars[ch].ty2 = chars[Undef].ty2;
235  chars[ch].width = chars[Undef].width;
236  }
237 }
CVPixelFormatGL
Pixel format according to OpenGL pixel format defines.
Definition: CVImage.h:24
@ PF_luminance
Definition: CVImage.h:28
@ PF_red
Definition: CVImage.h:34
unsigned char SLuchar
Definition: SL.h:163
#define SL_EXIT_MSG(message)
Definition: SL.h:240
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
@ IOK_font
Definition: SLFileStorage.h:43
OpenCV image class with the same interface as the former SLImage class.
Definition: CVImage.h:64
CVMat cvMat() const
Definition: CVImage.h:122
void load(const string &filename, bool flipVertical=true, bool loadGrayscaleIntoAlpha=false)
Loads the image with the appropriate image loader.
Definition: CVImage.cpp:379
Singleton class holding all OpenGL states.
Definition: SLGLState.h:71
static SLGLState * instance()
Public static instance getter for singleton pattern.
Definition: SLGLState.h:74
SLbool pixelFormatIsSupported(SLint pixelFormat)
Returns true if the according GL pixel format is valid in the GL context.
Definition: SLGLState.cpp:588
SLint _width
Texture image width in pixels (images exist either in _images or on the GPU or on both)
Definition: SLGLTexture.h:305
SLint _height
Texture image height in pixels (images exist either in _images or on the GPU or on both)
Definition: SLGLTexture.h:306
CVVImage _images
Vector of CVImage pointers.
Definition: SLGLTexture.h:302
SLint _depth
3D Texture image depth (images exist either in _images or on the GPU or on both)
Definition: SLGLTexture.h:307
bool exists(std::string path, SLIOStreamKind kind)
Checks whether a given file exists.
unsigned nextPowerOf2(unsigned num)
Returns the next power of 2 to a passed number.
Definition: Utils.cpp:1237
SLfloat width
Width of char. in tex-coord.
Definition: SLTexFont.h:61
SLfloat ty1
Max. Texture y-coord.
Definition: SLTexFont.h:63
SLfloat ty2
Min. Texture y-coord.
Definition: SLTexFont.h:65

◆ fontTexProgram()

SLGLProgram* SLTexFont::fontTexProgram ( )
inline

Definition at line 56 of file SLTexFont.h.

56 { return _fontTexProgram; }

◆ wrapTextToLines()

SLVstring SLTexFont::wrapTextToLines ( SLstring  text,
SLfloat  maxW 
)

Returns a vector of strings of the text to be wrapped to a max. with of maxW. The sum of all characters in lines must be equal to the length of the input text

Definition at line 277 of file SLTexFont.cpp.

279 {
280  SLVstring lines;
281  SLfloat curX = 0.0f;
282  SLfloat maxX = FLT_MIN;
283  SLfloat xBlank = 0.0f;
284  SLuint iBlank = 0;
285  SLuint iLineStart = 0;
286  SLuint len = (SLuint)text.length();
287 
288  // Loop through each character of text
289  for (SLuint i = 0; i < len; ++i)
290  {
291  SLchar c = text[i];
292 
293  if (c == '\\' && i < len - 1 && text[i + 1] == 'n')
294  {
295  i++;
296  if (curX > maxX) maxX = curX;
297  lines.push_back(text.substr(iLineStart, i - iLineStart - 1) + " ");
298  iLineStart = i + 1;
299  curX = 0.0f;
300  }
301  else // add next character
302  {
303  // keep last blank x
304  if (c == ' ')
305  {
306  xBlank = curX;
307  iBlank = i;
308  }
309 
310  curX += chars[(SLuint)c].width;
311 
312  // if width exceeded wrap at last blank position
313  if (curX > maxW)
314  { // wrap at last blank
315  if (xBlank > 0.0f)
316  {
317  // keep largest line width
318  if (xBlank > maxX) maxX = xBlank;
319  curX = curX - xBlank - chars[(SLuint)' '].width;
320  lines.push_back(text.substr(iLineStart, iBlank - iLineStart + 1));
321  iLineStart = iBlank + 1;
322  }
323  else // wrap in the word
324  {
325  if (curX - chars[(SLuint)c].width > maxX)
326  maxX = curX - chars[(SLuint)c].width;
327  lines.push_back(text.substr(iLineStart, i - iLineStart));
328  curX = chars[(SLuint)c].width;
329  iLineStart = i + 1;
330  }
331  }
332  }
333  }
334  SLstring newLine = text.substr(iLineStart, len - iLineStart);
335  lines.push_back(newLine);
336  return lines;
337 }
char SLchar
Definition: SL.h:162

Member Data Documentation

◆ _deleteProgram

bool SLTexFont::_deleteProgram = false

Definition at line 72 of file SLTexFont.h.

◆ _fontTexProgram

SLGLProgram* SLTexFont::_fontTexProgram = nullptr

Definition at line 71 of file SLTexFont.h.

◆ chars

SLTexFontChar SLTexFont::chars[256]

Definition at line 68 of file SLTexFont.h.

◆ charsHeight

SLint SLTexFont::charsHeight

Definition at line 69 of file SLTexFont.h.


The documentation for this class was generated from the following files: