Jump to content
  • Advertisement
Sign in to follow this  
floatingwoods

How To Draw Bitmap Fonts With Background Color

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!