Using custom shaders in Kivy
Apart from GLSL, we also need to have the usual Python code that initializes the window, loads shaders, and so on. The following program will serve as a good starting point:
from kivy.app import App from kivy.base import EventLoop from kivy.graphics import Mesh from kivy.graphics.instructions import RenderContext from kivy.uix.widget import Widget class GlslDemo(Widget): def __init__(self, **kwargs): Widget.__init__(self, **kwargs) self.canvas = RenderContext(use_parent_projection=True) self.canvas.shader.source = 'basic.glsl' # Set up geometry here. class GlslApp(App): def build(self): EventLoop.ensure_window() return GlslDemo() if __name__ == '__main__': GlslApp().run()
We created just one widget named GlslDemo
in this example; it will host all the rendering. RenderContext
is a customizable Canvas
subclass that allows us to replace shaders easily, as shown in the listing. The basic.glsl
file contains...