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() 将文本绘制到屏幕上?


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01