使用 GLUT 在 3D OpenGL 世界中显示固定位置 2D 文本

2023-11-22

我有一个OpenGL项目使用GLUT(不是 freeglut),其中我想在视口上的固定位置显示 2D 文本。我的其余对象位于 3D 世界坐标中。

这个答案一个相关的老问题说,

GLUT 附带的位图字体是简单的 2D 字体,不适合在 3D 环境中显示。然而,它们非常适合需要覆盖在显示窗口上的文本。

我已经尝试了接受的答案中概述的方法,但它没有给我所需的输出。以下是显示功能的代码片段:

void display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();  
  glTranslatef(0.0, 0.0, -(dis+ddis));   //Translate the camera
  glRotated(elev+delev, 1.0, 0.0, 0.0);  //Rotate the camera
  glRotated(azim+dazim, 0.0, 1.0, 0.0);  //Rotate the camera

       .
       .
       .
  draw 3D scene
       .
       .
       .

  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  gluOrtho2D(0.0, win_width, 0.0, win_height);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();
  glRasterPos2i(10, 10);
  string s = "Some text";
  void * font = GLUT_BITMAP_9_BY_15;
  for (string::iterator i = s.begin(); i != s.end(); ++i)
  {
    char c = *i;
    glColor3d(1.0, 0.0, 0.0);
    glutBitmapCharacter(font, c);
  }
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();

  glFlush();
  glSwapBuffers();
}

A 类似的问题我想要的似乎已经被问到,但作者没有接受的答案。

一般来说,答案似乎建议使用其他库(主要是特定于操作系统的)来实现覆盖。然而,没有明确的迹象表明这是否可以通过GLUT独自的。能做到吗?


我使用 glut 创建太阳系模型时遇到了同样的问题。这是我解决它的方法:(使用上面的代码)

glDisable(GL_TEXTURE_2D); //added this
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, win_width, 0.0, win_height);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRasterPos2i(10, 10);
string s = "Some text";
void * font = GLUT_BITMAP_9_BY_15;
for (string::iterator i = s.begin(); i != s.end(); ++i)
{
  char c = *i;
  glColor3d(1.0, 0.0, 0.0);
  glutBitmapCharacter(font, c);
}
glMatrixMode(GL_PROJECTION); //swapped this with...
glPopMatrix();
glMatrixMode(GL_MODELVIEW); //...this
glPopMatrix();
//added this
glEnable(GL_TEXTURE_2D);


你会注意到我换了glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);接近尾声。我决定这样做是因为这个网站。我还必须将代码部分括起来glDisable(GL_TEXTURE_2D) and glEnable(GL_TEXTURE_2D).

希望有帮助-我为我工作。我的太阳系使用类来执行所有这些操作,我很乐意根据要求分享更多详细信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 GLUT 在 3D OpenGL 世界中显示固定位置 2D 文本 的相关文章

随机推荐