2009-02-12 98 views
9

我正在尝试使用2d中的GLUT在屏幕上绘制文本。我想要使​​用glutBitmapString(),有人可以告诉我一个简单的例子,你必须做什么来设置和正确使用C++中的这个方法,所以我可以在(X,Y)位置绘制一个任意的字符串?如何在C++中使用glutBitmapString()将文本绘制到屏幕上?

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

我使用的是Linux操作系统,我知道我需要创建一个Font对象,虽然我不知道我究竟如何,可与字符串作为第二arguement提供它。但是,我该如何指定x/y位置?

一个很快的例子会对我有很大的帮助。如果你能从创建字体的角度向我展示,调用最好的方法。

回答

11

在调用glutBitmapString()之前,您必须使用glRasterPos来设置光栅位置。请注意,每次调用glutBitmapString()都会提高光栅位置,因此连续几次调用都会一个接一个地打印出字符串。您还可以使用glColor()设置文本颜色。这组可用字体列于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"); 
+2

谢谢。此外,很长一段时间,它一直告诉我glutBitmapString没有定义,并且我最终在GL/glui.h中发现它的名称为“_glutBitmapString”。任何想法为什么? – KingNestor 2009-02-13 00:19:13

0

添加到亚当的回答,

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更多字体选项的帮助亚当

相关问题