comparison pytouhou/ui/gamerenderer.pyx @ 435:878273a984c4

Improve Matrix representation, using float[16] instead of imbricated python lists.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 07 Aug 2013 11:34:40 +0200
parents 0604f4fbbe3c
children 1b56d62250ab
comparison
equal deleted inserted replaced
434:18e4b121646b 435:878273a984c4
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from libc.stdlib cimport malloc, free
16 from itertools import chain 15 from itertools import chain
17 16
18 from pytouhou.lib.opengl cimport \ 17 from pytouhou.lib.opengl cimport \
19 (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, glDisable, 18 (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, glDisable,
20 glEnable, glFogi, glFogf, glFogfv, 19 glEnable, glFogi, glFogf, glFogfv,
21 GL_PROJECTION, GL_MODELVIEW, GL_FOG, GL_FOG_MODE, GL_LINEAR, 20 GL_PROJECTION, GL_MODELVIEW, GL_FOG, GL_FOG_MODE, GL_LINEAR,
22 GL_FOG_START, GL_FOG_END, GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat) 21 GL_FOG_START, GL_FOG_END, GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat)
23 22
24 from pytouhou.utils.matrix cimport Matrix, matrix_to_floats 23 from pytouhou.utils.matrix cimport Matrix
25 from pytouhou.utils.maths cimport setup_camera 24 from pytouhou.utils.maths cimport setup_camera
26 25
27 from .renderer import Renderer 26 from .renderer import Renderer
28 27
29 28
32 def __init__(self, resource_loader): 31 def __init__(self, resource_loader):
33 Renderer.__init__(self, resource_loader) 32 Renderer.__init__(self, resource_loader)
34 33
35 34
36 def render(self): 35 def render(self):
37 cdef float* fog_data 36 cdef float fog_data[4]
37 cdef Matrix view, mvp
38 38
39 back = self.background 39 back = self.background
40 game = self.game 40 game = self.game
41 41
42 if self.use_fixed_pipeline: 42 if self.use_fixed_pipeline:
44 glLoadIdentity() 44 glLoadIdentity()
45 45
46 if game is not None and game.spellcard_effect is not None: 46 if game is not None and game.spellcard_effect is not None:
47 if self.use_fixed_pipeline: 47 if self.use_fixed_pipeline:
48 glMatrixMode(GL_MODELVIEW) 48 glMatrixMode(GL_MODELVIEW)
49 glLoadMatrixf(matrix_to_floats(self.game_mvp)) 49 glLoadMatrixf((<Matrix>self.game_mvp).data)
50 glDisable(GL_FOG) 50 glDisable(GL_FOG)
51 else: 51 else:
52 self.game_shader.bind() 52 self.game_shader.bind()
53 self.game_shader.uniform_matrix('mvp', self.game_mvp) 53 self.game_shader.uniform_matrix('mvp', self.game_mvp)
54 54
66 # distance at a single place. 66 # distance at a single place.
67 fog_start -= 101010101./2010101. 67 fog_start -= 101010101./2010101.
68 fog_end -= 101010101./2010101. 68 fog_end -= 101010101./2010101.
69 69
70 model = Matrix() 70 model = Matrix()
71 model.data[3] = [-x, -y, -z, 1] 71 model.data[12] = -x
72 model.data[13] = -y
73 model.data[14] = -z
72 view = setup_camera(dx, dy, dz) 74 view = setup_camera(dx, dy, dz)
73 mvp = model * view * self.proj 75 mvp = model * view * self.proj
74 76
75 if self.use_fixed_pipeline: 77 if self.use_fixed_pipeline:
76 glMatrixMode(GL_MODELVIEW) 78 glMatrixMode(GL_MODELVIEW)
77 glLoadMatrixf(matrix_to_floats(mvp)) 79 glLoadMatrixf(mvp.data)
78 80
79 glEnable(GL_FOG) 81 glEnable(GL_FOG)
80 glFogi(GL_FOG_MODE, GL_LINEAR) 82 glFogi(GL_FOG_MODE, GL_LINEAR)
81 glFogf(GL_FOG_START, fog_start) 83 glFogf(GL_FOG_START, fog_start)
82 glFogf(GL_FOG_END, fog_end) 84 glFogf(GL_FOG_END, fog_end)
83 85
84 fog_data = <float*>malloc(4 * sizeof(float))
85 fog_data[0] = fog_r / 255. 86 fog_data[0] = fog_r / 255.
86 fog_data[1] = fog_g / 255. 87 fog_data[1] = fog_g / 255.
87 fog_data[2] = fog_b / 255. 88 fog_data[2] = fog_b / 255.
88 fog_data[3] = 1. 89 fog_data[3] = 1.
89 glFogfv(GL_FOG_COLOR, fog_data) 90 glFogfv(GL_FOG_COLOR, fog_data)
90 free(fog_data)
91 else: 91 else:
92 self.background_shader.bind() 92 self.background_shader.bind()
93 self.background_shader.uniform_matrix('mvp', mvp) 93 self.background_shader.uniform_matrix('mvp', mvp)
94 94
95 self.background_shader.uniform_1('fog_scale', 1. / (fog_end - fog_start)) 95 self.background_shader.uniform_1('fog_scale', 1. / (fog_end - fog_start))
101 glClear(GL_COLOR_BUFFER_BIT) 101 glClear(GL_COLOR_BUFFER_BIT)
102 102
103 if game is not None: 103 if game is not None:
104 if self.use_fixed_pipeline: 104 if self.use_fixed_pipeline:
105 glMatrixMode(GL_MODELVIEW) 105 glMatrixMode(GL_MODELVIEW)
106 glLoadMatrixf(matrix_to_floats(self.game_mvp)) 106 glLoadMatrixf((<Matrix>self.game_mvp).data)
107 glDisable(GL_FOG) 107 glDisable(GL_FOG)
108 else: 108 else:
109 self.game_shader.bind() 109 self.game_shader.bind()
110 self.game_shader.uniform_matrix('mvp', self.game_mvp) 110 self.game_shader.uniform_matrix('mvp', self.game_mvp)
111 111