# HG changeset patch # User Thibaut Girka # Date 1324237668 -3600 # Node ID 5cac48b328ad2ab519cebcc10777c30df05d3f0d # Parent 5c3600e0f0cde4ec6e13547ed4b478ec0da4b15a Refactor rendering code a bit. Move duplicated camera setup code to a new “setup_camera” method, and move common methods to a new “Renderer” module in order to make individual sprite rendering easier. diff --git a/pytouhou/ui/gamerenderer.pyx b/pytouhou/ui/gamerenderer.pyx --- a/pytouhou/ui/gamerenderer.pyx +++ b/pytouhou/ui/gamerenderer.pyx @@ -12,92 +12,28 @@ ## GNU General Public License for more details. ## -from libc.stdlib cimport malloc, free -import ctypes - -from struct import pack from itertools import chain from pyglet.gl import * -from .texture import TextureManager -from .sprite cimport get_sprite_rendering_data +from .renderer cimport Renderer from .background import get_background_rendering_data -MAX_ELEMENTS = 10000 - -cdef struct Vertex: - int x, y, z - float u, v - unsigned char r, g, b, a - - -cdef class GameRenderer: - cdef public texture_manager +cdef class GameRenderer(Renderer): cdef public game cdef public background - cdef Vertex *vertex_buffer - - - def __cinit__(self): - # Allocate buffers - self.vertex_buffer = malloc(MAX_ELEMENTS * sizeof(Vertex)) - - - def __dealloc__(self): - free(self.vertex_buffer) - def __init__(self, resource_loader, game=None, background=None): - self.texture_manager = TextureManager(resource_loader) + Renderer.__init__(self, resource_loader) self.game = game self.background = background - cdef render_elements(self, elements): - cdef unsigned short nb_vertices = 0 - - indices_by_texture = {} - - for element in elements: - sprite = element._sprite - if sprite: - ox, oy = element.x, element.y - key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) - rec = indices_by_texture.setdefault(key, []) - - # Pack data in buffer - (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices - r1, g1, b1, a1, r2, g2, b2, a2, r3, g3, b3, a3, r4, g4, b4, a4 = colors - u1, v1, u2, v2, u3, v3, u4, v4 = uvs - self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, u1, v1, r1, g1, b1, a1) - self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, u2, v2, r2, g2, b2, a2) - self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, u3, v3, r3, g3, b3, a3) - self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, u4, v4, r4, g4, b4, a4) - - # Add indices - index = nb_vertices - rec.extend((index, index + 1, index + 2, index + 3)) - - nb_vertices += 4 - - for (texture_key, blendfunc), indices in indices_by_texture.items(): - glVertexPointer(3, GL_INT, 24, &self.vertex_buffer[0].x) - glTexCoordPointer(2, GL_FLOAT, 24, &self.vertex_buffer[0].u) - glColorPointer(4, GL_UNSIGNED_BYTE, 24, &self.vertex_buffer[0].r) - - nb_indices = len(indices) - indices = pack(str(nb_indices) + 'H', *indices) - glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) - glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key].id) - glDrawElements(GL_QUADS, nb_indices, GL_UNSIGNED_SHORT, indices) - - def render(self): glClear(GL_DEPTH_BUFFER_BIT) @@ -106,15 +42,7 @@ cdef class GameRenderer: texture_manager = self.texture_manager if game is not None and game.effect is not None: - glMatrixMode(GL_MODELVIEW) - glLoadIdentity() - # Some explanations on the magic constants: - # 192. = 384. / 2. = width / 2. - # 224. = 448. / 2. = height / 2. - # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) - # This is so that objects on the (O, x, y) plane use pixel coordinates - gluLookAt(192., 224., - 835.979370, - 192., 224., 0., 0., -1., 0.) + self.setup_camera(0, 0, 1) glDisable(GL_FOG) self.render_elements([game.effect]) @@ -129,15 +57,7 @@ cdef class GameRenderer: glFogf(GL_FOG_END, fog_end) glFogfv(GL_FOG_COLOR, (GLfloat * 4)(fog_r / 255., fog_g / 255., fog_b / 255., 1.)) - glMatrixMode(GL_MODELVIEW) - glLoadIdentity() - # Some explanations on the magic constants: - # 192. = 384. / 2. = width / 2. - # 224. = 448. / 2. = height / 2. - # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) - # This is so that objects on the (O, x, y) plane use pixel coordinates - gluLookAt(192., 224., - 835.979370 * dz, - 192. + dx, 224. - dy, 0., 0., -1., 0.) + self.setup_camera(dx, dy, dz) glTranslatef(-x, -y, -z) glEnable(GL_DEPTH_TEST) @@ -153,15 +73,7 @@ cdef class GameRenderer: glClear(GL_COLOR_BUFFER_BIT) if game is not None: - glMatrixMode(GL_MODELVIEW) - glLoadIdentity() - # Some explanations on the magic constants: - # 192. = 384. / 2. = width / 2. - # 224. = 448. / 2. = height / 2. - # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) - # This is so that objects on the (O, x, y) plane use pixel coordinates - gluLookAt(192., 224., - 835.979370, - 192., 224., 0., 0., -1., 0.) + self.setup_camera(0, 0, 1) glDisable(GL_FOG) self.render_elements(game.enemies) diff --git a/pytouhou/ui/renderer.pxd b/pytouhou/ui/renderer.pxd new file mode 100644 --- /dev/null +++ b/pytouhou/ui/renderer.pxd @@ -0,0 +1,12 @@ +cdef struct Vertex: + int x, y, z + float u, v + unsigned char r, g, b, a + + +cdef class Renderer: + cdef public texture_manager + cdef Vertex *vertex_buffer + + cpdef render_elements(self, elements) + cpdef setup_camera(self, dx, dy, dz) diff --git a/pytouhou/ui/renderer.pyx b/pytouhou/ui/renderer.pyx new file mode 100644 --- /dev/null +++ b/pytouhou/ui/renderer.pyx @@ -0,0 +1,93 @@ +# -*- encoding: utf-8 -*- +## +## Copyright (C) 2011 Thibaut Girka +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published +## by the Free Software Foundation; version 3 only. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## + +from libc.stdlib cimport malloc, free + +import ctypes + +from struct import pack + +from pyglet.gl import * + +from .sprite cimport get_sprite_rendering_data +from .texture import TextureManager + + +MAX_ELEMENTS = 10000 + + +cdef class Renderer: + def __cinit__(self): + # Allocate buffers + self.vertex_buffer = malloc(MAX_ELEMENTS * sizeof(Vertex)) + + + def __dealloc__(self): + free(self.vertex_buffer) + + + def __init__(self, resource_loader): + self.texture_manager = TextureManager(resource_loader) + + + cpdef render_elements(self, elements): + cdef unsigned short nb_vertices = 0 + + indices_by_texture = {} + + for element in elements: + sprite = element._sprite + if sprite: + ox, oy = element.x, element.y + key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) + rec = indices_by_texture.setdefault(key, []) + + # Pack data in buffer + (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices + r1, g1, b1, a1, r2, g2, b2, a2, r3, g3, b3, a3, r4, g4, b4, a4 = colors + u1, v1, u2, v2, u3, v3, u4, v4 = uvs + self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, u1, v1, r1, g1, b1, a1) + self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, u2, v2, r2, g2, b2, a2) + self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, u3, v3, r3, g3, b3, a3) + self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, u4, v4, r4, g4, b4, a4) + + # Add indices + index = nb_vertices + rec.extend((index, index + 1, index + 2, index + 3)) + + nb_vertices += 4 + + for (texture_key, blendfunc), indices in indices_by_texture.items(): + glVertexPointer(3, GL_INT, 24, &self.vertex_buffer[0].x) + glTexCoordPointer(2, GL_FLOAT, 24, &self.vertex_buffer[0].u) + glColorPointer(4, GL_UNSIGNED_BYTE, 24, &self.vertex_buffer[0].r) + + nb_indices = len(indices) + indices = pack(str(nb_indices) + 'H', *indices) + glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) + glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key].id) + glDrawElements(GL_QUADS, nb_indices, GL_UNSIGNED_SHORT, indices) + + + cpdef setup_camera(self, dx, dy, dz): + glMatrixMode(GL_MODELVIEW) + glLoadIdentity() + # Some explanations on the magic constants: + # 192. = 384. / 2. = width / 2. + # 224. = 448. / 2. = height / 2. + # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) + # This is so that objects on the (O, x, y) plane use pixel coordinates + gluLookAt(192., 224., - 835.979370 * dz, + 192. + dx, 224. - dy, 0., 0., -1., 0.) +