GL2 and texture atlas'

Started by
0 comments, last by bioglaze 10 years ago

Hi, I am working with GL2 & the fixed function pipeline. I am trying to gain a basic understanding of OpenGL.

I am trying to display a basic string on the screen using a font sheet. I have been trying to understand Texture Atlas' but because of my inexperience with OpenGL I don't know where to begin with implementing one.

The programming language I am using is Java.

Also, one more thing. How can I use integers for translating? And setting the screen origin 0, 0 to the top left?

Advertisement

Where did you get that font sheet? If it was generated by BMFont or similar, you will need a metadata file that was generated with the sheet that lists glyph positions and dimensions. If you manually created the sheet, you have to solve them manually.

Strings can be rendered by creating quads and assigning UV coordinates from the sheet metadata. Here's a string renderer from my engine:


// Characters are read from font metadata and stored in an array indexed by their code.
struct Character
{
    Character() :
          x( 0 )
        , y( 0 )
        , width( 0 )
        , height( 0 )
        , xOffset( 0 )
        , yOffset( 0 )
        , xAdvance( 0 ) {}

    float x, y;
    float width, height;
    float xOffset, yOffset;
    float xAdvance;
};

std::vector< Vec3uv > Fonter::CreateGeometry( const std::string& text, float x, float y, float scale ) const
{
    std::vector< Vec3uv > quad( text.size() * 6 );

    float accumX = 0;

    for (std::size_t c = 0; c < text.size(); ++c)
    {
        if ((int)text[ c ] < 0)
        {
            continue;
        }

        const Character& ch = m->chars[ (int)text[ c ] ];

        float offx = x + ch.xOffset * scale + accumX * scale;
        float offy = y + ch.yOffset * scale;

        accumX += ch.xAdvance;

        float u0 = ch.x / m->fontTexture->Width();
        float u1 = (ch.x + ch.width) / m->fontTexture->Width();

        float v0 = (ch.y + ch.height) / m->fontTexture->Height();
        float v1 = (ch.y) / m->fontTexture->Height();
        
        const float z = 0;

        // Upper triangle.
        quad[ c * 6 + 0 ] = RenderQueue::Vec3uv( offx, offy, z, u0, v1 );
        quad[ c * 6 + 1 ] = RenderQueue::Vec3uv( offx + ch.width * scale, offy, z, u1, v1 );
        quad[ c * 6 + 2 ] = RenderQueue::Vec3uv( offx, offy + ch.height * scale, z, u0, v0 );
        // Lower triangle.
        quad[ c * 6 + 3 ] = RenderQueue::Vec3uv( offx + ch.width * scale, offy, z, u1, v1 );
        quad[ c * 6 + 4 ] = RenderQueue::Vec3uv( offx + ch.width * scale, offy + ch.height * scale, z, u1, v0 );
        quad[ c * 6 + 5 ] = RenderQueue::Vec3uv( offx, offy + ch.height * scale, z, u0, v0 );
    }
    
    return quad;
}

Aether3D Game Engine: https://github.com/bioglaze/aether3d

Blog: http://twiren.kapsi.fi/blog.html

Control

This topic is closed to new replies.

Advertisement