【问题标题】:gluLookAt keeps rotating the cameragluLookAt 不断旋转相机
【发布时间】:2019-12-04 16:58:22
【问题描述】:

我正在开发一个简单的 2D 自上而下 OpenGL 游戏,其中相机跟随玩家。我使用 gluLookAt 将相机定位在玩家上方并朝其方向看。但是,在运行游戏时,即使没有输入,相机也会继续围绕轴旋转。

旋转速度取决于玩家和原点之间的距离,但是打印我传递给 gluLookAt 的值可以确认当相机旋转时它们没有被改变。那么当没有新的值被传递给它时,相机参数是如何变化的呢?

更新眼睛坐标使 2D 游戏平面绕其轴旋转,更新观察坐标使相机绕其自身的轴旋转,更新向上方向(使用鼠标坐标)使游戏在平面内旋转。

我有一个 glutMainLoop,它带有对渲染函数的回调,如 displayFunc 和 idleFunc。渲染函数(兼作游戏时间)如下:

void render()
{
    gameTick();

    // Move camera
    printf("pos: %.2f, %.2f, %.2f\tlookat: %.2f, %.2f, %.2f\n", player.pos.x, player.pos.y, 0.0f, 0.0, 0.0, -5.0);
    gluLookAt(
        //player.pos.x, player.pos.y, 0.0f,
        0.0,0.0,0.0,
        //player.pos.x, player.pos.y, -5.0f,
        0.0, 0.0, -5.0,
        0.0, 1.0, 0.0
    );

    // Clearing buffer
    glClearColor(g_backgroundColor.getR(), g_backgroundColor.getG(), g_backgroundColor.getB(), 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Drawing enemies
    for (size_t i = 0; i < agents.size(); i++)
        drawSquare(agents[i].pos, agents[i].ori, g_enemyColor);

    // Drawing player
    drawSquare(player.pos, player.ori, g_playerColor);

    glutSwapBuffers();
}

相机在 z=0,游戏平面在 z=-5。

更新相机或其他东西后我是否错过了必要的 OpenGL 调用?

【问题讨论】:

    标签: c++ opengl freeglut opengl-compat


    【解决方案1】:

    gluLookAt 不仅定义了一个视图矩阵。
    它定义了一个矩阵,并将当前矩阵乘以新矩阵。

    glLoadIdentity调用gluLookAt之前设置Identity matrix,解决问题:

    glMatrixMode(GL_MODEL_VIEW);
    glLoadIdentity();
    gluLookAt(
        //player.pos.x, player.pos.y, 0.0f,
        0.0,0.0,0.0,
        //player.pos.x, player.pos.y, -5.0f,
        0.0, 0.0, -5.0,
        0.0, 1.0, 0.0
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多