annotate pytouhou/opengl/gamerenderer.py @ 119:fad7b44cebf2

Switch from pygame + PyOpenGL to pyglet
author Thibaut Girka <thib@sitedethib.com>
date Wed, 07 Sep 2011 18:12:24 +0200
parents 340fcda8e64a
children 4300a832f033
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
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
58 pyglet.clock.schedule_interval(self.update, 1./120)
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
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
67 def update(self, dt):
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
68 if self.background:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
69 self.background.update(self.game.game_state.frame)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
70 if self.game:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
71 self.game.run_iter(0) #TODO: self.keys...
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
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
74 def on_key_press(self, symbol, modifiers):
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
75 if symbol == pyglet.window.key.ESCAPE:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
76 pyglet.app.exit()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
77 # 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
78 elif symbol == pyglet.window.key.F11:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
79 self.set_fullscreen(not self.fullscreen)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
80
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 def render_elements(self, elements):
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 texture_manager = self.texture_manager
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 objects_by_texture = {}
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 for element in elements:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 sprite = element._sprite
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 if sprite:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 ox, oy = element.x, element.y
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 rec = objects_by_texture.setdefault(key, ([], [], []))
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 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
92 rec[0].extend(vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 rec[1].extend(uvs)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 rec[2].extend(colors)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96 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
97 nb_vertices = len(vertices)
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
98 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
99 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
100 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
101 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
102 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
103 glVertexPointer(3, GL_FLOAT, 0, vertices)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
104 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
105 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106 glDrawArrays(GL_QUADS, 0, nb_vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
107
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
108
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
109 def on_draw(self):
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
110 glClear(GL_DEPTH_BUFFER_BIT)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
111
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
112 back = self.background
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113 game = self.game
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114 texture_manager = self.texture_manager
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
116 if back is not None:
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 108
diff changeset
117 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
118 x, y, z = back.position_interpolator.values
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119 dx, dy, dz = back.position2_interpolator.values
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
120
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
121 glFogi(GL_FOG_MODE, GL_LINEAR)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
122 glFogf(GL_FOG_START, fog_start)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
123 glFogf(GL_FOG_END, fog_end)
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
124 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
125
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
126 glMatrixMode(GL_MODELVIEW)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
127 glLoadIdentity()
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
128 # Some explanations on the magic constants:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
129 # 192. = 384. / 2. = width / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
130 # 224. = 448. / 2. = height / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
131 # 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
132 # 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
133 gluLookAt(192., 224., - 835.979370 * dz,
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
134 192. + dx, 224. - dy, 0., 0., -1., 0.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135 glTranslatef(-x, -y, -z)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
137 glEnable(GL_DEPTH_TEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
138 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
139 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
140 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
141 glVertexPointer(3, GL_FLOAT, 0, vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
142 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
143 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
144 glDrawArrays(GL_QUADS, 0, nb_vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
145 glDisable(GL_DEPTH_TEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
146 else:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
147 glClear(GL_COLOR_BUFFER_BIT)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
148
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
149 if game is not None:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
150 glMatrixMode(GL_MODELVIEW)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
151 glLoadIdentity()
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
152 # Some explanations on the magic constants:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
153 # 192. = 384. / 2. = width / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
154 # 224. = 448. / 2. = height / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
155 # 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
156 # 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
157 gluLookAt(192., 224., - 835.979370,
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
158 192., 224., 0., 0., -1., 0.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
159
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
160 glDisable(GL_FOG)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
161 self.render_elements(game.enemies)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
162 self.render_elements(game.game_state.bullets)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
163 glEnable(GL_FOG)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
164
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
165 #TODO
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
166 glMatrixMode(GL_MODELVIEW)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
167 glLoadIdentity()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
168 gluLookAt(192., 224., 835.979370,
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
169 192, 224., 0., 0., 1., 0.)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
170 self.fps_display.draw()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
171