请教大家个freetype的问题

simouse 2008-04-08 02:57:44
大家好,我用freetype2向一个OpenGL纹理里绘制文字,对bitmap_top和bitmap.rows有些不太明白,按freetype的一个例子上的代码是要用自己纹理高度减下bitmap_top作为偏移,我们发现我画出来的文字下面总有一小截儿画在了外面,字体越大越明显。下面是我的代码,请高人指点下。

void DrawCharToTexture( Texture* texture,
FT_Bitmap* bitmap,
short xOffset,
short yOffset
)
{

Byte* pTexBuf = texture->GetImageBuf();
short texWidth = texture->GetWidth();
short texHeight= texture->GetHeight();

for ( int x = 0; x < bitmap->width; x++ )
{
for ( int y = 0; y < bitmap->rows; y++ )
{
int X = x + xOffset;
int Y = y + yOffset;
if ( X < 0 || Y < 0 || X >= texWidth || Y >= texHeight )
{
continue;
}

Byte alpha = bitmap->buffer[(y * bitmap->width + x)];
alpha *= mTransparent;
if ( 0 != alpha )
{
Byte* pPixel = pTexBuf + ((texHeight-Y) * texWidth + X) * 4;

pPixel[0] = mColor.x; // r
pPixel[1] = mColor.y; // g
pPixel[2] = mColor.z; // b
pPixel[3] = alpha; // a

} // for ( int y = 0; y < bitmap->rows; y++ )
} // for ( int x = 0; x < bitmap->width; x++ )
}


// 下面是画文字的调用
FT_GlyphSlot slot = mFace->glyph;

for ( int n = 0; n < nCount; n++ )
{
// set transformation
FT_Set_Transform( mFace, &mMatrix, &pen );

// load glyph image into the slot (erase previous one)
FT_Load_Char( mFace, lpszText[n], FT_LOAD_RENDER );

//
// now, draw to our target texture
//
short xOffset = slot->bitmap_left;
short yOffset = texHeight - slot->bitmap_top;

DrawCharToTexture( texture, &slot->bitmap, xOffset, yOffset);

/* increment pen position */
pen.x += slot->advance.x;
pen.y += slot->advance.y;
}

...全文
890 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
abc1234ll34l3j 2008-07-06
  • 打赏
  • 举报
回复
直接用face->glyph->metrics下面的信息建立纹理,就可以将文字放在纹理的合适位置,利用下面三个成员即可完成字符定位的要求.

horiBearingX

horiBearingY

horiAdvance

具体手册请参见FreeType2 Doc下的Tutorial2文档
meiZiNick 2008-05-01
  • 打赏
  • 举报
回复
我也想知道,正在找這方面的資料~~~~~
simouse 2008-04-23
  • 打赏
  • 举报
回复
有人来接分没?
simouse 2008-04-14
  • 打赏
  • 举报
回复
看来大家没有搞明白问题,这不是一个OpenGL和帖图的问题,这是个freetype使用的问题,请大侠们了解下freetype帮帮我吧。
Yofoo 2008-04-11
  • 打赏
  • 举报
回复
文字下面总有一小截儿画在了外面,字体越大越明显
short yOffset = texHeight - slot->bitmap_top;

这句怎么回事, y偏移用字高去减??? 搞反了吧

调试函数看xOffset , yOffset值是否正确
rageliu 2008-04-11
  • 打赏
  • 举报
回复
OpenGL不会,不过bmp的width倒是有对齐要求,4的倍数
simouse 2008-04-11
  • 打赏
  • 举报
回复
这样不行的,这是个文字对齐的问题,bitmap_top是freetype输出的bitmap到文字Baseline的距离,我现在是不知道这个Baseline到底应放在什么位置才能让文字正好在我的纹理上。
cnzdgs 2008-04-08
  • 打赏
  • 举报
回复
画文字调用改成
short yOffset = slot->bitmap_top;
cnzdgs 2008-04-08
  • 打赏
  • 举报
回复
这样试试:
void DrawCharToTexture(Texture* texture, FT_Bitmap* bitmap, short  xOffset, short  yOffset)
{
Byte* pTexBuf = texture->GetImageBuf();
short texWidth = texture->GetWidth();
short texHeight= texture->GetHeight();

for (int x=0; x<bitmap->width; x++)
{
for (int y=0; y<bitmap->rows; y++)
{
int X = x + xOffset;
int Y = y + yOffset;
if (X < 0 || Y < 0 || X >= texWidth || Y >= texHeight) continue;
Byte alpha = bitmap->buffer[((bitmap->rows - y - 1) * bitmap->width + x)];
alpha *= mTransparent;
if (0 != alpha)
{
Byte* pPixel = pTexBuf + (Y * texWidth + X) * 4;
pPixel[0] = mColor.x;
pPixel[1] = mColor.y;
pPixel[2] = mColor.z;
pPixel[3] = alpha;
}
}
}
}
// 画文字的调用
short yOffset = texHeight - slot->bitmap_top;
wqm631268 2008-04-08
  • 打赏
  • 举报
回复
对不起,我也不知道.

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧