Created
January 23, 2014 14:29
-
-
Save QGully/8579328 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.app import App | |
from kivy.core.window import Window | |
from kivy.lang import Builder | |
from kivy.uix.widget import Widget | |
from kivy.graphics.texture import Texture | |
from kivy.graphics import RenderContext | |
from itertools import chain | |
shader_distort_samples = ''' | |
$HEADER$ | |
uniform sampler2D texture1; | |
void main() { | |
vec4 val = texture2D(texture1, vec2(tex_coord0.y, 0)); | |
gl_FragColor = texture2D(texture0, vec2(tex_coord0.x, val.x)); | |
} | |
''' | |
Builder.load_string(''' | |
#:import Logger kivy.logger.Logger | |
<MultitextureWidget>: | |
canvas: | |
Color: | |
rgba: 1, 1, 1, 1 | |
BindTexture: | |
texture: self.samples_texture | |
index: 1 | |
Rectangle: | |
size: self.size | |
pos: self.pos | |
source: 'test.png' | |
''') | |
class MultitextureWidget(Widget): | |
samples_texture = ObjectProperty(None) | |
def __init__(self, **kwargs): | |
self.canvas = RenderContext() | |
self.canvas.shader.fs = shader_distort_samples | |
self.samples_texture = Texture.create(size=(256, 1), colorfmt='RGB') | |
import math | |
samples = list((int(math.floor(math.sin((math.pi/2) * (i / 255.)) * 255.)), 0, 0) for i in range(255)) | |
buf = ''.join(map(chr, chain(*samples))) | |
self.samples_texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte') | |
self.setup_canvas() | |
# set the texture1 to use texture index 1 | |
self.canvas['texture1'] = 1 | |
# call the constructor of parent | |
# if they are any graphics object, they will be added on our new canvas | |
super(MultitextureWidget, self).__init__(**kwargs) | |
# self.update_glsl() | |
def on_size(self, i, v): | |
self.setup_canvas() | |
self.update_glsl() | |
def on_pos(self, i, v): | |
self.setup_canvas() | |
self.update_glsl() | |
def update_glsl(self, *largs): | |
# This is needed for the default vertex shader. | |
self.canvas['projection_mat'] = Window.render_context['projection_mat'] | |
self.canvas['modelview_mat'] = Window.render_context['modelview_mat'] | |
class StretchedImageApp(App): | |
def build(self): | |
return MultitextureWidget() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment