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

Source Code for Module toon.effects.leavecolor

  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 -class LeaveColorOptions(optgroup.OptionsGroup):
15 - def __init__(self):
16 self.target_color = [1.0, 0.0, 0.0] 17 self.avgluma = [1.0, 1.0, 1.0] 18 self.thresh = [0.5, 0.5, 0.5] 19 self.leave_saturation = 1.0 20 self.allover_saturation = 0.0 21 self.contrast = 1.0 22 self.brightness = 1.0 23 self.alpha = 1.0 24 self.texture_id = 0
25 26 # ---------------------------- glsl vertex shader ---------- 27 vert = """ 28 /** 29 * Vertex shader that does nothing 30 */ 31 // variables passed to the fragment shader 32 varying vec2 texcoord0; 33 varying vec2 texdim0; 34 35 void main() 36 { 37 gl_Position = ftransform(); 38 texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); 39 texdim0 = vec2(abs(gl_TextureMatrix[0][0][0]), abs(gl_TextureMatrix[0][1][1])); 40 } 41 """ 42 43 # ---------------------------- glsl fragment shader ---------- 44 frag = """ 45 #extension GL_ARB_texture_rectangle : enable 46 /** 47 * Leave color effect. 48 * 49 * :param target_color: The RGB keying color that will be made transparent. 50 * :param thresh: The distance from the color for a pixel to disappear. 51 * 52 * :author: Alexandre Quessy <alexandre@quessy.net> 2009 53 * :license: GNU Public License version 3 54 */ 55 56 //// user-configurable variables (read-only) 57 uniform vec3 target_color; 58 uniform vec3 thresh; // [0, 1.732] 59 uniform vec3 avgluma; 60 uniform float leave_saturation; 61 uniform float allover_saturation; 62 uniform float contrast; 63 uniform float brightness; 64 uniform float alpha; 65 66 // the texture 67 uniform sampler2DRect image; 68 69 // constants 70 const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721); 71 // data passed from vertex shader: 72 varying vec2 texcoord0; 73 varying vec2 texdim0; 74 75 void main(void) 76 { 77 // begin applying brcosa 78 vec3 input_color = texture2DRect(image, texcoord0).rgb; 79 vec3 intensity = vec3(dot(input_color, LumCoeff)); 80 vec3 color = mix(intensity, input_color, allover_saturation); 81 // check if close to target color 82 float input_alpha = gl_Color.a; 83 vec3 delta = abs(input_color - target_color); 84 if (delta.r <= thresh.r && delta.g <= thresh.g && delta.b <= thresh.b) 85 { 86 color = mix(intensity, input_color, leave_saturation); 87 } 88 // finish applying brcosa 89 color = mix(avgluma, color, contrast); 90 color *= brightness; 91 gl_FragColor = vec4(color, alpha * input_alpha); 92 } 93 """ 94
95 -class LeaveColorEffect(fx.Effect):
96 """ 97 Makes all the image desaturated expect target color. 98 """
99 - def __init__(self):
100 fx.Effect.__init__(self) 101 self.program = None 102 self.options = LeaveColorOptions() 103 self.name = "leavecolor"
104 #print("init %s" % (self.name)) 105
106 - def setup(self):
107 global vert 108 global frag 109 if True: 110 #try: 111 self.program = ShaderProgram() 112 self.program.add_shader_text(GL_VERTEX_SHADER_ARB, vert) 113 self.program.add_shader_text(GL_FRAGMENT_SHADER_ARB, frag) 114 self.program.linkShaders() 115 #except Exception, e: 116 # print(e.message) 117 #else: 118 self.loaded = True
119 #print("Set up effect %s" % (self.name)) 120 #print(self.config) 121
122 - def pre_draw(self):
123 if self.enabled and self.loaded: 124 try: 125 self.program.enable() 126 except Exception, e: 127 print(e.message) 128 self.program.glUniform1i("image", self.options.texture_id) 129 self.program.glUniform3f("target_color", *self.options.target_color) 130 self.program.glUniform3f("thresh", *self.options.thresh) 131 self.program.glUniform3f("avgluma", *self.options.avgluma) 132 self.program.glUniform1f("alpha", self.options.alpha) 133 self.program.glUniform1f("leave_saturation", self.options.leave_saturation) 134 self.program.glUniform1f("allover_saturation", self.options.allover_saturation) 135 self.program.glUniform1f("contrast", self.options.contrast) 136 self.program.glUniform1f("brightness", self.options.brightness)
137
138 - def post_draw(self):
139 if self.enabled and self.loaded: 140 try: 141 self.program.disable() 142 except Exception, e: 143 print e.message
144
145 -def create_effect():
146 return LeaveColorEffect()
147