comparison pytouhou/opengl/sprite.py @ 108:2a03940deea3

Move everything graphical to pytouhou.opengl!
author Thibaut Girka <thib@sitedethib.com>
date Tue, 06 Sep 2011 00:26:13 +0200
parents
children 4300a832f033
comparison
equal deleted inserted replaced
107:5d9052b9a4e8 108:2a03940deea3
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15
16 from math import pi
17
18 from pytouhou.utils.matrix import Matrix
19
20
21 def get_sprite_rendering_data(sprite):
22 if not sprite._changed:
23 return sprite._rendering_data
24
25 if sprite.fade_interpolator:
26 sprite.fade_interpolator.update(sprite.frame)
27 sprite.alpha = int(sprite.fade_interpolator.values[0])
28
29 if sprite.scale_interpolator:
30 sprite.scale_interpolator.update(sprite.frame)
31 sprite.rescale = sprite.scale_interpolator.values
32
33 if sprite.offset_interpolator:
34 sprite.offset_interpolator.update(sprite.frame)
35 sprite.dest_offset = sprite.offset_interpolator.values
36
37 vertmat = Matrix([[-.5, .5, .5, -.5],
38 [-.5, -.5, .5, .5],
39 [ .0, .0, .0, .0],
40 [ 1., 1., 1., 1.]])
41
42 tx, ty, tw, th = sprite.texcoords
43 sx, sy = sprite.rescale
44 width = sprite.width_override or (tw * sx)
45 height = sprite.height_override or (th * sy)
46
47 vertmat.scale2d(width, height)
48 if sprite.mirrored:
49 vertmat.flip()
50
51 rx, ry, rz = sprite.rotations_3d
52 if sprite.automatic_orientation:
53 rz += pi/2. - sprite.angle
54 elif sprite.force_rotation:
55 rz += sprite.angle
56
57 if (rx, ry, rz) != (0., 0., 0.):
58 if rx:
59 vertmat.rotate_x(-rx)
60 if ry:
61 vertmat.rotate_y(ry)
62 if rz:
63 vertmat.rotate_z(-rz) #TODO: minus, really?
64 if sprite.corner_relative_placement: # Reposition
65 vertmat.translate(width / 2., height / 2., 0.)
66 if sprite.allow_dest_offset:
67 vertmat.translate(*sprite.dest_offset)
68
69 x_1 = 1. / sprite.anm.size[0]
70 y_1 = 1. / sprite.anm.size[1]
71 tox, toy = sprite.texoffsets
72 uvs = [(tx * x_1 + tox, 1. - (ty * y_1) + toy),
73 ((tx + tw) * x_1 + tox, 1. - (ty * y_1) + toy),
74 ((tx + tw) * x_1 + tox, 1. - ((ty + th) * y_1 + toy)),
75 (tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy))]
76
77 d = vertmat.data
78 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.)
79
80 key = (sprite.anm.first_name, sprite.anm.secondary_name), sprite.blendfunc
81 values = zip(d[0], d[1], d[2]), uvs, [(sprite.color[0], sprite.color[1], sprite.color[2], sprite.alpha)] * 4
82 sprite._rendering_data = key, values
83 sprite._changed = False
84
85 return sprite._rendering_data
86