OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Text.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef TEXT_H
22 #define TEXT_H
23 
24 #include "Matrix.h"
25 
26 #include <GLES3/gl3.h>
27 
28 namespace Skybox
29 {
33  const char fontVertexShaderSource[] =
34  {
35  "#version 300 es\n"
36  "uniform mat4 u_m4Projection;\n"
37  "in vec4 a_v4Position;\n"
38  "in vec4 a_v4FontColor;\n"
39  "in vec2 a_v2TexCoord;\n"
40  "out vec4 v_v4FontColor;\n"
41  "out vec2 v_v2TexCoord;\n"
42  "void main() {\n"
43  " v_v4FontColor = a_v4FontColor;\n"
44  " v_v2TexCoord = a_v2TexCoord;\n"
45  " gl_Position = u_m4Projection * a_v4Position;\n"
46  "}\n"
47  };
48 
52  const char fontFragmentShaderSource[] =
53  {
54  "#version 300 es\n"
55  "precision mediump float;\n"
56  "uniform sampler2D u_s2dTexture;\n"
57  "in vec2 v_v2TexCoord;\n"
58  "in vec4 v_v4FontColor;\n"
59  "out vec4 color;\n"
60  "void main() {\n"
61  " vec4 v4Texel = texture(u_s2dTexture, v_v2TexCoord);\n"
62  " color = v_v4FontColor * v4Texel;\n"
63  "}\n"
64  };
65 
72  void loadData(const char* filename, unsigned char** textureData);
73 
81  GLint get_and_check_attrib_location(GLuint program, const GLchar* attrib_name);
82 
90  GLint get_and_check_uniform_location(GLuint program, const GLchar* uniform_name);
91 
95  typedef struct Vec2
96  {
97  int x;
98  int y;
99  } Vec2;
100 
107  class Text
108  {
109  private:
110  static const char textureFilename[];
111  static const char vertexShaderFilename[];
112  static const char fragmentShaderFilename[];
113 
120  static const float scale;
121 
124  float* textVertex;
126  float* color;
127  GLshort* textIndex;
137 
138  public:
143  static const int textureCharacterWidth;
144 
149  static const int textureCharacterHeight;
150 
158  Text(const char* resourceDirectory, int windowWidth, int windowHeight);
159 
163  Text(void);
164 
168  ~Text(void);
169 
175  void clear(void);
176 
188  void addString(int xPosition, int yPosition, const char* string, int red, int green, int blue, int alpha);
189 
195  void draw(void);
196  };
197 }
198 #endif /* TEXT_H */
void loadData(const char *filename, unsigned char **textureData)
Load texture data from a file into memory.
Definition: Text.cpp:38
int windowWidth
Definition: Cube.cpp:56
Functions for drawing text in OpenGL ES.
Definition: Text.h:107
Matrix projectionMatrix
Definition: Text.h:122
GLint get_and_check_attrib_location(GLuint program, const GLchar *attrib_name)
Invoke glGetAttribLocation(), if it has returned a positive value. Otherwise, print a message and exi...
Definition: Text.cpp:73
static const int textureCharacterHeight
The height (in pixels) of the characters in the text texture.
Definition: Text.h:149
float * textTextureCoordinates
Definition: Text.h:125
void clear(void)
Removes the current string from the class.
Definition: Text.cpp:170
GLuint vertexShaderID
Definition: Text.h:133
void draw(void)
Draw the text to the screen.
Definition: Text.cpp:285
int m_iLocTexCoord
Definition: Text.h:131
int m_iLocProjection
Definition: Text.h:129
GLint get_and_check_uniform_location(GLuint program, const GLchar *uniform_name)
Invoke glGetUniformLocation, if it has returned a positive value. Otherwise, print a message and exit...
Definition: Text.cpp:87
const char fontFragmentShaderSource[]
Fragment shader source code for text rendering.
Definition: Text.h:52
~Text(void)
Overloaded default destructor.
Definition: Text.cpp:353
struct Skybox::Vec2 Vec2
Type representing texture coordinates.
static const char fragmentShaderFilename[]
Definition: Text.h:112
float * color
Definition: Text.h:126
int windowHeight
Definition: Cube.cpp:57
const char fontVertexShaderSource[]
Vertex shader source code for text rendering.
Definition: Text.h:33
int m_iLocTexture
Definition: Text.h:132
int x
Definition: Text.h:97
int m_iLocTextColor
Definition: Text.h:130
int y
Definition: Text.h:98
int m_iLocPosition
Definition: Text.h:128
int numberOfCharacters
Definition: Text.h:123
GLuint fragmentShaderID
Definition: Text.h:134
static const int textureCharacterWidth
The width (in pixels) of the characters in the text texture.
Definition: Text.h:143
static const char textureFilename[]
Definition: Text.h:110
unsigned char * textureData
Definition: ThreadSync.cpp:109
string resourceDirectory
Definition: AntiAlias.cpp:55
float * textVertex
Definition: Text.h:124
Type representing texture coordinates.
Definition: Text.h:95
Text(void)
Overloaded default constructor.
Definition: Text.cpp:338
Functions for manipulating matrices.
Definition: Matrix.h:29
static const float scale
Scaling factor to use when rendering the text.
Definition: Text.h:120
GLuint program
Definition: gl2ext.h:1475
GLuint textureID
Definition: Text.h:136
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
GLuint programID
Definition: Text.h:135
GLshort * textIndex
Definition: Text.h:127
static const char vertexShaderFilename[]
Definition: Text.h:111
void addString(int xPosition, int yPosition, const char *string, int red, int green, int blue, int alpha)
Add a std::string to be drawn to the screen.
Definition: Text.cpp:181