Mercurial > touhou
comparison pytouhou/ui/opengl/renderer.pyx @ 520:c0b3f8709f74
Store the indices of the framebuffer in a static ibo.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 12 Dec 2013 13:15:43 +0100 |
parents | 75ae628522c9 |
children | dacdcca59b66 |
comparison
equal
deleted
inserted
replaced
519:b18f0bd30ad0 | 520:c0b3f8709f74 |
---|---|
29 glFramebufferTexture2D, glFramebufferRenderbuffer, | 29 glFramebufferTexture2D, glFramebufferRenderbuffer, |
30 glCheckFramebufferStatus, GL_FRAMEBUFFER, GL_TEXTURE_MIN_FILTER, | 30 glCheckFramebufferStatus, GL_FRAMEBUFFER, GL_TEXTURE_MIN_FILTER, |
31 GL_LINEAR, GL_TEXTURE_MAG_FILTER, GL_RGBA, GL_RENDERBUFFER, | 31 GL_LINEAR, GL_TEXTURE_MAG_FILTER, GL_RGBA, GL_RENDERBUFFER, |
32 GL_DEPTH_COMPONENT, GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT, | 32 GL_DEPTH_COMPONENT, GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT, |
33 GL_FRAMEBUFFER_COMPLETE, glClear, GL_COLOR_BUFFER_BIT, | 33 GL_FRAMEBUFFER_COMPLETE, glClear, GL_COLOR_BUFFER_BIT, |
34 GL_DEPTH_BUFFER_BIT, GLuint, glDeleteTextures) | 34 GL_DEPTH_BUFFER_BIT, GLuint, glDeleteTextures, |
35 GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW) | |
35 | 36 |
36 from pytouhou.lib.sdl import SDLError | 37 from pytouhou.lib.sdl import SDLError |
37 | 38 |
38 from pytouhou.game.element cimport Element | 39 from pytouhou.game.element cimport Element |
39 from .sprite cimport get_sprite_rendering_data | 40 from .sprite cimport get_sprite_rendering_data |
91 glDeleteBuffers(1, &self.framebuffer_vbo) | 92 glDeleteBuffers(1, &self.framebuffer_vbo) |
92 glDeleteBuffers(1, &self.vbo) | 93 glDeleteBuffers(1, &self.vbo) |
93 | 94 |
94 | 95 |
95 def __init__(self, resource_loader): | 96 def __init__(self, resource_loader): |
97 # Only used in modern GL. | |
98 cdef unsigned short framebuffer_indices[6] | |
99 | |
96 self.texture_manager = TextureManager(resource_loader, self, Texture) | 100 self.texture_manager = TextureManager(resource_loader, self, Texture) |
97 font_name = join(resource_loader.game_dir, 'font.ttf') | 101 font_name = join(resource_loader.game_dir, 'font.ttf') |
98 try: | 102 try: |
99 self.font_manager = FontManager(font_name, 16, self, Texture) | 103 self.font_manager = FontManager(font_name, 16, self, Texture) |
100 except SDLError: | 104 except SDLError: |
101 self.font_manager = None | 105 self.font_manager = None |
102 logger.error('Font file ā%sā not found, disabling text rendering altogether.', font_name) | 106 logger.error('Font file ā%sā not found, disabling text rendering altogether.', font_name) |
103 | 107 |
104 if not self.use_fixed_pipeline: | 108 if not self.use_fixed_pipeline: |
109 framebuffer_indices[:] = [0, 1, 2, 2, 3, 0] | |
110 | |
105 glGenBuffers(1, &self.vbo) | 111 glGenBuffers(1, &self.vbo) |
106 glGenBuffers(1, &self.framebuffer_vbo) | 112 glGenBuffers(1, &self.framebuffer_vbo) |
113 glGenBuffers(1, &self.framebuffer_ibo) | |
114 | |
115 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.framebuffer_ibo) | |
116 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(framebuffer_indices), framebuffer_indices, GL_STATIC_DRAW) | |
117 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) | |
107 | 118 |
108 | 119 |
109 cdef void render_elements(self, elements): | 120 cdef void render_elements(self, elements): |
110 cdef int key | 121 cdef int key |
111 cdef short x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, ox, oy | 122 cdef short x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, ox, oy |
234 glBindBuffer(GL_ARRAY_BUFFER, 0) | 245 glBindBuffer(GL_ARRAY_BUFFER, 0) |
235 | 246 |
236 | 247 |
237 cdef void render_framebuffer(self, Framebuffer fb): | 248 cdef void render_framebuffer(self, Framebuffer fb): |
238 cdef PassthroughVertex[4] buf | 249 cdef PassthroughVertex[4] buf |
239 cdef unsigned short indices[6] | |
240 indices[:] = [0, 1, 2, 2, 3, 0] | |
241 | 250 |
242 assert not self.use_fixed_pipeline | 251 assert not self.use_fixed_pipeline |
243 | 252 |
244 glBindFramebuffer(GL_FRAMEBUFFER, 0) | 253 glBindFramebuffer(GL_FRAMEBUFFER, 0) |
245 glViewport(self.x, self.y, self.width, self.height) | 254 glViewport(self.x, self.y, self.width, self.height) |
246 glBlendFunc(GL_ONE, GL_ZERO) | 255 glBlendFunc(GL_ONE, GL_ZERO) |
247 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | 256 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
248 | 257 |
249 glBindBuffer(GL_ARRAY_BUFFER, self.framebuffer_vbo) | 258 glBindBuffer(GL_ARRAY_BUFFER, self.framebuffer_vbo) |
259 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.framebuffer_ibo) | |
250 | 260 |
251 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. | 261 #TODO: find a way to use offsetof() instead of those ugly hardcoded values. |
252 glVertexAttribPointer(0, 2, GL_SHORT, False, sizeof(PassthroughVertex), <void*>0) | 262 glVertexAttribPointer(0, 2, GL_SHORT, False, sizeof(PassthroughVertex), <void*>0) |
253 glEnableVertexAttribArray(0) | 263 glEnableVertexAttribArray(0) |
254 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(PassthroughVertex), <void*>4) | 264 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(PassthroughVertex), <void*>4) |
259 buf[2] = PassthroughVertex(fb.x + fb.width, fb.y + fb.height, 1, 0) | 269 buf[2] = PassthroughVertex(fb.x + fb.width, fb.y + fb.height, 1, 0) |
260 buf[3] = PassthroughVertex(fb.x, fb.y + fb.height, 0, 0) | 270 buf[3] = PassthroughVertex(fb.x, fb.y + fb.height, 0, 0) |
261 glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(PassthroughVertex), buf, GL_DYNAMIC_DRAW) | 271 glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(PassthroughVertex), buf, GL_DYNAMIC_DRAW) |
262 | 272 |
263 glBindTexture(GL_TEXTURE_2D, fb.texture) | 273 glBindTexture(GL_TEXTURE_2D, fb.texture) |
264 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices) | 274 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL) |
265 glBindTexture(GL_TEXTURE_2D, 0) | 275 glBindTexture(GL_TEXTURE_2D, 0) |
266 | 276 |
267 glBindBuffer(GL_ARRAY_BUFFER, 0) | 277 glBindBuffer(GL_ARRAY_BUFFER, 0) |
278 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) | |
268 | 279 |
269 | 280 |
270 cdef class Framebuffer: | 281 cdef class Framebuffer: |
271 def __init__(self, int x, int y, int width, int height): | 282 def __init__(self, int x, int y, int width, int height): |
272 self.x = x | 283 self.x = x |