I'm making a Minecraft-like game using the Rust Vulkano Vulkan API binding to render the game. I have a player rotating either about the Y-axis or X-axis, specifically implemented with a FPS view matrix. The output expected from the view matrix is a 4x4 float, homogeneous matrix ([[f32; 4]; 4]
).
I've attempted to use the cgmath
library and nalgebra
library as my implementation for the FPS view matrix, though I decided to manually implement it only using nalgebra
for the matrix operations.
# cargo.toml
vulkano = "0.18.0"
vulkano-shaders = "0.13"
vulkano-win = "0.18.0"
winit = "0.22.0"
nalgebra = "0.21.1"
nalgebra-glm = "0.7.0"
extern crate nalgebra as na;
use na::{
Point3,
Rotation3,
Matrix4,
};
use crate::Dimension;
type Matrix3D = [[f32; 4]; 4];
// generates the mvp matrix for meshes and other pipelines
pub fn gen_mvp(&self, dimensions: Dimension<u32>) -> (Matrix3D, Matrix3D, Matrix3D) {
let proj = Perspective3::new(dimensions.aspect() as f32, 3.14 / 2.0, 0.1, 1000.0);
let eye = &self.position;
let target: Vector3<f32> = Vector3::new(1.0, 0.0, 1.0);
let up = Vector3::new(0.0, -1.0, 0.0);
println!("ROT MAT: {:?}", self.rotation.euler_angles());
println!("POS MAT: {:?}", self.position.coords.data);
println!("ROT POS: {:?}", self.rotation.transform_vector(&target));
let d = self.rotation.transform_vector(&target).data;
println!("TARGET : {:?}", self.rotation.transform_vector(&target));
let roty = Rotation3::from_euler_angles(self.rotation.euler_angles().0, self.rotation.euler_angles().1, 0.0);
// let view = Isometry3::look_at_lh(eye, &(&self.position + &roty * &target), &up);
let view = Self::fpv_view(eye, &self.rotation);
// let rot = UnitQuaternion::new(Vector3::new(0.0, self.rotation.euler_angles().1, self.rotation.euler_angles().2));
// let crd = self.position.coords.data;
// let view_pos = Isometry3::from_parts(Translation3::new(crd[0], crd[1], crd[2]), UnitQuaternion::new(Vector3::new(0.0, 0.0, 0.0)));
// let view_rot = Isometry3::from_parts(Translation3::new(0.0, 0.0, 1.0), rot);
// let view = view_rot * view_pos;
// let model = glm::identity::<f32, >();
let model = Matrix4::identity();
// let eye = Point3::new(0.0, 0.0, 1.0);
// let target = Point3::new(1.0, 0.0, 0.0);
// let view = Isometry3::look_at_rh(&eye, &target, &Vector3::y());
// let model = Isometry3::identity(); // the world
// let proj = perspective (self.fov, dimensions.aspect() as f32, 0.1 , 1000.0);
// let view = Matrix4::from_angle_x(self.rotation.x) * Matrix4::from_angle_y(self.rotation.y) *
// Matrix4::look_at(Point3::new(self.position.x, self.position.y, 1.0+self.position.z), self.position.into(), Vector3::new(0.0, -1.0, 0.0));
// let world = Matrix4::identity();
let proj_matrix = proj.as_matrix();
let view_matrix = view;
let proj_cooked: &[f32] = proj_matrix.as_slice();
let view_cooked: &[f32] = view.as_slice();
let model_cooked: &[f32] = model.as_slice();
let proj_dt;
let view_dt;
let model_dt;
unsafe {
assert_eq!(proj_cooked.len(), 16);
assert_eq!(view_cooked.len(), 16);
assert_eq!(model_cooked.len(), 16);
proj_dt = *(proj_cooked.as_ptr() as *const Matrix3D);
view_dt = *(view_cooked.as_ptr() as *const Matrix3D);
model_dt = *(model_cooked.as_ptr() as *const Matrix3D);
}
(proj_dt, view_dt, model_dt)
}
fn fpv_view(eye: &Point3<f32>, rot: &Rotation3<f32>) -> Matrix4<f32> {
let (roll, pitch, yaw) = rot.euler_angles();
let e = &eye.coords.data;
// -- snip --
let xrot = Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, pitch.cos(),-pitch.sin(), 0.0,
0.0, pitch.sin(), pitch.cos(), 0.0,
0.0, 0.0, 0.0, 1.0,
);
let yrot = Matrix4::new(
yaw.cos(), 0.0, yaw.sin(), 0.0,
0.0, 1.0, 0.0, 0.0,
-yaw.sin(), 0.0, yaw.cos(), 0.0,
0.0, 0.0, 0.0, 1.0,
);
// also tried to transpose the translation matrix too
// let translation = Matrix4::new(
// 1.0, 0.0, 0.0, 0.0,
// 0.0, 1.0, 0.0, 0.0,
// 0.0, 0.0, 1.0, 0.0,
// -e[0], -e[1], -e[2], 1.0,
// );
let translation = Matrix4::new(
1.0, 0.0, 0.0, -e[0],
0.0, 1.0, 0.0, -e[1],
0.0, 0.0, 1.0, -e[2],
0.0, 0.0, 0.0, 1.0,
);
// also tried inverting the view matrix
// (translation * yrot * xrot).try_inverse().unwrap()
translation * yrot * xrot
}
I also attempted to use the traditional look_at(eye, target, up)
function.
The result is either weird clipping/warping or not rotating properly. I tried to find some external resources such as MVP matrix tutorials, questions, and forums but the solution always results in a weird rendering.
Update 1
The environment I built currently in the game is just a large 64x64x64 block-sized hollow chunk with textures only on the chunk border with no face culling with alpha-blending enabled.
This is the closest I can get where the rotating about y-axis rotates around a imaginary large circle path, which should just be rotating about the player.
pub fn gen_mvp(&self, dimensions: Dimension<u32>) -> (Matrix3D, Matrix3D, Matrix3D) {
let proj = Perspective3::new(dimensions.aspect() as f32, 3.14 / 2.0, 0.1, 1000.0);
let eye = &self.position;
let target: Vector3<f32> = Vector3::new(0.0, 0.0, 1.0);
let up = Vector3::new(0.0, -1.0, 0.0);
let roty = Rotation3::from_euler_angles(self.rotation.euler_angles().0, self.rotation.euler_angles().1, 0.0);
let view = Isometry3::look_at_lh(eye, &(&self.position + (&roty * &target)), &up);
let model = Matrix4::identity();
let proj_matrix = proj.as_matrix();
let view_matrix = view.to_homogeneous();
let proj_cooked: &[f32] = proj_matrix.as_slice();
let view_cooked: &[f32] = view_matrix.as_slice();
let model_cooked: &[f32] = model.as_slice();
let proj_dt;
let view_dt;
let model_dt;
unsafe {
assert_eq!(proj_cooked.len(), 16);
assert_eq!(view_cooked.len(), 16);
assert_eq!(model_cooked.len(), 16);
proj_dt = *(proj_cooked.as_ptr() as *const Matrix3D);
view_dt = *(view_cooked.as_ptr() as *const Matrix3D);
model_dt = *(model_cooked.as_ptr() as *const Matrix3D);
}
(proj_dt, view_dt, model_dt)
}