   Rendering text without a recreation of the vertex buffer
  Hi!I am looking for a way to render text efficiently. so far I've tried two options, both of them being neither fast nor very elegant:option #1 - Create a vertex buffer which contains all the glyphs at startup and then render each glyph individually, advancing the raster position each time:// render all linesfor( uint i = 0; i < lines.size(); i++ ){// render all glyphs in a linefor( uint j = 0; j < lines.length(); j++ ){// look up glyph associated with character codemap<unsigned int, shared_ptr<Glyph>>::iterator glyphIndex = font->m_glyphs.find( lines[j] );// skip if not foundif( glyphIndex == font->m_glyphs.end() )continue;// get the glyph itselfshared_ptr<Glyph> glyph = glyphIndex->second;// setup view matrix from raster positionviewMatrix[3] = vec4( (vec2)rasterPosition, 0.0f, 1.0f );// bind view matrix to shaderEngine::getGraphicsEngine()->bindConstant( m_shader, "u_View", viewMatrix );// render glyphEngine::getGraphicsEngine()->renderVertexbuffer( font->m_vertexbuffer, glyph->m_indexbufferOffset, 6 );// advance raster positionrasterPosition.x += (int)((glyph->m_spacing.x)*spacing);}// start new linerasterPosition.x = position.x;rasterPosition.y -= (int)(font->m_size*spacing);}this is straight forward but results in a lot of draw calls.option #2 - each frame, compute a glpyh's vertices from its original vertices plus the current raster position and create and update the vertex buffer:vector<Vertex> vertices;vertices.reserve( text.length()*6 );// render all linesfor( uint i = 0; i < lines.size(); i++ ){// render all glyphs in a linefor( uint j = 0; j < lines.length(); j++ ){// look up glyph associated with character codemap<unsigned int, shared_ptr<Glyph>>::iterator glyphIndex = font->m_glyphs.find( lines[j] );// skip if not foundif( glyphIndex == font->m_glyphs.end() )continue;// get the glyph itselfshared_ptr<Glyph> glyph = glyphIndex->second;for( uint i = 0; i < 6; i++ ){Vertex vertex = glyph->m_vertices;vertex.position.x += rasterPosition.x;vertex.position.y += rasterPosition.y;vertices.push_back( vertex );}// advance raster positionrasterPosition.x += (int)((glyph->m_spacing.x)*spacing);}// start new linerasterPosition.x = position.x;rasterPosition.y -= (int)(font->m_size*spacing);}Engine::getGraphicsEngine()->updateVertexbuffer( font->m_vertexbuffer, vertices );Engine::getGraphicsEngine()->renderVertexbuffer( font->m_vertexbuffer );option #2 seems to make more sense since it requires a lot less draw calls but I don't like the idea of having to re-create and re-upload the vertex buffer every time the text changes.The only thing that has to be changed in between glyphs is the raster position. so let's say I store a glyph's horizontal and vertical spacing in the z and w components of its vertices. is there a way to update a global variable vec2 rasterPosition in the vertex shader? or how else could I keep track of the current raster position without having to split up rendering into the indivudual glyphs?I am using OpenGL right now but I'm looking for an API-independent solution.thank you for your input!   
  I´ve did it the very same way but it isn´t fast enough.. especially if it comes to a lot of text. Now I use the OS´ services to display text. That´s absolutely lightweight in terms of performance. But if you need accelerated text, you should render static text to textures instead of drawing every glyph into the screenbuffer. Your solution via a lookuptable for glyph information is great for dynamic text.   ;  yeah sure i'm gonna use textures for static text later! but first I'd like to decide on a way to draw dynamic text. and if I would find a way which does not require the recomputation of vertex buffers, I could also use precomputed vertex buffers for often used text strings and store these in a hash map.I'll try to illustrate what I want to do. So let's say the input vertex stores its position in its xy components and the glyph's spacing in the xw components. If I had a global variable to store the current raster position in the vertex shader, I could position the glyphs like this:vec2 rasterPosition; // <- a global variable that is incremented with each vertexvoid main(){...// advance raster position by glyph's spacingrasterPosition += gl_Vertex.zw;// final position is vertex position + raster positionvec2 Vertex = gl_Vertex.xy+rasterPosition;// multiply with modelview and projection matrix and output vertexgl_Position = u_ModelviewProjectionMatrix * vec4( Vertex, 0.0, 1.0 );}is that possible in OpenGL or Direct3D?   ;  Ok now I got it :) No unfortunately you can´t do that with shader variables :( It looks like your second option is the way to go. Because of the single draw call. And maybe it´s possible to reuse the old VBO if nothing changes.. so that you don´t have to rebuild the buffer every frame. These things induced me to do text rendering via OS...   ;Quote:Original post by qCringI´ve did it the very same way but it isn´t fast enough.. especially if it comes to a lot of text. Now I use the OS´ services to display text. That´s absolutely lightweight in terms of performance. But if you need accelerated text, you should render static text to textures instead of drawing every glyph into the screenbuffer. Your solution via a lookuptable for glyph information is great for dynamic text.I'll go as far as to call the method of drawing static text to textures the *ONLY* way to do text properly - glyph lookup tables become a huge waste of memory as soon as you start getting serious about your typography or internationalization. Try doing glyph tables for bold, italic, and underlined combinations in a few fonts and you'll appreciate FreeType2. Also, signed distance fields are your best friend, use them and you can save a lot of memory at the cost of a few shader instructions.- Sherief