annotate pytouhou/ui/renderer.pyx @ 436:cb5c68598ab0

Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 07 Aug 2013 11:34:42 +0200
parents 878273a984c4
children d56536ef28e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
15 from libc.stdlib cimport malloc, free
384
690b5faaa0e6 Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 383
diff changeset
16 from itertools import chain
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 from struct import pack
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
20 from pytouhou.lib.opengl cimport \
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
21 (glVertexPointer, glTexCoordPointer, glColorPointer,
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
22 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc,
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
23 glBindTexture, glDrawElements, glBindBuffer, glBufferData,
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
24 GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE,
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
25 GL_UNSIGNED_SHORT, GL_INT, GL_FLOAT, GL_SRC_ALPHA,
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
26 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, GL_TRIANGLES,
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
27 glGenBuffers)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 from .sprite cimport get_sprite_rendering_data
426
5d7bb2fd74f7 Never keep texture on the host when it has been uploaded, and prevent them from being decoded again.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
30 from .texture import TextureManager
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32
301
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
33 MAX_ELEMENTS = 640*4*3
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 cdef class Renderer:
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 def __cinit__(self):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 # Allocate buffers
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 self.vertex_buffer = <Vertex*> malloc(MAX_ELEMENTS * sizeof(Vertex))
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 def __dealloc__(self):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 free(self.vertex_buffer)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 def __init__(self, resource_loader):
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 self.texture_manager = TextureManager(resource_loader)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
49 if not self.use_fixed_pipeline:
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
50 glGenBuffers(1, &self.vbo)
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
51
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 cpdef render_elements(self, elements):
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
54 cdef unsigned short nb_vertices = 0, nb_indices, *new_indices
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 indices_by_texture = {}
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57
384
690b5faaa0e6 Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 383
diff changeset
58 objects = chain(*[element.objects for element in elements])
690b5faaa0e6 Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 383
diff changeset
59 for element in objects:
301
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
60 if nb_vertices >= MAX_ELEMENTS - 4:
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
61 break
6f1ca1cb5238 Avoid segfaults in the unlikely case a huge number of sprites is rendered.
Thibaut Girka <thib@sitedethib.com>
parents: 245
diff changeset
62
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 301
diff changeset
63 sprite = element.sprite
245
d3ba32a9096e Implement ANM0 instruction 29 and fix 24
Thibaut Girka <thib@sitedethib.com>
parents: 223
diff changeset
64 if sprite and sprite.visible:
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 ox, oy = element.x, element.y
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 rec = indices_by_texture.setdefault(key, [])
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 # Pack data in buffer
435
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 426
diff changeset
70 x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = vertices
397
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
71 left, right, bottom, top = uvs
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
72 r, g, b, a = colors
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
73 self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, left, bottom, r, g, b, a)
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
74 self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, right, bottom, r, g, b, a)
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
75 self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, right, top, r, g, b, a)
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
76 self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, left, top, r, g, b, a)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 # Add indices
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 index = nb_vertices
371
6702bc0215dc Replace GL_QUADS with GL_TRIANGLES, to be GLES compatible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
80 rec.extend((index, index + 1, index + 2, index + 2, index + 3, index))
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 nb_vertices += 4
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83
410
9d790ca73c13 Don’t render null-sized arrays of elements.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
84 if nb_vertices == 0:
9d790ca73c13 Don’t render null-sized arrays of elements.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
85 return
9d790ca73c13 Don’t render null-sized arrays of elements.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
86
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
87 if self.use_fixed_pipeline:
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
88 glVertexPointer(3, GL_INT, sizeof(Vertex), &self.vertex_buffer[0].x)
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
89 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u)
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
90 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r)
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
91 else:
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
92 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
93 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), &self.vertex_buffer[0], GL_DYNAMIC_DRAW)
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
94
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
95 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
96 glVertexAttribPointer(0, 3, GL_INT, False, sizeof(Vertex), <void*>0)
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
97 glEnableVertexAttribArray(0)
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
98 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12)
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
99 glEnableVertexAttribArray(1)
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
100 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20)
395
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
101 glEnableVertexAttribArray(2)
43413d4ff05b Don’t change the vertex attributes for each texture.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
102
426
5d7bb2fd74f7 Never keep texture on the host when it has been uploaded, and prevent them from being decoded again.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
103 for (texture, blendfunc), indices in indices_by_texture.items():
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
104
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
105 #TODO: find a more elegent way.
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106 nb_indices = len(indices)
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
107 new_indices = <unsigned short*> malloc(nb_indices * sizeof(unsigned short))
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
108 for i in xrange(nb_indices):
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
109 new_indices[i] = indices[i]
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
110
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
111 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
426
5d7bb2fd74f7 Never keep texture on the host when it has been uploaded, and prevent them from being decoded again.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
112 glBindTexture(GL_TEXTURE_2D, texture)
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
113 glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, new_indices)
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
114 free(new_indices)
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
116 if not self.use_fixed_pipeline:
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 395
diff changeset
117 glBindBuffer(GL_ARRAY_BUFFER, 0)