Mercurial > touhou
comparison pytouhou/opengl/gamerenderer.pyx @ 131:fab7ad2f0d8b
Use Cython, improve performances!
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 11 Sep 2011 02:02:59 +0200 |
parents | pytouhou/opengl/gamerenderer.py@11ab06f4c4c6 |
children | 982b21222602 |
comparison
equal
deleted
inserted
replaced
130:11ab06f4c4c6 | 131:fab7ad2f0d8b |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> | |
4 ## | |
5 ## This program is free software; you can redistribute it and/or modify | |
6 ## it under the terms of the GNU General Public License as published | |
7 ## by the Free Software Foundation; version 3 only. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 | |
15 from libc.stdlib cimport malloc, free | |
16 | |
17 import ctypes | |
18 | |
19 import struct | |
20 | |
21 from pyglet.gl import * | |
22 | |
23 from pytouhou.opengl.texture import TextureManager | |
24 from pytouhou.opengl.sprite cimport get_sprite_rendering_data | |
25 from pytouhou.opengl.background import get_background_rendering_data | |
26 | |
27 | |
28 MAX_ELEMENTS = 10000 | |
29 | |
30 | |
31 cdef struct Vertex: | |
32 float x, y, z | |
33 float u, v | |
34 unsigned char r, g, b, a | |
35 | |
36 | |
37 cdef class GameRenderer: | |
38 cdef public texture_manager | |
39 cdef public game | |
40 cdef public background | |
41 | |
42 cdef Vertex *vertex_buffer | |
43 | |
44 | |
45 def __cinit__(self, resource_loader, game=None, background=None): | |
46 # Allocate buffers | |
47 self.vertex_buffer = <Vertex*> malloc(MAX_ELEMENTS * sizeof(Vertex)) | |
48 | |
49 | |
50 def __dealloc__(self): | |
51 free(self.vertex_buffer) | |
52 | |
53 | |
54 def __init__(self, resource_loader, game=None, background=None): | |
55 self.texture_manager = TextureManager(resource_loader) | |
56 | |
57 self.game = game | |
58 self.background = background | |
59 | |
60 | |
61 cdef render_elements(self, elements): | |
62 cdef unsigned short nb_vertices = 0 | |
63 | |
64 indices_by_texture = {} | |
65 | |
66 for element in elements: | |
67 sprite = element._sprite | |
68 if sprite: | |
69 ox, oy = element.x, element.y | |
70 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) | |
71 rec = indices_by_texture.setdefault(key, []) | |
72 | |
73 # Pack data in buffer | |
74 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices | |
75 r1, g1, b1, a1, r2, g2, b2, a2, r3, g3, b3, a3, r4, g4, b4, a4 = colors | |
76 u1, v1, u2, v2, u3, v3, u4, v4 = uvs | |
77 self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, u1, v1, r1, g1, b1, a1) | |
78 self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, u2, v2, r2, g2, b2, a2) | |
79 self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, u3, v3, r3, g3, b3, a3) | |
80 self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, u4, v4, r4, g4, b4, a4) | |
81 | |
82 # Add indices | |
83 index = nb_vertices | |
84 rec.extend((index, index + 1, index + 2, index + 3)) | |
85 | |
86 nb_vertices += 4 | |
87 | |
88 glVertexPointer(3, GL_FLOAT, 24, <long> &self.vertex_buffer[0].x) | |
89 glTexCoordPointer(2, GL_FLOAT, 24, <long> &self.vertex_buffer[0].u) | |
90 glColorPointer(4, GL_UNSIGNED_BYTE, 24, <long> &self.vertex_buffer[0].r) | |
91 | |
92 for (texture_key, blendfunc), indices in indices_by_texture.items(): | |
93 nb_indices = len(indices) | |
94 indices = struct.pack(str(nb_indices) + 'H', *indices) | |
95 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) | |
96 glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key].id) | |
97 glDrawElements(GL_QUADS, nb_indices, GL_UNSIGNED_SHORT, indices) | |
98 | |
99 | |
100 def render(self): | |
101 glClear(GL_DEPTH_BUFFER_BIT) | |
102 | |
103 back = self.background | |
104 game = self.game | |
105 texture_manager = self.texture_manager | |
106 | |
107 if back is not None: | |
108 fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values | |
109 x, y, z = back.position_interpolator.values | |
110 dx, dy, dz = back.position2_interpolator.values | |
111 | |
112 glFogi(GL_FOG_MODE, GL_LINEAR) | |
113 glFogf(GL_FOG_START, fog_start) | |
114 glFogf(GL_FOG_END, fog_end) | |
115 glFogfv(GL_FOG_COLOR, (GLfloat * 4)(fog_r / 255., fog_g / 255., fog_b / 255., 1.)) | |
116 | |
117 glMatrixMode(GL_MODELVIEW) | |
118 glLoadIdentity() | |
119 # Some explanations on the magic constants: | |
120 # 192. = 384. / 2. = width / 2. | |
121 # 224. = 448. / 2. = height / 2. | |
122 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) | |
123 # This is so that objects on the (O, x, y) plane use pixel coordinates | |
124 gluLookAt(192., 224., - 835.979370 * dz, | |
125 192. + dx, 224. - dy, 0., 0., -1., 0.) | |
126 glTranslatef(-x, -y, -z) | |
127 | |
128 glEnable(GL_DEPTH_TEST) | |
129 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back): | |
130 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) | |
131 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id) | |
132 glVertexPointer(3, GL_FLOAT, 0, vertices) | |
133 glTexCoordPointer(2, GL_FLOAT, 0, uvs) | |
134 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors) | |
135 glDrawArrays(GL_QUADS, 0, nb_vertices) | |
136 glDisable(GL_DEPTH_TEST) | |
137 else: | |
138 glClear(GL_COLOR_BUFFER_BIT) | |
139 | |
140 if game is not None: | |
141 glMatrixMode(GL_MODELVIEW) | |
142 glLoadIdentity() | |
143 # Some explanations on the magic constants: | |
144 # 192. = 384. / 2. = width / 2. | |
145 # 224. = 448. / 2. = height / 2. | |
146 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) | |
147 # This is so that objects on the (O, x, y) plane use pixel coordinates | |
148 gluLookAt(192., 224., - 835.979370, | |
149 192., 224., 0., 0., -1., 0.) | |
150 | |
151 glDisable(GL_FOG) | |
152 self.render_elements(game.enemies) | |
153 self.render_elements(game.game_state.bullets) | |
154 self.render_elements(game.players) | |
155 glEnable(GL_FOG) | |
156 |