comparison pytouhou/ui/renderer.pyx @ 462:a71b912b45b7

Render to framebuffers first, and reposition some interface elements in the game area.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 11 Sep 2013 00:36:50 +0200
parents 6af3854ed826
children 36bc577b2392
comparison
equal deleted inserted replaced
461:6af3854ed826 462:a71b912b45b7
20 (glVertexPointer, glTexCoordPointer, glColorPointer, 20 (glVertexPointer, glTexCoordPointer, glColorPointer,
21 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc, 21 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc,
22 glBindTexture, glDrawElements, glBindBuffer, glBufferData, 22 glBindTexture, glDrawElements, glBindBuffer, glBufferData,
23 GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE, 23 GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE,
24 GL_UNSIGNED_SHORT, GL_INT, GL_FLOAT, GL_SRC_ALPHA, 24 GL_UNSIGNED_SHORT, GL_INT, GL_FLOAT, GL_SRC_ALPHA,
25 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, GL_TRIANGLES, 25 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO, GL_TEXTURE_2D, GL_TRIANGLES,
26 glGenBuffers) 26 glGenBuffers, glBindFramebuffer, glViewport, glDeleteBuffers,
27 glGenTextures, glTexParameteri, glTexImage2D, glGenRenderbuffers,
28 glBindRenderbuffer, glRenderbufferStorage, glGenFramebuffers,
29 glFramebufferTexture2D, glFramebufferRenderbuffer,
30 glCheckFramebufferStatus, GL_FRAMEBUFFER, GL_TEXTURE_MIN_FILTER,
31 GL_LINEAR, GL_TEXTURE_MAG_FILTER, GL_RGBA, GL_RENDERBUFFER,
32 GL_DEPTH_COMPONENT, GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT,
33 GL_FRAMEBUFFER_COMPLETE, glClear, GL_COLOR_BUFFER_BIT,
34 GL_DEPTH_BUFFER_BIT)
27 35
28 from pytouhou.lib.sdl import SDLError 36 from pytouhou.lib.sdl import SDLError
29 37
30 from pytouhou.game.element cimport Element 38 from pytouhou.game.element cimport Element
31 from .sprite cimport get_sprite_rendering_data 39 from .sprite cimport get_sprite_rendering_data
61 69
62 70
63 def __dealloc__(self): 71 def __dealloc__(self):
64 free(self.vertex_buffer) 72 free(self.vertex_buffer)
65 73
74 if not self.use_fixed_pipeline:
75 glDeleteBuffers(1, &self.framebuffer_vbo)
76 glDeleteBuffers(1, &self.vbo)
77
66 78
67 def __init__(self, resource_loader): 79 def __init__(self, resource_loader):
68 self.texture_manager = TextureManager(resource_loader, self) 80 self.texture_manager = TextureManager(resource_loader, self)
69 font_name = join(resource_loader.game_dir, 'font.ttf') 81 font_name = join(resource_loader.game_dir, 'font.ttf')
70 try: 82 try:
73 self.font_manager = None 85 self.font_manager = None
74 logger.error('Font file ā€œ%sā€ not found, disabling text rendering altogether.', font_name) 86 logger.error('Font file ā€œ%sā€ not found, disabling text rendering altogether.', font_name)
75 87
76 if not self.use_fixed_pipeline: 88 if not self.use_fixed_pipeline:
77 glGenBuffers(1, &self.vbo) 89 glGenBuffers(1, &self.vbo)
90 glGenBuffers(1, &self.framebuffer_vbo)
78 91
79 92
80 def add_texture(self, int texture): 93 def add_texture(self, int texture):
81 for i in xrange(2): 94 for i in xrange(2):
82 self.indices[i][texture] = <unsigned short*> malloc(65536 * sizeof(unsigned short)) 95 self.indices[i][texture] = <unsigned short*> malloc(65536 * sizeof(unsigned short))
200 glBindTexture(GL_TEXTURE_2D, texture) 213 glBindTexture(GL_TEXTURE_2D, texture)
201 glDrawElements(GL_TRIANGLES, 6 * length, GL_UNSIGNED_SHORT, indices) 214 glDrawElements(GL_TRIANGLES, 6 * length, GL_UNSIGNED_SHORT, indices)
202 215
203 if not self.use_fixed_pipeline: 216 if not self.use_fixed_pipeline:
204 glBindBuffer(GL_ARRAY_BUFFER, 0) 217 glBindBuffer(GL_ARRAY_BUFFER, 0)
218
219
220 cpdef render_framebuffer(self, Framebuffer fb):
221 cdef PassthroughVertex[4] buf
222 cdef unsigned short indices[6]
223 indices[:] = [0, 1, 2, 2, 3, 0]
224
225 assert not self.use_fixed_pipeline
226
227 glBindFramebuffer(GL_FRAMEBUFFER, 0)
228 glViewport(0, 0, 640, 480)
229 glBlendFunc(GL_ONE, GL_ZERO)
230 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
231
232 glBindBuffer(GL_ARRAY_BUFFER, self.framebuffer_vbo)
233
234 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
235 glVertexAttribPointer(0, 2, GL_INT, False, sizeof(PassthroughVertex), <void*>0)
236 glEnableVertexAttribArray(0)
237 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(PassthroughVertex), <void*>8)
238 glEnableVertexAttribArray(1)
239
240 buf[0] = PassthroughVertex(fb.x, fb.y, 0, 1)
241 buf[1] = PassthroughVertex(fb.x + fb.width, fb.y, 1, 1)
242 buf[2] = PassthroughVertex(fb.x + fb.width, fb.y + fb.height, 1, 0)
243 buf[3] = PassthroughVertex(fb.x, fb.y + fb.height, 0, 0)
244 glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(PassthroughVertex), buf, GL_DYNAMIC_DRAW)
245
246 glBindTexture(GL_TEXTURE_2D, fb.texture)
247 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices)
248 glBindTexture(GL_TEXTURE_2D, 0)
249
250 glBindBuffer(GL_ARRAY_BUFFER, 0)
251
252
253 cdef class Framebuffer:
254 def __init__(self, int x, int y, int width, int height):
255 self.x = x
256 self.y = y
257 self.width = width
258 self.height = height
259
260 glGenTextures(1, &self.texture)
261 glBindTexture(GL_TEXTURE_2D, self.texture)
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
264 glTexImage2D(GL_TEXTURE_2D, 0,
265 GL_RGBA,
266 width, height,
267 0,
268 GL_RGBA, GL_UNSIGNED_BYTE,
269 NULL)
270 glBindTexture(GL_TEXTURE_2D, 0)
271
272 glGenRenderbuffers(1, &self.rbo)
273 glBindRenderbuffer(GL_RENDERBUFFER, self.rbo)
274 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height)
275 glBindRenderbuffer(GL_RENDERBUFFER, 0)
276
277 glGenFramebuffers(1, &self.fbo)
278 glBindFramebuffer(GL_FRAMEBUFFER, self.fbo)
279 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture, 0)
280 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, self.rbo)
281 assert glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE
282 glBindFramebuffer(GL_FRAMEBUFFER, 0)
283
284 cpdef bind(self):
285 glBindFramebuffer(GL_FRAMEBUFFER, self.fbo)