我正在OpenGL中嘗試3D圖形,並設法創建3D級別。但是,我還想在屏幕上顯示一些2D文本(類似於您在第一人稱射擊中看到的HP或彈藥統計)。這是我用來渲染關卡的整個方法。一切都按照我想要的那樣工作,除了文字(根本沒有出現在屏幕上)。我如何使文本正確顯示?在OpenGL中繪製3D場景中的2D文本(C++)
void render_Scene(void)
{
int i;
int j;
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Camera perspective
gluLookAt(x, y, 1.0, x + lx, y + ly, 1.0, 0.0, 0.0, 1.0);
glColor3f(0.4, 0.4, 0.4);
//Draw walls around the level
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, wall_Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-75, -75, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-75, -75, 1.2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(75, -75, 1.2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(75, -75, 0);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-75, 72, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-75, 72, 1.2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(75, 72, 1.2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(75, 72, 0);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-75, -75, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-75, 72, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-75, 72, 1.2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-75, -75, 1.2);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(75, -75, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(75, 72, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(75, 72, 1.2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(75, -75, 1.2);
glEnd();
glDisable(GL_TEXTURE_2D);
//Draw ground texture
for (int m = -150; m <= 140; m += 10)
{
for (int n = -150; n <= 140; n += 10)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ground_Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(n, m, 0.0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(n, m + 10, 0.0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(n + 10, m + 10, 0.0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(n + 10, m, 0.0);
glEnd();
glDisable(GL_TEXTURE_2D);
}
}
//Draw trees in level (uses a seperate method)
for (i = -9; i < 9; i++)
{
for (j = -9; j < 9; j++)
{
glPushMatrix();
glTranslatef(i*7.5, j*7.5, 0);
draw_Trees();
glPopMatrix();
}
}
//Create light
GLfloat lightColor0[] = { 1.0, 1.0f, 1.0f, 1.0f };
GLfloat lightPos0[] = { lx, ly, deltaMove, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//Here's where I'm trying to render the text (I've heard that glPushAttrib and glPopAttrib may be important for this part) but it does not appear anywhere on the screen
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
glPushAttrib(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glLoadIdentity();
glRasterPos2i(10, 30);
void * font = GLUT_BITMAP_8_BY_13;
for (string::iterator i = strings[stage].begin(); i != strings[stage].end(); ++i)
{
glutBitmapCharacter(font, *i);
}
glPopAttrib();
glPopMatrix();
glutSwapBuffers();
}
編輯:我被告知應該使用glWindowPos2i而不是glRasterPos2i。這個解決方案的問題是,glWindowPos2i沒有被識別並導致構建錯誤。繼一些在線調查,我添加以下代碼行:
PFNGLWINDOWPOS2IPROC glWindowPos2i;
glWindowPos2i = (PFNGLWINDOWPOS2IPROC)glutGetProcAddress("glWindowPos2i");
現在程序運行正常,但是當我嘗試使用glWindowPos2i功能崩潰。我正在使用和作爲包含的頭文件,以幫助任何人想出解決方案讓glWindowPos2i正常工作。
我不知道glutBitmapCharacter但我敢肯定GL_LIGHTING是'glPushAttrib'沒有有效的參數。這使我缺乏任何'glGetError',它可以讓你檢測更多的錯誤...... – Thomas 2014-09-20 21:56:56
確實,'glPushAttrib(...)'使用了一個位掩碼,因此所有有效的枚舉都將以'_BIT'結尾。 – 2014-09-20 22:02:15
這有幫助嗎? http://stackoverflow.com/questions/9430852/glutbitmapcharacter-positions-text-wrong – jozxyqk 2014-09-21 06:12:17