c++ - OpenGL 文本随相机移动,纹理也表现出与专家建议相同的方式

标签 c++ opengl animation glut

我正在使用 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/

相关文章:

c++ - 通过HTML访问程序资源时如何获取可执行文件名?

opengl - 在 GLSL 中模拟扩散方程

python - 旋转汽车轮胎(open gl)

javascript - 元素上的 CSS 过渡更平滑

c# - 如何计算 Storyboard动画循环了多少次?

c++ - 如何在QIoDevice中摆脱memcpy

c++ - 硬件帮助 : get char instead of get line C++

javascript - 未捕获的 TypeError : 1 argument required, 但只有 0 存在...但是我的函数没有参数?

c++ - 将五位整数如12345分隔成1 2 3 4 5,每个数字之间用制表符隔开

opengl - OpenGL GLSL 采样器是否总是返回从 0.0 到 1.0 的 float ?