glDrawPixels draws only garbage. Need some help.

Started by
2 comments, last by SimonForsman 12 years, 10 months ago
Edit: Problem solved. Don't bother reading this thread.

I have all printable ASCII characters in this picture, excluding empty space:


I converted it to raw binary:
convert font2.png -size 1128x16 -depth 8 RGBA:font.dat

Then I wrote some code to load and display that font:
static uint8_t **pixels = NULL;static const uint16_t font_w = 1128;static const uint16_t font_h = 16;static const uint16_t char_w = 12;static int read_pixels( void ){    FILE *fp;    int c;    size_t ch_size;    fp = fopen( "data/font.dat", "r" );    if ( !fp )        return 0;    pixels = malloc( sizeof(uint8_t*) * 95 );    ch_size = font_h * char_w * 4;    for( c=0; c<95; c++ )    {        pixels[c] = malloc( ch_size );        fseek( fp, SEEK_SET, c*ch_size );        fread( pixels[c], ch_size, 1, fp );    }    fclose( fp );    return 1;}int load_font( void ){    printf( "Loading font...\n" );    if ( !read_pixels() )        return 0;    return 1;}void draw_text( int x, int y, char *str ){    char *c;    int index;    for( c=str; *c; c++ )    {        if ( *c == ' ' )        {            x++;            continue;        }        if ( !isprint(*c) )            index = 32;        else            index = (*c) - 32;        if ( index < 0 || index > 127 )            index = 32;        glWindowPos2i( x++ * char_w, y );        glDrawPixels( char_w, font_h, GL_RGBA, GL_UNSIGNED_BYTE, pixels[index] );    }}


There are 2 problems with draw_text(). It draws the text at wrong coordinates, and the text looks like this:

Above should read "Hello world!" (the space shows as green background color because it is not drawn).

Do you see the problem in my code?

[Edited by - usbman3 on December 12, 2010 10:06:10 AM]
Advertisement
I don't think your characters are stored contiguously in the file. You'll need to read them one line at a time.

But I suggest you rather look at one of the many good OpenGL font tutorial, and avoid using glDrawPixels. There are faster and better ways of drawing text.
I'd also suggest to look at other font rendering methods, e.g. texture mapped fonts.

If you want to continue with glDrawPixels you need to change your pixel reading routine, like Silhouette suggested.

To clarify the problem:

Suppose we have a texture containing 4 characters.
The pixels now are laid out in the following way (the numbers represent which character the pixel belongs to):

111222333444
111222333444
111222333444

or in the file: 111222333444111222333444111222333444

Our character size in pixels is 3 x 3 = 9.

So if you read all 9 pixels at once and in a contigous array, you'd get the following for your 4 characters:

111444333222
222111444333
333222111444

You see that pixels are intermixed and thus characters are not displayed correctly.

So you need to either apply an offset of one line when reading the characters one by one, e.g. linestart = char_offset + linenum * linesize.

Or you read line by line and distribute the pixels to the different pixels, i.e.
in the example you'd read each 111222333444 line and put 111 into the corresponding line of character 1, 222 into the line of character 2 etc.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

Edit: Problem solved. Don't bother reading this thread.


Don't presume that a thread only exists for your benefit just because you created it, these topics tend to be very high up on googles results,

if i enter "glDrawpixels fontrendering garbage" in google (not a unlikely search term for someone faced with the same problem you had) this very topic is the top result.

Posting the solution to the problem if you figure it out on your own may be valuable to others and even if you consider a problem to be solved others might benefit from further discussion on the topic at hand.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement