OpenGL bitmap questions...


void drawText(){
	glColor3f(0.0f, 1.0f, 0.0f);
	glRasterPos2i(1, 13);
   	glBitmap (10, 12, 0.0, 0.0, 11.0, 0.0, rasters);
   	glColor3f(0.0f, 0.0f, 1.0f);
   	glBitmap (10, 12, 0.0, 0.0, 11.0, 0.0, rasters);
   	glColor3f(1.0f, 0.0f, 0.0f);
   	glBitmap (10, 12, 0.0, 0.0, 11.0, 0.0, rasters);
   	glFlush();
}

  1. How come all 3 bitmaps have same color? (Green)
    How do I make each text with different color?

  2. When I run my program, sometimes the position of my text (F) is slightly different.

picture 1: https://www.dropbox.com/s/n3b1pis0cfr20br/Untitled.png?dl=0
picture 2: https://www.dropbox.com/s/bi3m99gqecn0bqm/2.png?dl=0

If you look closely, in picture 2, the x position of my first F is 0 even though the rasterPosition is 1.
Sometimes it’s not just first F. It could be second one or third one…

So why does this happen and how do I fix it?

Bitmaps are rendered using the current raster colour. The current raster colour is set by glRasterPos() from either the current colour or (if lighting is enabled) the colour produced by the lighting equations. As you only call glRasterPos() once, all of the bitmaps have the same colour.

Thanks, I think that makes sense…

but the one that doesn’t make sense is the question 2…I really don’t know why my text’s position is different each time I run my program…

The coordinates passed to glRasterPos() are transformed by the current model-view and projection matrices to obtain window coordinates. These are floating-point values which are rounded down to an integer (as if by floor()) in order to determine the bitmap position. If the computed window coordinates lie on the boundary between pixels, even the smallest error could affect the result.

Try adjusting the coordinates passed to glRasterPos() so that the transformed coordinates lie in the centre of the pixel rather than at the corner. Or try using glWindowPos() rather than glRasterPos().

Thanks! it works!

For those who are struggling with the same problem, just change

glRasterPos2i(10, 10);

to

glRasterPos2f(10.5, 10.5);

This is well explained in MSDN, Direct3D Directly Mapping Texels to Pixels.

link: https://msdn.microsoft.com/en-us/library/windows/desktop/bb219690(v=vs.85).aspx