Mercurial > touhou
comparison pytouhou/ui/opengl/renderer.pyx @ 627:ec972eb44391
Use a specific vbo/vao pair for text rendering.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 05 Mar 2015 20:36:41 +0100 |
parents | a6af3ff86612 |
children |
comparison
equal
deleted
inserted
replaced
626:13789ac717c4 | 627:ec972eb44391 |
---|---|
88 if not is_legacy: | 88 if not is_legacy: |
89 glDeleteBuffers(1, &self.vbo) | 89 glDeleteBuffers(1, &self.vbo) |
90 | 90 |
91 if use_vao: | 91 if use_vao: |
92 glDeleteVertexArrays(1, &self.vao) | 92 glDeleteVertexArrays(1, &self.vao) |
93 glDeleteVertexArrays(1, &self.text_vao) | |
93 | 94 |
94 | 95 |
95 def __init__(self, resource_loader): | 96 def __init__(self, resource_loader): |
96 self.texture_manager = TextureManager(resource_loader, self, Texture) | 97 self.texture_manager = TextureManager(resource_loader, self, Texture) |
97 font_name = join(resource_loader.game_dir, 'font.ttf') | 98 font_name = join(resource_loader.game_dir, 'font.ttf') |
104 if not is_legacy: | 105 if not is_legacy: |
105 if use_debug_group: | 106 if use_debug_group: |
106 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Renderer creation") | 107 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Renderer creation") |
107 | 108 |
108 glGenBuffers(1, &self.vbo) | 109 glGenBuffers(1, &self.vbo) |
110 glGenBuffers(1, &self.text_vbo) | |
109 | 111 |
110 if use_vao: | 112 if use_vao: |
111 glGenVertexArrays(1, &self.vao) | 113 glGenVertexArrays(1, &self.vao) |
112 glBindVertexArray(self.vao) | 114 glBindVertexArray(self.vao) |
113 self.set_state() | 115 self.set_state() |
116 | |
117 glGenVertexArrays(1, &self.text_vao) | |
118 glBindVertexArray(self.text_vao) | |
119 self.set_text_state() | |
114 | 120 |
115 if use_debug_group: | 121 if use_debug_group: |
116 glPopDebugGroup() | 122 glPopDebugGroup() |
117 | 123 |
118 | 124 |
123 glVertexAttribPointer(0, 3, GL_SHORT, False, sizeof(Vertex), <void*>0) | 129 glVertexAttribPointer(0, 3, GL_SHORT, False, sizeof(Vertex), <void*>0) |
124 glEnableVertexAttribArray(0) | 130 glEnableVertexAttribArray(0) |
125 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>8) | 131 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>8) |
126 glEnableVertexAttribArray(1) | 132 glEnableVertexAttribArray(1) |
127 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>16) | 133 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>16) |
134 glEnableVertexAttribArray(2) | |
135 | |
136 glBindBuffer(GL_ARRAY_BUFFER, 0) | |
137 | |
138 | |
139 cdef void set_text_state(self) nogil: | |
140 glBindBuffer(GL_ARRAY_BUFFER, self.text_vbo) | |
141 | |
142 #TODO: find a way to use offsetof() instead of those ugly substractions. | |
143 glVertexAttribPointer(0, 2, GL_SHORT, False, sizeof(Vertex), <void*>0) | |
144 glEnableVertexAttribArray(0) | |
145 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>4) | |
146 glEnableVertexAttribArray(1) | |
147 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>12) | |
128 glEnableVertexAttribArray(2) | 148 glEnableVertexAttribArray(2) |
129 | 149 |
130 glBindBuffer(GL_ARRAY_BUFFER, 0) | 150 glBindBuffer(GL_ARRAY_BUFFER, 0) |
131 | 151 |
132 | 152 |
232 glPopDebugGroup() | 252 glPopDebugGroup() |
233 | 253 |
234 | 254 |
235 cdef bint render_quads(self, rects, colors, GLuint texture) except True: | 255 cdef bint render_quads(self, rects, colors, GLuint texture) except True: |
236 # There is nothing that batch more than two quads on the same texture, currently. | 256 # There is nothing that batch more than two quads on the same texture, currently. |
237 cdef Vertex buf[8] | 257 cdef TextVertex buf[8] |
238 cdef unsigned short indices[12] | 258 cdef unsigned short indices[12] |
239 | 259 |
240 if not is_legacy: | 260 if not is_legacy: |
241 if use_primitive_restart: | 261 if use_primitive_restart: |
242 indices[:] = [0, 1, 2, 3, 0xffff, 4, 5, 6, 7, 0, 0, 0] | 262 indices[:] = [0, 1, 2, 3, 0xffff, 4, 5, 6, 7, 0, 0, 0] |
247 assert length == len(colors) | 267 assert length == len(colors) |
248 | 268 |
249 for i, r in enumerate(rects): | 269 for i, r in enumerate(rects): |
250 c1, c2, c3, c4 = colors[i] | 270 c1, c2, c3, c4 = colors[i] |
251 | 271 |
252 buf[4*i] = Vertex(r.x, r.y, 0, 0, 0, 0, c1.r, c1.g, c1.b, c1.a) | 272 buf[4*i] = TextVertex(r.x, r.y, 0, 0, c1.r, c1.g, c1.b, c1.a) |
253 buf[4*i+1] = Vertex(r.x + r.w, r.y, 0, 0, 1, 0, c2.r, c2.g, c2.b, c2.a) | 273 buf[4*i+1] = TextVertex(r.x + r.w, r.y, 1, 0, c2.r, c2.g, c2.b, c2.a) |
254 buf[4*i+2] = Vertex(r.x + r.w, r.y + r.h, 0, 0, 1, 1, c3.r, c3.g, c3.b, c3.a) | 274 buf[4*i+2] = TextVertex(r.x + r.w, r.y + r.h, 1, 1, c3.r, c3.g, c3.b, c3.a) |
255 buf[4*i+3] = Vertex(r.x, r.y + r.h, 0, 0, 0, 1, c4.r, c4.g, c4.b, c4.a) | 275 buf[4*i+3] = TextVertex(r.x, r.y + r.h, 0, 1, c4.r, c4.g, c4.b, c4.a) |
256 | 276 |
257 if use_debug_group: | 277 if use_debug_group: |
258 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Quads drawing") | 278 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Quads drawing") |
259 | 279 |
260 if is_legacy: | 280 if is_legacy: |
261 glVertexPointer(3, GL_SHORT, sizeof(Vertex), &buf[0].x) | 281 glVertexPointer(2, GL_SHORT, sizeof(TextVertex), &buf[0].x) |
262 glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &buf[0].u) | 282 glTexCoordPointer(2, GL_FLOAT, sizeof(TextVertex), &buf[0].u) |
263 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &buf[0].r) | 283 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(TextVertex), &buf[0].r) |
264 else: | 284 else: |
265 glBindBuffer(GL_ARRAY_BUFFER, self.vbo) | 285 glBindBuffer(GL_ARRAY_BUFFER, self.text_vbo) |
266 glBufferData(GL_ARRAY_BUFFER, 4 * length * sizeof(Vertex), buf, GL_DYNAMIC_DRAW) | 286 glBufferData(GL_ARRAY_BUFFER, 4 * length * sizeof(TextVertex), buf, GL_DYNAMIC_DRAW) |
267 glBindBuffer(GL_ARRAY_BUFFER, 0) | 287 glBindBuffer(GL_ARRAY_BUFFER, 0) |
268 | 288 |
269 if use_vao: | 289 if use_vao: |
270 glBindVertexArray(self.vao) | 290 glBindVertexArray(self.text_vao) |
271 else: | 291 else: |
272 self.set_state() | 292 self.set_text_state() |
273 | 293 |
274 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | 294 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) |
275 glBindTexture(GL_TEXTURE_2D, texture) | 295 glBindTexture(GL_TEXTURE_2D, texture) |
276 | 296 |
277 if is_legacy: | 297 if is_legacy: |