Next: , Previous: ClutterScript, Up: Top


58 ClutterShaderEffect

Base class for shader effects

58.1 Overview

<clutter-shader-effect> is a class that implements all the plumbing for creating <clutter-effect>s using GLSL shaders.

<clutter-shader-effect> creates an offscreen buffer and then applies the GLSL shader (after checking whether the compilation and linking were successfull) to the buffer before painting it on screen.

58.2 Implementing a ClutterShaderEffect

Creating a sub-class of <clutter-shader-effect> requires the overriding of the paint-target virtual function from the <clutter-offscreen-effect> class as well as the get-static-shader-source virtual from the <clutter-shader-effect> class.

The get-static-shader-source function should return a copy of the shader source to use. This function is only called once per subclass of <clutter-shader-effect> regardless of how many instances of the effect are created. The source for the shader is typically stored in a static const string which is returned from this function via g-strdup.

The paint-target should set the shader's uniforms if any. This is done by calling clutter-shader-effect-set-uniform-value or clutter-shader-effect-set-uniform. The sub-class should then chain up to the <clutter-shader-effect> implementation.

The example below shows a typical implementation of the get-static-shader-source and paint-target phases of a <clutter-shader-effect> sub-class.

     
      static gchar *
      my_effect_get_static_shader_source (ClutterShaderEffect *effect)
      {
        return g_strdup (shader_source);
      }
     
      static gboolean
      my_effect_paint_target (ClutterOffscreenEffect *effect)
      {
        MyEffect *self = MY_EFFECT (effect);
        ClutterShaderEffect *shader = CLUTTER_SHADER_EFFECT (effect);
        ClutterEffectClass *parent_class;
        gfloat component_r, component_g, component_b;
     
        /&#x002A; the "tex" uniform is declared in the shader as:
         &#x002A;
         &#x002A;   uniform int tex;
         &#x002A;
         &#x002A; and it is passed a constant value of 0
         &#x002A;/
        clutter_shader_effect_set_uniform (shader, "tex", G_TYPE_INT, 1, 0);
     
        /&#x002A; the "component" uniform is declared in the shader as:
         &#x002A;
         &#x002A;   uniform vec3 component;
         &#x002A;
         &#x002A; and it's defined to contain the normalized components
         &#x002A; of a ClutterColor
         &#x002A;/
        component_r = self->color.red   / 255.0f;
        component_g = self->color.green / 255.0f;
        component_b = self->color.blue  / 255.0f;
        clutter_shader_effect_set_uniform (shader, "component",
                                           G_TYPE_FLOAT, 3,
                                           component_r,
                                           component_g,
                                           component_b);
     
        /&#x002A; chain up to the parent's implementation &#x002A;/
        parent_class = CLUTTER_OFFSCREEN_EFFECT_CLASS (my_effect_parent_class);
        return parent_class->paint_target (effect);
      }
     

<clutter-shader-effect> is available since Clutter 1.4

58.3 Usage

— Function: clutter-shader-effect-new (shader_type <clutter-shader-type>) ⇒  (ret <clutter-effect>)

Creates a new <clutter-shader-effect>, to be applied to an actor using clutter-actor-add-effect.

The effect will be empty until clutter-shader-effect-set-shader-source is called.

shader-type
the type of the shader, either ‘CLUTTER_FRAGMENT_SHADER’, or ‘CLUTTER_VERTEX_SHADER
ret
the newly created <clutter-shader-effect>. Use g-object-unref when done.

Since 1.8