annotate stageviewer.py @ 8:f3ff96192476

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