7 m_VBO(6,sizeof(float)*2,GL_STATIC_DRAW),
8 m_Shader(
"resources/shaders/text/vertex.glsl",
"resources/shaders/text/fragment.glsl"),
10 m_LoadedFontPath(font_path),
11 m_LoadedGlyphSize(glyph_size),
12 m_ID(std::numeric_limits<uint32_t>::max()),
13 m_FontPath(font_path),
14 m_GlyphSize(glyph_size),
18 m_LoadedFontPath.resize(100);
20 Init(font_path,glyph_size,fixed);
25 m_VBO(6,sizeof(float)*2,GL_STATIC_DRAW),
26 m_Shader(
"resources/shaders/text/vertex.glsl",
"resources/shaders/text/fragment.glsl"),
28 m_LoadedFontPath(font_path),
29 m_LoadedGlyphSize(glyph_size),
31 m_FontPath(font_path),
32 m_GlyphSize(glyph_size),
36 m_LoadedFontPath.resize(100);
38 Init(font_path,glyph_size,fixed);
48 glDeleteTextures(1,&m_TextureArrayID);
71 glActiveTexture(GL_TEXTURE0);
72 glBindTexture(GL_TEXTURE_2D_ARRAY,m_TextureArrayID);
80 Character
ch=m_Characters[c];
91 m_ToRender[workingIndex]=
ch.TexID;
100 Render(workingIndex);
107 return std::make_pair(0.0f,0.0f);
112 float max_width=0.0f;
116 Character
ch=m_Characters[c];
119 max_width=std::max(max_width,width_);
130void TextRenderer::Init(
const std::string &font_path,
float glyph_size,
bool fixed){
137 if(FT_Init_FreeType(&m_FT))
138 perror(
"FREETYPE ERROR: Couldn't init FreeType Library\n");
140 if(FT_New_Face(m_FT,font_path.c_str(),0,&m_Face)){
141 perror(
"FREETYPE ERROR: Failed to load font\n");
145 FT_Select_Charmap(m_Face,ft_encoding_unicode);
146 FT_Set_Pixel_Sizes(m_Face,glyph_size,glyph_size);
147 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
149 glGenTextures(1,&m_TextureArrayID);
150 glActiveTexture(GL_TEXTURE0);
151 glBindTexture(GL_TEXTURE_2D_ARRAY,m_TextureArrayID);
152 glTexImage3D(GL_TEXTURE_2D_ARRAY,0,GL_R8,glyph_size,glyph_size,
CH_NUM,0,GL_RED,GL_UNSIGNED_BYTE,0);
155 if(FT_Load_Char(m_Face,
ch,FT_LOAD_RENDER)){
156 perror(
"FREETYPE ERROR: Failed to load Glyph\n");
160 if(m_Face->glyph->bitmap.rows>glyph_size){
162 printf(
"FREETYPE WARNING: Glyph %c is too tall\n",
ch);
170 m_Face->glyph->bitmap.width,
171 m_Face->glyph->bitmap.rows,1,
172 GL_RED,GL_UNSIGNED_BYTE,
173 m_Face->glyph->bitmap.buffer
176 glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
177 glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
178 glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
179 glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
181 Character character={
183 glm::ivec2(m_Face->glyph->bitmap.width,m_Face->glyph->bitmap.rows),
184 glm::ivec2(m_Face->glyph->bitmap_left,m_Face->glyph->bitmap_top),
185 (
unsigned int)m_Face->glyph->advance.x
187 m_Characters[
ch]=character;
189 FT_Done_Face(m_Face);
191 FT_Done_FreeType(m_FT);
200 m_VBO.
SetData(0,vertices,4,
sizeof(
float)*2);
201 m_VBL.
Push(GL_FLOAT,2,
false);
205void TextRenderer::Render(
int num_characters){
208 if(num_characters!=0){
209 m_Shader.
SetUniformMat4fv(
"transforms",glm::value_ptr(m_Transforms[0]),num_characters);
210 m_Shader.
SetUniform1iv(
"letterMap",&m_ToRender[0],num_characters);
211 glDrawArraysInstanced(GL_TRIANGLE_STRIP,0,4,num_characters);
void SetUniform1iv(const std::string &name, int *v, unsigned int num_elem)
void SetUniformMat4fv(const std::string &name, float *proj, unsigned int num_elem)
void SetUniform3f(const std::string &name, float v0, float v1, float v2)
void Push(TextRenderer *renderer, const std::string &text, float x, float y, float scale, Vec3 color, int layer)
std::pair< float, float > GetTextSize(std::string text, float scale)
void DrawText(const std::string &text, float x, float y, float scale, int layer, Vec3 color)
void _DrawText(const std::string &text, float x, float y, float scale, Vec3 color)
void AddBuffer(const VertexBuffer &vb, const VertexBufferLayout &layout)
void Push(unsigned int type, unsigned int count, bool normalized)
void SetData(unsigned int vertex_index, float *data, unsigned int num_vertices, unsigned int VertexSize)
void FreeMemory(void *ptr, std::source_location src=std::source_location::current())
void * AllocateMemory(size_t size, std::source_location src=std::source_location::current())
constexpr unsigned int CH_LIMIT
constexpr unsigned int CH_NUM
TextQueue * TEXT_QUEUE
The text queue.