Yesterday, I struggled about how to render FTGL font in a window whose origin is at top-left
.
Retaining this kind of orthographic settings makes it difficult for me to properly align FTGL font, esp. in y-axis
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);
}
Then rendered it like this:
glPushMatrix();
glTranslated(X, Y + font.LineHeight(), 0);
glScalef(1, -1, 0); //reverse scaling of y
font.Render(str);
glPopMatrix();
I try to measure the bounding boxes of different fonts but it gives me inconsistent results.
Here they are:
Notice the
inconsistency of y-position of boxes
And also the code:
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();
}
But I want to achieve a box that is totally fitted to the rendered font like this:
I just manually adjust some values to make it fit
Specific question is, how do I calculate y-position of the rendered font in this kind of settings?
I don't know what FTGL::Descender()
does but I think it somewhat related to this?
I will accept as answer any links that discusses this kind of topic.