Created own rotation matrix function
@@ -9,6 +9,17 @@ pub struct Wireframe {
pub color: Vec3
}
+fn rotation(axis: Vec3, cos_angle: f32) -> Mat4
+{
+ let c = cos_angle;
+ let s = (1.0 - c*c).sqrt();
+ let oc = 1.0 - c;
+ return Mat4::new(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
+ oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
+ oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
+ 0.0, 0.0, 0.0, 1.0);
+}
+
impl Wireframe
{
pub fn create(gl: &gl::Gl, mesh: &::mesh::DynamicMesh) -> Wireframe
@@ -36,9 +47,14 @@ impl Wireframe
let length = (p1 - p0).norm();
let dir = (p1 - p0)/length;
- let cross = vec3(1.0, 0.0, 0.0).cross(&dir);
- let axis = Unit::new_normalize(cross);
- let local_to_world = Mat4::from_axis_angle(&axis, cross.norm().asin()) * Mat4::new_nonuniform_scaling(&vec3(length, 0.01, 0.01));
+ let cos_angle = vec3(1.0, 0.0, 0.0).dot(&dir);
+ let rotation_axis = if cos_angle.abs() > 0.999 {
+ vec3(0.0, 1.0, 0.0)
+ }
+ else {
+ vec3(1.0, 0.0, 0.0).cross(&dir).normalize()
+ };
+ let local_to_world = rotation(rotation_axis, cos_angle) * Mat4::new_nonuniform_scaling(&vec3(length, 0.01, 0.01));
let normal_matrix = local_to_world.try_inverse().unwrap().transpose();
for i in 0..3 {
