【问题标题】:GLUT bitmap fonts have unexpected positionGLUT 位图字体有意外的位置
【发布时间】:2013-06-11 04:19:26
【问题描述】:

我正在使用 UNIX 上的 GLUT 在 OpenGL 中制作游戏。这是一个 3D 游戏,玩家可以左右移动(x 轴)跳跃(y 轴)并且不断向前移动并且必须避开迎面而来的障碍物(对于我的实现,玩家实际静止不动,而世界不断移动播放器)。

我在尝试绘制带有位图文本的 HUD 时遇到问题。我尝试过创建一个正交视图,然后绘制文本,但它总是在 x 轴上的一个随机点结束,并不断地向玩家移动,世界在 z 轴上。一旦它越过玩家,它就会消失(这是所有世界对象都需要切割处理的情况)。我希望将文本放在一个地方并留在那里。

gameSpeed = Accumulator*6;

DrawRobot(); //player

ModelTrans.loadIdentity(); //ModelTrans has helper functions to manipulate
ModelTrans.pushMatrix();   //the current matrix stack
ModelTrans.translate(vec3(0, 0, -gameSpeed)); //move the whole world

...然后我画了一堆游戏对象...

在这里我尝试做一些位图字体。禁用深度测试有助于将文本放在所有其他对象的前面,但实际上可以注释掉创建正交视图的其他代码,我仍然会遇到同样的问题。

ModelTrans.popMatrix();

glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
ModelTrans.pushMatrix();
glLoadIdentity();
gluOrtho2D(0, WindowWidth, 0, WindowHeight);
glScalef(1, -1, 1);
glTranslatef(0, -WindowHeight, 0);
glMatrixMode(GL_MODELVIEW);

std::string str = "sup";
renderBitmapString(0.5 + xText, 5.0, GLUT_BITMAP_HELVETICA_18, str);
//xText adjusts for the moving left and right of the player

glMatrixMode(GL_PROJECTION);
ModelTrans.popMatrix();
glMatrixMode(GL_MODELVIEW);

glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();
printOpenGLError();

这里有一些其他可能有用的代码:

void renderBitmapString(float x, float y, void *font, std::string s)
{
   glRasterPos2f(x, y);

   for (string::iterator i = s.begin(); i != s.end(); ++i)
   {
      char c = *i;
      glutBitmapCharacter(font, c);
   }
}

void Timer(int param)
{
    Accumulator += StepSize * 0.001f;
    glutTimerFunc(StepSize, Timer, 1);
}

void Reshape(int width, int height)
{
    WindowWidth = width;
    WindowHeight = height;
    glViewport(0, 0, width, height);
}

【问题讨论】:

    标签: c++ opengl bitmap glut hud


    【解决方案1】:

    我在 UNIX 上使用 GLUT 在 OpenGL 中制作游戏

    首先,您不是在 Unix 上执行此操作,而是很可能使用 X11。此外,我很确定您的操作系统是 Linux 的变体,它 不是 Unix(...BSD 将是真正的 Unix)。

    无论如何,在这段代码 sn-p 中,您正在调整投影,而不是模型视图矩阵

    glMatrixMode(GL_PROJECTION);
    ModelTrans.pushMatrix();
    glLoadIdentity();
    gluOrtho2D(0, WindowWidth, 0, WindowHeight);
    glScalef(1, -1, 1);
    glTranslatef(0, -WindowHeight, 0);
    glMatrixMode(GL_MODELVIEW);
    
    std::string str = "sup";
    renderBitmapString(0.5 + xText, 5.0, GLUT_BITMAP_HELVETICA_18, str);
    //xText adjusts for the moving left and right of the player
    
    glMatrixMode(GL_PROJECTION);
    ModelTrans.popMatrix();
    glMatrixMode(GL_MODELVIEW);
    

    我不完全确定ModelTrans 是什么,但它有pushMatrixpopMatrix,如果我假设它们只是glPushMatrixglPopMatrix,那么你的代码就错了

    glPushMatrix();
    glLoadIdentity()
    
    …
    
    glPopMatrix();
    

    块作用于模型视图矩阵。 Modelview 和 Projection 矩阵在 OpenGL 中有自己的堆栈,必须单独推送/弹出。

    【讨论】:

      猜你喜欢
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 2016-12-21
      相关资源
      最近更新 更多