Mercurial > touhou
comparison stageviewer.py @ 7:02a5f5314a19
Add preliminary fog support
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Tue, 02 Aug 2011 10:21:48 +0200 |
parents | 9159fa222923 |
children | f3ff96192476 |
comparison
equal
deleted
inserted
replaced
6:9159fa222923 | 7:02a5f5314a19 |
---|---|
138 # Initialize OpenGL | 138 # Initialize OpenGL |
139 glMatrixMode(GL_PROJECTION) | 139 glMatrixMode(GL_PROJECTION) |
140 glLoadIdentity() | 140 glLoadIdentity() |
141 gluPerspective(30, float(window.get_width())/window.get_height(), 20, 2000) | 141 gluPerspective(30, float(window.get_width())/window.get_height(), 20, 2000) |
142 | 142 |
143 glHint(GL_FOG_HINT, GL_NICEST) | |
143 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) | 144 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) |
144 glEnable(GL_DEPTH_TEST) | 145 glEnable(GL_DEPTH_TEST) |
145 glEnable(GL_BLEND) | 146 glEnable(GL_BLEND) |
146 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | 147 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) |
147 glEnable(GL_TEXTURE_2D) | 148 glEnable(GL_TEXTURE_2D) |
149 glEnable(GL_FOG) | |
148 glEnableClientState(GL_VERTEX_ARRAY) | 150 glEnableClientState(GL_VERTEX_ARRAY) |
149 glEnableClientState(GL_TEXTURE_COORD_ARRAY) | 151 glEnableClientState(GL_TEXTURE_COORD_ARRAY) |
150 | 152 |
151 # Load data | 153 # Load data |
152 with open(path, 'rb') as file: | 154 with open(path, 'rb') as file: |
183 glTexCoordPointer(2, GL_FLOAT, 0, struct.pack(uvs_format, *chain(*uvs))) | 185 glTexCoordPointer(2, GL_FLOAT, 0, struct.pack(uvs_format, *chain(*uvs))) |
184 | 186 |
185 x, y, z = 0, 0, 0 | 187 x, y, z = 0, 0, 0 |
186 frame = 0 | 188 frame = 0 |
187 interpolation = 0, 0, 0 | 189 interpolation = 0, 0, 0 |
190 interpolation2 = 0, 0, 0 | |
188 | 191 |
189 # Main loop | 192 # Main loop |
190 clock = pygame.time.Clock() | 193 clock = pygame.time.Clock() |
191 while True: | 194 while True: |
192 for event in pygame.event.get(): | 195 for event in pygame.event.get(): |
197 pygame.display.toggle_fullscreen() | 200 pygame.display.toggle_fullscreen() |
198 | 201 |
199 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | 202 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
200 | 203 |
201 for frame_num, message_type, data in stage.script: | 204 for frame_num, message_type, data in stage.script: |
205 if frame_num == frame and message_type == 1: | |
206 #TODO: move interpolation elsewhere | |
207 next_fog_b, next_fog_g, next_fog_r, _, next_fog_start, next_fog_end = struct.unpack('<BBBBff', data) | |
202 if frame_num == frame and message_type == 3: | 208 if frame_num == frame and message_type == 3: |
203 duration, junk1, junk2 = struct.unpack('<III', data) | 209 duration, junk1, junk2 = struct.unpack('<III', data) |
204 interpolation = frame_num, duration, frame_num + duration | 210 interpolation = frame_num, duration, frame_num + duration |
205 old_unknownx, old_dy, old_fov = unknownx, dy, fov | 211 old_unknownx, old_dy, old_dz = unknownx, dy, dz |
212 if frame_num == frame and message_type == 4: | |
213 duration, junk1, junk2 = struct.unpack('<III', data) | |
214 interpolation2 = frame_num, duration, frame_num + duration | |
215 old_fog_b, old_fog_g, old_fog_r, old_fog_start, old_fog_end = fog_b, fog_g, fog_r, fog_start, fog_end | |
206 if frame_num <= frame and message_type == 0: | 216 if frame_num <= frame and message_type == 0: |
207 last_message = frame_num, message_type, data | 217 last_message = frame_num, message_type, data |
208 if frame_num <= frame and message_type == 2: | 218 if frame_num <= frame and message_type == 2: |
209 next_unknownx, next_dy, next_fov = struct.unpack('<fff', data) | 219 next_unknownx, next_dy, next_dz = struct.unpack('<fff', data) |
210 if frame_num > frame and message_type == 0: | 220 if frame_num > frame and message_type == 0: |
211 next_message = frame_num, message_type, data | 221 next_message = frame_num, message_type, data |
212 break | 222 break |
213 | 223 |
214 if frame < interpolation[2]: | 224 if frame < interpolation[2]: |
215 truc = float(frame - interpolation[0]) / interpolation[1] | 225 truc = float(frame - interpolation[0]) / interpolation[1] |
216 unknownx = old_unknownx + (next_unknownx - old_unknownx) * truc | 226 unknownx = old_unknownx + (next_unknownx - old_unknownx) * truc |
217 dy = old_dy + (next_dy - old_dy) * truc | 227 dy = old_dy + (next_dy - old_dy) * truc |
218 fov = old_fov + (next_fov - old_fov) * truc | 228 dz = old_dz + (next_dz - old_dz) * truc |
219 else: | 229 else: |
220 unknownx, dy, fov = next_unknownx, next_dy, next_fov | 230 unknownx, dy, dz = next_unknownx, next_dy, next_dz |
231 | |
232 if frame < interpolation2[2]: | |
233 truc = float(frame - interpolation2[0]) / interpolation2[1] | |
234 fog_b = old_fog_b + (next_fog_b - old_fog_b) * truc | |
235 fog_g = old_fog_g + (next_fog_g - old_fog_g) * truc | |
236 fog_r = old_fog_r + (next_fog_r - old_fog_r) * truc | |
237 fog_start = old_fog_start + (next_fog_start - old_fog_start) * truc | |
238 fog_end = old_fog_end + (next_fog_end - old_fog_end) * truc | |
239 else: | |
240 fog_r, fog_g, fog_b, fog_start, fog_end = next_fog_r, next_fog_g, next_fog_b, next_fog_start, next_fog_end | |
241 | |
242 | |
243 glFogi(GL_FOG_MODE, GL_LINEAR) | |
244 glFogf(GL_FOG_START, fog_start) | |
245 glFogf(GL_FOG_END, fog_end) | |
246 glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.)) | |
247 | |
221 | 248 |
222 x1, y1, z1 = struct.unpack('<fff', last_message[2]) | 249 x1, y1, z1 = struct.unpack('<fff', last_message[2]) |
223 x2, y2, z2 = struct.unpack('<fff', next_message[2]) | 250 x2, y2, z2 = struct.unpack('<fff', next_message[2]) |
224 | 251 |
225 truc = (float(frame) - last_message[0]) / (next_message[0] - last_message[0]) | 252 truc = (float(frame) - last_message[0]) / (next_message[0] - last_message[0]) |
229 z = z1 + (z2 - z1) * truc | 256 z = z1 + (z2 - z1) * truc |
230 | 257 |
231 | 258 |
232 glMatrixMode(GL_MODELVIEW) | 259 glMatrixMode(GL_MODELVIEW) |
233 glLoadIdentity() | 260 glLoadIdentity() |
234 gluLookAt(192., 224., - 835.979370 * fov, | 261 gluLookAt(192., 224., - 835.979370 * dz, |
235 192., 224. - dy, 750 - 835.979370 * fov, 0., -1., 0.) #TODO: 750 might not be accurate | 262 192., 224. - dy, 750 - 835.979370 * dz, 0., -1., 0.) #TODO: 750 might not be accurate |
236 #print(glGetFloat(GL_MODELVIEW_MATRIX)) | 263 #print(glGetFloat(GL_MODELVIEW_MATRIX)) |
237 glTranslatef(-x, -y, -z) | 264 glTranslatef(-x, -y, -z) |
238 | 265 |
239 glDrawArrays(GL_QUADS, 0, nb_vertices) | 266 glDrawArrays(GL_QUADS, 0, nb_vertices) |
240 | 267 |