四元数旋转导致场景伸展
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四元数旋转导致场景伸展相关的知识,希望对你有一定的参考价值。
我已经改编了Vulkano,GLTF viewer example,允许键盘和鼠标输入,以应用平移和旋转到场景。平移工作正常,但旋转会导致场景中的物体向上移动到相机并伸展直到它们看起来像垃圾,因为旋转接近90-180度。
这是旋转前场景的样子:
这是旋转大约90度时:
我正在使用cgmath Quaternions进行轮换。我之前没有使用四元数,所以我想知道是否有人能告诉我我是否正确使用它们,我怀疑这是问题所在。
这就是我创建初始四元数的方法,使用我发现的here方程式:
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);
然后我修改四元数以对鼠标输入应用旋转,如下所示:
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;
要应用平移和旋转,我将这样的矩阵相乘:
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,所以我不知道还有什么地方可以转。关于这类东西的一些阅读资源也将受到赞赏。
答案
拉伸的原因是因为我错误地组合了初始和后续的四元数。你不能只将它们加在一起,但*运算符可以连接旋转。
所以我将代码的鼠标输入部分更改为此,它可以工作:
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;
以上是关于四元数旋转导致场景伸展的主要内容,如果未能解决你的问题,请参考以下文章