如何在 C++ 中使用 glutBitmapString() 将文本绘制到屏幕上?

2021-12-30 00:00:00 graphics c++ glut

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

I'm attempting to draw text to the screen using GLUT in 2d.

我想使用 glutBitmapString(),有人可以向我展示一个简单的示例,说明在 C++ 中设置和正确使用此方法必须执行的操作,以便我可以在 (X,Y) 位置绘制任意字符串?

I want to use glutBitmapString(), can someone show me a simple example of what you have to do to setup and properly use this method in C++ so I can draw an arbitrary string at an (X,Y) position?

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

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

I'm using linux, and I know I need to create a Font object, although I'm not sure exactly how and I can supply it with the string as the second arguement. However, how do I also specify the x/y position?

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

A quick example of this would help me greatly. If you can show me from creating the font, to calling the method that would be best.

推荐答案

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

You have to use glRasterPos to set the raster position before calling glutBitmapString(). Note that each call to glutBitmapString() advances the raster position, so several consecutive calls will print out the strings one after another. You can also set the text color by using glColor(). The set of available fonts are listed here.

// 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");

相关文章