annotate pytouhou/opengl/gamerenderer.py @ 122:174324a4da51

Add support for launch animations! (Warning: slow :()
author Thibaut Girka <thib@sitedethib.com>
date Sat, 10 Sep 2011 01:26:30 +0200
parents 4300a832f033
children d1c82d43bbf3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 import struct
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 from itertools import chain
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
18 import pyglet
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
19 from pyglet.gl import *
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 from pytouhou.opengl.texture import TextureManager
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 from pytouhou.opengl.sprite import get_sprite_rendering_data
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 from pytouhou.opengl.background import get_background_rendering_data
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
26 class GameRenderer(pyglet.window.Window):
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 def __init__(self, resource_loader, game=None, background=None):
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
28 pyglet.window.Window.__init__(self, caption='PyTouhou', resizable=False)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
29 self.keys = pyglet.window.key.KeyStateHandler()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
30 self.push_handlers(self.keys)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
31
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 self.texture_manager = TextureManager(resource_loader)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
34 self.fps_display = pyglet.clock.ClockDisplay()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
35
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 self.game = game
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 self.background = background
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 def start(self, width=384, height=448):
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
41 self.set_size(width, height)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 # Initialize OpenGL
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 glMatrixMode(GL_PROJECTION)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 glLoadIdentity()
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 gluPerspective(30, float(width)/float(height),
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 101010101./2010101., 101010101./10101.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 glEnable(GL_BLEND)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 glEnable(GL_TEXTURE_2D)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 glEnable(GL_FOG)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 glHint(GL_FOG_HINT, GL_NICEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 glEnableClientState(GL_COLOR_ARRAY)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 glEnableClientState(GL_VERTEX_ARRAY)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
58 pyglet.clock.schedule_interval(self.update, 1./60)
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
59 pyglet.app.run()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
60
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
61
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
62 def on_resize(self, width, height):
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
63 glViewport(0, 0, width, height)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
64
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
65
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
66 def update(self, dt):
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
67 if self.background:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
68 self.background.update(self.game.game_state.frame)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
69 if self.game:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
70 self.game.run_iter(0) #TODO: self.keys...
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
71
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
72
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
73 def on_key_press(self, symbol, modifiers):
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
74 if symbol == pyglet.window.key.ESCAPE:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
75 pyglet.app.exit()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
76 # XXX: Fullscreen will be enabled the day pyglet stops sucking
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
77 elif symbol == pyglet.window.key.F11:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
78 self.set_fullscreen(not self.fullscreen)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
79
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 def render_elements(self, elements):
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 texture_manager = self.texture_manager
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 objects_by_texture = {}
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 for element in elements:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 sprite = element._sprite
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 if sprite:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 ox, oy = element.x, element.y
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 rec = objects_by_texture.setdefault(key, ([], [], []))
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 vertices = ((x + ox, y + oy, z) for x, y, z in vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 rec[0].extend(vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 rec[1].extend(uvs)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 rec[2].extend(colors)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95 for (texture_key, blendfunc), (vertices, uvs, colors) in objects_by_texture.items():
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96 nb_vertices = len(vertices)
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
97 vertices = struct.pack(str(3 * nb_vertices) + 'f', *chain(*vertices))
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
98 uvs = struct.pack(str(2 * nb_vertices) + 'f', *chain(*uvs))
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
99 colors = struct.pack(str(4 * nb_vertices) + 'B', *chain(*colors))
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
100 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
101 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
102 glVertexPointer(3, GL_FLOAT, 0, vertices)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
103 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
104 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105 glDrawArrays(GL_QUADS, 0, nb_vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
107
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
108 def on_draw(self):
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
109 glClear(GL_DEPTH_BUFFER_BIT)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
110
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
111 back = self.background
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
112 game = self.game
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113 texture_manager = self.texture_manager
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115 if back is not None:
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 108
diff changeset
116 fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117 x, y, z = back.position_interpolator.values
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118 dx, dy, dz = back.position2_interpolator.values
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
120 glFogi(GL_FOG_MODE, GL_LINEAR)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
121 glFogf(GL_FOG_START, fog_start)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
122 glFogf(GL_FOG_END, fog_end)
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
123 glFogfv(GL_FOG_COLOR, (GLfloat * 4)(fog_r / 255., fog_g / 255., fog_b / 255., 1.))
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
124
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
125 glMatrixMode(GL_MODELVIEW)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
126 glLoadIdentity()
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
127 # Some explanations on the magic constants:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
128 # 192. = 384. / 2. = width / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
129 # 224. = 448. / 2. = height / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
130 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
131 # This is so that objects on the (O, x, y) plane use pixel coordinates
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132 gluLookAt(192., 224., - 835.979370 * dz,
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133 192. + dx, 224. - dy, 0., 0., -1., 0.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
134 glTranslatef(-x, -y, -z)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136 glEnable(GL_DEPTH_TEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
137 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back):
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
138 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
139 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
140 glVertexPointer(3, GL_FLOAT, 0, vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
141 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
142 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
143 glDrawArrays(GL_QUADS, 0, nb_vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
144 glDisable(GL_DEPTH_TEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
145 else:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
146 glClear(GL_COLOR_BUFFER_BIT)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
147
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
148 if game is not None:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
149 glMatrixMode(GL_MODELVIEW)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
150 glLoadIdentity()
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
151 # Some explanations on the magic constants:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
152 # 192. = 384. / 2. = width / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
153 # 224. = 448. / 2. = height / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
154 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
155 # This is so that objects on the (O, x, y) plane use pixel coordinates
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
156 gluLookAt(192., 224., - 835.979370,
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
157 192., 224., 0., 0., -1., 0.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
158
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
159 glDisable(GL_FOG)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
160 self.render_elements(game.enemies)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
161 self.render_elements(game.game_state.bullets)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
162 glEnable(GL_FOG)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
163
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
164 #TODO
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
165 glMatrixMode(GL_MODELVIEW)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
166 glLoadIdentity()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
167 gluLookAt(192., 224., 835.979370,
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
168 192, 224., 0., 0., 1., 0.)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
169 self.fps_display.draw()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
170