annotate pytouhou/opengl/sprite.py @ 120:4300a832f033

Small refactoring and massive performance improvement
author Thibaut Girka <thib@sitedethib.com>
date Thu, 08 Sep 2011 12:46:05 +0200
parents 2a03940deea3
children 0313ca2c50e9
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
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 from math import pi
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 from pytouhou.utils.matrix import Matrix
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
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 def get_sprite_rendering_data(sprite):
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 if not sprite._changed:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 return sprite._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 vertmat = Matrix([[-.5, .5, .5, -.5],
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 [-.5, -.5, .5, .5],
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 [ .0, .0, .0, .0],
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 [ 1., 1., 1., 1.]])
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 tx, ty, tw, th = sprite.texcoords
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 sx, sy = sprite.rescale
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 width = sprite.width_override or (tw * sx)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 height = sprite.height_override or (th * sy)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 vertmat.scale2d(width, height)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 if sprite.mirrored:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 vertmat.flip()
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 rx, ry, rz = sprite.rotations_3d
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 if sprite.automatic_orientation:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 rz += pi/2. - sprite.angle
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 elif sprite.force_rotation:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 rz += sprite.angle
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 if (rx, ry, rz) != (0., 0., 0.):
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 if rx:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 vertmat.rotate_x(-rx)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 if ry:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 vertmat.rotate_y(ry)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 if rz:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 vertmat.rotate_z(-rz) #TODO: minus, really?
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 if sprite.corner_relative_placement: # Reposition
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 vertmat.translate(width / 2., height / 2., 0.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 if sprite.allow_dest_offset:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 vertmat.translate(*sprite.dest_offset)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 x_1 = 1. / sprite.anm.size[0]
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 y_1 = 1. / sprite.anm.size[1]
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 tox, toy = sprite.texoffsets
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 uvs = [(tx * x_1 + tox, 1. - (ty * y_1) + toy),
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 ((tx + tw) * x_1 + tox, 1. - (ty * y_1) + toy),
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 ((tx + tw) * x_1 + tox, 1. - ((ty + th) * y_1 + toy)),
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 (tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy))]
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 d = vertmat.data
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68 key = (sprite.anm.first_name, sprite.anm.secondary_name), sprite.blendfunc
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 values = zip(d[0], d[1], d[2]), uvs, [(sprite.color[0], sprite.color[1], sprite.color[2], sprite.alpha)] * 4
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 sprite._rendering_data = key, values
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 sprite._changed = False
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 return sprite._rendering_data
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74