-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_functions.py
More file actions
77 lines (65 loc) · 1.7 KB
/
Copy pathmatrix_functions.py
File metadata and controls
77 lines (65 loc) · 1.7 KB
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
import math
import numpy as np
def translate(pos):
tx, ty, tz = pos
return np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[tx, ty, tz, 1]
])
def rotate_x(a):
return np.array([
[1, 0, 0, 0],
[0, math.cos(a), math.sin(a), 0],
[0, -math.sin(a), math.cos(a), 0],
[0, 0, 0, 1]
])
def rotate_y(a):
return np.array([
[math.cos(a), 0, -math.sin(a), 0],
[0, 1, 0, 0],
[math.sin(a), 0, math.cos(a), 0],
[0, 0, 0, 1]
])
def rotate_z(a):
return np.array([
[math.cos(a), math.sin(a), 0, 0],
[-math.sin(a), math.cos(a), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
def scale(n):
return np.array([
[n, 0, 0, 0],
[0, n, 0, 0],
[0, 0, n, 0],
[0, 0, 0, 1]
])
def scale_xyz(sx, sy, sz):
return np.array([
[sx, 0, 0, 0],
[0, sy, 0, 0],
[0, 0, sz, 0],
[0, 0, 0, 1]
])
def look_at(eye, target, up):
forward = (target - eye)
forward = forward / np.linalg.norm(forward)
right = np.cross(forward, up)
right = right / np.linalg.norm(right)
new_up = np.cross(right, forward)
return np.array([
[right[0], new_up[0], -forward[0], 0],
[right[1], new_up[1], -forward[1], 0],
[right[2], new_up[2], -forward[2], 0],
[-np.dot(right, eye), -np.dot(new_up, eye), np.dot(forward, eye), 1]
])
def perspective(fov, aspect, near, far):
f = 1.0 / math.tan(fov / 2.0)
return np.array([
[f / aspect, 0, 0, 0],
[0, f, 0, 0],
[0, 0, (far + near) / (near - far), -1],
[0, 0, (2 * far * near) / (near - far), 0]
])