Rendering Font in GLUT - 1
There are two ways to display text in GLUT. The first is using bitmap fonts -- unscalable and doesn’t response to modelview transform. My preferred way is stroke fonts -- fonts that are drawn from OpenGL primitives.
(i) To insert fonts in GLUT (see here for documentation)
void glutStrokeCharacter(void *font, int character);
font:
character:
Takes in any ASCII character from 32 through 127
(ii) To get the measurement of its length
int glutStrokeLength(void *font, const unsigned char *string);
This method returns the width in units from the unscaled modeling units
This is how to use them:
(1) Define a string generator function
This function serves to render the string being passed to it.
void renderStrokeString( void *font, const string& str )
string str = "12";
float strLength = glutStrokeLength(GLUT_STROKE_ROMAN, (const unsigned char*)str.c_str() );
float strScale = 0.3;
glPushMatrix();
glLineWidth(2.0);
glTranslatef( -strLength * strScale / 2, 0.0, 0.0 );
glScalef(strScale, strScale, strScale);
glColor3ub(255, 0, 0);
renderStrokeString( GLUT_STROKE_ROMAN, str );
glPopMatrix();
That’s it!
(i) To insert fonts in GLUT (see here for documentation)
void glutStrokeCharacter(void *font, int character);
font:
This method can handle two types of font (first argument), namelyGLUT_STROKE_ROMANThe MONO version is a fixed-width font (like Courier New, etc)
GLUT_STROKE_MONO_ROMAN
character:
Takes in any ASCII character from 32 through 127
(ii) To get the measurement of its length
int glutStrokeLength(void *font, const unsigned char *string);
This method returns the width in units from the unscaled modeling units
This is how to use them:
(1) Define a string generator function
This function serves to render the string being passed to it.
void renderStrokeString( void *font, const string& str )
{(2) Set up position / scaling / rotation, etc
for (int i = 0; i < str.length(); i++)
glutStrokeCharacter(font, str[i]);
}
string str = "12";
float strLength = glutStrokeLength(GLUT_STROKE_ROMAN, (const unsigned char*)str.c_str() );
float strScale = 0.3;
glPushMatrix();
glLineWidth(2.0);
glTranslatef( -strLength * strScale / 2, 0.0, 0.0 );
glScalef(strScale, strScale, strScale);
glColor3ub(255, 0, 0);
renderStrokeString( GLUT_STROKE_ROMAN, str );
glPopMatrix();
That’s it!

0 Comments:
Post a Comment
<< Home