comparison pytouhou/utils/maths.pyx @ 523:6e3b3d5d4691

Make matrix a struct.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 18 Dec 2013 17:53:29 +0100
parents cb5c68598ab0
children 7f016dfbdfb1
comparison
equal deleted inserted replaced
522:e8496e5ba056 523:6e3b3d5d4691
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from libc.math cimport tan, M_PI as pi 15 from libc.math cimport tan, M_PI as pi
16 16
17 from .matrix cimport new_matrix, new_identity
17 from .vector cimport Vector, normalize, cross, dot 18 from .vector cimport Vector, normalize, cross, dot
18 19
19 20
20 cdef double radians(double degrees) nogil: 21 cdef double radians(double degrees) nogil:
21 return degrees * pi / 180 22 return degrees * pi / 180
22 23
23 24
24 cdef Matrix ortho_2d(float left, float right, float bottom, float top): 25 cdef Matrix *ortho_2d(float left, float right, float bottom, float top):
25 cdef float *data 26 mat = new_identity()
26 27 data = <float*>mat
27 mat = Matrix()
28 data = mat.data
29 data[4*0+0] = 2 / (right - left) 28 data[4*0+0] = 2 / (right - left)
30 data[4*1+1] = 2 / (top - bottom) 29 data[4*1+1] = 2 / (top - bottom)
31 data[4*2+2] = -1 30 data[4*2+2] = -1
32 data[4*3+0] = -(right + left) / (right - left) 31 data[4*3+0] = -(right + left) / (right - left)
33 data[4*3+1] = -(top + bottom) / (top - bottom) 32 data[4*3+1] = -(top + bottom) / (top - bottom)
34 return mat 33 return mat
35 34
36 35
37 cdef Matrix look_at(Vector eye, Vector center, Vector up): 36 cdef Matrix *look_at(Vector eye, Vector center, Vector up):
37 cdef Matrix mat
38
38 f = normalize(center.sub(eye)) 39 f = normalize(center.sub(eye))
39 u = normalize(up) 40 u = normalize(up)
40 s = normalize(cross(f, u)) 41 s = normalize(cross(f, u))
41 u = cross(s, f) 42 u = cross(s, f)
42 43
43 return Matrix([s.x, u.x, -f.x, 0, 44 mat = Matrix(s.x, u.x, -f.x, 0,
44 s.y, u.y, -f.y, 0, 45 s.y, u.y, -f.y, 0,
45 s.z, u.z, -f.z, 0, 46 s.z, u.z, -f.z, 0,
46 -dot(s, eye), -dot(u, eye), dot(f, eye), 1]) 47 -dot(s, eye), -dot(u, eye), dot(f, eye), 1)
48
49 return new_matrix(&mat)
47 50
48 51
49 cdef Matrix perspective(float fovy, float aspect, float z_near, float z_far): 52 cdef Matrix *perspective(float fovy, float aspect, float z_near, float z_far):
50 cdef float *data
51
52 top = tan(radians(fovy / 2)) * z_near 53 top = tan(radians(fovy / 2)) * z_near
53 bottom = -top 54 bottom = -top
54 left = -top * aspect 55 left = -top * aspect
55 right = top * aspect 56 right = top * aspect
56 57
57 mat = Matrix() 58 mat = new_identity()
58 data = mat.data 59 data = <float*>mat
59 data[4*0+0] = (2 * z_near) / (right - left) 60 data[4*0+0] = (2 * z_near) / (right - left)
60 data[4*1+1] = (2 * z_near) / (top - bottom) 61 data[4*1+1] = (2 * z_near) / (top - bottom)
61 data[4*2+2] = -(z_far + z_near) / (z_far - z_near) 62 data[4*2+2] = -(z_far + z_near) / (z_far - z_near)
62 data[4*2+3] = -1 63 data[4*2+3] = -1
63 data[4*3+2] = -(2 * z_far * z_near) / (z_far - z_near) 64 data[4*3+2] = -(2 * z_far * z_near) / (z_far - z_near)
64 data[4*3+3] = 0 65 data[4*3+3] = 0
65 return mat 66 return mat
66 67
67 68
68 cdef Matrix setup_camera(float dx, float dy, float dz): 69 cdef Matrix *setup_camera(float dx, float dy, float dz):
69 # Some explanations on the magic constants: 70 # Some explanations on the magic constants:
70 # 192. = 384. / 2. = width / 2. 71 # 192. = 384. / 2. = width / 2.
71 # 224. = 448. / 2. = height / 2. 72 # 224. = 448. / 2. = height / 2.
72 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) 73 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
73 # This is so that objects on the (O, x, y) plane use pixel coordinates 74 # This is so that objects on the (O, x, y) plane use pixel coordinates