Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ GLUT位图字体具有意外的位置_C++_Opengl_Bitmap_Glut_Hud - Fatal编程技术网

C++ GLUT位图字体具有意外的位置

C++ GLUT位图字体具有意外的位置,c++,opengl,bitmap,glut,hud,C++,Opengl,Bitmap,Glut,Hud,我正在用UNIX上的GLUT在OpenGL中制作一个游戏。这是一款3D游戏,玩家可以左右移动(x轴)跳跃(y轴),不断向前移动,并且必须避开迎面而来的障碍物(在我的实现中,玩家实际站立不动,而世界不断地向玩家移动) 我在尝试绘制带有位图文本的HUD时遇到问题。我曾尝试创建一个正交视图,然后绘制文本,但它总是在x轴上的一个随机点结束,并不断向玩家移动,世界在z轴上。一旦它越过玩家,它就会消失(这就是所有要切割处理的世界对象的情况)。我想把文字放在一个地方,并留在那里 gameSpeed = Acc

我正在用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);
}
我正在用UNIX上的GLUT在OpenGL中制作一个游戏

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

无论如何,在这个代码片段中,您调整的是投影,而不是modelview矩阵

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
,但它有
pushMatrix
popMatrix
,如果我假设它们只是
glPushMatrix
glPopMatrix
,那么您的代码就是

glPushMatrix();
glLoadIdentity()

…

glPopMatrix();
作用于Modelview矩阵的块。Modelview和投影矩阵在OpenGL中有自己的堆栈,必须单独推送/弹出