Sign in to follow this  
usbman3

glDrawPixels draws only garbage. Need some help.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='usbman3' timestamp='1292082550' post='590190']
Edit: Problem solved. Don't bother reading this thread.
[/quote]

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this