| Trees | Indices | Help |
|
|---|
|
|
1 #!/usr/bin/env python
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 # Vertex shader that does nothing
15 #
16 # :texcoord0: variable passed to the fragment shader
17 # :texdim0: variable passed to the fragment shader
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 # fragment shader for adjusting brightness, contrast and saturation
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 """
63
66 fx.Effect.__init__(self)
67 self.program = None
68 self.options = BrCoSaOptions()
69 self.name = "brcosa"
70 #print("init %s" % (self.name))
71
73 global vert
74 global frag
75 if True:
76 #try:
77 self.program = ShaderProgram()
78 self.program.add_shader_text(GL_VERTEX_SHADER_ARB, vert)
79 self.program.add_shader_text(GL_FRAGMENT_SHADER_ARB, frag)
80 self.program.linkShaders()
81 #except Exception, e:
82 # print(e.message)
83 #else:
84 self.loaded = True
85
87 if self.enabled and self.loaded:
88 try:
89 self.program.enable()
90 except Exception, e:
91 print(e.message)
92 self.program.glUniform1i("image", self.options.texture_id)
93 self.program.glUniform1f("saturation", self.options.saturation)
94 self.program.glUniform1f("contrast", self.options.contrast)
95 self.program.glUniform1f("brightness", self.options.brightness)
96 self.program.glUniform1f("alpha", self.options.alpha)
97 self.program.glUniform3f("avgluma", *self.options.avgluma)
98
105
107 return BrCoSaEffect()
108
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Tue Dec 29 13:08:13 2009 | http://epydoc.sourceforge.net |