NeHe OpenGL Lesson13 – Bitmap Fonts
2012-08-23 22:19
417 查看
![](http://images.cnblogs.com/cnblogs_com/open-coder/201208/201208232219189065.png)
This demo shows us how to display some characters on the screen with OpenGL bitmap fonts. This is a very useful feature that we could used to display some text, debug information, fps or even menu content on the screen. Those staffs are not part of 3D scene, but usually head up display(HUD for short). Some very cool effect could be made by Flash, then those flashes could be integrated into the game as the menu pages or elements. Well, Scaleform does that.
Bitmap fonts use any of your computers built in fonts. Bitmaps font’s are 2D scalable fonts, they can not be rotated. They always face forward. Of course you could make your own text with texture mapping. But obviously, bitmap fonts has more flexibility than text texture mapping.
Go though the source code, display list and wglUseFontBitmaps works together to make the 96 characters. And one some platform specific code there: GDI device context object and font objects. Set the fonts object to the GDI device content before you create the bitmap fonts.
Here are the funny thing when draw the text on the screen:
GLvoid glPrint(const char *fmt, …) { char text[256]; va_list ap; if (fmt == NULL) return; va_start(ap, fmt); vsprintf(text, fmt, ap); va_end(ap); glPushAttrib(GL_LIST_BIT); glListBase(base – 32); glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); glPopAttrib(); }
One point is that the parameters for this function are not fixed. It does as you see with printf function.
The other point is that draw bitmap fonts text is the same as call display list. No special operation should be take care of. Here are the code the call this function:
glRasterPos2f(-0.45f+0.05f*float(cos(cnt1)), 0.32f*float(sin(cnt2))); glPrint(“Active OpenGL Text With NeHe – %7.2f”, cnt1);
Here glRasterPos2f used to locate where the text will be display. This function locate the position in the raster space instead of world space.
The full source code could be downloaded from here.
相关文章推荐
- NeHe OpenGL Lesson15 – Texture Mapped Outline Fonts
- NeHe OpenGL Lesson14 – Outline Fonts
- NeHe OpenGL Lesson28 – Bezier Patches(贝叶茨曲面) / Fullscreen Fix
- NeHe OpenGL Lesson34 – Beautiful Landscapes By Means Of Height Mapping
- NeHe OpenGL Lesson27 – Volume Shadows(体积阴影)
- NeHe OpenGL Lesson33 – Loading Compressed And Uncompressed TGA’s
- NeHe OpenGL Lesson26 – Clipping & Reflections Using The Stencil Buffer
- NeHe OpenGL Lesson 10 – Loading And Moving Through A 3D World
- NeHe OpenGL Lesson25 – Morphing & Loading Objects From A File
- NeHe OpenGL Lesson24 – Tokens, Extensions, Scissor Testing And TGA Loading
- NeHe OpenGL Lesson20 – Masking
- NeHe OpenGL Lesson43 - FreeType Fonts in OpenGL
- NeHe OpenGL Lesson19 – Particle Engine Using Triangle Strips(粒子引擎)
- NeHe OpenGL Lesson18 – Quadrics (二次曲面)
- NeHe OpenGL Lesson39 – Introduction to Physical Simulations
- NeHe OpenGL Lesson38 – Loading Textures From A Resource File
- NeHe OpenGL Lesson36 – Radial Blur(径向模糊)
- NeHe OpenGL Lesson29 – Blitter Function, RAW Texture Loading
- Nehe OpenGL lesson 8
- NeHe OpenGL Lesson35 – Playing AVI Files In OpenGL