# How To Draw Bitmap Fonts With Background Color

This topic is 2720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I am drawing text into my scene with the following few commands:

[source]
glRasterPos3i(posX,posY,posZ);
glPushAttrib(GL_LIST_BIT);
glListBase(fontHandles[fontIndex]->base);
glCallLists(txt.length(),GL_UNSIGNED_BYTE,txt.c_str());
glPopAttrib();
[/source]

Now I want to have a rectangular square behind my text so that it becomes more easily readable. Is there a relatively simple way of doing that? I know one way would be to use outline fonts, but that's not the way I wanna go (at least at this stage).

My problem (or where I am stuck right now) is that while for the font, I simply specify a 3D coordinate, and it gets printed in "2D" (i.e. the text is always horizontal and the same size). But how can I do the same for a rectangle?
Thanks

##### Share on other sites

glRasterPos3i(posX,posY,posZ); glPushAttrib(GL_LIST_BIT); glListBase(fontHandles[fontIndex]->base); glCallLists(txt.length(),GL_UNSIGNED_BYTE,txt.c_str()); glPopAttrib();

Solution
 //Global Variables GLfloat square_x_size =1.0f, square_y_size = 1.0f, square_z_size = 1.0f,; // make a function to draw a basic square face bool construct_square_bg ( ) { glBegin ( GL_QUADS ); glVertex3f ( 1.0f, 1.0f, 0.0f ); glVertex3f ( 1.0f, 1.0f, 0.0f ); glVertex3f ( 1.0f, 1.0f, 0.0f ); glVertex3f ( 1.0f, 1.0f, 0.0f ); glEnd ( ); } glRasterPos3i(posX,posY,posZ); glPushAttrib(GL_LIST_BIT); glListBase(fontHandles[fontIndex]->base); glCallLists(txt.length(),GL_UNSIGNED_BYTE,txt.c_str()); glScalef ( square_x_size/txt.length ( ), square_y_size/txt.length ( ), square_z_size/txt.length ( ) ); glTranslatef (posX,posY,posZ); construct_square_bg ( ); glPopAttrib(); 

Something along the lines of making a basic square, then scaling it as the text's length increases, and translating the said same square based on the text positon is your solution.

##### Share on other sites
Hello GMA965_X3100,

Thank you for your reply and sorry for the delay..
I had a look at your code and I am not sure it works, did you actually test it?
First the 4 vertices of the quad are same and I don't see where we have the conversions between 3D coordinates and 2D window coordinates.

Maybe I didn't understand the problem correctly.

If I print a text into my scene using glRasterPos3f(coordIn3D), then I am using 3D coordinates to specify the start of the text. I can calculate the size of my text in 2D, but how do I convert all this into 3D to finally add my background rectangle around the text?

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 15
• 13
• 9
• 12
• 10
• ### Forum Statistics

• Total Topics
631442
• Total Posts
3000086
×