gpt4 book ai didi

c++ - gluLookAt 不断旋转相机

转载 作者:搜寻专家 更新时间:2023-10-31 01:25:07 25 4
gpt4 key购买 nike

我正在开发一个简单的 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 调用?

最佳答案

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

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

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
);

关于c++ - gluLookAt 不断旋转相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57218489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com