annotate stageviewer.py @ 13:58bc264aba38

Refactor
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 13:23:33 +0200
parents 548662d70860
children 07a7f28c8aaa
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
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
13 from pytouhou.formats.pbg3 import PBG3
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
14 from pytouhou.game.background import Background
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
15
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 import OpenGL
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 OpenGL.FORWARD_COMPATIBLE_ONLY = True
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 from OpenGL.GL import *
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 from OpenGL.GLU import *
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
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 def load_texture(image, alpha_image=None):
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
23 #TODO: move elsewhere
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 textureSurface = pygame.image.load(image).convert_alpha()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 if alpha_image:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 alphaSurface = pygame.image.load(alpha_image)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 assert textureSurface.get_size() == alphaSurface.get_size()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 for x in range(alphaSurface.get_width()):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 for y in range(alphaSurface.get_height()):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 r, g, b, a = textureSurface.get_at((x, y))
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 color2 = alphaSurface.get_at((x, y))
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 textureSurface.set_at((x, y), (r, g, b, color2[0]))
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
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 width = textureSurface.get_width()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 height = textureSurface.get_height()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 texture = glGenTextures(1)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 glBindTexture(GL_TEXTURE_2D, texture)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 GL_UNSIGNED_BYTE, textureData)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 return texture, width, height
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
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 def main(path, stage_num):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 # Initialize pygame
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 pygame.init()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 window = pygame.display.set_mode((384, 448), pygame.OPENGL | pygame.DOUBLEBUF)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 # Initialize OpenGL
5
aa201d8cfc19 Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
58 glMatrixMode(GL_PROJECTION)
aa201d8cfc19 Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
59 glLoadIdentity()
10
059ea0ea8d38 Use the original game's near and far planes.
Thibaut Girka <thib@sitedethib.com>
parents: 9
diff changeset
60 gluPerspective(30, float(window.get_width())/window.get_height(), 101010101./2010101., 101010101./10101.)
5
aa201d8cfc19 Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
61
7
02a5f5314a19 Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents: 6
diff changeset
62 glHint(GL_FOG_HINT, GL_NICEST)
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 glEnable(GL_DEPTH_TEST)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 glEnable(GL_BLEND)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 glEnable(GL_TEXTURE_2D)
7
02a5f5314a19 Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents: 6
diff changeset
68 glEnable(GL_FOG)
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 glEnableClientState(GL_VERTEX_ARRAY)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 # Load data
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 with open(path, 'rb') as file:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 archive = PBG3.read(file)
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
75 background = Background(archive, stage_num)
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
77 texture = load_texture(*background.texture_components)
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
79 print(background.stage.name)
8
f3ff96192476 Fix z-sorting issues
Thibaut Girka <thib@sitedethib.com>
parents: 7
diff changeset
80
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 frame = 0
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 # Main loop
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 clock = pygame.time.Clock()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 while True:
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
86 # Check events
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 for event in pygame.event.get():
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 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
89 sys.exit(0)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 elif event.type == pygame.KEYDOWN:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 pygame.display.toggle_fullscreen()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
94 # Update game
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
95 background.update(frame)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
96
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
97 # Draw everything
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
99
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
100 glVertexPointer(3, GL_FLOAT, 0, background._vertices)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
101 glTexCoordPointer(2, GL_FLOAT, 0, background._uvs)
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
102
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
103 fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
104 x, y, z = background.position_interpolator.values
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
105 unknownx, dy, dz = background.position2_interpolator.values
7
02a5f5314a19 Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents: 6
diff changeset
106
02a5f5314a19 Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents: 6
diff changeset
107 glFogi(GL_FOG_MODE, GL_LINEAR)
02a5f5314a19 Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents: 6
diff changeset
108 glFogf(GL_FOG_START, fog_start)
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
109 glFogf(GL_FOG_END, fog_end)
7
02a5f5314a19 Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents: 6
diff changeset
110 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
111
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
112 #TODO
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113 glMatrixMode(GL_MODELVIEW)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114 glLoadIdentity()
11
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
115 # Some explanations on the magic constants:
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
116 # 192. = 384. / 2. = width / 2.
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
117 # 224. = 448. / 2. = height / 2.
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
118 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
119 # This is so that objects on the (O, x, y) plane use pixel coordinates
7
02a5f5314a19 Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents: 6
diff changeset
120 gluLookAt(192., 224., - 835.979370 * dz,
02a5f5314a19 Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents: 6
diff changeset
121 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
122 #print(glGetFloat(GL_MODELVIEW_MATRIX))
aa201d8cfc19 Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
123 glTranslatef(-x, -y, -z)
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
124
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 11
diff changeset
125 glDrawArrays(GL_QUADS, 0, background.nb_vertices)
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
126
11
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
127 #TODO: show the game itself
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
128 # It is displayed on (0, 0, 0), (0, 448, 0), (388, 448, 0), (388, 0, 0)
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
129 # using a camera at (192, 224, -835.979370) looking right behind itself
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
130 # Depth test should be disabled when rendering the game
548662d70860 Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents: 10
diff changeset
131
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132 pygame.display.flip()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133 clock.tick(120)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
134 frame += 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136
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 try:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
139 file_path, stage_num = sys.argv[1:]
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
140 stage_num = int(stage_num)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
141 except ValueError:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
142 print('Usage: %s std_dat_path stage_num' % sys.argv[0])
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
143 else:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
144 main(file_path, stage_num)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
145