annotate pytouhou/opengl/gamerenderer.py @ 126:9d7129ee2c4f

Fix a rendering bug
author Thibaut Girka <thib@sitedethib.com>
date Sat, 10 Sep 2011 15:35:18 +0200
parents 0313ca2c50e9
children 11ab06f4c4c6
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
125
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
17 import ctypes
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
19 import pyglet
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
20 from pyglet.gl import *
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 from pytouhou.opengl.texture import TextureManager
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 from pytouhou.opengl.sprite import get_sprite_rendering_data
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 from pytouhou.opengl.background import get_background_rendering_data
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26
125
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
27 MAX_ELEMENTS = 10000
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
28
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
29
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
30 class GameRenderer(pyglet.window.Window):
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 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
32 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
33 self.keys = pyglet.window.key.KeyStateHandler()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
34 self.push_handlers(self.keys)
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.texture_manager = TextureManager(resource_loader)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
38 self.fps_display = pyglet.clock.ClockDisplay()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
39
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 self.game = game
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 self.background = background
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
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 def start(self, width=384, height=448):
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
45 self.set_size(width, height)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 # Initialize OpenGL
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 glMatrixMode(GL_PROJECTION)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 glLoadIdentity()
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 gluPerspective(30, float(width)/float(height),
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 101010101./2010101., 101010101./10101.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 glEnable(GL_BLEND)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 glEnable(GL_TEXTURE_2D)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 glEnable(GL_FOG)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 glHint(GL_FOG_HINT, GL_NICEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 glEnableClientState(GL_COLOR_ARRAY)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 glEnableClientState(GL_VERTEX_ARRAY)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61
125
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
62 # Allocate buffers
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
63 buff = ctypes.c_buffer(MAX_ELEMENTS * 4 * (3 * 4 + 2 * 4 + 4))
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
64 self.buffers = (buff,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
65 ctypes.byref(buff, 3 * 4),
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
66 ctypes.byref(buff, 3 * 4 + 2 * 4))
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
67
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
68 pyglet.clock.schedule_interval(self.update, 1./120.)
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
69 pyglet.app.run()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
70
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 def on_resize(self, width, height):
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
73 glViewport(0, 0, width, height)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
74
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
75
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
76 def update(self, dt):
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
77 if self.background:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
78 self.background.update(self.game.game_state.frame)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
79 if self.game:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
80 self.game.run_iter(0) #TODO: self.keys...
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
81
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
82
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
83 def on_key_press(self, symbol, modifiers):
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
84 if symbol == pyglet.window.key.ESCAPE:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
85 pyglet.app.exit()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
86 # 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
87 elif symbol == pyglet.window.key.F11:
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
88 self.set_fullscreen(not self.fullscreen)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
89
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 def render_elements(self, elements):
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 texture_manager = self.texture_manager
125
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
93
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
94 pack_data = struct.Struct('fff ff BBBB' * 4).pack_into
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
95 _vertices, _uvs, _colors = self.buffers
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
96
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
97 nb_vertices = 0
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
98 indices_by_texture = {}
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
99
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
100 for element in elements:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
101 sprite = element._sprite
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
102 if sprite:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
103 ox, oy = element.x, element.y
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
104 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
126
9d7129ee2c4f Fix a rendering bug
Thibaut Girka <thib@sitedethib.com>
parents: 125
diff changeset
105 rec = indices_by_texture.setdefault(key, [])
125
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
106
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
107 # Pack data in buffer
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
108 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
109 r1, g1, b1, a1, r2, g2, b2, a2, r3, g3, b3, a3, r4, g4, b4, a4 = colors
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
110 u1, v1, u2, v2, u3, v3, u4, v4 = uvs
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
111 pack_data(_vertices, nb_vertices * (3 * 4 + 2 * 4 + 4),
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
112 x1 + ox, y1 + oy, z1,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
113 u1, v1,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
114 r1, g1, b1, a1,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
115
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
116 x2 + ox, y2 + oy, z2,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
117 u2, v2,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
118 r2, g2, b2, a2,
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119
125
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
120 x3 + ox, y3 + oy, z3,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
121 u3, v3,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
122 r3, g3, b3, a3,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
123
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
124 x4 + ox, y4 + oy, z4,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
125 u4, v4,
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
126 r4, g4, b4, a4)
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
127
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
128 # Add indices
126
9d7129ee2c4f Fix a rendering bug
Thibaut Girka <thib@sitedethib.com>
parents: 125
diff changeset
129 index = nb_vertices
9d7129ee2c4f Fix a rendering bug
Thibaut Girka <thib@sitedethib.com>
parents: 125
diff changeset
130 rec.extend((index, index + 1, index + 2, index + 3))
125
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
131
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
132 nb_vertices += 4
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
133
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
134 glVertexPointer(3, GL_FLOAT, 24, _vertices)
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
135 glTexCoordPointer(2, GL_FLOAT, 24, _uvs)
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
136 glColorPointer(4, GL_UNSIGNED_BYTE, 24, _colors)
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
137
126
9d7129ee2c4f Fix a rendering bug
Thibaut Girka <thib@sitedethib.com>
parents: 125
diff changeset
138 for (texture_key, blendfunc), indices in indices_by_texture.items():
9d7129ee2c4f Fix a rendering bug
Thibaut Girka <thib@sitedethib.com>
parents: 125
diff changeset
139 nb_indices = len(indices)
125
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
140 indices = struct.pack(str(nb_indices) + 'H', *indices)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
141 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
142 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id)
125
0313ca2c50e9 Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
143 glDrawElements(GL_QUADS, nb_indices, GL_UNSIGNED_SHORT, indices)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
144
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
145
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
146 def on_draw(self):
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
147 glClear(GL_DEPTH_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 back = self.background
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
150 game = self.game
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
151 texture_manager = self.texture_manager
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
152
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
153 if back is not None:
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 108
diff changeset
154 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
155 x, y, z = back.position_interpolator.values
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
156 dx, dy, dz = back.position2_interpolator.values
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
157
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
158 glFogi(GL_FOG_MODE, GL_LINEAR)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
159 glFogf(GL_FOG_START, fog_start)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
160 glFogf(GL_FOG_END, fog_end)
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
161 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
162
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
163 glMatrixMode(GL_MODELVIEW)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
164 glLoadIdentity()
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
165 # Some explanations on the magic constants:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
166 # 192. = 384. / 2. = width / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
167 # 224. = 448. / 2. = height / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
168 # 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
169 # 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
170 gluLookAt(192., 224., - 835.979370 * dz,
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
171 192. + dx, 224. - dy, 0., 0., -1., 0.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
172 glTranslatef(-x, -y, -z)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
173
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
174 glEnable(GL_DEPTH_TEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
175 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
176 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
177 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
178 glVertexPointer(3, GL_FLOAT, 0, vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
179 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
180 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
181 glDrawArrays(GL_QUADS, 0, nb_vertices)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
182 glDisable(GL_DEPTH_TEST)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
183 else:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
184 glClear(GL_COLOR_BUFFER_BIT)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
185
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
186 if game is not None:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
187 glMatrixMode(GL_MODELVIEW)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
188 glLoadIdentity()
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
189 # Some explanations on the magic constants:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
190 # 192. = 384. / 2. = width / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
191 # 224. = 448. / 2. = height / 2.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
192 # 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
193 # 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
194 gluLookAt(192., 224., - 835.979370,
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
195 192., 224., 0., 0., -1., 0.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
196
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
197 glDisable(GL_FOG)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
198 self.render_elements(game.enemies)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
199 self.render_elements(game.game_state.bullets)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
200 glEnable(GL_FOG)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
201
119
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
202 #TODO
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
203 glMatrixMode(GL_MODELVIEW)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
204 glLoadIdentity()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
205 gluLookAt(192., 224., 835.979370,
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
206 192, 224., 0., 0., 1., 0.)
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
207 self.fps_display.draw()
fad7b44cebf2 Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
208