Skip to content

Commit c4f7351

Browse files
committed
remove cgmath dependency
1 parent 6be3442 commit c4f7351

File tree

7 files changed

+276
-246
lines changed

7 files changed

+276
-246
lines changed

Cargo.lock

Lines changed: 39 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ git = "https://github.com/bfops/sdl2_game_window"
2222
[dependencies.piston]
2323
git = "https://github.com/PistonDevelopers/piston"
2424

25-
[dependencies.cgmath]
26-
git = "https://github.com/bjz/cgmath-rs"
25+
[dependencies.nalgebra]
26+
git = "https://github.com/sebcrozet/nalgebra"
27+
28+
[dependencies.ncollide3df32]
29+
git = "https://github.com/sebcrozet/ncollide"

src/glw.rs

Lines changed: 75 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
//!
44
//! GLW stands for "OpenGL wrapper".
55
pub use color::Color4;
6-
use cgmath::angle;
7-
use cgmath::array::Array2;
8-
pub use cgmath::matrix::Matrix4;
9-
use cgmath::vector::Vector3;
106
use cstr_cache;
117
use libc::types::common::c95;
8+
use nalgebra::na::{Mat3, Mat4, Vec3, Eye, Outer};
129
use gl;
1310
use gl::types::*;
1411
pub use gl::types::GLfloat;
1512
use std::mem;
16-
use std::num;
1713
use std::ptr;
1814
use std::raw;
1915
use std::rc::Rc;
@@ -35,7 +31,7 @@ impl Shader {
3531
}
3632

3733
/// Sets the variable `projection_matrix` in some shader.
38-
pub fn set_projection_matrix(&self, gl: &mut GLContext, m: &Matrix4<GLfloat>) {
34+
pub fn set_projection_matrix(&self, gl: &mut GLContext, m: &Mat4<GLfloat>) {
3935
let var_name = gl.scache.convert("projection_matrix").as_ptr();
4036
gl.use_shader(self, |_gl| {
4137
unsafe {
@@ -47,11 +43,12 @@ impl Shader {
4743
err => fail!("OpenGL error 0x{:x} in GetUniformLocation", err),
4844
}
4945

50-
gl::UniformMatrix4fv(loc, 1, 0, mem::transmute(m.ptr()));
46+
let p = mem::transmute(m);
47+
gl::UniformMatrix4fv(loc, 1, 0, p);
5148

5249
match gl::GetError() {
5350
gl::NO_ERROR => {},
54-
err => fail!("OpenGL error 0x{:x} in UniformMatrix4fv", err),
51+
err => fail!("OpenGL error 0x{:x} in UniformMat4fv", err),
5552
}
5653
}
5754
})
@@ -352,47 +349,75 @@ impl Drop for Texture {
352349

353350
pub struct Camera {
354351
// projection matrix components
355-
pub translation: Matrix4<GLfloat>,
356-
pub rotation: Matrix4<GLfloat>,
357-
pub fov: Matrix4<GLfloat>,
352+
pub translation: Mat4<GLfloat>,
353+
pub rotation: Mat4<GLfloat>,
354+
pub fov: Mat4<GLfloat>,
358355
}
359356

360357
/// Create a 3D translation matrix.
361-
pub fn translation(t: Vector3<GLfloat>) -> Matrix4<GLfloat> {
362-
Matrix4::new(
363-
1.0, 0.0, 0.0, 0.0,
364-
0.0, 1.0, 0.0, 0.0,
365-
0.0, 0.0, 1.0, 0.0,
366-
t.x, t.y, t.z, 1.0,
367-
)
358+
pub fn translation(t: Vec3<GLfloat>) -> Mat4<GLfloat> {
359+
Mat4 {
360+
m11: 1.0, m12: 0.0, m13: 0.0, m14: t.x,
361+
m21: 0.0, m22: 1.0, m23: 0.0, m24: t.y,
362+
m31: 0.0, m32: 0.0, m33: 1.0, m34: t.z,
363+
m41: 0.0, m42: 0.0, m43: 0.0, m44: 1.0,
364+
}
365+
}
366+
367+
pub fn from_axis_angle3(axis: Vec3<GLfloat>, angle: GLfloat) -> Mat3<GLfloat> {
368+
let (s, c) = angle.sin_cos();
369+
let Vec3 { x: xs, y: ys, z: zs } = axis * s;
370+
let vsub1c = axis * (1.0 - c);
371+
Outer::outer(&vsub1c, &vsub1c) +
372+
Mat3 {
373+
m11: c, m12: -zs, m13: ys,
374+
m21: zs, m22: c, m23: -xs,
375+
m31: -ys, m32: xs, m33: c,
376+
}
368377
}
369378

370379
/// Create a matrix from a rotation around an arbitrary axis.
371-
pub fn from_axis_angle(axis: Vector3<GLfloat>, angle: angle::Rad<GLfloat>) -> Matrix4<GLfloat> {
372-
let (s, c) = angle::sin_cos(angle);
373-
let _1subc = num::one::<GLfloat>() - c;
374-
375-
Matrix4::new(
376-
_1subc * axis.x * axis.x + c,
377-
_1subc * axis.x * axis.y + s * axis.z,
378-
_1subc * axis.x * axis.z - s * axis.y,
379-
num::zero(),
380-
381-
_1subc * axis.x * axis.y - s * axis.z,
382-
_1subc * axis.y * axis.y + c,
383-
_1subc * axis.y * axis.z + s * axis.x,
384-
num::zero(),
385-
386-
_1subc * axis.x * axis.z + s * axis.y,
387-
_1subc * axis.y * axis.z - s * axis.x,
388-
_1subc * axis.z * axis.z + c,
389-
num::zero(),
390-
391-
num::zero(),
392-
num::zero(),
393-
num::zero(),
394-
num::one(),
395-
)
380+
pub fn from_axis_angle4(axis: Vec3<GLfloat>, angle: GLfloat) -> Mat4<GLfloat> {
381+
let (s, c) = angle.sin_cos();
382+
let sub1c = 1.0 - c;
383+
let Vec3 { x: xs, y: ys, z: zs } = axis * s;
384+
let (x, y, z) = (axis.x, axis.y, axis.z);
385+
Mat4 {
386+
m11: x*x*sub1c + c, m12: x*y*sub1c - zs, m13: x*z*sub1c + ys, m14: 0.0,
387+
m21: y*x*sub1c + zs, m22: y*y*sub1c + c, m23: y*z*sub1c - xs, m24: 0.0,
388+
m31: z*x*sub1c - ys, m32: z*y*sub1c + xs, m33: z*z*sub1c + c, m34: 0.0,
389+
m41: 0.0, m42: 0.0, m43: 0.0, m44: 1.0,
390+
}
391+
}
392+
393+
/// Create a 3D perspective initialization matrix.
394+
pub fn perspective(fovy: GLfloat, aspect: GLfloat, near: GLfloat, far: GLfloat) -> Mat4<GLfloat> {
395+
Mat4 {
396+
m11: fovy / aspect, m12: 0.0, m13: 0.0, m14: 0.0,
397+
m21: 0.0, m22: fovy, m23: 0.0, m24: 0.0,
398+
m31: 0.0, m32: 0.0, m33: (near + far) / (near - far), m34: 2.0 * near * far / (near - far),
399+
m41: 0.0, m42: 0.0, m43: -1.0, m44: 0.0,
400+
}
401+
}
402+
403+
#[allow(dead_code)]
404+
pub fn ortho(left: GLfloat, right: GLfloat, bottom: GLfloat, top: GLfloat, near: GLfloat, far: GLfloat) -> Mat4<GLfloat> {
405+
Mat4 {
406+
m11: 2.0 / (right - left), m12: 0.0, m13: 0.0, m14: (left + right) / (left - right),
407+
m21: 0.0, m22: 2.0 / (top - bottom), m23: 0.0, m24: (bottom + top) / (bottom - top),
408+
m31: 0.0, m32: 0.0, m33: 2.0 / (near - far), m34: (near + far) / (near - far),
409+
m41: 0.0, m42: 0.0, m43: 0.0, m44: 1.0,
410+
}
411+
}
412+
413+
/// Create a XY symmetric ortho matrix.
414+
pub fn sortho(dx: GLfloat, dy: GLfloat, near: GLfloat, far: GLfloat) -> Mat4<GLfloat> {
415+
Mat4 {
416+
m11: 1.0 / dx, m12: 0.0, m13: 0.0, m14: 0.0,
417+
m21: 0.0, m22: 1.0 / dy, m23: 0.0, m24: 0.0,
418+
m31: 0.0, m32: 0.0, m33: 2.0 / (near - far), m34: (near + far) / (near - far),
419+
m41: 0.0, m42: 0.0, m43: 0.0, m44: 1.0,
420+
}
396421
}
397422

398423
impl Camera {
@@ -402,24 +427,24 @@ impl Camera {
402427
/// and [0, -1] in z in depth.
403428
pub fn unit() -> Camera {
404429
Camera {
405-
translation: Matrix4::identity(),
406-
rotation: Matrix4::identity(),
407-
fov: Matrix4::identity(),
430+
translation: Eye::new_identity(4),
431+
rotation: Eye::new_identity(4),
432+
fov: Eye::new_identity(4),
408433
}
409434
}
410435

411-
pub fn projection_matrix(&self) -> Matrix4<GLfloat> {
436+
pub fn projection_matrix(&self) -> Mat4<GLfloat> {
412437
self.fov * self.rotation * self.translation
413438
}
414439

415440
/// Shift the camera by a vector.
416-
pub fn translate(&mut self, v: Vector3<GLfloat>) {
441+
pub fn translate(&mut self, v: Vec3<GLfloat>) {
417442
self.translation = self.translation * translation(-v);
418443
}
419444

420445
/// Rotate about a given vector, by `r` radians.
421-
pub fn rotate(&mut self, v: Vector3<GLfloat>, r: angle::Rad<GLfloat>) {
422-
self.rotation = self.rotation * from_axis_angle(v, -r);
446+
pub fn rotate(&mut self, v: Vec3<GLfloat>, r: GLfloat) {
447+
self.rotation = self.rotation * from_axis_angle4(v, -r);
423448
}
424449
}
425450

0 commit comments

Comments
 (0)