过剩的位图字体有意想不到的位置
GLUT bitmap fonts have unexpected position
我正在使用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)。
无论如何,在这个代码片段中,您调整的是投影,而不是模型视图矩阵
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矩阵。模型视图和投影矩阵在OpenGL中有自己的堆栈,必须单独推送/弹出
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 如何使用cudaMallocManaged在指针位置初始化对象?(C++)
- 无法获取webot::PositionSensor对象中位置传感器的值
- 非常量变量只读位置的赋值
- 如何定义更改car类中car位置的方法
- 使用迭代器时如何访问对象在向量中的位置?
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 求最大元素位置的分治算法
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 如何将元素添加到向量的字符串位置
- 如何选择在 csv 文件中输出的位置
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- 如何在特定位置追加 json 文件
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 为什么在递归中使用循环会产生意想不到的结果?