comparison pytouhou/ui/opengl/background.pyx @ 611:a6a191e371c7

Add back a GL_QUADS path for legacy applications.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 21 Dec 2014 19:15:59 +0100
parents 3c2f96f1d715
children a6af3ff86612
comparison
equal deleted inserted replaced
610:1b31169dc344 611:a6a191e371c7
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_UNSIGNED_SHORT, 23 glDisable, GL_DEPTH_TEST, glDrawElements, GL_UNSIGNED_SHORT,
24 GL_ELEMENT_ARRAY_BUFFER, glDeleteBuffers, glGenVertexArrays, 24 GL_ELEMENT_ARRAY_BUFFER, glDeleteBuffers, glGenVertexArrays,
25 glDeleteVertexArrays, glBindVertexArray, glPushDebugGroup, 25 glDeleteVertexArrays, glBindVertexArray, glPushDebugGroup,
26 GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup) 26 GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup, glDrawArrays)
27 27
28 from .sprite cimport get_sprite_rendering_data 28 from .sprite cimport get_sprite_rendering_data
29 from .backend cimport primitive_mode, is_legacy, use_debug_group, use_vao, use_primitive_restart 29 from .backend cimport primitive_mode, is_legacy, use_debug_group, use_vao, use_primitive_restart
30 30
31 31
32 cdef class BackgroundRenderer: 32 cdef class BackgroundRenderer:
33 def __dealloc__(self): 33 def __dealloc__(self):
34 if is_legacy: 34 if is_legacy:
35 if self.vertex_buffer != NULL: 35 if self.vertex_buffer != NULL:
36 free(self.vertex_buffer) 36 free(self.vertex_buffer)
37 if self.indices != NULL:
38 free(self.indices)
39 else: 37 else:
40 glDeleteBuffers(1, &self.vbo) 38 glDeleteBuffers(1, &self.vbo)
41 glDeleteBuffers(1, &self.ibo) 39 glDeleteBuffers(1, &self.ibo)
42 40
43 if use_vao: 41 if use_vao:
78 cdef void render_background(self) nogil: 76 cdef void render_background(self) nogil:
79 if use_debug_group: 77 if use_debug_group:
80 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Background drawing") 78 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Background drawing")
81 79
82 if is_legacy: 80 if is_legacy:
83 indices = self.indices
84 glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x) 81 glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x)
85 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u) 82 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u)
86 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r) 83 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r)
87 else: 84 else:
88 indices = NULL
89 if use_vao: 85 if use_vao:
90 glBindVertexArray(self.vao) 86 glBindVertexArray(self.vao)
91 else: 87 else:
92 self.set_state() 88 self.set_state()
93 89
94 glEnable(GL_DEPTH_TEST) 90 glEnable(GL_DEPTH_TEST)
95 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 91 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
96 glBindTexture(GL_TEXTURE_2D, self.texture) 92 glBindTexture(GL_TEXTURE_2D, self.texture)
97 glDrawElements(primitive_mode, self.nb_indices, GL_UNSIGNED_SHORT, indices) 93 if is_legacy:
94 glDrawArrays(primitive_mode, 0, self.nb_vertices)
95 else:
96 glDrawElements(primitive_mode, self.nb_indices, GL_UNSIGNED_SHORT, NULL)
98 glDisable(GL_DEPTH_TEST) 97 glDisable(GL_DEPTH_TEST)
99 98
100 if not is_legacy: 99 if not is_legacy:
101 if use_vao: 100 if use_vao:
102 glBindVertexArray(0) 101 glBindVertexArray(0)
111 cdef void load(self, background, GLuint[MAX_TEXTURES] textures) except *: 110 cdef void load(self, background, GLuint[MAX_TEXTURES] textures) except *:
112 cdef float ox, oy, oz, ox2, oy2, oz2 111 cdef float ox, oy, oz, ox2, oy2, oz2
113 cdef GLsizei nb_vertices = 0, nb_indices = 0 112 cdef GLsizei nb_vertices = 0, nb_indices = 0
114 113
115 vertex_buffer = <Vertex*> malloc(65536 * sizeof(Vertex)) 114 vertex_buffer = <Vertex*> malloc(65536 * sizeof(Vertex))
116 indices = <GLushort*> malloc(65536 * sizeof(GLushort)) 115
116 if not is_legacy:
117 indices = <GLushort*> malloc(65536 * sizeof(GLushort))
117 118
118 for ox, oy, oz, model_id, model in background.object_instances: 119 for ox, oy, oz, model_id, model in background.object_instances:
119 for ox2, oy2, oz2, width_override, height_override, sprite in model: 120 for ox2, oy2, oz2, width_override, height_override, sprite in model:
120 #TODO: view frustum culling 121 #TODO: view frustum culling
121 data = get_sprite_rendering_data(sprite) 122 data = get_sprite_rendering_data(sprite)
125 r, g, b, a = data.color[0], data.color[1], data.color[2], data.color[3] 126 r, g, b, a = data.color[0], data.color[1], data.color[2], data.color[3]
126 127
127 # Pack data 128 # Pack data
128 vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, data.left, data.bottom, r, g, b, a) 129 vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, data.left, data.bottom, r, g, b, a)
129 vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, data.right, data.bottom, r, g, b, a) 130 vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, data.right, data.bottom, r, g, b, a)
130 vertex_buffer[nb_vertices+2] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, data.left, data.top, r, g, b, a) 131 vertex_buffer[nb_vertices+2] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, data.right, data.top, r, g, b, a)
131 vertex_buffer[nb_vertices+3] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, data.right, data.top, r, g, b, a) 132 vertex_buffer[nb_vertices+3] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, data.left, data.top, r, g, b, a)
132 133
133 # Add indices 134 if not is_legacy:
134 if use_primitive_restart: 135 # Add indices
135 indices[nb_indices] = nb_vertices 136 if use_primitive_restart:
136 indices[nb_indices+1] = nb_vertices + 1 137 indices[nb_indices] = nb_vertices
137 indices[nb_indices+2] = nb_vertices + 2 138 indices[nb_indices+1] = nb_vertices + 1
138 indices[nb_indices+3] = nb_vertices + 3 139 indices[nb_indices+2] = nb_vertices + 3
139 indices[nb_indices+4] = 0xFFFF 140 indices[nb_indices+3] = nb_vertices + 2
140 else: 141 indices[nb_indices+4] = 0xFFFF
141 indices[nb_indices] = nb_vertices 142 else:
142 indices[nb_indices+1] = nb_vertices + 1 143 indices[nb_indices] = nb_vertices
143 indices[nb_indices+2] = nb_vertices + 2 144 indices[nb_indices+1] = nb_vertices + 1
144 indices[nb_indices+3] = nb_vertices + 1 145 indices[nb_indices+2] = nb_vertices + 3
145 indices[nb_indices+4] = nb_vertices + 2 146 indices[nb_indices+3] = nb_vertices + 1
146 indices[nb_indices+5] = nb_vertices + 3 147 indices[nb_indices+4] = nb_vertices + 2
148 indices[nb_indices+5] = nb_vertices + 3
149
150 nb_indices += 5 if use_primitive_restart else 6
147 151
148 nb_vertices += 4 152 nb_vertices += 4
149 nb_indices += 5 if use_primitive_restart else 6 153
154 self.texture = textures[key >> 1]
150 155
151 # We only need to keep the rendered vertices and indices in memory, 156 # We only need to keep the rendered vertices and indices in memory,
152 # either in RAM or in VRAM, they will never change until we implement 157 # either in RAM or in VRAM, they will never change until we implement
153 # background animation. 158 # background animation.
154 159
155 self.texture = textures[key >> 1]
156 self.nb_indices = nb_indices
157
158 if is_legacy: 160 if is_legacy:
159 self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex)) 161 self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex))
160 self.indices = <GLushort*> realloc(indices, nb_indices * sizeof(GLushort)) 162 self.nb_vertices = nb_vertices
161 else: 163 else:
162 if use_debug_group: 164 if use_debug_group:
163 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Background uploading") 165 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Background uploading")
164 166
165 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) 167 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
168 170
169 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo) 171 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo)
170 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nb_indices * sizeof(GLushort), indices, GL_STATIC_DRAW) 172 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nb_indices * sizeof(GLushort), indices, GL_STATIC_DRAW)
171 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) 173 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
172 174
175 self.nb_indices = nb_indices
176
173 if use_debug_group: 177 if use_debug_group:
174 glPopDebugGroup() 178 glPopDebugGroup()