cgmath Rust库是否存在Bug?光线追踪视图矩阵计算异常
光线追踪器中cgmath Matrix4计算结果错误排查
我正在开发光线追踪器,使用cgmath库的Matrix4相关函数进行数学计算时,发现输出结果严重错误。即便考虑浮点精度误差,结果也与计算器验证值不符。
视图矩阵更新相关代码
pub fn recalculate_view(self) { let mut view_lock = VIEW.lock().unwrap(); *view_lock = Matrix4::look_to_rh(Point3::new(self.cam_pos.x, self.cam_pos.y, self.cam_pos.z), self.cam_pos + self.cam_dir, Vector3::new(0., 1., 0.)); println!("view: {:?}",view_lock); if let Some(matrix) = view_lock.invert() { *INVERSE_VIEW.lock().unwrap() = matrix; } else { return; } }
recalculate_view函数通过Camera对象的cam_pos和cam_dir更新静态Lazy<Mutex<Matrix4<f32>>>类型的VIEW变量。
可疑的四元数变换代码
我怀疑问题可能出在之前对相机应用的四元数变换,但不确定,相关代码如下:
if input.mouse_held(1) == true { let_cursor_move = false; hide_cursor = true; if delta.x != 0.0 || delta.y != 0.0 { let pitch_delta = delta.y * 0.3; let yaw_delta = delta.x * 0.3; let rotation_pitch = Quaternion::from_axis_angle(right_direction,-Rad(pitch_delta)); let rotation_yaw = Quaternion::from_axis_angle(Vector3::new(0.0,1.0,0.0),-Rad(yaw_delta)); let q = quaternion_normalize(quaternion_cross(rotation_pitch, rotation_yaw)); self.cam_dir = q.rotate_vector(self.cam_dir); moved = true; } }
目前我正在跟进TheCherno的光线追踪系列教程,进度到第6集,完整代码可查看GitHub仓库的working_snapshot分支src目录。
内容的提问来源于stack exchange,提问作者Question Asker
相关产品推荐
相关产品推荐

