comparison pytouhou/ui/anmrenderer.py @ 405:402e96a0baeb

Make the anmviewer use the programmable pipeline too.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 24 Mar 2013 10:31:23 +0100
parents 690b5faaa0e6
children 5fe6cd6ceb48
comparison
equal deleted inserted replaced
404:6c0cb3eee33e 405:402e96a0baeb
14 ## 14 ##
15 15
16 import pyglet 16 import pyglet
17 import traceback 17 import traceback
18 18
19 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, 19 from pyglet.gl import (glMatrixMode, glEnable,
20 glHint, glEnableClientState, glViewport, 20 glHint, glEnableClientState, glViewport,
21 glLoadMatrixf, GL_PROJECTION, GL_MODELVIEW, 21 glLoadMatrixf, GL_PROJECTION, GL_MODELVIEW,
22 GL_TEXTURE_2D, GL_BLEND, 22 GL_TEXTURE_2D, GL_BLEND,
23 GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST, 23 GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST,
24 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY, 24 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
25 glClearColor, glClear, GL_COLOR_BUFFER_BIT) 25 glClearColor, glClear, GL_COLOR_BUFFER_BIT,
26 glGenBuffers)
26 27
27 from pytouhou.game.sprite import Sprite 28 from pytouhou.game.sprite import Sprite
28 from pytouhou.vm.anmrunner import ANMRunner 29 from pytouhou.vm.anmrunner import ANMRunner
29 30
30 from pytouhou.utils.helpers import get_logger 31 from pytouhou.utils.helpers import get_logger
31 32
32 from .renderer import Renderer 33 from .renderer import Renderer
34 from .shaders.eosd import GameShader
35
36 from ctypes import c_uint
33 37
34 38
35 logger = get_logger(__name__) 39 logger = get_logger(__name__)
36 40
37 41
38 class ANMRenderer(pyglet.window.Window, Renderer): 42 class ANMRenderer(pyglet.window.Window, Renderer):
39 def __init__(self, resource_loader, anm_wrapper, index=0, sprites=False): 43 def __init__(self, resource_loader, anm_wrapper, index=0, sprites=False,
44 fixed_pipeline=False):
40 Renderer.__init__(self, resource_loader) 45 Renderer.__init__(self, resource_loader)
46 self.texture_manager.preload(resource_loader.instanced_anms.values())
41 47
42 width, height = 384, 448 48 width, height = 384, 448
43 pyglet.window.Window.__init__(self, width=width, height=height, 49 pyglet.window.Window.__init__(self, width=width, height=height,
44 caption='PyTouhou', resizable=False) 50 caption='PyTouhou', resizable=False)
51
52 self.use_fixed_pipeline = fixed_pipeline
45 53
46 self._anm_wrapper = anm_wrapper 54 self._anm_wrapper = anm_wrapper
47 self.sprites = sprites 55 self.sprites = sprites
48 self.clear_color = (0., 0., 0., 1.) 56 self.clear_color = (0., 0., 0., 1.)
49 self.force_allow_dest_offset = False 57 self.force_allow_dest_offset = False
59 if (width, height) != (self.width, self.height): 67 if (width, height) != (self.width, self.height):
60 self.set_size(width, height) 68 self.set_size(width, height)
61 69
62 # Initialize OpenGL 70 # Initialize OpenGL
63 glEnable(GL_BLEND) 71 glEnable(GL_BLEND)
64 glEnable(GL_TEXTURE_2D) 72 if self.use_fixed_pipeline:
65 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) 73 glEnable(GL_TEXTURE_2D)
66 glEnableClientState(GL_COLOR_ARRAY) 74 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
67 glEnableClientState(GL_VERTEX_ARRAY) 75 glEnableClientState(GL_COLOR_ARRAY)
68 glEnableClientState(GL_TEXTURE_COORD_ARRAY) 76 glEnableClientState(GL_VERTEX_ARRAY)
77 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
69 78
70 # Switch to game projection 79 # Switch to game projection
71 proj = self.perspective(30, float(self.width) / float(self.height), 80 proj = self.perspective(30, float(self.width) / float(self.height),
72 101010101./2010101., 101010101./10101.) 81 101010101./2010101., 101010101./10101.)
73 glMatrixMode(GL_PROJECTION)
74 glLoadMatrixf(proj.get_c_data())
75
76 view = self.setup_camera(0, 0, 1) 82 view = self.setup_camera(0, 0, 1)
77 glMatrixMode(GL_MODELVIEW) 83
78 glLoadMatrixf(view.get_c_data()) 84 if not self.use_fixed_pipeline:
85 shader = GameShader()
86
87 vbo_array = (c_uint * 1)()
88 glGenBuffers(1, vbo_array)
89 self.vbo, = vbo_array
90
91 mvp = view * proj
92 shader.bind()
93 shader.uniform_matrixf('mvp', mvp.get_c_data())
94 else:
95 glMatrixMode(GL_PROJECTION)
96 glLoadMatrixf(proj.get_c_data())
97
98 glMatrixMode(GL_MODELVIEW)
99 glLoadMatrixf(view.get_c_data())
79 100
80 # Use our own loop to ensure 60 fps 101 # Use our own loop to ensure 60 fps
81 pyglet.clock.set_fps_limit(60) 102 pyglet.clock.set_fps_limit(60)
82 while not self.has_exit: 103 while not self.has_exit:
83 pyglet.clock.tick() 104 pyglet.clock.tick()