comparison pytouhou/ui/gamerenderer.pyx @ 205:ee6dfd14a785

Rename pytouhou.opengl to pytouhou.ui, makes much more sense that way.
author Thibaut Girka <thib@sitedethib.com>
date Tue, 01 Nov 2011 13:50:33 +0100
parents pytouhou/opengl/gamerenderer.pyx@220c122f428c
children 577f45454402
comparison
equal deleted inserted replaced
204:88361534c77e 205:ee6dfd14a785
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 from struct import pack
20 from itertools import chain
21
22 from pyglet.gl import *
23
24 from .texture import TextureManager
25 from .sprite cimport get_sprite_rendering_data
26 from .background import get_background_rendering_data
27
28
29 MAX_ELEMENTS = 10000
30
31
32 cdef struct Vertex:
33 int x, y, z
34 float u, v
35 unsigned char r, g, b, a
36
37
38 cdef class GameRenderer:
39 cdef public texture_manager
40 cdef public game
41 cdef public background
42
43 cdef Vertex *vertex_buffer
44
45
46 def __cinit__(self):
47 # Allocate buffers
48 self.vertex_buffer = <Vertex*> malloc(MAX_ELEMENTS * sizeof(Vertex))
49
50
51 def __dealloc__(self):
52 free(self.vertex_buffer)
53
54
55 def __init__(self, resource_loader, game=None, background=None):
56 self.texture_manager = TextureManager(resource_loader)
57
58 self.game = game
59 self.background = background
60
61
62 cdef render_elements(self, elements):
63 cdef unsigned short nb_vertices = 0
64
65 indices_by_texture = {}
66
67 for element in elements:
68 sprite = element._sprite
69 if sprite:
70 ox, oy = element.x, element.y
71 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
72 rec = indices_by_texture.setdefault(key, [])
73
74 # Pack data in buffer
75 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices
76 r1, g1, b1, a1, r2, g2, b2, a2, r3, g3, b3, a3, r4, g4, b4, a4 = colors
77 u1, v1, u2, v2, u3, v3, u4, v4 = uvs
78 self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, u1, v1, r1, g1, b1, a1)
79 self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, u2, v2, r2, g2, b2, a2)
80 self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, u3, v3, r3, g3, b3, a3)
81 self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, u4, v4, r4, g4, b4, a4)
82
83 # Add indices
84 index = nb_vertices
85 rec.extend((index, index + 1, index + 2, index + 3))
86
87 nb_vertices += 4
88
89 for (texture_key, blendfunc), indices in indices_by_texture.items():
90 glVertexPointer(3, GL_INT, 24, <long> &self.vertex_buffer[0].x)
91 glTexCoordPointer(2, GL_FLOAT, 24, <long> &self.vertex_buffer[0].u)
92 glColorPointer(4, GL_UNSIGNED_BYTE, 24, <long> &self.vertex_buffer[0].r)
93
94 nb_indices = len(indices)
95 indices = pack(str(nb_indices) + 'H', *indices)
96 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
97 glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key].id)
98 glDrawElements(GL_QUADS, nb_indices, GL_UNSIGNED_SHORT, indices)
99
100
101 def render(self):
102 glClear(GL_DEPTH_BUFFER_BIT)
103
104 back = self.background
105 game = self.game
106 texture_manager = self.texture_manager
107
108 if back is not None:
109 fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values
110 x, y, z = back.position_interpolator.values
111 dx, dy, dz = back.position2_interpolator.values
112
113 glFogi(GL_FOG_MODE, GL_LINEAR)
114 glFogf(GL_FOG_START, fog_start)
115 glFogf(GL_FOG_END, fog_end)
116 glFogfv(GL_FOG_COLOR, (GLfloat * 4)(fog_r / 255., fog_g / 255., fog_b / 255., 1.))
117
118 glMatrixMode(GL_MODELVIEW)
119 glLoadIdentity()
120 # Some explanations on the magic constants:
121 # 192. = 384. / 2. = width / 2.
122 # 224. = 448. / 2. = height / 2.
123 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
124 # This is so that objects on the (O, x, y) plane use pixel coordinates
125 gluLookAt(192., 224., - 835.979370 * dz,
126 192. + dx, 224. - dy, 0., 0., -1., 0.)
127 glTranslatef(-x, -y, -z)
128
129 glEnable(GL_DEPTH_TEST)
130 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back):
131 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
132 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id)
133 glVertexPointer(3, GL_FLOAT, 0, vertices)
134 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
135 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
136 glDrawArrays(GL_QUADS, 0, nb_vertices)
137 glDisable(GL_DEPTH_TEST)
138 else:
139 glClear(GL_COLOR_BUFFER_BIT)
140
141 if game is not None:
142 glMatrixMode(GL_MODELVIEW)
143 glLoadIdentity()
144 # Some explanations on the magic constants:
145 # 192. = 384. / 2. = width / 2.
146 # 224. = 448. / 2. = height / 2.
147 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
148 # This is so that objects on the (O, x, y) plane use pixel coordinates
149 gluLookAt(192., 224., - 835.979370,
150 192., 224., 0., 0., -1., 0.)
151
152 glDisable(GL_FOG)
153 self.render_elements(game.enemies)
154 self.render_elements(game.effects)
155 self.render_elements(chain(game.players_bullets,
156 game.players,
157 *(player.objects() for player in game.players)))
158 self.render_elements(chain(game.bullets, game.cancelled_bullets, game.items))
159 #TODO: display item indicators
160 glEnable(GL_FOG)
161