我该如何在C++中使用glutBitmapString()来在屏幕上绘制文本?

11

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

我想使用glutBitmapString(),有人能给我展示一个简单的示例,说明如何设置并正确使用这种方法在C++中,以便我可以在(X,Y)位置绘制任意字符串吗?

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

我正在使用Linux操作系统,我知道我需要创建一个字体对象,但不确定如何创建,并且我可以在第二个参数中提供字符串。 但是,如何指定x / y位置呢?

如果你能快速给我展示一个例子,那会帮助我很多。最好能够从创建字体开始一直到调用方法。

2个回答

14
您需要在调用glutBitmapString()之前使用glRasterPos设置光栅位置。请注意,每次调用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");

2
感谢你的帮助,Adam。同时,有一段时间它一直告诉我glutBitmapString未定义,最终我在GL/glui.h中找到了它被命名为“_glutBitmapString”。你知道为什么吗? - KingNestor
glutBitmapString is an extension implemented on freeglut, no present in the old glut, GL/freeglut.h must be included instead of GL/glut.h - Alex

0

补充Adam的回答,

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以获取更多字体选项


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接