PShader

processing.opengl

## Class PShader

- java.lang.Object
- - processing.opengl.PShader

- All Implemented Interfaces:
    PConstants

      public class PShader
      extends Object
      implements PConstants

  This class encapsulates a GLSL shader program, including a vertex and a fragment shader. Based on the GLSLShader class from GLGraphics, which in turn was originally based in the code by JohnG: http://processing.org/discourse/beta/num_1159494801.html

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `int` | `glFragment`
    `int` | `glProgram`
    `int` | `glVertex`

    - ### Fields inherited from interface processing.core.PConstants

      `ADD, ALPHA, ALT, AMBIENT, ARC, ARGB, ARROW, BACKSPACE, BASELINE, BEVEL, BEZIER_VERTEX, BLEND, BLUR, BOTTOM, BOX, BREAK, BURN, CENTER, CHATTER, CHORD, CLAMP, CLOSE, CODED, COMPLAINT, CONTROL, CORNER, CORNERS, CROSS, CURVE_VERTEX, CUSTOM, DARKEST, DEG_TO_RAD, DELETE, DIAMETER, DIFFERENCE, DILATE, DIRECTIONAL, DISABLE_DEPTH_MASK, DISABLE_DEPTH_SORT, DISABLE_DEPTH_TEST, DISABLE_NATIVE_FONTS, DISABLE_OPENGL_ERRORS, DISABLE_OPTIMIZED_STROKE, DISABLE_RETINA_PIXELS, DISABLE_STROKE_PERSPECTIVE, DISABLE_STROKE_PURE, DISABLE_TEXTURE_MIPMAPS, DODGE, DOWN, DXF, ELLIPSE, ENABLE_DEPTH_MASK, ENABLE_DEPTH_SORT, ENABLE_DEPTH_TEST, ENABLE_NATIVE_FONTS, ENABLE_OPENGL_ERRORS, ENABLE_OPTIMIZED_STROKE, ENABLE_RETINA_PIXELS, ENABLE_STROKE_PERSPECTIVE, ENABLE_STROKE_PURE, ENABLE_TEXTURE_MIPMAPS, ENTER, EPSILON, ERODE, ERROR_BACKGROUND_IMAGE_FORMAT, ERROR_BACKGROUND_IMAGE_SIZE, ERROR_PUSHMATRIX_OVERFLOW, ERROR_PUSHMATRIX_UNDERFLOW, ERROR_TEXTFONT_NULL_PFONT, ESC, EXCLUSION, GIF, GRAY, GROUP, HALF_PI, HAND, HARD_LIGHT, HINT_COUNT, HSB, IMAGE, INVERT, JAVA2D, JPEG, LANDSCAPE, LEFT, LIGHTEST, LINE_LOOP, LINE_STRIP, LINES, LINUX, MACOSX, MAX_FLOAT, MAX_INT, MIN_FLOAT, MIN_INT, MITER, MODEL, MODELVIEW, MOVE, MULTIPLY, NORMAL, OPAQUE, OPEN, OPENGL, ORTHOGRAPHIC, OTHER, OVERLAY, P2D, P3D, PATH, PDF, PERSPECTIVE, PI, PIE, platformNames, POINTS, POLYGON, PORTRAIT, POSTERIZE, PROBLEM, PROJECT, PROJECTION, QUAD, QUAD_BEZIER_VERTEX, QUAD_STRIP, QUADRATIC_VERTEX, QUADS, QUARTER_PI, RAD_TO_DEG, RADIUS, RECT, REPEAT, REPLACE, RETURN, RGB, RIGHT, ROUND, SCREEN, SHAPE, SHIFT, SOFT_LIGHT, SPHERE, SPOT, SQUARE, SUBTRACT, TAB, TARGA, TAU, TEXT, THIRD_PI, THRESHOLD, TIFF, TOP, TRIANGLE, TRIANGLE_FAN, TRIANGLE_STRIP, TRIANGLES, TWO_PI, UP, VERTEX, WAIT, WHITESPACE, WINDOWS, X, Y, Z`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PShader()`
    `PShader(PApplet parent)`
    `PShader(PApplet parent, String[] vertSource, String[] fragSource)`
    `PShader(PApplet parent, String vertFilename, String fragFilename)` Creates a shader program using the specified vertex and fragment shaders.
    `PShader(PApplet parent, URL vertURL, URL fragURL)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `bind()` Initializes (if needed) and binds the shader program.
    `boolean` | `bound()` Returns true if the shader is bound, false otherwise.
    `void` | `set(String name, boolean x)`
    `void` | `set(String name, boolean[] vec)`
    `void` | `set(String name, boolean[] boolvec, int ncoords)`
    `void` | `set(String name, boolean x, boolean y)`
    `void` | `set(String name, boolean x, boolean y, boolean z)`
    `void` | `set(String name, boolean x, boolean y, boolean z, boolean w)`
    `void` | `set(String name, float x)`
    `void` | `set(String name, float[] vec)`
    `void` | `set(String name, float[] vec, int ncoords)`
    `void` | `set(String name, float x, float y)`
    `void` | `set(String name, float x, float y, float z)`
    `void` | `set(String name, float x, float y, float z, float w)`
    `void` | `set(String name, int x)`
    `void` | `set(String name, int[] vec)`
    `void` | `set(String name, int[] vec, int ncoords)`
    `void` | `set(String name, int x, int y)`
    `void` | `set(String name, int x, int y, int z)`
    `void` | `set(String name, int x, int y, int z, int w)`
    `void` | `set(String name, PImage tex)`
    `void` | `set(String name, PMatrix2D mat)`
    `void` | `set(String name, PMatrix3D mat)`
    `void` | `set(String name, PMatrix3D mat, boolean use3x3)`
    `void` | `set(String name, PVector vec)`
    `void` | `setFragmentShader(String fragFilename)`
    `void` | `setFragmentShader(String[] fragSource)`
    `void` | `setFragmentShader(URL fragURL)`
    `void` | `setVertexShader(String vertFilename)`
    `void` | `setVertexShader(String[] vertSource)`
    `void` | `setVertexShader(URL vertURL)`
    `void` | `unbind()` Unbinds the shader program.

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### glProgram

          public int glProgram

    - #### glVertex

          public int glVertex

    - #### glFragment

          public int glFragment

  - ### Constructor Detail

    - #### PShader

          public PShader()

    - #### PShader

          public PShader(PApplet parent)

    - #### PShader

          public PShader(PApplet parent,
                 String vertFilename,
                 String fragFilename)

      Creates a shader program using the specified vertex and fragment shaders.
      Parameters:
        `parent` - the parent program
        `vertFilename` - name of the vertex shader
        `fragFilename` - name of the fragment shader

    - #### PShader

          public PShader(PApplet parent,
                 URL vertURL,
                 URL fragURL)

      Parameters:
        `vertURL` - network location of the vertex shader
        `fragURL` - network location of the fragment shader

    - #### PShader

          public PShader(PApplet parent,
                 String[] vertSource,
                 String[] fragSource)

  - ### Method Detail

    - #### setVertexShader

          public void setVertexShader(String vertFilename)

    - #### setVertexShader

          public void setVertexShader(URL vertURL)

    - #### setVertexShader

          public void setVertexShader(String[] vertSource)

    - #### setFragmentShader

          public void setFragmentShader(String fragFilename)

    - #### setFragmentShader

          public void setFragmentShader(URL fragURL)

    - #### setFragmentShader

          public void setFragmentShader(String[] fragSource)

    - #### bind

          public void bind()

      Initializes (if needed) and binds the shader program.

    - #### unbind

          public void unbind()

      Unbinds the shader program.

    - #### bound

          public boolean bound()

      Returns true if the shader is bound, false otherwise.

    - #### set

          public void set(String name,
                 int x)

      Parameters:
        `name` - the name of the uniform variable to modify
        `x` - first component of the variable to modify

    - #### set

          public void set(String name,
                 int x,
                 int y)

      Parameters:
        `y` - second component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[2], vec2)

    - #### set

          public void set(String name,
                 int x,
                 int y,
                 int z)

      Parameters:
        `z` - third component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[3], vec3)

    - #### set

          public void set(String name,
                 int x,
                 int y,
                 int z,
                 int w)

      Parameters:
        `w` - fourth component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[4], vec4)

    - #### set

          public void set(String name,
                 float x)

    - #### set

          public void set(String name,
                 float x,
                 float y)

    - #### set

          public void set(String name,
                 float x,
                 float y,
                 float z)

    - #### set

          public void set(String name,
                 float x,
                 float y,
                 float z,
                 float w)

    - #### set

          public void set(String name,
                 PVector vec)

      Parameters:
        `vec` - modifies all the components of an array/vector uniform variable. PVector can only be used if the type of the variable is vec3.

    - #### set

          public void set(String name,
                 boolean x)

    - #### set

          public void set(String name,
                 boolean x,
                 boolean y)

    - #### set

          public void set(String name,
                 boolean x,
                 boolean y,
                 boolean z)

    - #### set

          public void set(String name,
                 boolean x,
                 boolean y,
                 boolean z,
                 boolean w)

    - #### set

          public void set(String name,
                 int[] vec)

    - #### set

          public void set(String name,
                 int[] vec,
                 int ncoords)

      Parameters:
        `ncoords` - number of coordinates per element, max 4

    - #### set

          public void set(String name,
                 float[] vec)

    - #### set

          public void set(String name,
                 float[] vec,
                 int ncoords)

    - #### set

          public void set(String name,
                 boolean[] vec)

    - #### set

          public void set(String name,
                 boolean[] boolvec,
                 int ncoords)

    - #### set

          public void set(String name,
                 PMatrix2D mat)

      Parameters:
        `mat` - matrix of values

    - #### set

          public void set(String name,
                 PMatrix3D mat)

    - #### set

          public void set(String name,
                 PMatrix3D mat,
                 boolean use3x3)

      Parameters:
        `use3x3` - enforces the matrix is 3 x 3

    - #### set

          public void set(String name,
                 PImage tex)

      Parameters:
        `tex` - sets the sampler uniform variable to read from this image texture