我正在使用 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();
}
最佳答案
glRasterPos()
通过模型 View 和投影矩阵转换给定位置。您必须将它们重置为正确定位文本的内容:
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()
,它绕过两个矩阵并直接设置光栅位置。
关于c++ - OpenGL 文本随相机移动,纹理也表现出与专家建议相同的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22991874/