comparison pytouhou/ui/renderer.pyx @ 412:5fe6cd6ceb48

Refactor the maths functions out of Renderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 12 Jun 2013 18:30:08 +0200
parents 2428296cccab
children d8630c086926
comparison
equal deleted inserted replaced
411:2428296cccab 412:5fe6cd6ceb48
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, realloc 15 from libc.stdlib cimport malloc, free, realloc
16 from libc.math cimport tan
17 from math import radians
18 from itertools import chain 16 from itertools import chain
19 17
20 import ctypes 18 import ctypes
21 19
22 from struct import pack 20 from struct import pack
30 GL_ONE, GL_TEXTURE_2D, GL_TRIANGLES, 28 GL_ONE, GL_TEXTURE_2D, GL_TRIANGLES,
31 glEnable, glDisable, GL_DEPTH_TEST, glDrawArrays, GL_QUADS) 29 glEnable, glDisable, GL_DEPTH_TEST, glDrawArrays, GL_QUADS)
32 30
33 from .sprite cimport get_sprite_rendering_data 31 from .sprite cimport get_sprite_rendering_data
34 from .texture cimport TextureManager 32 from .texture cimport TextureManager
35 from pytouhou.utils.matrix cimport Matrix
36 from pytouhou.utils.vector import Vector, normalize, cross, dot
37 33
38 34
39 MAX_ELEMENTS = 640*4*3 35 MAX_ELEMENTS = 640*4*3
40 36
41 37
170 166
171 if not self.use_fixed_pipeline: 167 if not self.use_fixed_pipeline:
172 glBindBuffer(GL_ARRAY_BUFFER, self.back_vbo) 168 glBindBuffer(GL_ARRAY_BUFFER, self.back_vbo)
173 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(VertexFloat), <long> &self.background_vertex_buffer[0], GL_STATIC_DRAW) 169 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(VertexFloat), <long> &self.background_vertex_buffer[0], GL_STATIC_DRAW)
174 glBindBuffer(GL_ARRAY_BUFFER, 0) 170 glBindBuffer(GL_ARRAY_BUFFER, 0)
175
176
177 cpdef ortho_2d(self, left, right, bottom, top):
178 mat = Matrix()
179 data = mat.data
180 data[0][0] = 2 / (right - left)
181 data[1][1] = 2 / (top - bottom)
182 data[2][2] = -1
183 data[3][0] = -(right + left) / (right - left)
184 data[3][1] = -(top + bottom) / (top - bottom)
185 return mat
186
187
188 cpdef look_at(self, eye, center, up):
189 eye = Vector(eye)
190 center = Vector(center)
191 up = Vector(up)
192
193 f = normalize(center - eye)
194 u = normalize(up)
195 s = normalize(cross(f, u))
196 u = cross(s, f)
197
198 return Matrix([[s[0], u[0], -f[0], 0],
199 [s[1], u[1], -f[1], 0],
200 [s[2], u[2], -f[2], 0],
201 [-dot(s, eye), -dot(u, eye), dot(f, eye), 1]])
202
203
204 cpdef perspective(self, fovy, aspect, z_near, z_far):
205 top = tan(radians(fovy / 2)) * z_near
206 bottom = -top
207 left = -top * aspect
208 right = top * aspect
209
210 mat = Matrix()
211 data = mat.data
212 data[0][0] = (2 * z_near) / (right - left)
213 data[1][1] = (2 * z_near) / (top - bottom)
214 data[2][2] = -(z_far + z_near) / (z_far - z_near)
215 data[2][3] = -1
216 data[3][2] = -(2 * z_far * z_near) / (z_far - z_near)
217 data[3][3] = 0
218 return mat
219
220
221 cpdef setup_camera(self, dx, dy, dz):
222 # Some explanations on the magic constants:
223 # 192. = 384. / 2. = width / 2.
224 # 224. = 448. / 2. = height / 2.
225 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
226 # This is so that objects on the (O, x, y) plane use pixel coordinates
227 return self.look_at((192., 224., - 835.979370 * dz),
228 (192. + dx, 224. - dy, 0.), (0., -1., 0.))
229