使用 GLUT 位图字体 [英] Using GLUT bitmap fonts

查看:31
本文介绍了使用 GLUT 位图字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用 GLUT 的简单 OpenGL 应用程序.我不想滚动自己的字体渲染代码,而是想使用 GLUT 附带的简单位图字体.使它们工作的步骤是什么?

I'm writing a simple OpenGL application that uses GLUT. I don't want to roll my own font rendering code, instead I want to use the simple bitmap fonts that ship with GLUT. What are the steps to get them working?

推荐答案

在 OpenGL 中使用 GLUT 位图字体很容易实现简单的文本显示.这些是简单的 2D 字体,适合在您的 3D 环境中显示.但是,它们非常适合需要覆盖在显示窗口上的文本.

Simple text display is easy to do in OpenGL using GLUT bitmap fonts. These are simple 2D fonts and are not suitable for display inside your 3D environment. However, they're perfect for text that needs to be overlayed on the display window.

以下是在 GLUT 窗口上以绿色显示埃里克·卡特曼最喜欢的报价的示例步骤:

Here are the sample steps to display Eric Cartman's favorite quote colored in green on a GLUT window:

我们将在屏幕坐标中设置光栅位置.因此,为 2D 渲染设置投影和模型视图矩阵:

We'll be setting the raster position in screen coordinates. So, setup the projection and modelview matrices for 2D rendering:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, WIN_WIDTH, 0.0, WIN_HEIGHT);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

设置字体颜色.(现在设置,而不是以后设置.)

Set the font color. (Set this now, not later.)

glColor3f(0.0, 1.0, 0.0); // Green

设置应显示文本的窗口位置.这是通过在屏幕坐标中设置光栅位置来完成的.窗口左下角为(0, 0).

Set the window location where the text should be displayed. This is done by setting the raster position in screen coordinates. Lower left corner of the window is (0, 0).

glRasterPos2i(10, 10);

使用glutBitmapCharacter设置字体和显示字符串字符.

Set the font and display the string characters using glutBitmapCharacter.

string s = "Respect mah authoritah!";
void * font = GLUT_BITMAP_9_BY_15;
for (string::iterator i = s.begin(); i != s.end(); ++i)
{
    char c = *i;
    glutBitmapCharacter(font, c);
}

恢复矩阵.

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

这篇关于使用 GLUT 位图字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆