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