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 frag = """
30 #extension GL_ARB_texture_rectangle : enable
31 // user configurable variables (read-only here)
32 varying vec2 texcoord0;
33 varying vec2 texdim0;
34 // the texture
35 uniform sampler2DRect image;
36 // arguments
37 uniform vec3 avgluma;
38 uniform float saturation;
39 uniform float contrast;
40 uniform float brightness;
41 uniform float alpha;
42 // constants
43 const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721);
44 void main (void)
45 {
46 vec3 texColor = texture2DRect(image, texcoord0).rgb;
47 vec3 intensity = vec3(dot(texColor, LumCoeff));
48 vec3 color = mix(intensity, texColor, saturation);
49 color = mix(avgluma, color, contrast);
50 color *= brightness;
51 //gl_FragColor = vec4(color, color.g*alpha);
52 gl_FragColor = vec4(color, alpha);
53 }
54 """
57 self.avgluma = [1.0, 1.0, 1.0]
58 self.saturation = 0.1
59 self.contrast = 1.0
60 self.brightness = 1.0
61 self.alpha = 1.0
62 self.texture_id = 0
63
70
71
85
98
99 - def post_draw(self):
100 if self.enabled and self.loaded:
101 try:
102 self.program.disable()
103 except Exception, e:
104 print e.message
105
108