comparison pytouhou/ui/renderer.pyx @ 396:34a91f918e7c

Use Buffer Objects instead of host pointers.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 06 Feb 2013 21:19:20 +0100
parents 43413d4ff05b
children c5ba11ede097
comparison
equal deleted inserted replaced
395:43413d4ff05b 396:34a91f918e7c
22 from struct import pack 22 from struct import pack
23 23
24 from pyglet.gl import (glVertexPointer, glTexCoordPointer, glColorPointer, 24 from pyglet.gl import (glVertexPointer, glTexCoordPointer, glColorPointer,
25 glVertexAttribPointer, glEnableVertexAttribArray, 25 glVertexAttribPointer, glEnableVertexAttribArray,
26 glBlendFunc, glBindTexture, glDrawElements, 26 glBlendFunc, glBindTexture, glDrawElements,
27 GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_INT, GL_FLOAT, 27 glBindBuffer, glBufferData, GL_ARRAY_BUFFER,
28 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, 28 GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT,
29 GL_TEXTURE_2D, GL_TRIANGLES) 29 GL_INT, GL_FLOAT, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
30 GL_ONE, GL_TEXTURE_2D, GL_TRIANGLES)
30 31
31 from .sprite cimport get_sprite_rendering_data 32 from .sprite cimport get_sprite_rendering_data
32 from .texture cimport TextureManager 33 from .texture cimport TextureManager
33 from pytouhou.utils.matrix cimport Matrix 34 from pytouhou.utils.matrix cimport Matrix
34 from pytouhou.utils.vector import Vector, normalize, cross, dot 35 from pytouhou.utils.vector import Vector, normalize, cross, dot
85 if self.use_fixed_pipeline: 86 if self.use_fixed_pipeline:
86 glVertexPointer(3, GL_INT, sizeof(Vertex), <long> &self.vertex_buffer[0].x) 87 glVertexPointer(3, GL_INT, sizeof(Vertex), <long> &self.vertex_buffer[0].x)
87 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), <long> &self.vertex_buffer[0].u) 88 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), <long> &self.vertex_buffer[0].u)
88 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), <long> &self.vertex_buffer[0].r) 89 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), <long> &self.vertex_buffer[0].r)
89 else: 90 else:
90 glVertexAttribPointer(0, 3, GL_INT, False, sizeof(Vertex), <long> &self.vertex_buffer[0].x) 91 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
92 glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), <long> &self.vertex_buffer[0], GL_DYNAMIC_DRAW)
93
94 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
95 glVertexAttribPointer(0, 3, GL_INT, False, sizeof(Vertex), 0)
91 glEnableVertexAttribArray(0) 96 glEnableVertexAttribArray(0)
92 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <long> &self.vertex_buffer[0].u) 97 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), 12)
93 glEnableVertexAttribArray(1) 98 glEnableVertexAttribArray(1)
94 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <long> &self.vertex_buffer[0].r) 99 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), 20)
95 glEnableVertexAttribArray(2) 100 glEnableVertexAttribArray(2)
96 101
97 for (texture_key, blendfunc), indices in indices_by_texture.items(): 102 for (texture_key, blendfunc), indices in indices_by_texture.items():
98 nb_indices = len(indices) 103 nb_indices = len(indices)
99 indices = pack(str(nb_indices) + 'H', *indices) 104 indices = pack(str(nb_indices) + 'H', *indices)
100 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) 105 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
101 glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key]) 106 glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key])
102 glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, indices) 107 glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, indices)
108
109 if not self.use_fixed_pipeline:
110 glBindBuffer(GL_ARRAY_BUFFER, 0)
103 111
104 112
105 cpdef ortho_2d(self, left, right, bottom, top): 113 cpdef ortho_2d(self, left, right, bottom, top):
106 mat = Matrix() 114 mat = Matrix()
107 mat[0][0] = 2 / (right - left) 115 mat[0][0] = 2 / (right - left)