1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use basenum::BaseFloat;
use builtin::{ cross, dot, normalize };
use traits::GenFloat;
use num;
use mat::mat::{ Matrix3, Matrix4 };
use vec::vec::{ Vector3, Vector4 };
#[inline]
pub fn translate<T>(
m: &Matrix4<T>,
v: Vector3<T>
) -> Matrix4<T>
where
T : BaseFloat
{
Matrix4::new(
m.c0, m.c1, m.c2,
m.c0 * v.x + m.c1 * v.y + m.c2 * v.z + m.c3)
}
#[inline]
pub fn perspective<T>(
fov_y: T,
aspect: T,
z_near: T,
z_far: T
) -> Matrix4<T>
where
T : BaseFloat
{
perspective_rh(fov_y, aspect, z_near, z_far)
}
#[inline]
pub fn perspective_rh<T>(
fov_y: T,
aspect: T,
z_near: T,
z_far: T
) -> Matrix4<T>
where
T : BaseFloat
{
let zero = num::zero::<T>();
let one = num::one::<T>();
let two = one + one;
let q = one / (fov_y / two).tan();
let a = q / aspect;
let b = (z_near + z_far) / (z_near - z_far);
let c = (two * z_near * z_far) / (z_near - z_far);
Matrix4::new(
Vector4::new( a, zero, zero, zero),
Vector4::new(zero, q, zero, zero),
Vector4::new(zero, zero, b, zero - one),
Vector4::new(zero, zero, c, zero)
)
}
#[inline]
pub fn rotate<T>(
m: &Matrix4<T>,
angle: T,
v: Vector3<T>
) -> Matrix4<T>
where
T : BaseFloat + GenFloat<T>
{
let zero = num::zero::<T>();
let one = num::one::<T>();
let a = angle;
let (s, c) = a.sin_cos();
let axis = normalize(v);
let temp = axis * (one - c);
let rotate = Matrix3::new(
Vector3::new(
c + temp.x * axis.x,
temp.x * axis.y + s * axis.z,
temp.x * axis.z - s * axis.y),
Vector3::new(
temp.y * axis.x - s * axis.z,
c + temp.y * axis.y,
temp.y * axis.z + s * axis.x),
Vector3::new(
temp.z * axis.x + s * axis.y,
temp.z * axis.y - s * axis.x,
c + temp.z * axis.z)
);
Matrix4::new(
m.c0 * rotate.c0.x + m.c1 * rotate.c0.y + m.c2 * rotate.c0.z,
m.c0 * rotate.c1.x + m.c1 * rotate.c1.y + m.c2 * rotate.c1.z,
m.c0 * rotate.c2.x + m.c1 * rotate.c2.y + m.c2 * rotate.c2.z,
m.c3
)
}
#[inline]
pub fn scale<T>(
m: &Matrix4<T>,
v: Vector3<T>
) -> Matrix4<T>
where
T : BaseFloat + GenFloat<T>
{
Matrix4::new(
m.c0 * v.x,
m.c1 * v.y,
m.c2 * v.z,
m.c3)
}
#[inline]
pub fn look_at<T>(
eye: Vector3<T>,
center: Vector3<T>,
up: Vector3<T>
) -> Matrix4<T>
where
T : BaseFloat + GenFloat<T>
{
look_at_rh::<T>(eye, center, up)
}
#[inline]
pub fn look_at_rh<T>(
eye: Vector3<T>,
center: Vector3<T>,
up: Vector3<T>
) -> Matrix4<T>
where
T : BaseFloat + GenFloat<T>
{
let zero = num::zero::<T>();
let one = num::one::<T>();
let f = normalize(center - eye);
let s = normalize(cross(f, up));
let u = cross(s, f);
Matrix4::new(
Vector4::new(s.x, u.x,-f.x, zero),
Vector4::new(s.y, u.y,-f.y, zero),
Vector4::new(s.z, u.z,-f.z, zero),
Vector4::new(-dot(s, eye), -dot(u, eye), dot(f, eye), one)
)
}
#[cfg(test)]
mod test {
use num;
use std::f32;
use vec::vec::{ vec3, vec4 };
use ext::{ perspective, translate };
#[test]
fn test_translate() {
let v = vec3(1.0, 3.0, 2.0);
let m = num::one();
let t = translate(&m, v);
assert_eq!(t[0], vec4(1., 0., 0., 0.));
assert_eq!(t[1], vec4(0., 1., 0., 0.));
assert_eq!(t[2], vec4(0., 0., 1., 0.));
assert_eq!(t[3], v.extend(1.));
}
#[test]
fn test_perspective() {
let p = perspective(f32::consts::PI * 2.0 * 45.0 / 360.0, 1920.0 / 1080.0, 0.1, 100.0);
}
}