我在OpenGL中尝试使用3D图形,并且我设法创建了3D级别。但是,我还希望在屏幕上显示一些2D文本(类似于您在第一人称射击游戏中看到的HP或弹药属性)。这是我用来渲染关卡的整个方法。除了文本(根本没有出现在屏幕上)之外,一切都按照我的要求完成。如何使文字正确显示?
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正常工作。
答案 0 :(得分:0)
使用glWindowPos2i
代替glRasterPos2i
。 glRasterPos2i
使用世界坐标中的位置。 glWindowPos2i
使用屏幕坐标中的位置。
https://www.opengl.org/sdk/docs/man2/xhtml/glWindowPos.xml
https://www.opengl.org/sdk/docs/man2/xhtml/glRasterPos.xml