Mercurial > touhou
annotate pytouhou/utils/matrix.pyx @ 427:0604f4fbbe3c
Move the depth buffer clear outside of scissor, as per apitrace’s advice.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 03 Aug 2013 15:48:51 +0200 |
parents | f4d76d3d6f2a |
children | 878273a984c4 |
rev | line source |
---|---|
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
2 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
4 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
12 ## GNU General Public License for more details. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
13 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
44
diff
changeset
|
14 |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
15 from libc.math cimport sin, cos |
423
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
16 from libc.stdlib cimport malloc, free |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
17 |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
18 |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
19 cdef float* matrix_to_floats(Matrix self): |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
20 for i in xrange(4): |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
21 for j in xrange(4): |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
22 self.c_data[i*4+j] = self.data[i][j] |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
23 return self.c_data |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
24 |
4 | 25 |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
26 cdef class Matrix: |
423
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
27 def __cinit__(self): |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
28 self.c_data = <float*>malloc(16 * sizeof(float)) |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
29 |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
30 |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
31 def __init__(self, data=None): |
370
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
32 self.data = data or [[1, 0, 0, 0], |
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
33 [0, 1, 0, 0], |
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
34 [0, 0, 1, 0], |
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
35 [0, 0, 0, 1]] |
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
36 |
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
37 |
423
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
38 def __dealloc__(self): |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
39 free(self.c_data) |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
40 |
d8630c086926
Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
41 |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
42 def __mul__(self, Matrix other): |
370
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
43 out = Matrix() |
411
2428296cccab
Remove indirect access to Matrix values.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
370
diff
changeset
|
44 d1 = self.data |
2428296cccab
Remove indirect access to Matrix values.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
370
diff
changeset
|
45 d2 = other.data |
2428296cccab
Remove indirect access to Matrix values.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
370
diff
changeset
|
46 d3 = out.data |
370
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
47 for i in xrange(4): |
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
48 for j in xrange(4): |
411
2428296cccab
Remove indirect access to Matrix values.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
370
diff
changeset
|
49 d3[i][j] = sum(d1[i][k] * d2[k][j] for k in xrange(4)) |
370
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
50 return out |
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
51 |
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
131
diff
changeset
|
52 |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
53 cpdef flip(self): |
123 | 54 data = self.data |
55 a, b, c, d = data[0] | |
56 data[0] = [-a, -b, -c, -d] | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
57 |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
58 |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
59 cpdef scale(self, x, y, z): |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
60 d1 = self.data |
123 | 61 d1[0] = [a * x for a in d1[0]] |
62 d1[1] = [a * y for a in d1[1]] | |
63 d1[2] = [a * z for a in d1[2]] | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
64 |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
65 |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
66 cpdef scale2d(self, x, y): |
123 | 67 data = self.data |
68 d1a, d1b, d1c, d1d = data[0] | |
69 d2a, d2b, d2c, d2d = data[1] | |
70 data[0] = [d1a * x, d1b * x, d1c * x, d1d * x] | |
71 data[1] = [d2a * y, d2b * y, d2c * y, d2d * y] | |
31 | 72 |
73 | |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
74 cpdef translate(self, x, y, z): |
123 | 75 data = self.data |
76 a, b, c = data[3][:3] | |
77 a, b, c = a * x, b * y, c * z | |
78 d1a, d1b, d1c, d1d = data[0] | |
79 d2a, d2b, d2c, d2d = data[1] | |
80 d3a, d3b, d3c, d3d = data[2] | |
81 data[0] = [d1a + a, d1b + a, d1c + a, d1d + a] | |
82 data[1] = [d2a + b, d2b + b, d2c + b, d2d + b] | |
83 data[2] = [d3a + c, d3b + c, d3c + c, d3d + c] | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
84 |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
85 |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
86 cpdef rotate_x(self, angle): |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
87 d1 = self.data |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
88 cos_a = cos(angle) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
89 sin_a = sin(angle) |
123 | 90 d1[1], d1[2] = ([cos_a * d1[1][i] - sin_a * d1[2][i] for i in range(4)], |
91 [sin_a * d1[1][i] + cos_a * d1[2][i] for i in range(4)]) | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
92 |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
93 |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
94 cpdef rotate_y(self, angle): |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
95 d1 = self.data |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
96 cos_a = cos(angle) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
97 sin_a = sin(angle) |
123 | 98 d1[0], d1[2] = ([cos_a * d1[0][i] + sin_a * d1[2][i] for i in range(4)], |
99 [- sin_a * d1[0][i] + cos_a * d1[2][i] for i in range(4)]) | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
100 |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
101 |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
412
diff
changeset
|
102 cpdef rotate_z(self, angle): |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
103 d1 = self.data |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
104 cos_a = cos(angle) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
105 sin_a = sin(angle) |
123 | 106 d1[0], d1[1] = ([cos_a * d1[0][i] - sin_a * d1[1][i] for i in range(4)], |
107 [sin_a * d1[0][i] + cos_a * d1[1][i] for i in range(4)]) |