comparison stageviewer.py @ 16:66ce9bb440ac

Refactor in order to support multiple textures
author Thibaut Girka <thib@sitedethib.com>
date Sat, 06 Aug 2011 12:36:25 +0200
parents 07fba4e1da65
children bf225780973f
comparison
equal deleted inserted replaced
15:07fba4e1da65 16:66ce9bb440ac
12 12
13 from pytouhou.formats.pbg3 import PBG3 13 from pytouhou.formats.pbg3 import PBG3
14 from pytouhou.formats.std import Stage 14 from pytouhou.formats.std import Stage
15 from pytouhou.formats.anm0 import Animations 15 from pytouhou.formats.anm0 import Animations
16 from pytouhou.game.background import Background 16 from pytouhou.game.background import Background
17 from pytouhou.opengl.texture import load_texture 17 from pytouhou.opengl.texture import TextureManager
18 18
19 import OpenGL 19 import OpenGL
20 OpenGL.FORWARD_COMPATIBLE_ONLY = True 20 OpenGL.FORWARD_COMPATIBLE_ONLY = True
21 from OpenGL.GL import * 21 from OpenGL.GL import *
22 from OpenGL.GLU import * 22 from OpenGL.GLU import *
43 glEnableClientState(GL_TEXTURE_COORD_ARRAY) 43 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
44 44
45 # Load data 45 # Load data
46 with open(path, 'rb') as file: 46 with open(path, 'rb') as file:
47 archive = PBG3.read(file) 47 archive = PBG3.read(file)
48 texture_manager = TextureManager(archive)
49
48 stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)), stage_num) 50 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))) 51 background_anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num)))
50 background = Background(stage, background_anim) 52 background = Background(stage, background_anim)
51 texture = load_texture(archive, background.anim)
52 53
53 print(background.stage.name) 54 print(background.stage.name)
54 55
55 frame = 0 56 frame = 0
56 57
57 # Main loop 58 # Main loop
58 clock = pygame.time.Clock() 59 clock = pygame.time.Clock()
59 while True: 60 while True:
60 # Check events 61 # Check events
61 for event in pygame.event.get(): 62 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 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 sys.exit(0)
64 elif event.type == pygame.KEYDOWN: 65 elif event.type == pygame.KEYDOWN:
65 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT: 66 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT:
66 pygame.display.toggle_fullscreen() 67 pygame.display.toggle_fullscreen()
67 68
68 # Update game 69 # Update game
69 background.update(frame) 70 background.update(frame)
70 71
71 # Draw everything 72 # Draw everything
72 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
73 74
74 glVertexPointer(3, GL_FLOAT, 0, background._vertices) 75 fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values
75 glTexCoordPointer(2, GL_FLOAT, 0, background._uvs) 76 x, y, z = background.position_interpolator.values
77 unknownx, dy, dz = background.position2_interpolator.values
76 78
77 fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values 79 glFogi(GL_FOG_MODE, GL_LINEAR)
78 x, y, z = background.position_interpolator.values 80 glFogf(GL_FOG_START, fog_start)
79 unknownx, dy, dz = background.position2_interpolator.values 81 glFogf(GL_FOG_END, fog_end)
82 glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.))
80 83
81 glFogi(GL_FOG_MODE, GL_LINEAR) 84 #TODO
82 glFogf(GL_FOG_START, fog_start) 85 glMatrixMode(GL_MODELVIEW)
83 glFogf(GL_FOG_END, fog_end) 86 glLoadIdentity()
84 glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.)) 87 # Some explanations on the magic constants:
88 # 192. = 384. / 2. = width / 2.
89 # 224. = 448. / 2. = height / 2.
90 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
91 # This is so that objects on the (O, x, y) plane use pixel coordinates
92 gluLookAt(192., 224., - 835.979370 * dz,
93 192., 224. - dy, 750 - 835.979370 * dz, 0., -1., 0.) #TODO: 750 might not be accurate
94 #print(glGetFloat(GL_MODELVIEW_MATRIX))
95 glTranslatef(-x, -y, -z)
85 96
86 #TODO 97 for texture_key, (nb_vertices, vertices, uvs) in background.objects_by_texture.items():
87 glMatrixMode(GL_MODELVIEW) 98 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key])
88 glLoadIdentity() 99 glVertexPointer(3, GL_FLOAT, 0, vertices)
89 # Some explanations on the magic constants: 100 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
90 # 192. = 384. / 2. = width / 2. 101 glDrawArrays(GL_QUADS, 0, nb_vertices)
91 # 224. = 448. / 2. = height / 2.
92 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
93 # This is so that objects on the (O, x, y) plane use pixel coordinates
94 gluLookAt(192., 224., - 835.979370 * dz,
95 192., 224. - dy, 750 - 835.979370 * dz, 0., -1., 0.) #TODO: 750 might not be accurate
96 #print(glGetFloat(GL_MODELVIEW_MATRIX))
97 glTranslatef(-x, -y, -z)
98 102
99 glDrawArrays(GL_QUADS, 0, background.nb_vertices) 103 #TODO: show the game itself
104 # It is displayed on (0, 0, 0), (0, 448, 0), (388, 448, 0), (388, 0, 0)
105 # using a camera at (192, 224, -835.979370) looking right behind itself
106 # Depth test should be disabled when rendering the game
100 107
101 #TODO: show the game itself 108 pygame.display.flip()
102 # It is displayed on (0, 0, 0), (0, 448, 0), (388, 448, 0), (388, 0, 0) 109 clock.tick(120)
103 # using a camera at (192, 224, -835.979370) looking right behind itself 110 frame += 1
104 # Depth test should be disabled when rendering the game
105
106 pygame.display.flip()
107 clock.tick(120)
108 frame += 1
109 111
110 112
111 113
112 try: 114 try:
113 file_path, stage_num = sys.argv[1:] 115 file_path, stage_num = sys.argv[1:]