[英]Properly position y-axis of font in FTGL
昨天,我在如何在一個起源於top-left
的窗口中渲染FTGL字體方面苦苦掙扎。
保留這種正交設置使我很難正確對齊FTGL字體,尤其是。 在y軸上
void enable2D(int w, int h)
{
winWidth = w;
winHeight = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, h, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
}
然后渲染它像這樣:
glPushMatrix();
glTranslated(X, Y + font.LineHeight(), 0);
glScalef(1, -1, 0); //reverse scaling of y
font.Render(str);
glPopMatrix();
我嘗試測量不同字體的邊界框,但它給我不一致的結果。
他們來了:
注意inconsistency of y-position of boxes
還有代碼:
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
///Draw the fonts
for (int i = 0;i < N; ++i)
{
auto &X = x[i];
auto &Y = y[i];
auto &font = fonts[i];
glColor3ub(0, 0, 0);
glPushMatrix();
glTranslated(X, Y + font.LineHeight(), 0);
glScalef(1, -1, 0);
font.Render(str);
glPopMatrix();
}
///Draw the boxes
for (int i = 0;i < N; ++i)
{
auto &X = x[i];
auto &Y = y[i];
auto &box = boxes[i];
glColor3ub(255, 0, 0);
glPushMatrix();
glTranslated(X, Y, 0);
glBegin(GL_LINE_LOOP);
glVertex2f(box.Lower().X(), -box.Lower().Y()); //top-left
glVertex2f(box.Upper().X() - box.Lower().X(), -box.Lower().Y()); //top-right
glVertex2f(box.Upper().X() - box.Lower().X(), +box.Upper().Y() - box.Lower().Y() * 2); //bottom-right
glVertex2f(box.Lower().X(), +box.Upper().Y() - box.Lower().Y() * 2); //bottom-left
glEnd();
glPopMatrix();
}
但我希望實現一個完全適合渲染字體的方框,如下所示:
我只是手動調整一些值以使其適合
具體問題是,如何在這種設置中計算渲染字體的y位置 ?
我不知道FTGL::Descender()
會做什么,但我認為它與此有些相關?
我將接受討論此類主題的任何鏈接作為答案。
然而,經過反復試驗,我發現了我正在做的錯誤。
首先,我應該考慮在做這種坐標系時,框的最左邊和最上面的部分應該設置zero
,即:
glVertex2f( 0, 0 );
glVertex2f( w, 0 );
glVertex2f( w, h );
glVertex2f( 0, h );
所以我不需要擔心其他事情。 從那以后,我確保在將font
轉換為指定坐標時,它必須相對於左上角的窗口(沒有填充,偏移等等...)
現在在字體部分進行翻譯,我這樣做:
float x_offset = (font.BBox(str).Lower().X() * font_scale);
float y_offset = (font.BBox(str).Upper().Y() * font_scale);
glPushMatrix();
///the coorinate should now be relative the box!
glTranslatef( x - x_offset,
y + y_offset,
0
);
glScalef( font_scale, -font_scale, 0); //notice the negative in y
font.Render(str);
glPopMatrix();
在框外抽出的字體 (#6)
,可能是由於字體的樣式。
謝謝! 希望有人能幫到這個:D
更新:
我之前的計算中有一個錯誤,無論如何,我更新了我的答案以便更准確。 您可以在我的編輯歷史記錄中查看更改。
更新:
這里仍然有一個錯誤。 FTGL::BBox
僅返回當前文本的當前邊界框,w / c當當前字符串中不存在最大字形高度時,高度可能會發生變化。 我再次檢查源代碼,但我找不到yMax
w / c是它可以返回的最大高度。 但是,我可以iterate all over the available glpyhs and get the maximum height
,我認為freetype
中現有的函數已經可以做到了嗎? 誰知道呢?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.