如何使用glutBitmapString()在C ++中绘制文本到屏幕上?
题
我试图绘制文本到2d中使用GLUT屏幕。
我想用glutBitmapString(),可有人告诉我,你所要做的安装和正确使用C使用这种方法的简单例子++这样我就可以在绘制任意字符串(X,Y)的位置?
glutBitmapString(void *font, const unsigned char *string);
我使用的是Linux操作系统,我知道我需要创建一个Font对象,虽然我不知道我究竟如何,可与字符串作为第二arguement提供它。然而,我怎么也指定x / y位置?
这方面的一个简单的例子会对我帮助很大。如果你能告诉我从创建字体,调用这将是最好的方法。
解决方案
您必须使用 glRasterPos
调用glutBitmapString()
之前设置光栅位置。注意,每次调用glutBitmapString()
推进光栅位置,这样几个连续的调用将打印出字符串一个接一个。您还可以通过使用 glColor()
。该组可用字体列出 rel="noreferrer">。
// 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");
其他提示
添加到亚当的答案,
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 了解字体选项
不隶属于 StackOverflow