How do I use glutBitmapString() in C++ to draw text to the screen?(如何在 C++ 中使用 glutBitmapString() 将文本绘制到屏幕上?)
问题描述
我正在尝试在 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");
这篇关于如何在 C++ 中使用 glutBitmapString() 将文本绘制到屏幕上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 C++ 中使用 glutBitmapString() 将文本绘制到屏幕上?
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
