From 8d4a06005cfe327068179140ddf6d0f2fa88edd0 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 3 Dec 2013 20:11:39 +1000 Subject: [PATCH] Add Mat4::look_at Hopefully I got this right... --- src/cgmath/matrix.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index 23437b0..c9e7fcf 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -19,6 +19,7 @@ use std::num::{Zero, zero, One, one, cast, sqrt}; use angle::{Rad, sin, cos, sin_cos}; use array::{Array, build}; +use point::{Point, Point3}; use quaternion::{Quat, ToQuat}; use vector::{Vector, EuclideanVector}; use vector::{Vec2, Vec3, Vec4}; @@ -222,6 +223,19 @@ impl Mat4 { } } +impl Mat4 { + pub fn look_at(eye: &Point3, center: &Point3, up: &Vec3) -> Mat4 { + let f = center.sub_p(eye).normalize(); + let s = f.cross(&up.normalize()); + let u = s.cross(&f).normalize(); + + Mat4::new(s.x.clone(), s.y.clone(), s.z.clone(), zero(), + u.x.clone(), u.y.clone(), u.z.clone(), zero(), + -f.x.clone(), -f.y.clone(), -f.z.clone(), zero(), + -eye.dot(&s), -eye.dot(&u), -eye.dot(&f), one()) + } +} + impl One for Mat2 { #[inline] fn one() -> Mat2 { Mat2::identity() } } impl One for Mat3 { #[inline] fn one() -> Mat3 { Mat3::identity() } } impl One for Mat4 { #[inline] fn one() -> Mat4 { Mat4::identity() } }