3
3
//!
4
4
//! GLW stands for "OpenGL wrapper".
5
5
pub use color:: Color4 ;
6
- use cgmath:: angle;
7
- use cgmath:: array:: Array2 ;
8
- pub use cgmath:: matrix:: Matrix4 ;
9
- use cgmath:: vector:: Vector3 ;
10
6
use cstr_cache;
11
7
use libc:: types:: common:: c95;
8
+ use nalgebra:: na:: { Mat3 , Mat4 , Vec3 , Eye , Outer } ;
12
9
use gl;
13
10
use gl:: types:: * ;
14
11
pub use gl:: types:: GLfloat ;
15
12
use std:: mem;
16
- use std:: num;
17
13
use std:: ptr;
18
14
use std:: raw;
19
15
use std:: rc:: Rc ;
@@ -35,7 +31,7 @@ impl Shader {
35
31
}
36
32
37
33
/// 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 > ) {
39
35
let var_name = gl. scache . convert ( "projection_matrix" ) . as_ptr ( ) ;
40
36
gl. use_shader ( self , |_gl| {
41
37
unsafe {
@@ -47,11 +43,12 @@ impl Shader {
47
43
err => fail ! ( "OpenGL error 0x{:x} in GetUniformLocation" , err) ,
48
44
}
49
45
50
- gl:: UniformMatrix4fv ( loc, 1 , 0 , mem:: transmute ( m. ptr ( ) ) ) ;
46
+ let p = mem:: transmute ( m) ;
47
+ gl:: UniformMatrix4fv ( loc, 1 , 0 , p) ;
51
48
52
49
match gl:: GetError ( ) {
53
50
gl:: NO_ERROR => { } ,
54
- err => fail ! ( "OpenGL error 0x{:x} in UniformMatrix4fv " , err) ,
51
+ err => fail ! ( "OpenGL error 0x{:x} in UniformMat4fv " , err) ,
55
52
}
56
53
}
57
54
} )
@@ -352,47 +349,75 @@ impl Drop for Texture {
352
349
353
350
pub struct Camera {
354
351
// 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 > ,
358
355
}
359
356
360
357
/// 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
+ }
368
377
}
369
378
370
379
/// 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
+ }
396
421
}
397
422
398
423
impl Camera {
@@ -402,24 +427,24 @@ impl Camera {
402
427
/// and [0, -1] in z in depth.
403
428
pub fn unit ( ) -> Camera {
404
429
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 ) ,
408
433
}
409
434
}
410
435
411
- pub fn projection_matrix ( & self ) -> Matrix4 < GLfloat > {
436
+ pub fn projection_matrix ( & self ) -> Mat4 < GLfloat > {
412
437
self . fov * self . rotation * self . translation
413
438
}
414
439
415
440
/// Shift the camera by a vector.
416
- pub fn translate ( & mut self , v : Vector3 < GLfloat > ) {
441
+ pub fn translate ( & mut self , v : Vec3 < GLfloat > ) {
417
442
self . translation = self . translation * translation ( -v) ;
418
443
}
419
444
420
445
/// 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) ;
423
448
}
424
449
}
425
450
0 commit comments