Font rendering in GLUT using bitmap fonts with Sample example.
GLUT supports two types of font rendering: stroke fonts, meaning each character is rendered as a set of line segments; and bitmap fonts, where each character is a bitmap generated with glBitmap. Stroke fonts have the advantage that because they are geometry, they can be arbitrarily scale and rendered. Bitmap fonts are less flexible since they are rendered as bitmaps but are usually faster than stroke fonts. In this tutorial, I will explain about Bitmap fonts. The function that renders bitmap character using OpenGL is glutBitmapCharacter. The syntax is
glutBitmapCharacter (void *font, int character);
font –> bitmap font to use and character –> Character to render.
For example,
glutBitmapCharacter(GLUT_HELVETICA_10,'a');
outputs a single character ‘a’ at the current raster position.
There are 7 bitmap fonts available in GLUT. They are
- GLUT_BITMAP_8_BY_13
- GLUT_BITMAP_9_BY_15
- GLUT_BITMAP_TIMES_ROMAN_10
- GLUT_BITMAP_TIMES_ROMAN_24
- GLUT_BITMAP_HELVETICA_10
- GLUT_BITMAP_HELVETICA_12
- GLUT_BITMAP_HELVETICA_18
You can render the character in any raster position of the window. The raster position can be set using family of functions glRasterPos from OpenGL library. The syntax of the functions are:
void glRasterPos2f(float x, float y);
void glRasterPos3f(float x, float y, float z);
Source code of Sample Example
#include <windows.h> #include <GL/glut.h> #include <stdio.h> int w, h; const int font=(int)GLUT_BITMAP_9_BY_15; char s[30]; double t; static void resize(int width, int height) { const float ar = (float) width / (float) height; w = width; h = height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity() ; } void setOrthographicProjection() { glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0, w, 0, h); glScalef(1, -1, 1); glTranslatef(0, -h, 0); glMatrixMode(GL_MODELVIEW); } void resetPerspectiveProjection() { glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); } void renderBitmapString(float x, float y, void *font,const char *string){ const char *c; glRasterPos2f(x, y); for (c=string; *c != '\0'; c++) { glutBitmapCharacter(font, *c); } } static void display(void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3d(1.0, 0.0, 0.0); setOrthographicProjection(); glPushMatrix(); glLoadIdentity(); renderBitmapString(200,200,(void *)font,"Font Rendering - Programming Techniques"); renderBitmapString(300,220, (void*)font, s); renderBitmapString(300,240,(void *)font,"Esc - Quit"); glPopMatrix(); resetPerspectiveProjection(); glutSwapBuffers(); } void update(int value){ t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; int time = (int)t; sprintf(s, "TIME : %2d Sec", time); glutTimerFunc(1000, update, 0); glutPostRedisplay(); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowSize(640,480); glutInitWindowPosition(10,10); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Font Rendering Using Bitmap Font - Programming Techniques0"); glutReshapeFunc(resize); glutDisplayFunc(display); glutTimerFunc(25, update, 0); glutMainLoop(); return EXIT_SUCCESS; }
Output
Thank you for bitmap fonts guide! Really helped a lot!
Thank you, this was the only example, that I could use! Very helpful!!!
Thanks!
can we add new font in GLUT because i need bigger size font for this GLUT_BITMAP_TIMES_ROMAN_24 font