我正在使用openGL制作游戏,发现这个问题会有所不同。 当我试图在屏幕上打印一个文本并移动我的相机时,文本也会在窗口中保留其原始位置并随着相机一起移动。
这是我在draw()中的代码。
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
glLoadIdentity();
// GLfloat position1[] = { 00.0, 100.0, 00.0, 1.0 };
//cam position update
gluLookAt( world.camera->cameraFrom.x,world.camera->cameraFrom.y,world.camera->cameraFrom.z, world.camera->cameraTo.x,0,world.camera->cameraTo.z, 0,1,0); // Define a viewing transformation
// Pop the current matrix stack
//**************************************************************
drawTable(world.table);
world.update();
glPushMatrix();
sprintf(str, "Player 1 Score: 1, Player 2 Score: 10");
glRasterPos2f(10, 10);
glutBitmapString(font,(unsigned char*)str);
glPopMatrix();
glutSwapBuffers();
}
答案 0 :(得分:2)
glRasterPos()
通过模型视图和投影矩阵转换给定位置。您必须将这些重置为能够正确定位文本的内容:
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// set projection matrix here
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//cam position update
gluLookAt
(
world.camera->cameraFrom.x, world.camera->cameraFrom.y, world.camera->cameraFrom.z,
world.camera->cameraTo.x, 0, world.camera->cameraTo.z,
0,1,0
);
drawTable(world.table);
world.update();
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// set appropriate projection matrix here
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
sprintf(str, "Player 1 Score: 1, Player 2 Score: 10");
glRasterPos2f(10, 10);
glutBitmapString(font,(unsigned char*)str);
glutSwapBuffers();
}
或者改为使用glWindowPos2f()
,绕过两个矩阵并直接设置栅格位置。