Skip to content

Commit

Permalink
Fix visual glitches caused by weird Z basis vector
Browse files Browse the repository at this point in the history
  • Loading branch information
hikari-no-yume committed Nov 25, 2022
1 parent a363a59 commit 01fd5f6
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ impl<const N: usize> Vector<N> {
}
}

impl Vector<3> {
fn cross_product(self, rhs: Vector<3>) -> Vector<3> {
Vector([
self[1] * rhs[2] - self[2] * rhs[1],
self[2] * rhs[0] - self[0] * rhs[2],
self[0] * rhs[1] - self[1] * rhs[0],
])
}
}

type Vertex = Vector<3>;

type Face = [Vertex; 3];
Expand Down Expand Up @@ -263,11 +273,19 @@ fn main() {
let (width, x_basis_vector) = x_basis_vector.normalise();
let (height, y_basis_vector) = y_basis_vector.normalise();

// Technically we don't need to change the Z basis vector from
// (0, 0, 1, 0) if we assume the input Z co-ordinate is always going to
// be zero, but in practice we get exciting visual glitches in Chrome
// and Firefox if we leave it like that. They're probably trying to
// decompose the matrix and getting weird results.
// So: set the Z basis vector to the normal.
let z_basis_vector = x_basis_vector.cross_product(y_basis_vector);

#[cfg_attr(rustfmt, rustfmt_skip)]
let matrix: [f32; 16] = [
x_basis_vector[0], x_basis_vector[1], x_basis_vector[2], 0f32,
y_basis_vector[0], y_basis_vector[1], y_basis_vector[2], 0f32,
0f32, 0f32, 1f32, 0f32,
z_basis_vector[0], z_basis_vector[1], z_basis_vector[2], 0f32,
translation[0], translation[1], translation[2], 1f32,
];

Expand Down

0 comments on commit 01fd5f6

Please sign in to comment.