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

Source Code for Module toon.effects.simplechroma

  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   
 29  # Simple fragment shader for boolean chroma-keying.  
 30  #  
 31  # Makes pixels of a texture transparent if proximity to keying_color is under threshold. 
 32  # Make sure to use rectangle textures.  
 33  # :param keying_color: The RGB keying color that will be made transparent. 
 34  # :param thresh: The distance from the color for a pixel to disappear. 
 35  # :author: Alexandre Quessy <alexandre@quessy.net> 2009 
 36  # :license: GNU Public License version 3 
 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  # ----------------------------  functions ---------------------------- 
63 -class SimpleChromaOptions(optgroup.OptionsGroup):
64 - def __init__(self):
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
70 -class SimpleChromaEffect(fx.Effect):
71 - def __init__(self):
72 fx.Effect.__init__(self) 73 self.program = None 74 self.options = SimpleChromaOptions() 75 self.name = "simplechroma"
76 #print("init %s" % (self.name)) 77
78 - def setup(self):
79 global vert 80 global frag 81 if True: 82 #try: 83 self.program = ShaderProgram() 84 self.program.add_shader_text(GL_VERTEX_SHADER_ARB, vert) 85 self.program.add_shader_text(GL_FRAGMENT_SHADER_ARB, frag) 86 self.program.linkShaders() 87 #except Exception, e: 88 # print(e.message) 89 #else: 90 self.loaded = True
91 #print(self.config) 92
93 - def pre_draw(self):
94 if self.enabled and self.loaded: 95 try: 96 self.program.enable() 97 except Exception, e: 98 print(e.message) 99 self.program.glUniform1i("image", self.options.texture_id) 100 self.program.glUniform3f( 101 "keying_color", *self.options.color) 102 self.program.glUniform3f( 103 "thresh", *self.options.thresh) 104 self.program.glUniform1f("alpha_under_thresh", self.options.alpha_under_thresh)
105 #self.config['alpha_under_thresh']) 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
114 -def create_effect():
115 return SimpleChromaEffect()
116