Table Of Contents
Shader¶
The Shader class handle the compilation of the Vertex and Fragment shader, and the creation of the program in OpenGL.
Todo
Write a more complete documentation about shader.
Header inclusion¶
New in version 1.0.7.
When you are creating a Shader, Kivy will always include default parameters. If you don’t want to rewrite it each time you want to customize / write a new shader, you can add the “$HEADER$” token, and it will be replaced by the corresponding shader header.
Here is the header for Fragment Shader:
#ifdef GL_ES
precision highp float;
#endif
/* Outputs from the vertex shader */
varying vec4 frag_color;
varying vec2 tex_coord0;
/* uniform texture samplers */
uniform sampler2D texture0;
And the header for Vertex Shader:
#ifdef GL_ES
precision highp float;
#endif
/* Outputs to the fragment shader */
varying vec4 frag_color;
varying vec2 tex_coord0;
/* vertex attributes */
attribute vec2 vPosition;
attribute vec2 vTexCoords0;
/* uniform variables */
uniform mat4 modelview_mat;
uniform mat4 projection_mat;
uniform vec4 color;
uniform float opacity;
Single file glsl shader programs¶
New in version 1.6.0.
To simplify shader management, the vertex and fragment shaders can be loaded automatically from a single glsl source file (plain text). The file should contain sections identified by a line starting with ‘—vertex’ and —fragment respectively (case insensitive) like e.g.:
// anything before a meaningful section such as this comment are ignored
---VERTEX SHADER--- // vertex shader starts here
void main(){
...
}
---FRAGMENT SHADER--- // fragment shader starts here
void main(){
...
}
The source property of the Shader should be set tpo the filename of a glsl shader file (of the above format), like e.g. phong.glsl
- class kivy.graphics.shader.Shader¶
Bases: object
Create a vertex or fragment shader
Parameters : - vs: string, default to None
source code for vertex shader
- fs: string, default to None
source code for fragment shader
- fs¶
Fragment shader source code.
If you set a new fragment shader source code, it will be automatically compiled and replace the current one.
- source¶
glsl source code.
source shoudl be a filename of a glsl shader, that contains both vertex and fragment shader sourcecode; each designated by a section header consisting of one line starting with either “–VERTEX” or “–FRAGMENT” (case insensitive).
New in version 1.6.0.
- success¶
Indicate if shader is ok for usage or not.
- vs¶
Vertex shader source code.
If you set a new vertex shader source code, it will be automatically compiled and replace the current one.