相机的旋转方向在一段时间后反转
Rotate Direction of Camera Inverses Itself After Some Time
>我正在尝试创建迷你应用程序,该应用程序通过鼠标移动围绕立方体旋转相机。它在绕 Y 轴旋转时完美运行,但我在绕 X 轴旋转时遇到了问题。当我不断向上移动鼠标时,立方体开始沿正 X 方向旋转。一段时间后,它反转旋转方向并开始在负 X 方向上旋转,然后在一段时间后,再次在正 X 方向上旋转。我想让它围绕正 X 旋转,只要我向上移动鼠标。这里有什么问题?
立方体位于坐标系的中心。投影是透视:
编辑:这是与问题相关的视频:https://youtu.be/997ZdUM8fcQ
vec4 eye;
vec4 at;
vec4 up;
GLfloat mouseX = -1;
GLfloat mouseY = -1;
// Bound to glutPassiveMotionFunc
void mouse(int x, int y) {
// This rotation works perfect, cube rotates on same direction continuously as far as I move the mouse left or right
GLfloat acceleration_x = mouseX - x;
eye = RotateY(acceleration_x / 10.f) * eye;
mouseX = x;
// This rotation doesn't work properly, after some time, cube's rotation direction inverses
GLfloat acceleration_y = mouseY - y;
eye = RotateX(acceleration_y / 10.f) * eye;
mouseY = y;
// This part teleports pointer to opposite side of window after reaching window bounds
if (x >= 1364) {
glutWarpPointer(1, y);
mouseX = 1;
}
else if (x <= 0) {
glutWarpPointer(1363, y);
mouseX = 1363;
}
if (y >= 751) {
glutWarpPointer(x, 3);
mouseY = 3;
}
else if (y <= 2) {
glutWarpPointer(x, 750);
mouseY = 750;
}
}
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
vec4 up( 0.0, 1.0, 0.0, 0.0 );
mat4 model_view = LookAt( eye, at, up );
glUniformMatrix4fv( ModelView, 1, GL_TRUE, model_view );
glDrawArrays( GL_TRIANGLES, 0, NumVertices );
glutSwapBuffers();
}
// Bound to glutTimerFunc
void timer( int id )
{
glutPostRedisplay();
glutTimerFunc(1000.f/60.f, timer, 0);
}
int main( int argc, char **argv )
{
......
......
eye = vec4(0, 0, 2, 0);
at = vec4(0, 0, 0, 0);
up = vec4(0.0, 1.0, 0.0, 0.0);
.....
.....
}
这是因为相机被翻转了。像这样用你的手:让食指是dir
,你的拇指是up
(拇指=(0, 1, 0((。将索引查找器向前,拇指向上。现在,开始围绕X旋转你的手:你的食指开始向下,而你的拇指向前(在此期间,thumb.y是正的(。当您将手旋转 90 度时,食指向下指向,拇指指向前方 (thumb.y 为 0(。当您继续旋转时,您的拇指开始指向前方+向下(thumb.y 变为负数(。
此时,LookAt 函数不会计算你想要的矩阵,因为您希望up
向量向下指向(正如我们所说,thumb.y 是负数(,但在代码中,up
向量是一个常量 (0, 1, 0(,所以 LookAt 将计算一个具有正up.y
的矩阵:相机被翻转。
解决方案可能是注册所需的旋转角度,并使用这些角度旋转相机矩阵(而不是dir
(
相关文章:
- 在做一段时间内检查字符的无限循环
- ExtTextOut 文本的持续闪烁,在一段时间后,文本将恢复为默认字体
- 对于等待以 std::future wait() 返回的函数的 CPU 使用率或检查标志在循环中休眠一段时间哪个更好?
- ::grpc::ServerReaderWriter 对象在另一个线程中一段时间后无法调用
- 一段时间后 CPU 使用率高
- 有没有办法在一段时间后将流程从流出中分离出来
- 在一段时间内更改标签文本
- 布尔函数在一段时间内不起作用
- 解决PNP在一段时间后返回坏的rvec和tvec
- 相机的旋转方向在一段时间后反转
- boost::asio::d eadline_timer 1ms 滞后一段时间
- SDL_Mixer再次播放时,从随机位置开始一段时间,然后从头开始
- 您可以在一段时间内迭代向量
- 列表STL的时间复杂性在一段时间内进行排序
- 我的开关菜单在一段时间循环中出现问题
- 如何在一段时间内更新 QLabel?
- 我可以在开关(消息)语句的同时运行一段时间吗?(C )
- 如何从一段时间返回继续
- 执行/在循环中停留一段时间,例如 1 分钟
- 如何在嵌套的 for 循环中使用“继续一段时间”循环