gpt4 book ai didi

c++ - OpenGL 自由类型文本,y 偏移到字符的最上边界

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:44 25 4
gpt4 key购买 nike

在 OpenGL 中绘制文本我使用的是 freetype。我现在已经成功渲染文本,但我仍然遇到字符未指向正确的 y 坐标的问题(这些字符不是平面的)。

看下面的图片,我制作了“正在加载...”文本。

enter image description here

如您所见,y 坐标的位置不正确。这让我很困惑。这是我所做的代码:

class FontBitmap{
private:
struct character_info {
float bw; // bitmap.width;
float bh; // bitmap.rows;
} c[256];
int next_p2 (int a )
{
int rval=1;
while(rval<a) rval<<=1;
return rval;
}
GLuint texture[256]; //character textures
public:
FT_Library library; /* handle to library */
FT_Face face; /* handle to face object */
FontBitmap(){}

void Print(char * str,GLfloat x,GLfloat y){ //the function where to render the text
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glPushMatrix();
glTranslatef(x,y,0.0f);
for(int i = 0;i < strlen(str);i++)
{
glPushMatrix();
glBindTexture(GL_TEXTURE_2D,texture[str[i]]);
glTranslatef(x,0,0); //the placement of per character
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(0,0,0.0f);
glTexCoord2f(1,0);glVertex3f(c[str[i]].bw,0,0.0f);
glTexCoord2f(1,1); glVertex3f(c[str[i]].bw, c[str[i]].bh ,0.0f);
glTexCoord2f(0,1); glVertex3f(0, c[str[i]].bh,0.0f);
glEnd();
x +=c[str[i]].bw;
glPopMatrix();
}
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
}

FontBitmap(char * filePath, int size) //the constructor
{
glEnable(GL_TEXTURE_2D);

if (FT_Init_FreeType (&library))
{
//on error
return;
}
if (FT_New_Face(library,filePath,0,&face))
{
//on error
return;
}
FT_Set_Pixel_Sizes (face, 0, size);
for (unsigned long i = 33; i < 256; i++)
{
if(FT_Load_Char(face, i, FT_LOAD_RENDER))
{
//on error;
return;
}
int width = next_p2( face->glyph->bitmap.width );
int height = next_p2( face->glyph->bitmap.rows );

c[i].bw = width;
c[i].bh = height;

GLubyte* expanded_data = new GLubyte[ 2 * width * height];

for(int j=0; j <height;j++) {
for(int i=0; i < width; i++){
expanded_data[2*(i+j*width)]= expanded_data[2*(i+j*width)+1] =
(i>=face->glyph->bitmap.width || j>=face->glyph->bitmap.rows) ?
0 : face->glyph->bitmap.buffer[i + face->glyph->bitmap.width*j];
}
}
glGenTextures(1,&texture[i]);
glBindTexture( GL_TEXTURE_2D, texture[i]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height,
0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data );
delete [] expanded_data;
}

}

};

最佳答案

关于您的问题,Ascent 和 Descent 是您感兴趣的两个 FreeType 指标。

字符之间的水平间距由 Advance 和 Kerning 控制

关于c++ - OpenGL 自由类型文本,y 偏移到字符的最上边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21117174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com