Mercurial > touhou
comparison pytouhou/utils/maths.pyx @ 436:cb5c68598ab0
Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 07 Aug 2013 11:34:42 +0200 |
parents | 878273a984c4 |
children | 6e3b3d5d4691 |
comparison
equal
deleted
inserted
replaced
435:878273a984c4 | 436:cb5c68598ab0 |
---|---|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 ## GNU General Public License for more details. | 12 ## GNU General Public License for more details. |
13 ## | 13 ## |
14 | 14 |
15 from math import radians | 15 from libc.math cimport tan, M_PI as pi |
16 from libc.math cimport tan | |
17 | 16 |
18 from .matrix cimport Matrix | 17 from .vector cimport Vector, normalize, cross, dot |
19 from .vector import Vector, normalize, cross, dot | |
20 | 18 |
21 | 19 |
22 cpdef ortho_2d(left, right, bottom, top): | 20 cdef double radians(double degrees) nogil: |
21 return degrees * pi / 180 | |
22 | |
23 | |
24 cdef Matrix ortho_2d(float left, float right, float bottom, float top): | |
23 cdef float *data | 25 cdef float *data |
24 | 26 |
25 mat = Matrix() | 27 mat = Matrix() |
26 data = mat.data | 28 data = mat.data |
27 data[4*0+0] = 2 / (right - left) | 29 data[4*0+0] = 2 / (right - left) |
30 data[4*3+0] = -(right + left) / (right - left) | 32 data[4*3+0] = -(right + left) / (right - left) |
31 data[4*3+1] = -(top + bottom) / (top - bottom) | 33 data[4*3+1] = -(top + bottom) / (top - bottom) |
32 return mat | 34 return mat |
33 | 35 |
34 | 36 |
35 cpdef look_at(eye, center, up): | 37 cdef Matrix look_at(Vector eye, Vector center, Vector up): |
36 eye = Vector(eye) | 38 f = normalize(center.sub(eye)) |
37 center = Vector(center) | |
38 up = Vector(up) | |
39 | |
40 f = normalize(center - eye) | |
41 u = normalize(up) | 39 u = normalize(up) |
42 s = normalize(cross(f, u)) | 40 s = normalize(cross(f, u)) |
43 u = cross(s, f) | 41 u = cross(s, f) |
44 | 42 |
45 return Matrix([s[0], u[0], -f[0], 0, | 43 return Matrix([s.x, u.x, -f.x, 0, |
46 s[1], u[1], -f[1], 0, | 44 s.y, u.y, -f.y, 0, |
47 s[2], u[2], -f[2], 0, | 45 s.z, u.z, -f.z, 0, |
48 -dot(s, eye), -dot(u, eye), dot(f, eye), 1]) | 46 -dot(s, eye), -dot(u, eye), dot(f, eye), 1]) |
49 | 47 |
50 | 48 |
51 cpdef perspective(fovy, aspect, z_near, z_far): | 49 cdef Matrix perspective(float fovy, float aspect, float z_near, float z_far): |
52 cdef float *data | 50 cdef float *data |
53 | 51 |
54 top = tan(radians(fovy / 2)) * z_near | 52 top = tan(radians(fovy / 2)) * z_near |
55 bottom = -top | 53 bottom = -top |
56 left = -top * aspect | 54 left = -top * aspect |
65 data[4*3+2] = -(2 * z_far * z_near) / (z_far - z_near) | 63 data[4*3+2] = -(2 * z_far * z_near) / (z_far - z_near) |
66 data[4*3+3] = 0 | 64 data[4*3+3] = 0 |
67 return mat | 65 return mat |
68 | 66 |
69 | 67 |
70 cpdef setup_camera(dx, dy, dz): | 68 cdef Matrix setup_camera(float dx, float dy, float dz): |
71 # Some explanations on the magic constants: | 69 # Some explanations on the magic constants: |
72 # 192. = 384. / 2. = width / 2. | 70 # 192. = 384. / 2. = width / 2. |
73 # 224. = 448. / 2. = height / 2. | 71 # 224. = 448. / 2. = height / 2. |
74 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) | 72 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) |
75 # This is so that objects on the (O, x, y) plane use pixel coordinates | 73 # This is so that objects on the (O, x, y) plane use pixel coordinates |
76 return look_at((192., 224., - 835.979370 * dz), | 74 return look_at(Vector(192., 224., - 835.979370 * dz), |
77 (192. + dx, 224. - dy, 0.), (0., -1., 0.)) | 75 Vector(192. + dx, 224. - dy, 0.), Vector(0., -1., 0.)) |