Mercurial > touhou
annotate 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 |
rev | line source |
---|---|
412
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 ## |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 ## |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 ## |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 ## GNU General Public License for more details. |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 ## |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 from math import radians |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 from libc.math cimport tan |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 from .matrix cimport Matrix |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 from .vector import Vector, normalize, cross, dot |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 cpdef ortho_2d(left, right, bottom, top): |
435
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
23 cdef float *data |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
24 |
412
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 mat = Matrix() |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 data = mat.data |
435
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
27 data[4*0+0] = 2 / (right - left) |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
28 data[4*1+1] = 2 / (top - bottom) |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
29 data[4*2+2] = -1 |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
30 data[4*3+0] = -(right + left) / (right - left) |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
31 data[4*3+1] = -(top + bottom) / (top - bottom) |
412
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 return mat |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 cpdef look_at(eye, center, up): |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 eye = Vector(eye) |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 center = Vector(center) |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 up = Vector(up) |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 f = normalize(center - eye) |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 u = normalize(up) |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 s = normalize(cross(f, u)) |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 u = cross(s, f) |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 |
435
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
45 return Matrix([s[0], u[0], -f[0], 0, |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
46 s[1], u[1], -f[1], 0, |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
47 s[2], u[2], -f[2], 0, |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
48 -dot(s, eye), -dot(u, eye), dot(f, eye), 1]) |
412
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 cpdef perspective(fovy, aspect, z_near, z_far): |
435
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
52 cdef float *data |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
53 |
412
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 top = tan(radians(fovy / 2)) * z_near |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 bottom = -top |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 left = -top * aspect |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 right = top * aspect |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 mat = Matrix() |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 data = mat.data |
435
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
61 data[4*0+0] = (2 * z_near) / (right - left) |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
62 data[4*1+1] = (2 * z_near) / (top - bottom) |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
63 data[4*2+2] = -(z_far + z_near) / (z_far - z_near) |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
64 data[4*2+3] = -1 |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
65 data[4*3+2] = -(2 * z_far * z_near) / (z_far - z_near) |
878273a984c4
Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
66 data[4*3+3] = 0 |
412
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 return mat |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
69 |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 cpdef setup_camera(dx, dy, dz): |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 # Some explanations on the magic constants: |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 # 192. = 384. / 2. = width / 2. |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 # 224. = 448. / 2. = height / 2. |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 # This is so that objects on the (O, x, y) plane use pixel coordinates |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 return look_at((192., 224., - 835.979370 * dz), |
5fe6cd6ceb48
Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 (192. + dx, 224. - dy, 0.), (0., -1., 0.)) |