Mercurial > touhou
comparison pytouhou/ui/renderer.pyx @ 370:74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 27 Jul 2012 18:43:48 +0200 |
parents | f3099ebf4f61 |
children | 6702bc0215dc |
comparison
equal
deleted
inserted
replaced
369:f305cdd6f6c5 | 370:74471afbac37 |
---|---|
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 libc.stdlib cimport malloc, free | 15 from libc.stdlib cimport malloc, free |
16 from libc.math cimport tan | |
17 from math import radians | |
16 | 18 |
17 import ctypes | 19 import ctypes |
18 | 20 |
19 from struct import pack | 21 from struct import pack |
20 | 22 |
21 from pyglet.gl import * | 23 from pyglet.gl import * |
22 | 24 |
23 from .sprite cimport get_sprite_rendering_data | 25 from .sprite cimport get_sprite_rendering_data |
24 from .texture cimport TextureManager | 26 from .texture cimport TextureManager |
27 from pytouhou.utils.matrix cimport Matrix | |
28 from pytouhou.utils.vector import Vector, normalize, cross, dot | |
25 | 29 |
26 | 30 |
27 MAX_ELEMENTS = 640*4*3 | 31 MAX_ELEMENTS = 640*4*3 |
28 | 32 |
29 | 33 |
81 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) | 85 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) |
82 glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key].id) | 86 glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key].id) |
83 glDrawElements(GL_QUADS, nb_indices, GL_UNSIGNED_SHORT, indices) | 87 glDrawElements(GL_QUADS, nb_indices, GL_UNSIGNED_SHORT, indices) |
84 | 88 |
85 | 89 |
90 cpdef ortho_2d(self, left, right, bottom, top): | |
91 mat = Matrix() | |
92 mat[0][0] = 2 / (right - left) | |
93 mat[1][1] = 2 / (top - bottom) | |
94 mat[2][2] = -1 | |
95 mat[3][0] = -(right + left) / (right - left) | |
96 mat[3][1] = -(top + bottom) / (top - bottom) | |
97 return mat | |
98 | |
99 | |
100 cpdef look_at(self, eye, center, up): | |
101 eye = Vector(eye) | |
102 center = Vector(center) | |
103 up = Vector(up) | |
104 | |
105 f = normalize(center - eye) | |
106 u = normalize(up) | |
107 s = normalize(cross(f, u)) | |
108 u = cross(s, f) | |
109 | |
110 return Matrix([[s[0], u[0], -f[0], 0], | |
111 [s[1], u[1], -f[1], 0], | |
112 [s[2], u[2], -f[2], 0], | |
113 [-dot(s, eye), -dot(u, eye), dot(f, eye), 1]]) | |
114 | |
115 | |
116 cpdef perspective(self, fovy, aspect, z_near, z_far): | |
117 top = tan(radians(fovy / 2)) * z_near | |
118 bottom = -top | |
119 left = -top * aspect | |
120 right = top * aspect | |
121 | |
122 mat = Matrix() | |
123 mat[0][0] = (2 * z_near) / (right - left) | |
124 mat[1][1] = (2 * z_near) / (top - bottom) | |
125 mat[2][2] = -(z_far + z_near) / (z_far - z_near) | |
126 mat[2][3] = -1 | |
127 mat[3][2] = -(2 * z_far * z_near) / (z_far - z_near) | |
128 mat[3][3] = 0 | |
129 return mat | |
130 | |
131 | |
86 cpdef setup_camera(self, dx, dy, dz): | 132 cpdef setup_camera(self, dx, dy, dz): |
87 glMatrixMode(GL_MODELVIEW) | 133 # Some explanations on the magic constants: |
88 glLoadIdentity() | 134 # 192. = 384. / 2. = width / 2. |
89 # Some explanations on the magic constants: | 135 # 224. = 448. / 2. = height / 2. |
90 # 192. = 384. / 2. = width / 2. | 136 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) |
91 # 224. = 448. / 2. = height / 2. | 137 # This is so that objects on the (O, x, y) plane use pixel coordinates |
92 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) | 138 return self.look_at((192., 224., - 835.979370 * dz), |
93 # This is so that objects on the (O, x, y) plane use pixel coordinates | 139 (192. + dx, 224. - dy, 0.), (0., -1., 0.)) |
94 gluLookAt(192., 224., - 835.979370 * dz, | |
95 192. + dx, 224. - dy, 0., 0., -1., 0.) | |
96 | 140 |