Mercurial > touhou
annotate stageviewer.py @ 15:07fba4e1da65
Refactor
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Fri, 05 Aug 2011 21:21:06 +0200 |
parents | 07a7f28c8aaa |
children | 66ce9bb440ac |
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 | 13 from pytouhou.formats.pbg3 import PBG3 |
15 | 14 from pytouhou.formats.std import Stage |
15 from pytouhou.formats.anm0 import Animations | |
13 | 16 from pytouhou.game.background import Background |
14 | 17 from pytouhou.opengl.texture import load_texture |
13 | 18 |
4 | 19 import OpenGL |
20 OpenGL.FORWARD_COMPATIBLE_ONLY = True | |
21 from OpenGL.GL import * | |
22 from OpenGL.GLU import * | |
23 | |
24 | |
25 def main(path, stage_num): | |
26 # Initialize pygame | |
27 pygame.init() | |
28 window = pygame.display.set_mode((384, 448), pygame.OPENGL | pygame.DOUBLEBUF) | |
29 | |
30 # Initialize OpenGL | |
5
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
31 glMatrixMode(GL_PROJECTION) |
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
32 glLoadIdentity() |
10
059ea0ea8d38
Use the original game's near and far planes.
Thibaut Girka <thib@sitedethib.com>
parents:
9
diff
changeset
|
33 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
|
34 |
4 | 35 glEnable(GL_DEPTH_TEST) |
36 glEnable(GL_BLEND) | |
37 glEnable(GL_TEXTURE_2D) | |
7
02a5f5314a19
Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents:
6
diff
changeset
|
38 glEnable(GL_FOG) |
14 | 39 glHint(GL_FOG_HINT, GL_NICEST) |
40 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) | |
41 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | |
4 | 42 glEnableClientState(GL_VERTEX_ARRAY) |
43 glEnableClientState(GL_TEXTURE_COORD_ARRAY) | |
44 | |
45 # Load data | |
46 with open(path, 'rb') as file: | |
47 archive = PBG3.read(file) | |
15 | 48 stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)), stage_num) |
49 background_anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num))) | |
50 background = Background(stage, background_anim) | |
14 | 51 texture = load_texture(archive, background.anim) |
4 | 52 |
13 | 53 print(background.stage.name) |
8 | 54 |
4 | 55 frame = 0 |
56 | |
57 # Main loop | |
58 clock = pygame.time.Clock() | |
59 while True: | |
13 | 60 # Check events |
4 | 61 for event in pygame.event.get(): |
62 if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in (pygame.K_ESCAPE, pygame.K_q)): | |
63 sys.exit(0) | |
64 elif event.type == pygame.KEYDOWN: | |
65 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT: | |
66 pygame.display.toggle_fullscreen() | |
67 | |
13 | 68 # Update game |
69 background.update(frame) | |
70 | |
71 # Draw everything | |
4 | 72 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
73 | |
13 | 74 glVertexPointer(3, GL_FLOAT, 0, background._vertices) |
75 glTexCoordPointer(2, GL_FLOAT, 0, background._uvs) | |
4 | 76 |
13 | 77 fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values |
78 x, y, z = background.position_interpolator.values | |
79 unknownx, dy, dz = background.position2_interpolator.values | |
7
02a5f5314a19
Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents:
6
diff
changeset
|
80 |
02a5f5314a19
Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents:
6
diff
changeset
|
81 glFogi(GL_FOG_MODE, GL_LINEAR) |
02a5f5314a19
Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents:
6
diff
changeset
|
82 glFogf(GL_FOG_START, fog_start) |
13 | 83 glFogf(GL_FOG_END, fog_end) |
7
02a5f5314a19
Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents:
6
diff
changeset
|
84 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
|
85 |
13 | 86 #TODO |
4 | 87 glMatrixMode(GL_MODELVIEW) |
88 glLoadIdentity() | |
11
548662d70860
Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents:
10
diff
changeset
|
89 # Some explanations on the magic constants: |
548662d70860
Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents:
10
diff
changeset
|
90 # 192. = 384. / 2. = width / 2. |
548662d70860
Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents:
10
diff
changeset
|
91 # 224. = 448. / 2. = height / 2. |
548662d70860
Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents:
10
diff
changeset
|
92 # 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
|
93 # 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
|
94 gluLookAt(192., 224., - 835.979370 * dz, |
02a5f5314a19
Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents:
6
diff
changeset
|
95 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
|
96 #print(glGetFloat(GL_MODELVIEW_MATRIX)) |
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
97 glTranslatef(-x, -y, -z) |
4 | 98 |
13 | 99 glDrawArrays(GL_QUADS, 0, background.nb_vertices) |
4 | 100 |
11
548662d70860
Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents:
10
diff
changeset
|
101 #TODO: show the game itself |
548662d70860
Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents:
10
diff
changeset
|
102 # 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
|
103 # 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
|
104 # 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
|
105 |
4 | 106 pygame.display.flip() |
107 clock.tick(120) | |
108 frame += 1 | |
109 | |
110 | |
111 | |
112 try: | |
113 file_path, stage_num = sys.argv[1:] | |
114 stage_num = int(stage_num) | |
115 except ValueError: | |
116 print('Usage: %s std_dat_path stage_num' % sys.argv[0]) | |
117 else: | |
118 main(file_path, stage_num) | |
119 |