Search code examples
openglbitmapbyterasteropengl-compat

glBitmap: bitmap size issue


I have a doubt trying to understand and use glBitmap function. I started from this example and trying to draw a 40x40 "bitmap" and avoiding a situation like this I tried this:

40 x 40 is 1600 bits -> so I need 200 bytes of info (1600/8)

GLubyte rasters[ 200 ] =
{
    0xff, 0xff, //.. and so on 200 times
};


 // main code calling the function below

void display(x, y)
{
    float size = 40.0;
    glColor3f( 0.0, 1.0, 0.0 );
    glRasterPos2i(x, y );
    glBitmap(size, size, 0.0, 0.0, 0.0, 0.0, rasters );
}  

I was expected to draw a fully green square 40 x 40 pixel. Turns out that I met the unwanted situation instead with the dirty upper part of the square. It seems that raster[200] is enough to draw only a 32 x 32 bitmap. So what I would like to ask:

  1. what's wrong in my math (or in understading the example)?
  2. what I need to have in my raster to draw a 40x40 (and why, next step I will need to draw something playing with the bits, so how to now were pixel (x,y) is located)
  3. final goal is drawing something starting from a GLubyte array, is there a way to do the same with glTexImage2D? (I tried to followed this suggestion but I failed)

Solution

  • You missed to set the alignment. By default OpenGL assumes that the start of each row of the raster is aligned to 4 bytes. This is because the GL_UNPACK_ALIGNMENT parameter by default is 4. Each row in the raster has 5 bytes (40 / 8 = 5). Therefore you need to change the alignment to 1:

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glBitmap(size, size, 0.0, 0.0, 0.0, 0.0, rasters);