1
2 """
3 GLSL shaders with SDL, OpenGL texture and Python
4 """
5 from pygame.locals import *
6 from OpenGL.GL import *
7 from OpenGL.error import GLError
8 from OpenGL.GLU import *
9 from rats.glsl import ShaderProgram
10 from rats.glsl import ShaderError
11 from toon import fx
12 from toon import optgroup
13
14
15
16
17
18 vert = """
19 varying vec2 texcoord0;
20 varying vec2 texdim0;
21 void main()
22 {
23 gl_Position = ftransform();
24 texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);
25 texdim0 = vec2(abs(gl_TextureMatrix[0][0][0]), abs(gl_TextureMatrix[0][1][1]));
26 }
27 """
28
29
30
31
32
33
34
35
36
37 frag = """
38 #extension GL_ARB_texture_rectangle : enable
39 // user configurable variables (read-only here)
40 uniform vec3 keying_color;
41 uniform vec3 thresh;
42 uniform float alpha_under_thresh;
43 // the texture
44 uniform sampler2DRect image;
45 // data passed from vertex shader:
46 varying vec2 texcoord0;
47 varying vec2 texdim0;
48
49 void main(void)
50 {
51 vec3 input_color = texture2DRect(image, texcoord0).rgb;
52 float input_alpha = gl_Color.a;
53 float output_alpha = input_alpha;
54 vec3 delta = abs(input_color - keying_color);
55 if (delta.r <= thresh.r && delta.g <= thresh.g && delta.b <= thresh.b)
56 {
57 output_alpha = alpha_under_thresh;
58 }
59 gl_FragColor = vec4(input_color, output_alpha);
60 }
61 """
62
65 self.color = [0.2, 0.8, 0.0]
66 self.thresh = [0.8, 0.8, 0.8]
67 self.texture_id = 0
68 self.alpha_under_thresh = 0.1
69
76
77
91
92
105
106
107 - def post_draw(self):
108 if self.enabled and self.loaded:
109 try:
110 self.program.disable()
111 except Exception, e:
112 print e.message
113
116