2009-02-12 96 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更多字體選項的幫助亞當

相關問題