GteFont.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.1 (2016/06/26)
7 
8 #include <GTEnginePCH.h>
10 #include <LowLevel/GteWrapper.h>
11 #include <Graphics/GteFont.h>
12 #include <cstring>
13 using namespace gte;
14 
15 Font::Font(std::shared_ptr<ProgramFactory> const& factory, unsigned int width, unsigned int height,
16  char const* texels, float const* characterData, unsigned int maxMessageLength)
17  :
18  mMaxMessageLength(maxMessageLength)
19 {
20  // Create a vertex buffer to hold the maximum specified message.
21  struct Vertex
22  {
23  Vector2<float> position, tcoord;
24  };
25 
26  VertexFormat vformat;
27  vformat.Bind(VA_POSITION, DF_R32G32_FLOAT, 0);
28  vformat.Bind(VA_TEXCOORD, DF_R32G32_FLOAT, 0);
29  unsigned int numVertices = 4 * mMaxMessageLength;
30  mVertexBuffer = std::make_shared<VertexBuffer>(vformat, numVertices);
32 
33  // Set the y values for top vertex positions and all texture
34  // coordinates, since they do not change.
35  // 0 -- 2 4 -- 6 ... <-- pos.y = 0, tex.y = 0
36  // | \ | | \ |
37  // | \ | | \ |
38  // 1 -- 3 5 -- 7 ... <-- tex.y = 1
39  Vertex* vertices = mVertexBuffer->Get<Vertex>();
40  memset(vertices, 0, numVertices * sizeof(Vertex));
41  for (unsigned int i = 0; i < mMaxMessageLength; ++i)
42  {
43  Vertex& v0 = vertices[4 * i + 0];
44  Vertex& v1 = vertices[4 * i + 1];
45  Vertex& v2 = vertices[4 * i + 2];
46  Vertex& v3 = vertices[4 * i + 3];
47 
48  v0.position[1] = 0.0f;
49  v0.tcoord[1] = 0.0f;
50  v1.tcoord[1] = 1.0f;
51  v2.position[1] = 0.0f;
52  v2.tcoord[1] = 0.0f;
53  v3.tcoord[1] = 1.0f;
54  }
55 
56  // Set the x coordinates on the first two vertices to zero,
57  // since they do not change.
58  vertices[0].position[0] = 0.0f;
59  vertices[1].position[0] = 0.0f;
60 
61  // Create and set the index buffer data.
62  // 0 -- 2 4 -- 6 ...
63  // | \ | | \ |
64  // | \ | | \ |
65  // 1 -- 3 5 -- 7 ...
66  unsigned int numTriangles = 2 * mMaxMessageLength;
67  mIndexBuffer = std::make_shared<IndexBuffer>(IP_TRIMESH, numTriangles,
68  sizeof(unsigned int));
69  unsigned int* ibuf = mIndexBuffer->Get<unsigned int>();
70  for (unsigned int i = 0; i < mMaxMessageLength; ++i)
71  {
72  // Bottom triangle
73  ibuf[6 * i + 0] = 4 * i;
74  ibuf[6 * i + 1] = 4 * i + 3;
75  ibuf[6 * i + 2] = 4 * i + 1;
76 
77  // Top triangle
78  ibuf[6 * i + 3] = 4 * i;
79  ibuf[6 * i + 4] = 4 * i + 2;
80  ibuf[6 * i + 5] = 4 * i + 3;
81  }
82 
83  // Create a texture from the specified monochrome bitmap.
84  mTexture = std::make_shared<Texture2>(DF_R8_UNORM, width, height);
85  Memcpy(mTexture->GetData(), texels, mTexture->GetNumBytes());
86  Memcpy(mCharacterData, characterData, 257 * sizeof(float));
87 
88  // Create an effect for drawing text.
89  mTextEffect = std::make_shared<TextEffect>(factory, mTexture);
90 }
91 
92 void Font::Typeset(int viewportWidth, int viewportHeight, int x, int y,
93  Vector4<float> const& color, std::string const& message) const
94 {
95  // Get texel translation units, depends on viewport width and height.
96  float const vdx = 1.0f / static_cast<float>(viewportWidth);
97  float const vdy = 1.0f / static_cast<float>(viewportHeight);
98 
99  // Get texture information.
100  float tw = static_cast<float>(mTexture->GetWidth());
101  float th = static_cast<float>(mTexture->GetHeight());
102 
103  // Get vertex buffer information.
104  unsigned int vertexSize = mVertexBuffer->GetFormat().GetVertexSize();
105  char* data = mVertexBuffer->GetData();
106 
107  float x0 = 0.0f;
108  unsigned int const length = std::min(
109  static_cast<unsigned int>(message.length()), mMaxMessageLength);
110  for (unsigned int i = 0; i < length; ++i)
111  {
112  // Get character data.
113  int c = static_cast<int>(message[i]);
114  float const tx0 = mCharacterData[c];
115  float const tx1 = mCharacterData[c + 1];
116  float charWidthM1 = (tx1 - tx0)*tw - 1.0f; // in pixels
117 
118  // 0 -- 2 4 -- 6 ...
119  // | \ | | \ |
120  // | \ | | \ |
121  // 1 -- 3 5 -- 7 ...
122  float* v0 = reinterpret_cast<float*>(data + (4 * i + 0)*vertexSize);
123  float* v1 = reinterpret_cast<float*>(data + (4 * i + 1)*vertexSize);
124  float* v2 = reinterpret_cast<float*>(data + (4 * i + 2)*vertexSize);
125  float* v3 = reinterpret_cast<float*>(data + (4 * i + 3)*vertexSize);
126 
127  // Set bottom left vertex y coordinate.
128  v1[1] = vdy*th;
129 
130  // Set x-coordinates.
131  float x1 = x0 + charWidthM1*vdx;
132  v0[0] = x0;
133  v1[0] = x0;
134  v2[0] = x1;
135  v3[0] = x1;
136 
137  // Set bottom right-side y-coordinate.
138  v3[1] = vdy*th;
139 
140  // Set the four texture x-coordinates. The y-coordinates were set in
141  // the constructor.
142  v0[2] = tx0;
143  v1[2] = tx0;
144  v2[2] = tx1;
145  v3[2] = tx1;
146 
147  // Update left x coordinate for next quad
148  x0 = x1;
149  }
150 
151  // Update the number of triangles that should be drawn.
152  mVertexBuffer->SetNumActiveElements(4 * length);
153  mIndexBuffer->SetNumActivePrimitives(2 * length);
154 
155  // Set effect parameters.
156  float trnX = vdx*static_cast<float>(x);
157  float trnY = 1.0f - vdy*static_cast<float>(y);
158  mTextEffect->SetTranslate(trnX, trnY);
159  mTextEffect->SetColor(color);
160 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
VA_TEXCOORD
GLint GLsizei width
Definition: glcorearb.h:98
IP_TRIMESH
bool Bind(VASemantic semantic, DFType type, unsigned int unit)
GLuint color
Definition: glcorearb.h:1256
GLfloat GLfloat v1
Definition: glcorearb.h:812
GLint GLenum GLint x
Definition: glcorearb.h:404
DF_R8_UNORM
Definition: GteDataFormat.h:20
const GLubyte * c
Definition: glext.h:11671
VA_POSITION
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
GLboolean * data
Definition: glcorearb.h:126
GLuint GLfloat x0
Definition: glext.h:9013
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2538
GLfloat GLfloat GLfloat GLfloat v3
Definition: glcorearb.h:814
GLfloat v0
Definition: glcorearb.h:811
GLint GLsizei GLsizei height
Definition: glcorearb.h:98
void Typeset(int viewportWidth, int viewportHeight, int x, int y, Vector4< float > const &color, std::string const &message) const
Definition: GteFont.cpp:92
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:790
std::shared_ptr< IndexBuffer > mIndexBuffer
Definition: GteFont.h:36
GLuint GLfloat GLfloat GLfloat x1
Definition: glext.h:9013
DF_R32G32_FLOAT
Definition: GteDataFormat.h:20
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:813
void Memcpy(void *target, void const *source, size_t count)
Definition: GteWrapper.cpp:16
float mCharacterData[257]
Definition: GteFont.h:39
GLfloat f
Definition: glcorearb.h:1921
std::shared_ptr< Texture2 > mTexture
Definition: GteFont.h:37
Font(std::shared_ptr< ProgramFactory > const &factory, unsigned int width, unsigned int height, char const *texels, float const *characterData, unsigned int maxMessageLength)
Definition: GteFont.cpp:15
GLint y
Definition: glcorearb.h:98
std::shared_ptr< VertexBuffer > mVertexBuffer
Definition: GteFont.h:35
std::shared_ptr< TextEffect > mTextEffect
Definition: GteFont.h:38
unsigned int mMaxMessageLength
Definition: GteFont.h:34


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59