【发布时间】:2015-12-22 21:00:15
【问题描述】:
您好,我在 openGL 中的鼠标移动遇到了一个奇怪的问题。这是我用鼠标移动相机的代码
void camera(int x, int y)
{
GLfloat xoff = x- lastX;
GLfloat yoff = lastY - y; // Reversed since y-coordinates range from bottom to top
lastX = x;
lastY = y;
GLfloat sensitivity = 0.5f;
xoff *= sensitivity;
yoff *= sensitivity;
yaw += xoff; // yaw is x
pitch += yoff; // pitch is y
// Limit up and down camera movement to 90 degrees
if (pitch > 89.0)
pitch = 89.0;
if (pitch < -89.0)
pitch = -89.0;
// Update camera position and viewing angle
Front.x = cos(convertToRads(yaw) * cos(convertToRads(pitch)));
Front.y = sin(convertToRads(pitch));
Front.z = sin(convertToRads(yaw)) * cos(convertToRads(pitch));
}
convertToRads() 是我创建的一个小函数,用于将鼠标坐标转换为弧度。
使用此代码,我可以随心所欲地移动我的相机,但如果我尝试在达到大约 45 度时一直向上移动,它会围绕 x 轴旋转 1-2 次,然后继续增加 y 轴。我不明白我是否做错了什么,如果有人能提供帮助,我将不胜感激。
【问题讨论】:
-
我认为将 cos() 的结果与 convertToRads 的 x 值相乘是不正确的。 (用一个简单的例子,yaw = 45 和 pitch = 45 度,看看结果如何。
-
你可能想告诉我,我猜想我放错了一个括号:P 谢谢你的帮助
-
是的 :-) ... 不足以回答 :-))
标签: c++ opengl camera mousemove glulookat