Mercurial > touhou
annotate stageviewer.py @ 6:9159fa222923
Fix sprite size/scale thing \o/
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Tue, 02 Aug 2011 00:45:30 +0200 |
parents | aa201d8cfc19 |
children | 02a5f5314a19 |
rev | line source |
---|---|
4 | 1 #!/usr/bin/env python |
2 | |
3 import sys | |
4 import os | |
5 | |
6 import struct | |
7 from math import degrees, radians | |
8 from io import BytesIO | |
9 from itertools import chain | |
10 | |
11 import pygame | |
12 | |
13 import OpenGL | |
14 OpenGL.FORWARD_COMPATIBLE_ONLY = True | |
15 from OpenGL.GL import * | |
16 from OpenGL.GLU import * | |
17 | |
18 from pytouhou.formats.pbg3 import PBG3 | |
19 from pytouhou.formats.std import Stage | |
20 from pytouhou.formats.anm0 import Animations | |
21 | |
22 from pytouhou.utils.matrix import Matrix | |
23 | |
24 | |
25 def load_texture(image, alpha_image=None): | |
26 textureSurface = pygame.image.load(image).convert_alpha() | |
27 | |
28 if alpha_image: | |
29 alphaSurface = pygame.image.load(alpha_image) | |
30 assert textureSurface.get_size() == alphaSurface.get_size() | |
31 for x in range(alphaSurface.get_width()): | |
32 for y in range(alphaSurface.get_height()): | |
33 r, g, b, a = textureSurface.get_at((x, y)) | |
34 color2 = alphaSurface.get_at((x, y)) | |
35 textureSurface.set_at((x, y), (r, g, b, color2[0])) | |
36 | |
37 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1) | |
38 | |
39 width = textureSurface.get_width() | |
40 height = textureSurface.get_height() | |
41 | |
42 texture = glGenTextures(1) | |
43 glBindTexture(GL_TEXTURE_2D, texture) | |
44 | |
45 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, | |
46 GL_UNSIGNED_BYTE, textureData) | |
47 | |
48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) | |
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) | |
50 | |
51 return texture, width, height | |
52 | |
53 | |
54 | |
55 def build_objects_faces(stage, anim): | |
56 objects_faces = [] | |
57 for i, obj in enumerate(stage.objects): | |
58 faces = [] | |
59 for script_index, x, y, z, width_override, height_override in obj.quads: | |
60 #TODO: move mof of it elsewhere | |
61 vertices = [] | |
62 uvs = [] | |
63 vertmat = Matrix() | |
64 vertmat.data[0][0] = -.5 | |
65 vertmat.data[1][0] = -.5 | |
66 | |
67 vertmat.data[0][1] = .5 | |
68 vertmat.data[1][1] = -.5 | |
69 | |
70 vertmat.data[0][2] = .5 | |
71 vertmat.data[1][2] = .5 | |
72 | |
73 vertmat.data[0][3] = -.5 | |
74 vertmat.data[1][3] = .5 | |
75 | |
76 for i in range(4): | |
77 vertmat.data[2][i] = 0. | |
78 vertmat.data[3][i] = 1. | |
79 | |
80 properties = {} | |
81 for time, instr_type, data in anim.scripts[script_index]: | |
82 if instr_type == 15: | |
83 properties[15] = b'' | |
84 break | |
85 elif time == 0: #TODO | |
86 properties[instr_type] = data | |
87 #if 15 not in properties: #TODO: Skip properties | |
88 # continue | |
89 | |
90 #TODO: properties 3 and 4 | |
91 if 1 in properties: | |
92 tx, ty, tw, th = anim.sprites[struct.unpack('<I', properties[1])[0]] | |
93 width, height = 1., 1. | |
94 if 2 in properties: | |
95 width, height = struct.unpack('<ff', properties[2]) | |
6
9159fa222923
Fix sprite size/scale thing \o/
Thibaut Girka <thib@sitedethib.com>
parents:
5
diff
changeset
|
96 width = width_override or width * tw |
9159fa222923
Fix sprite size/scale thing \o/
Thibaut Girka <thib@sitedethib.com>
parents:
5
diff
changeset
|
97 height = height_override or height * th |
4 | 98 transform = Matrix.get_scaling_matrix(width, height, 1.) |
99 if 7 in properties: | |
100 transform = Matrix.get_scaling_matrix(-1., 1., 1.).mult(transform) | |
101 if 9 in properties: | |
102 rx, ry, rz = struct.unpack('<fff', properties[9]) | |
103 transform = Matrix.get_rotation_matrix(-rx, 'x').mult(transform) | |
104 transform = Matrix.get_rotation_matrix(ry, 'y').mult(transform) | |
105 transform = Matrix.get_rotation_matrix(-rz, 'z').mult(transform) #TODO: minus, really? | |
106 if 23 in properties: # Reposition | |
107 transform = Matrix.get_translation_matrix(width / 2., height / 2., 0.).mult(transform) | |
108 | |
109 transform = Matrix.get_translation_matrix(x, y, z).mult(transform) | |
110 vertmat = transform.mult(vertmat) | |
111 | |
112 uvs = [(tx / 256., 1. - (ty / 256.)), | |
113 ((tx + tw) / 256., 1. - (ty / 256.)), | |
114 ((tx + tw) / 256., 1. - ((ty + th) / 256.)), | |
115 (tx / 256., 1. - ((ty + th) / 256.))] | |
116 | |
117 for i in xrange(4): | |
118 w = vertmat.data[3][i] | |
119 vertices.append((vertmat.data[0][i] / w, vertmat.data[1][i] / w, vertmat.data[2][i] / w)) | |
120 faces.append((vertices, uvs)) | |
121 objects_faces.append(faces) | |
122 return objects_faces | |
123 | |
124 | |
125 | |
126 def objects_faces_to_vertices_uvs(objects): | |
127 vertices = tuple(vertex for obj in objects for face in obj for vertex in face[0]) #TODO: check | |
128 uvs = tuple(uv for obj in objects for face in obj for uv in face[1]) #TODO: check | |
129 return vertices, uvs | |
130 | |
131 | |
132 | |
133 def main(path, stage_num): | |
134 # Initialize pygame | |
135 pygame.init() | |
136 window = pygame.display.set_mode((384, 448), pygame.OPENGL | pygame.DOUBLEBUF) | |
137 | |
138 # Initialize OpenGL | |
5
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
139 glMatrixMode(GL_PROJECTION) |
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
140 glLoadIdentity() |
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
141 gluPerspective(30, float(window.get_width())/window.get_height(), 20, 2000) |
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
142 |
4 | 143 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) |
144 glEnable(GL_DEPTH_TEST) | |
145 glEnable(GL_BLEND) | |
146 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | |
147 glEnable(GL_TEXTURE_2D) | |
148 glEnableClientState(GL_VERTEX_ARRAY) | |
149 glEnableClientState(GL_TEXTURE_COORD_ARRAY) | |
150 | |
151 # Load data | |
152 with open(path, 'rb') as file: | |
153 archive = PBG3.read(file) | |
154 stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num))) | |
155 anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num))) | |
156 textures_components = [None, None] | |
157 for i, component_name in enumerate((anim.first_name, anim.secondary_name)): | |
158 if component_name: | |
159 textures_components[i] = BytesIO(archive.extract(os.path.basename(component_name))) | |
160 texture = load_texture(*textures_components) | |
161 | |
162 print(stage.name) | |
163 | |
164 uvs = [] | |
165 vertices = [] | |
166 objects_faces = build_objects_faces(stage, anim) | |
167 objects_instances_faces = [] | |
168 for obj, ox, oy, oz in stage.object_instances: | |
169 obj_id = stage.objects.index(obj) | |
170 | |
171 obj_instance = [] | |
172 for face_vertices, face_uvs in objects_faces[obj_id]: | |
173 obj_instance.append((tuple((x + ox, y + oy, z + oz) for x, y, z in face_vertices), | |
174 face_uvs)) | |
175 objects_instances_faces.append(obj_instance) | |
176 | |
177 vertices, uvs = objects_faces_to_vertices_uvs(objects_instances_faces) | |
178 nb_vertices = len(vertices) | |
179 vertices_format = 'f' * (3 * nb_vertices) | |
180 uvs_format = 'f' * (2 * nb_vertices) | |
181 vertices, uvs = objects_faces_to_vertices_uvs(objects_instances_faces) | |
182 glVertexPointer(3, GL_FLOAT, 0, struct.pack(vertices_format, *chain(*vertices))) | |
183 glTexCoordPointer(2, GL_FLOAT, 0, struct.pack(uvs_format, *chain(*uvs))) | |
184 | |
185 x, y, z = 0, 0, 0 | |
186 frame = 0 | |
187 interpolation = 0, 0, 0 | |
188 | |
189 # Main loop | |
190 clock = pygame.time.Clock() | |
191 while True: | |
192 for event in pygame.event.get(): | |
193 if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in (pygame.K_ESCAPE, pygame.K_q)): | |
194 sys.exit(0) | |
195 elif event.type == pygame.KEYDOWN: | |
196 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT: | |
197 pygame.display.toggle_fullscreen() | |
198 | |
199 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
200 | |
201 for frame_num, message_type, data in stage.script: | |
202 if frame_num == frame and message_type == 3: | |
203 duration, junk1, junk2 = struct.unpack('<III', data) | |
204 interpolation = frame_num, duration, frame_num + duration | |
205 old_unknownx, old_dy, old_fov = unknownx, dy, fov | |
206 if frame_num <= frame and message_type == 0: | |
207 last_message = frame_num, message_type, data | |
208 if frame_num <= frame and message_type == 2: | |
209 next_unknownx, next_dy, next_fov = struct.unpack('<fff', data) | |
210 if frame_num > frame and message_type == 0: | |
211 next_message = frame_num, message_type, data | |
212 break | |
213 | |
214 if frame < interpolation[2]: | |
215 truc = float(frame - interpolation[0]) / interpolation[1] | |
216 unknownx = old_unknownx + (next_unknownx - old_unknownx) * truc | |
217 dy = old_dy + (next_dy - old_dy) * truc | |
218 fov = old_fov + (next_fov - old_fov) * truc | |
219 else: | |
220 unknownx, dy, fov = next_unknownx, next_dy, next_fov | |
221 | |
222 x1, y1, z1 = struct.unpack('<fff', last_message[2]) | |
223 x2, y2, z2 = struct.unpack('<fff', next_message[2]) | |
224 | |
225 truc = (float(frame) - last_message[0]) / (next_message[0] - last_message[0]) | |
226 | |
5
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
227 x = x1 + (x2 - x1) * truc |
4 | 228 y = y1 + (y2 - y1) * truc |
5
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
229 z = z1 + (z2 - z1) * truc |
4 | 230 |
231 | |
232 glMatrixMode(GL_MODELVIEW) | |
233 glLoadIdentity() | |
5
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
234 gluLookAt(192., 224., - 835.979370 * fov, |
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
235 192., 224. - dy, 750 - 835.979370 * fov, 0., -1., 0.) #TODO: 750 might not be accurate |
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
236 #print(glGetFloat(GL_MODELVIEW_MATRIX)) |
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
237 glTranslatef(-x, -y, -z) |
4 | 238 |
239 glDrawArrays(GL_QUADS, 0, nb_vertices) | |
240 | |
241 pygame.display.flip() | |
242 clock.tick(120) | |
243 frame += 1 | |
244 | |
245 | |
246 | |
247 try: | |
248 file_path, stage_num = sys.argv[1:] | |
249 stage_num = int(stage_num) | |
250 except ValueError: | |
251 print('Usage: %s std_dat_path stage_num' % sys.argv[0]) | |
252 else: | |
253 main(file_path, stage_num) | |
254 |