All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
TextMeshHelpers.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "FontCharacter.h"
6 #include "VectorMath.h"
7 
8 namespace Eegeo
9 {
10  namespace TextMeshes
11  {
12  namespace TextMeshHelpers
13  {
14  inline void CalcLocalQuadPointsSceenSpace(const Fonts::FontCharacter& fontCharacter, float x, float y, float z, float fontBaselineOffset, float sizeFactor, v3 out_points[4])
15  {
16  // http://www.angelcode.com/products/bmfont/doc/render_text.html
17 
18  // screen origin top left, [x,y] is red baseline dot on diagram
19  const float left = x + fontCharacter.offsetX * sizeFactor;
20  const float right = left + fontCharacter.width * sizeFactor;
21  const float top = y + (fontCharacter.offsetY - fontBaselineOffset)*sizeFactor;
22  const float bottom = top + fontCharacter.height * sizeFactor;
23 
24  out_points[0].Set(left, top, z);
25  out_points[1].Set(right, top, z);
26  out_points[2].Set(left, bottom, z);
27  out_points[3].Set(right, bottom, z);
28  }
29 
30  inline void CalcLocalQuadPoints(const Fonts::FontCharacter& fontCharacter, float x, float y, float z, float fontLineHeight, float sizeFactor, v3 out_points[4], bool anchoredBottom=false)
31  {
32  // http://www.angelcode.com/products/bmfont/doc/render_text.html
33 
34  const float w = fontCharacter.width * sizeFactor;
35  const float h = fontCharacter.height * sizeFactor;
36  const float ox = fontCharacter.offsetX * sizeFactor;
37 
38  const float left = x + ox;
39  const float right = left + w;
40 
41  float top = 0.f;
42  float bottom = 0.f;
43  if (anchoredBottom)
44  {
45  bottom = y + (fontLineHeight*sizeFactor);
46  top = bottom - h;
47  }
48  else
49  {
50  top = y + (fontLineHeight - fontCharacter.offsetY)*sizeFactor;
51  bottom = top - h;
52  }
53 
54  out_points[0].Set(left, top, z);
55  out_points[1].Set(right, top, z);
56  out_points[2].Set(left, bottom, z);
57  out_points[3].Set(right, bottom, z);
58  }
59 
60  inline void CalcQuadTexCoords(const Fonts::FontCharacter& fontCharacter, float oneOverScaleW, float oneOverScaleH, v2& out_leftBottom, v2& out_rightTop)
61  {
62  const float left = fontCharacter.x * oneOverScaleW;
63  const float right = (fontCharacter.x + fontCharacter.width) * oneOverScaleW;
64  // OpenGL texcoord(0,0) is bottom left
65  // fontCharacter x,y (0,0) is top left of texture, y addresses top of glyph
66  const float top = 1.0f - fontCharacter.y * oneOverScaleH;
67  const float bottom = 1.0f - (fontCharacter.y + fontCharacter.height) * oneOverScaleH;
68  out_leftBottom.Set(left, bottom);
69  out_rightTop.Set(right, top);
70  }
71 
72  }
73  }
74 }