Package toon :: Package effects :: Module lumakey
[hide private]
[frames] | no frames]

Source Code for Module toon.effects.lumakey

  1  #!/usr/bin/env python 
  2  """ 
  3  GLSL shaders with SDL, OpenGL texture and Python 
  4   
  5  To test with glslang-run :  
  6  ./scripts/glslang-run -f lumakey -d 1 
  7  telnet localhost 15555 
  8  b 0,0,0 
  9  x invert 1 
 10  x alpha_over 0 
 11  x alpha_under 1 
 12  """ 
 13  from pygame.locals import * 
 14  from OpenGL.GL import * 
 15  from OpenGL.error import GLError 
 16  from OpenGL.GLU import * 
 17  from rats.glsl import ShaderProgram 
 18  from rats.glsl import ShaderError 
 19  from toon import fx 
 20  from toon import optgroup 
 21   
22 -class LumaKeyOptions(optgroup.OptionsGroup):
23 - def __init__(self):
24 self.avgluma = [1.0, 1.0, 1.0] 25 self.luma_gate = 0.3 26 self.saturation = 1.0 27 self.contrast = 1.0 28 self.invert = 1 # 0 or 1 29 self.brightness = 1.0 30 self.alpha_under = 1.0 31 self.alpha_over = 0.0 32 self.texture_id = 0
33 34 # ---------------------------- glsl vertex shader ---------- 35 vert = """ 36 /** 37 * Vertex shader that does nothing 38 */ 39 // variables passed to the fragment shader 40 varying vec2 texcoord0; 41 varying vec2 texdim0; 42 43 void main() 44 { 45 gl_Position = ftransform(); 46 texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); 47 texdim0 = vec2(abs(gl_TextureMatrix[0][0][0]), abs(gl_TextureMatrix[0][1][1])); 48 } 49 """ 50 51 # ---------------------------- glsl fragment shader ---------- 52 frag = """ 53 #extension GL_ARB_texture_rectangle : enable 54 /** 55 * Binary luma keying. 56 * 57 * :author: Alexandre Quessy <alexandre@quessy.net> 2009 58 * :license: GNU Public License version 3 59 */ 60 61 //// user-configurable variables (read-only) 62 uniform float luma_gate; // [0., 1.] 63 uniform vec3 avgluma; 64 uniform float saturation; 65 uniform float contrast; 66 uniform int invert; 67 uniform float brightness; 68 uniform float alpha_under; 69 uniform float alpha_over; 70 71 // the texture 72 uniform sampler2DRect image; 73 74 // constants 75 const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721); 76 const vec3 unity = vec3(1.0, 1.0, 1.0); 77 // data passed from vertex shader: 78 varying vec2 texcoord0; 79 varying vec2 texdim0; 80 81 void main(void) 82 { 83 // begin applying brcosa 84 vec3 input_color = texture2DRect(image, texcoord0).rgb; 85 vec3 intensity = vec3(dot(input_color, LumCoeff)); 86 vec3 color = mix(intensity, input_color, saturation); 87 float alpha = alpha_under; 88 // check if close to target luminosity 89 float input_alpha = gl_Color.a; 90 if (intensity[0] >= luma_gate) 91 { 92 alpha = alpha_over; 93 } 94 // finish applying brcosa 95 color = mix(avgluma, color, contrast); 96 if (invert == 1) 97 { 98 color = unity - color; 99 } 100 color *= brightness; 101 gl_FragColor = vec4(color, alpha * input_alpha); 102 } 103 """ 104
105 -class LumaKeyEffect(fx.Effect):
106 """ 107 Makes all the image desaturated expect target color. 108 """
109 - def __init__(self):
110 fx.Effect.__init__(self) 111 self.program = None 112 self.options = LumaKeyOptions() 113 self.name = "lumakey"
114 #print("init %s" % (self.name)) 115
116 - def setup(self):
117 global vert 118 global frag 119 if True: 120 #try: 121 self.program = ShaderProgram() 122 self.program.add_shader_text(GL_VERTEX_SHADER_ARB, vert) 123 self.program.add_shader_text(GL_FRAGMENT_SHADER_ARB, frag) 124 self.program.linkShaders() 125 #except Exception, e: 126 # print(e.message) 127 #else: 128 self.loaded = True
129 #print("Set up effect %s" % (self.name)) 130 #print(self.config) 131
132 - def pre_draw(self):
133 if self.enabled and self.loaded: 134 try: 135 self.program.enable() 136 except Exception, e: 137 print(e.message) 138 self.program.glUniform1i("image", self.options.texture_id) 139 self.program.glUniform1f("luma_gate", self.options.luma_gate) 140 self.program.glUniform3f("avgluma", *self.options.avgluma) 141 self.program.glUniform1f("alpha_over", self.options.alpha_over) 142 self.program.glUniform1f("alpha_under", self.options.alpha_under) 143 self.program.glUniform1f("saturation", self.options.saturation) 144 self.program.glUniform1f("contrast", self.options.contrast) 145 self.program.glUniform1f("brightness", self.options.brightness) 146 self.program.glUniform1i("invert", self.options.invert)
147
148 - def post_draw(self):
149 if self.enabled and self.loaded: 150 try: 151 self.program.disable() 152 except Exception, e: 153 print e.message
154
155 -def create_effect():
156 return LumaKeyEffect()
157