Mercurial > touhou
comparison pytouhou/utils/maths.pyx @ 435:878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 07 Aug 2013 11:34:40 +0200 |
parents | 5fe6cd6ceb48 |
children | cb5c68598ab0 |
comparison
equal
deleted
inserted
replaced
434:18e4b121646b | 435:878273a984c4 |
---|---|
18 from .matrix cimport Matrix | 18 from .matrix cimport Matrix |
19 from .vector import Vector, normalize, cross, dot | 19 from .vector import Vector, normalize, cross, dot |
20 | 20 |
21 | 21 |
22 cpdef ortho_2d(left, right, bottom, top): | 22 cpdef ortho_2d(left, right, bottom, top): |
23 cdef float *data | |
24 | |
23 mat = Matrix() | 25 mat = Matrix() |
24 data = mat.data | 26 data = mat.data |
25 data[0][0] = 2 / (right - left) | 27 data[4*0+0] = 2 / (right - left) |
26 data[1][1] = 2 / (top - bottom) | 28 data[4*1+1] = 2 / (top - bottom) |
27 data[2][2] = -1 | 29 data[4*2+2] = -1 |
28 data[3][0] = -(right + left) / (right - left) | 30 data[4*3+0] = -(right + left) / (right - left) |
29 data[3][1] = -(top + bottom) / (top - bottom) | 31 data[4*3+1] = -(top + bottom) / (top - bottom) |
30 return mat | 32 return mat |
31 | 33 |
32 | 34 |
33 cpdef look_at(eye, center, up): | 35 cpdef look_at(eye, center, up): |
34 eye = Vector(eye) | 36 eye = Vector(eye) |
38 f = normalize(center - eye) | 40 f = normalize(center - eye) |
39 u = normalize(up) | 41 u = normalize(up) |
40 s = normalize(cross(f, u)) | 42 s = normalize(cross(f, u)) |
41 u = cross(s, f) | 43 u = cross(s, f) |
42 | 44 |
43 return Matrix([[s[0], u[0], -f[0], 0], | 45 return Matrix([s[0], u[0], -f[0], 0, |
44 [s[1], u[1], -f[1], 0], | 46 s[1], u[1], -f[1], 0, |
45 [s[2], u[2], -f[2], 0], | 47 s[2], u[2], -f[2], 0, |
46 [-dot(s, eye), -dot(u, eye), dot(f, eye), 1]]) | 48 -dot(s, eye), -dot(u, eye), dot(f, eye), 1]) |
47 | 49 |
48 | 50 |
49 cpdef perspective(fovy, aspect, z_near, z_far): | 51 cpdef perspective(fovy, aspect, z_near, z_far): |
52 cdef float *data | |
53 | |
50 top = tan(radians(fovy / 2)) * z_near | 54 top = tan(radians(fovy / 2)) * z_near |
51 bottom = -top | 55 bottom = -top |
52 left = -top * aspect | 56 left = -top * aspect |
53 right = top * aspect | 57 right = top * aspect |
54 | 58 |
55 mat = Matrix() | 59 mat = Matrix() |
56 data = mat.data | 60 data = mat.data |
57 data[0][0] = (2 * z_near) / (right - left) | 61 data[4*0+0] = (2 * z_near) / (right - left) |
58 data[1][1] = (2 * z_near) / (top - bottom) | 62 data[4*1+1] = (2 * z_near) / (top - bottom) |
59 data[2][2] = -(z_far + z_near) / (z_far - z_near) | 63 data[4*2+2] = -(z_far + z_near) / (z_far - z_near) |
60 data[2][3] = -1 | 64 data[4*2+3] = -1 |
61 data[3][2] = -(2 * z_far * z_near) / (z_far - z_near) | 65 data[4*3+2] = -(2 * z_far * z_near) / (z_far - z_near) |
62 data[3][3] = 0 | 66 data[4*3+3] = 0 |
63 return mat | 67 return mat |
64 | 68 |
65 | 69 |
66 cpdef setup_camera(dx, dy, dz): | 70 cpdef setup_camera(dx, dy, dz): |
67 # Some explanations on the magic constants: | 71 # Some explanations on the magic constants: |