iDevGames Forums
FTGL point size question - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: FTGL point size question (/thread-5131.html)



FTGL point size question - TomorrowPlusX - Aug 24, 2005 03:22 PM

I'm using FTGL for the text rendering in my GUI, and for my game's HUD. Specifically, I'm using texture fonts and a vanilla ortho projection for the HUD.

The oddity is that I can't figure out how to corrolate FTGL's notion of a point size to the pixels rendered on my screen. For example, if I ask for a 14 pt helvetica, I get a font which when asked for it's ascender, returns 14 -- as I'd expect -- but which when rendered, gives me something between 10 and 12 pixels tall ( due to antialiasing ). Because of this, I can't reliably center text vertically. Oddly, I can center text horizontally...

So, here's a shot of what I'm seeing:
[Image: pointsizes.png]

The bottom red line is the baseline I calculate for centered text if the font really were the 14 pixels tall it reports. The top red line is 14 pixels over the bottom. As you can see, the font isn't nearly 14 pixels tall.

So I have to assume that there's some non-1-to-1 scaling going on here, or that I'm miscalculating something massively.

Here's a bunch of code. First, the button's drawing function:

Code:
void ButtonWidget::display( void )
{
    if ( _font && !_label.empty() )
    {    
        calculateMetrics();
        vec2i origin, outerSize(size()), insetSize;
        
        origin.x = 0;
        origin.y = 0;
        DrawRoundRect( vec2i( 0,0 ), outerSize, DrawRoundRect_MaxCornerRadius, _outlineColor );

        origin.x = Button_Inset;
        origin.y = Button_Inset;
        insetSize.x = outerSize.x - ( 2 * Button_Inset );
        insetSize.y = outerSize.y - ( 2 * Button_Inset );
        
        DrawRoundRect( origin, insetSize,
                       DrawRoundRect_MaxCornerRadius,
                       _trackingMouseDown ? (_hover ? _outlineColor : _color ) : _color );

        vec2i pos(( size().x / 2 ) - ( _labelWidth / 2 ),
                  ( (int)( ceilf( (float)size().y / 2.0f )) - (int) floorf( (float)_labelHeight / 2.0f ) ));

        glBegin( GL_LINES );
        
        glColor3f( 1,0,0 );
        glVertex3f( 0, pos.y, 0 );
        glVertex3f( size().x, pos.y, 0 );

        glVertex3f( 0, pos.y + _labelHeight, 0 );
        glVertex3f( size().x, pos.y + _labelHeight, 0 );
        
        glEnd();
            
        DrawString( _font, _label, pos, _labelColor );
    }
}

Second, the functions to get the sizing for the font -- first, the button's calculateMetrics() code:

Code:
void ButtonWidget::calculateMetrics( void )
{
    if ( _font )
    {
        SizeForString( _font, _label, _labelWidth, _labelHeight );
    }
}

And now the function SizeForString()
Code:
void SizeForString( FTFont *font, std::string str, int &width, int &height )
{
    float llx, lly, llz, urx, ury, urz;

    font->BBox( str.c_str(), llx, lly, llz, urx, ury, urz );
    width = (int) rint( urx - llx );
    height = (int) rint( font->Ascender() );
}

And finally my DrawString function:

Code:
void DrawString( FTFont *font, std::string str, vec2i position, vec4 color )
{
    glPushMatrix();
    glTranslatef( position.x, position.y, 0 );
    glEnable( GL_TEXTURE_2D );
    
    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
    glColor4fv( color.v );
    font->Render( str.c_str() );
    glDisable( GL_TEXTURE_2D );
    glPopMatrix();
}

Is there something I'm missing? This seems so straighforward to me.


FTGL point size question - TomorrowPlusX - Aug 25, 2005 02:12 PM

For what it's worth, I've come up with a hack that sort of works. I offset the baseline by half of the font's descender. This is brittle, but works so far for me.


FTGL point size question - isgoed - Aug 25, 2005 02:19 PM

I have not looked at your code, but I just want to comment that points is not the same as pixels. Using OpenGL or variable resolutions I would stay away from points if I were you.


FTGL point size question - TomorrowPlusX - Aug 26, 2005 04:56 AM

I'm aware of that, and that's actually the point of my question. How exactly do I convert from FTGL's notion of point size to pixels?

FTGL provides all sorts of nice classes like FTSize but doesn't explain how to use it.