关于3d:四元数旋转导致场景拉伸

Quaternion rotation causes scene to stretch

我改编了Vulkano GLTF查看器示例,以允许键盘和鼠标输入,将平移和旋转应用于场景。平移效果很好,但是旋转会导致场景中的对象向右移动到相机并伸展直到它们看起来像垃圾一样,因为旋转接近90-180度。

这是旋转之前的场景:

This

这是当旋转大约90度时:

And

我正在使用cgmath四元数进行旋转。我以前没有使用过四元数,所以我想知道是否有人可以告诉我我是否正确使用了四元数,所以我怀疑这是问题所在。

这是我使用在这里找到的方程式创建初始四元数的方式:

1
2
3
4
5
6
7
8
9
10
11
let rotation_speed_deg = 0.01;
let rotation_speed = rotation_speed_deg * f32::consts::PI / 180.0;
let rotation_angle_deg = 0.0;
let rotation_angle = rotation_angle_deg * f32::consts::PI / 180.0;
let rotation_axis = Vector3::new(0.0, 1.0, 0.0);
let rotation_scalar = (rotation_angle / 2.0 as f32).cos();
let rotation_vec = Vector3::new(
    rotation_axis.x * (rotation_angle / 2.0 as f32).sin(),
    rotation_axis.y * (rotation_angle / 2.0 as f32).sin(),
    rotation_axis.z * (rotation_angle / 2.0 as f32).sin());
let mut rotation_quat = Quaternion::from_sv(rotation_scalar, rotation_vec);

然后我正在修改四元数以对鼠标输入施加旋转,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let diff_x = position.0 - last_x;
if diff_x > 0.0 {
    // println!("turn right");
    rotation_quat.v.x += rotation_axis.x * ((rotation_angle + rotation_speed) / 2.0 as f32).sin();
    rotation_quat.v.y += rotation_axis.y * ((rotation_angle + rotation_speed) / 2.0 as f32).sin();
    rotation_quat.v.z += rotation_axis.z * ((rotation_angle + rotation_speed) / 2.0 as f32).sin();
    rotation_quat.s += ((rotation_angle + rotation_speed) / 2.0 as f32).cos();
} else if diff_x < 0.0 {
    // println!("turn left");
    rotation_quat.v.x += rotation_axis.x * ((rotation_angle - rotation_speed) / 2.0 as f32).sin();
    rotation_quat.v.y += rotation_axis.y * ((rotation_angle - rotation_speed) / 2.0 as f32).sin();
    rotation_quat.v.z += rotation_axis.z * ((rotation_angle - rotation_speed) / 2.0 as f32).sin();
    rotation_quat.s += ((rotation_angle - rotation_speed) / 2.0 as f32).cos();
}

last_x = position.0;

要应用平移和旋转,我要像这样乘以我的矩阵:

1
2
3
4
5
6
7
8
9
let fovy = Rad(60.0 * f32::consts::PI / 180.0);
let mut aspect = viewport_dimensions[0] as f32 / viewport_dimensions[1] as f32;
let near = 0.1;
let far = 100.0;
let mut proj = perspective(fovy, aspect, near, far);
let view = Matrix4::look_at(Point3::new(0.0, 1.0, -5.0), Point3::new(0.0, 1.0, 0.0), Vector3::new(0.0, -1.0, 0.0));
let trans_mat = Matrix4::from_translation(trans);
let rot_mat = Matrix4::from(rot);
builder = self.draw_node(node.index(), proj * view * trans_mat * rot_mat, viewport_dimensions, builder);

所以您可以看到,我正在乘以proj * view * trans_mat * rot_mat ...也许这是错误的顺序?

任何帮助将不胜感激。我没有很强的数学背景,并且多年来一直尝试自己学习CG,所以我不知道该怎么办。一些有关此类资料的阅读资源也将不胜感激。


拉伸的原因是因为我错误地组合了初始和随后的四元数。您不仅可以将它们加在一起,还可以使用*运算符来连接旋转。

因此我将代码的鼠标输入部分更改为此,并且它起作用了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let diff_x = position.0 - last_x;
if diff_x > 0.0 {
    // println!("turn right");
    let rotation_scalar = ((rotation_angle + rotation_speed) / 2.0 as f32).cos();
    let rotation_vec = Vector3::new(
        rotation_axis.x * ((rotation_angle + rotation_speed) / 2.0 as f32).sin(),
        rotation_axis.y * ((rotation_angle + rotation_speed) / 2.0 as f32).sin(),
        rotation_axis.z * ((rotation_angle + rotation_speed) / 2.0 as f32).sin());
    let rotation_quat2 = Quaternion::from_sv(rotation_scalar, rotation_vec);
    rotation_quat = rotation_quat * rotation_quat2;
} else if diff_x < 0.0 {
    // println!("turn left");
    let rotation_scalar = ((rotation_angle - rotation_speed) / 2.0 as f32).cos();
    let rotation_vec = Vector3::new(
        rotation_axis.x * ((rotation_angle - rotation_speed) / 2.0 as f32).sin(),
        rotation_axis.y * ((rotation_angle - rotation_speed) / 2.0 as f32).sin(),
        rotation_axis.z * ((rotation_angle - rotation_speed) / 2.0 as f32).sin());
    let rotation_quat2 = Quaternion::from_sv(rotation_scalar, rotation_vec);
    rotation_quat = rotation_quat * rotation_quat2;
}
last_x = position.0;