11

我正在尝试在 2d 中使用 GLUT 将文本绘制到屏幕上。

我想使用 glutBitmapString(),有人可以给我看一个简单的例子,说明你必须在 C++ 中设置和正确使用此方法,以便我可以在 (X,Y) 位置绘制任意字符串吗?

glutBitmapString(void *font, const unsigned char *string); 

我正在使用 linux,并且我知道我需要创建一个 Font 对象,尽管我不确定如何以及我可以将字符串作为第二个参数提供给它。但是,我如何也指定 x/y 位置?

一个简单的例子对我有很大帮助。如果你能告诉我从创建字体到调用最好的方法。

4

2 回答 2

14

您必须glRasterPos在调用之前使用设置光栅位置glutBitmapString()。请注意,每次调用都会glutBitmapString()推进光栅位置,因此几个连续的调用将一个接一个地打印出字符串。您还可以使用 设置文本颜色glColor()此处列出了可用字体集。

// Draw blue text at screen coordinates (100, 120), where (0, 0) is the top-left of the
// screen in an 18-point Helvetica font
glRasterPos2i(100, 120);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");
于 2009-02-12T23:52:04.123 回答
0

添加到亚当的答案,

glColor4f(0.0f, 0.0f, 1.0f, 1.0f);  //RGBA values of text color
glRasterPos2i(100, 120);            //Top left corner of text
const unsigned char* t = reinterpret_cast<const unsigned char *>("text to render");
// Since 2nd argument of glutBitmapString must be const unsigned char*
glutBitmapString(GLUT_BITMAP_HELVETICA_18,t);

查看https://www.opengl.org/resources/libraries/glut/spec3/node76.html了解更多字体选项

于 2017-07-31T12:07:07.787 回答