如何在FreeType中重置字符的X位置

问题描述 投票:0回答:1

我想在字符串中输入'\ n'来换行并呈现文本。

当前,如果我尝试在输入'\ n'之后换行,则渲染输出看起来像这样。enter image description here

我希望第二行与第一行对齐。

这是代码

// Iterate through all characters
    std::string::const_iterator c;
    for (c = text.begin(); c != text.end(); c++)
    {
        Character ch = Characters[*c];

        GLfloat xpos = x + ch.Bearing.x * scale;
        GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale;

         // i check the input here          
       //   if( *ch == '\n')
         //  Reset the Xpos value in the freetype.

        GLfloat w = ch.Size.x * scale;
        GLfloat h = ch.Size.y * scale;
        // Update VBO for each character
        GLfloat vertices[6][4] = {
            { xpos,     ypos + h,   0.0, 0.0 },            
            { xpos,     ypos,       0.0, 1.0 },
            { xpos + w, ypos,       1.0, 1.0 },

            { xpos,     ypos + h,   0.0, 0.0 },
            { xpos + w, ypos,       1.0, 1.0 },
            { xpos + w, ypos + h,   1.0, 0.0 }           
        };
        // Render glyph texture over quad
        glBindTexture(GL_TEXTURE_2D, ch.textureID);
        // Update content of VBO memory
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); 
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        // Render quad
        glDrawArrays(GL_TRIANGLES, 0, 6);
        x += (ch.Advance >> 6) * scale; 
    }

尽管我已经能够通过给定值增加一个float变量并将该变量减去xpos来实现这一目标,但我认为这是一种解决方法

我们在freetype中是否有任何功能可以将字体的Xpos重置到开始位置。

opengl glsl freetype freetype2
1个回答
0
投票

您可以做这样的事情。

std::vector<std::string> stdvecInput;
boost::split(stdvecInput, stdstrTxt, boost::is_any_of("\n"), boost::token_compress_on);

for (auto &str : stdvecInput)
{
   for (cTmp = str.begin(); cTmp != str.end(); cTmp++) 
      {
           // Draw your font.
      }
 }
© www.soinside.com 2019 - 2024. All rights reserved.