comparison pytouhou/ui/opengl/background.pyx @ 558:94725968dabb

Use vertex array objects, to be compatible with OpenGL 3.1+ core profile.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 12 Dec 2013 13:47:17 +0100
parents dacdcca59b66
children c759b97f4f81
comparison
equal deleted inserted replaced
557:0f2af7552462 558:94725968dabb
19 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc, 19 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc,
20 glBindTexture, glBindBuffer, glBufferData, GL_ARRAY_BUFFER, 20 glBindTexture, glBindBuffer, glBufferData, GL_ARRAY_BUFFER,
21 GL_STATIC_DRAW, GL_UNSIGNED_BYTE, GL_FLOAT, GL_SRC_ALPHA, 21 GL_STATIC_DRAW, GL_UNSIGNED_BYTE, GL_FLOAT, GL_SRC_ALPHA,
22 GL_ONE_MINUS_SRC_ALPHA, GL_TEXTURE_2D, glGenBuffers, glEnable, 22 GL_ONE_MINUS_SRC_ALPHA, GL_TEXTURE_2D, glGenBuffers, glEnable,
23 glDisable, GL_DEPTH_TEST, glDrawElements, GL_TRIANGLES, 23 glDisable, GL_DEPTH_TEST, glDrawElements, GL_TRIANGLES,
24 GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER) 24 GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER, glDeleteBuffers,
25 glGenVertexArrays, glDeleteVertexArrays, glBindVertexArray)
25 26
26 from .sprite cimport get_sprite_rendering_data 27 from .sprite cimport get_sprite_rendering_data
28 from .backend cimport use_vao
27 29
28 30
29 cdef class BackgroundRenderer: 31 cdef class BackgroundRenderer:
30 def __dealloc__(self): 32 def __dealloc__(self):
31 if self.vertex_buffer != NULL: 33 if self.use_fixed_pipeline:
32 free(self.vertex_buffer) 34 if self.vertex_buffer != NULL:
33 if self.indices != NULL: 35 free(self.vertex_buffer)
34 free(self.indices) 36 if self.indices != NULL:
37 free(self.indices)
38 else:
39 glDeleteBuffers(1, &self.vbo)
40 glDeleteBuffers(1, &self.ibo)
41
42 if use_vao:
43 glDeleteVertexArrays(1, &self.vao)
35 44
36 45
37 def __init__(self, use_fixed_pipeline): 46 def __init__(self, use_fixed_pipeline):
38 self.use_fixed_pipeline = use_fixed_pipeline 47 self.use_fixed_pipeline = use_fixed_pipeline
39 48
40 if not use_fixed_pipeline: 49 if not use_fixed_pipeline:
41 glGenBuffers(1, &self.vbo) 50 glGenBuffers(1, &self.vbo)
42 glGenBuffers(1, &self.ibo) 51 glGenBuffers(1, &self.ibo)
43 52
53 if use_vao:
54 glGenVertexArrays(1, &self.vao)
55
56 glBindVertexArray(self.vao)
57 self.set_state()
58 glBindVertexArray(0)
59
60
61 cdef void set_state(self) nogil:
62 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
63 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo)
64
65 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
66 glVertexAttribPointer(0, 3, GL_FLOAT, False, sizeof(Vertex), <void*>0)
67 glEnableVertexAttribArray(0)
68 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12)
69 glEnableVertexAttribArray(1)
70 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20)
71 glEnableVertexAttribArray(2)
72
44 73
45 cdef void render_background(self): 74 cdef void render_background(self):
46 if self.use_fixed_pipeline: 75 if self.use_fixed_pipeline:
47 indices = self.indices 76 indices = self.indices
48 glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x) 77 glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x)
49 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u) 78 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u)
50 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r) 79 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r)
51 else: 80 else:
52 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
53 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo)
54 indices = NULL 81 indices = NULL
55 82 if use_vao:
56 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. 83 glBindVertexArray(self.vao)
57 glVertexAttribPointer(0, 3, GL_FLOAT, False, sizeof(Vertex), <void*>0) 84 else:
58 glEnableVertexAttribArray(0) 85 self.set_state()
59 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12)
60 glEnableVertexAttribArray(1)
61 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20)
62 glEnableVertexAttribArray(2)
63 86
64 glEnable(GL_DEPTH_TEST) 87 glEnable(GL_DEPTH_TEST)
65 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 88 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
66 glBindTexture(GL_TEXTURE_2D, self.texture) 89 glBindTexture(GL_TEXTURE_2D, self.texture)
67 glDrawElements(GL_TRIANGLES, self.nb_indices, GL_UNSIGNED_SHORT, indices) 90 glDrawElements(GL_TRIANGLES, self.nb_indices, GL_UNSIGNED_SHORT, indices)
68 glDisable(GL_DEPTH_TEST) 91 glDisable(GL_DEPTH_TEST)
69 92
70 if not self.use_fixed_pipeline: 93 if not self.use_fixed_pipeline:
71 glBindBuffer(GL_ARRAY_BUFFER, 0) 94 if use_vao:
72 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) 95 glBindVertexArray(0)
96 else:
97 glBindBuffer(GL_ARRAY_BUFFER, 0)
98 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
73 99
74 100
75 cdef void load(self, background, Renderer renderer): 101 cdef void load(self, background, Renderer renderer):
76 cdef float ox, oy, oz, ox2, oy2, oz2 102 cdef float ox, oy, oz, ox2, oy2, oz2
77 cdef GLsizei nb_vertices = 0, nb_indices = 0 103 cdef GLsizei nb_vertices = 0, nb_indices = 0