comparison pytouhou/ui/opengl/gamerenderer.pyx @ 523:6e3b3d5d4691

Make matrix a struct.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 18 Dec 2013 17:53:29 +0100
parents b18f0bd30ad0
children 53fa73932e9a
comparison
equal deleted inserted replaced
522:e8496e5ba056 523:6e3b3d5d4691
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 free
15 from itertools import chain 16 from itertools import chain
16 17
17 from pytouhou.lib.opengl cimport \ 18 from pytouhou.lib.opengl cimport \
18 (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, glDisable, 19 (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, glDisable,
19 glEnable, glFogi, glFogf, glFogfv, GL_PROJECTION, GL_MODELVIEW, 20 glEnable, glFogi, glFogf, glFogfv, GL_PROJECTION, GL_MODELVIEW,
20 GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, GL_FOG_END, 21 GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, GL_FOG_END,
21 GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat, glViewport, glScissor, 22 GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat, glViewport, glScissor,
22 GL_SCISSOR_TEST, GL_DEPTH_BUFFER_BIT) 23 GL_SCISSOR_TEST, GL_DEPTH_BUFFER_BIT)
23 24
25 from pytouhou.utils.matrix cimport mul, new_identity
24 from pytouhou.utils.maths cimport perspective, setup_camera, ortho_2d 26 from pytouhou.utils.maths cimport perspective, setup_camera, ortho_2d
25 from pytouhou.game.text cimport NativeText, GlyphCollection 27 from pytouhou.game.text cimport NativeText, GlyphCollection
26 from .shaders.eosd import GameShader, BackgroundShader, PassthroughShader 28 from .shaders.eosd import GameShader, BackgroundShader, PassthroughShader
27 from .renderer cimport Texture 29 from .renderer cimport Texture
28 30
44 self.passthrough_shader = PassthroughShader() 46 self.passthrough_shader = PassthroughShader()
45 47
46 self.framebuffer = Framebuffer(0, 0, 640, 480) 48 self.framebuffer = Framebuffer(0, 0, 640, 480)
47 49
48 50
51 def __dealloc__(self):
52 if self.game_mvp != NULL:
53 free(self.game_mvp)
54 if self.interface_mvp != NULL:
55 free(self.interface_mvp)
56 if self.proj != NULL:
57 free(self.proj)
58
59
49 property size: 60 property size:
50 # We never need to get back the computed size, so size is write-only. 61 # We never need to get back the computed size, so size is write-only.
51 def __set__(self, tuple size): 62 def __set__(self, tuple size):
52 self.x, self.y, self.width, self.height = size 63 self.x, self.y, self.width, self.height = size
53 64
66 77
67 78
68 def start(self, common): 79 def start(self, common):
69 self.proj = perspective(30, float(common.width) / float(common.height), 80 self.proj = perspective(30, float(common.width) / float(common.height),
70 101010101./2010101., 101010101./10101.) 81 101010101./2010101., 101010101./10101.)
71 game_view = setup_camera(0, 0, 1) 82 self.game_mvp = setup_camera(0, 0, 1)
72 self.game_mvp = game_view * self.proj 83 mul(self.game_mvp, self.proj)
73 self.interface_mvp = ortho_2d(0., float(common.interface.width), 84 self.interface_mvp = ortho_2d(0., float(common.interface.width),
74 float(common.interface.height), 0.) 85 float(common.interface.height), 0.)
75 86
76 87
77 def render(self, Game game): 88 def render(self, Game game):
90 101
91 cdef void render_game(self, Game game): 102 cdef void render_game(self, Game game):
92 cdef long game_x, game_y 103 cdef long game_x, game_y
93 cdef float x, y, z, dx, dy, dz, fog_data[4], fog_start, fog_end 104 cdef float x, y, z, dx, dy, dz, fog_data[4], fog_start, fog_end
94 cdef unsigned char fog_r, fog_g, fog_b 105 cdef unsigned char fog_r, fog_g, fog_b
95 cdef Matrix mvp 106 cdef Matrix *mvp
96 107
97 game_x, game_y = game.interface.game_pos 108 game_x, game_y = game.interface.game_pos
98 glViewport(game_x, game_y, game.width, game.height) 109 glViewport(game_x, game_y, game.width, game.height)
99 glClear(GL_DEPTH_BUFFER_BIT) 110 glClear(GL_DEPTH_BUFFER_BIT)
100 glScissor(game_x, game_y, game.width, game.height) 111 glScissor(game_x, game_y, game.width, game.height)
105 glLoadIdentity() 116 glLoadIdentity()
106 117
107 if game is not None and game.spellcard_effect is not None: 118 if game is not None and game.spellcard_effect is not None:
108 if self.use_fixed_pipeline: 119 if self.use_fixed_pipeline:
109 glMatrixMode(GL_MODELVIEW) 120 glMatrixMode(GL_MODELVIEW)
110 glLoadMatrixf(self.game_mvp.data) 121 glLoadMatrixf(<GLfloat*>self.game_mvp)
111 glDisable(GL_FOG) 122 glDisable(GL_FOG)
112 else: 123 else:
113 self.game_shader.bind() 124 self.game_shader.bind()
114 self.game_shader.uniform_matrix('mvp', self.game_mvp) 125 self.game_shader.uniform_matrix('mvp', self.game_mvp)
115 126
127 #TODO: investigate, and use a variable to keep the near plane 138 #TODO: investigate, and use a variable to keep the near plane
128 # distance at a single place. 139 # distance at a single place.
129 fog_start -= 101010101./2010101. 140 fog_start -= 101010101./2010101.
130 fog_end -= 101010101./2010101. 141 fog_end -= 101010101./2010101.
131 142
132 model = Matrix() 143 mvp = new_identity()
133 model.data[12] = -x 144 mvp_data = <GLfloat*>mvp
134 model.data[13] = -y 145 mvp_data[12] = -x
135 model.data[14] = -z 146 mvp_data[13] = -y
147 mvp_data[14] = -z
136 view = setup_camera(dx, dy, dz) 148 view = setup_camera(dx, dy, dz)
137 mvp = model * view * self.proj 149 mul(mvp, view)
150 free(view)
151 mul(mvp, self.proj)
138 152
139 if self.use_fixed_pipeline: 153 if self.use_fixed_pipeline:
140 glMatrixMode(GL_MODELVIEW) 154 glMatrixMode(GL_MODELVIEW)
141 glLoadMatrixf(mvp.data) 155 glLoadMatrixf(mvp_data)
142 156
143 glEnable(GL_FOG) 157 glEnable(GL_FOG)
144 glFogi(GL_FOG_MODE, GL_LINEAR) 158 glFogi(GL_FOG_MODE, GL_LINEAR)
145 glFogf(GL_FOG_START, fog_start) 159 glFogf(GL_FOG_START, fog_start)
146 glFogf(GL_FOG_END, fog_end) 160 glFogf(GL_FOG_END, fog_end)
156 170
157 self.background_shader.uniform_1('fog_scale', 1. / (fog_end - fog_start)) 171 self.background_shader.uniform_1('fog_scale', 1. / (fog_end - fog_start))
158 self.background_shader.uniform_1('fog_end', fog_end) 172 self.background_shader.uniform_1('fog_end', fog_end)
159 self.background_shader.uniform_4('fog_color', fog_r / 255., fog_g / 255., fog_b / 255., 1.) 173 self.background_shader.uniform_4('fog_color', fog_r / 255., fog_g / 255., fog_b / 255., 1.)
160 174
175 free(mvp)
161 self.background_renderer.render_background() 176 self.background_renderer.render_background()
162 else: 177 else:
163 glClear(GL_COLOR_BUFFER_BIT) 178 glClear(GL_COLOR_BUFFER_BIT)
164 179
165 if game is not None: 180 if game is not None:
166 if self.use_fixed_pipeline: 181 if self.use_fixed_pipeline:
167 glMatrixMode(GL_MODELVIEW) 182 glMatrixMode(GL_MODELVIEW)
168 glLoadMatrixf(self.game_mvp.data) 183 glLoadMatrixf(<GLfloat*>self.game_mvp)
169 glDisable(GL_FOG) 184 glDisable(GL_FOG)
170 else: 185 else:
171 self.game_shader.bind() 186 self.game_shader.bind()
172 self.game_shader.uniform_matrix('mvp', self.game_mvp) 187 self.game_shader.uniform_matrix('mvp', self.game_mvp)
173 188
218 233
219 elements = [] 234 elements = []
220 235
221 if self.use_fixed_pipeline: 236 if self.use_fixed_pipeline:
222 glMatrixMode(GL_MODELVIEW) 237 glMatrixMode(GL_MODELVIEW)
223 glLoadMatrixf(self.interface_mvp.data) 238 glLoadMatrixf(<GLfloat*>self.interface_mvp)
224 glDisable(GL_FOG) 239 glDisable(GL_FOG)
225 else: 240 else:
226 self.interface_shader.bind() 241 self.interface_shader.bind()
227 self.interface_shader.uniform_matrix('mvp', self.interface_mvp) 242 self.interface_shader.uniform_matrix('mvp', self.interface_mvp)
228 glViewport(0, 0, interface.width, interface.height) 243 glViewport(0, 0, interface.width, interface.height)