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. It is compatible with P2D and P3D, but not with the default renderer. Use the loadShader() function to load your shader code. Note: It's strongly encouraged to use loadShader() to create a PShader object, rather than calling the PShader constructor manually.
  • Field Details

    • POINT

      protected static final int POINT
      See Also:
    • LINE

      protected static final int LINE
      See Also:
    • POLY

      protected static final int POLY
      See Also:
    • COLOR

      protected static final int COLOR
      See Also:
    • LIGHT

      protected static final int LIGHT
      See Also:
    • TEXTURE

      protected static final int TEXTURE
      See Also:
    • TEXLIGHT

      protected static final int TEXLIGHT
      See Also:
    • pointShaderAttrRegexp

      protected static String pointShaderAttrRegexp
    • pointShaderInRegexp

      protected static String pointShaderInRegexp
    • lineShaderAttrRegexp

      protected static String lineShaderAttrRegexp
    • lineShaderInRegexp

      protected static String lineShaderInRegexp
    • pointShaderDefRegexp

      protected static String pointShaderDefRegexp
    • lineShaderDefRegexp

      protected static String lineShaderDefRegexp
    • colorShaderDefRegexp

      protected static String colorShaderDefRegexp
    • lightShaderDefRegexp

      protected static String lightShaderDefRegexp
    • texShaderDefRegexp

      protected static String texShaderDefRegexp
    • texlightShaderDefRegexp

      protected static String texlightShaderDefRegexp
    • polyShaderDefRegexp

      protected static String polyShaderDefRegexp
    • triShaderAttrRegexp

      protected static String triShaderAttrRegexp
    • quadShaderAttrRegexp

      protected static String quadShaderAttrRegexp
    • parent

      protected PApplet parent
    • primaryPG

      protected PGraphicsOpenGL primaryPG
    • currentPG

      protected PGraphicsOpenGL currentPG
    • pgl

      protected PGL pgl
    • context

      protected int context
    • type

      protected int type
    • glProgram

      public int glProgram
    • glVertex

      public int glVertex
    • glFragment

      public int glFragment
    • vertexURL

      protected URL vertexURL
    • fragmentURL

      protected URL fragmentURL
    • vertexFilename

      protected String vertexFilename
    • fragmentFilename

      protected String fragmentFilename
    • vertexShaderSource

      protected String[] vertexShaderSource
    • fragmentShaderSource

      protected String[] fragmentShaderSource
    • bound

      protected boolean bound
    • uniformValues

      protected HashMap<String,PShader.UniformValue> uniformValues
    • textures

      protected HashMap<Integer,Texture> textures
    • texUnits

      protected HashMap<Integer,Integer> texUnits
    • intBuffer

      protected IntBuffer intBuffer
    • floatBuffer

      protected FloatBuffer floatBuffer
    • loadedAttributes

      protected boolean loadedAttributes
    • loadedUniforms

      protected boolean loadedUniforms
    • transformMatLoc

      protected int transformMatLoc
    • modelviewMatLoc

      protected int modelviewMatLoc
    • projectionMatLoc

      protected int projectionMatLoc
    • ppixelsLoc

      protected int ppixelsLoc
    • ppixelsUnit

      protected int ppixelsUnit
    • viewportLoc

      protected int viewportLoc
    • resolutionLoc

      protected int resolutionLoc
    • perspectiveLoc

      protected int perspectiveLoc
    • scaleLoc

      protected int scaleLoc
    • lightCountLoc

      protected int lightCountLoc
    • lightPositionLoc

      protected int lightPositionLoc
    • lightNormalLoc

      protected int lightNormalLoc
    • lightAmbientLoc

      protected int lightAmbientLoc
    • lightDiffuseLoc

      protected int lightDiffuseLoc
    • lightSpecularLoc

      protected int lightSpecularLoc
    • lightFalloffLoc

      protected int lightFalloffLoc
    • lightSpotLoc

      protected int lightSpotLoc
    • texture

      protected Texture texture
    • texUnit

      protected int texUnit
    • textureLoc

      protected int textureLoc
    • texMatrixLoc

      protected int texMatrixLoc
    • texOffsetLoc

      protected int texOffsetLoc
    • tcmat

      protected float[] tcmat
    • vertexLoc

      protected int vertexLoc
    • colorLoc

      protected int colorLoc
    • normalLoc

      protected int normalLoc
    • texCoordLoc

      protected int texCoordLoc
    • normalMatLoc

      protected int normalMatLoc
    • directionLoc

      protected int directionLoc
    • offsetLoc

      protected int offsetLoc
    • ambientLoc

      protected int ambientLoc
    • specularLoc

      protected int specularLoc
    • emissiveLoc

      protected int emissiveLoc
    • shininessLoc

      protected int shininessLoc
  • Constructor Details

    • 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 Details

    • 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)
      Sets the uniform variables inside the shader to modify the effect while the program is running.
      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
    • setup

      protected void setup()
      Extra initialization method that can be used by subclasses, called after compiling and attaching the vertex and fragment shaders, and before linking the shader program.
    • draw

      protected void draw(int idxId, int count, int offset)
    • getAttributeLoc

      protected int getAttributeLoc(String name)
      Returns the ID location of the attribute parameter given its name.
      Parameters:
      name - String
      Returns:
      int
    • getUniformLoc

      protected int getUniformLoc(String name)
      Returns the ID location of the uniform parameter given its name.
      Parameters:
      name - String
      Returns:
      int
    • setAttributeVBO

      protected void setAttributeVBO(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset)
    • setUniformValue

      protected void setUniformValue(int loc, int x)
    • setUniformValue

      protected void setUniformValue(int loc, int x, int y)
    • setUniformValue

      protected void setUniformValue(int loc, int x, int y, int z)
    • setUniformValue

      protected void setUniformValue(int loc, int x, int y, int z, int w)
    • setUniformValue

      protected void setUniformValue(int loc, float x)
    • setUniformValue

      protected void setUniformValue(int loc, float x, float y)
    • setUniformValue

      protected void setUniformValue(int loc, float x, float y, float z)
    • setUniformValue

      protected void setUniformValue(int loc, float x, float y, float z, float w)
    • setUniformVector

      protected void setUniformVector(int loc, int[] vec, int ncoords, int length)
    • setUniformVector

      protected void setUniformVector(int loc, float[] vec, int ncoords, int length)
    • setUniformMatrix

      protected void setUniformMatrix(int loc, float[] mat)
    • setUniformTex

      protected void setUniformTex(int loc, Texture tex)
    • setUniformImpl

      protected void setUniformImpl(String name, int type, Object value)
    • consumeUniforms

      protected void consumeUniforms()
    • updateIntBuffer

      protected void updateIntBuffer(int[] vec)
    • updateFloatBuffer

      protected void updateFloatBuffer(float[] vec)
    • bindTextures

      protected void bindTextures()
    • unbindTextures

      protected void unbindTextures()
    • init

      public void init()
    • create

      protected void create()
    • compile

      protected boolean compile()
    • validate

      protected void validate()
    • contextIsOutdated

      protected boolean contextIsOutdated()
    • hasVertexShader

      protected boolean hasVertexShader()
    • hasFragmentShader

      protected boolean hasFragmentShader()
    • compileVertexShader

      protected boolean compileVertexShader()
    • compileFragmentShader

      protected boolean compileFragmentShader()
    • dispose

      protected void dispose()
    • getShaderType

      protected static int getShaderType(String[] source, int defaultType)
    • getType

      protected int getType()
    • setType

      protected void setType(int type)
    • hasType

      protected boolean hasType()
    • isPointShader

      protected boolean isPointShader()
    • isLineShader

      protected boolean isLineShader()
    • isPolyShader

      protected boolean isPolyShader()
    • checkPolyType

      protected boolean checkPolyType(int type)
    • getLastTexUnit

      protected int getLastTexUnit()
    • setRenderer

      protected void setRenderer(PGraphicsOpenGL pg)
    • loadAttributes

      protected void loadAttributes()
    • loadUniforms

      protected void loadUniforms()
    • setCommonUniforms

      protected void setCommonUniforms()
    • bindTyped

      protected void bindTyped()
    • unbindTyped

      protected void unbindTyped()
    • setTexture

      protected void setTexture(Texture tex)
    • supportsTexturing

      protected boolean supportsTexturing()
    • supportLighting

      protected boolean supportLighting()
    • accessTexCoords

      protected boolean accessTexCoords()
    • accessNormals

      protected boolean accessNormals()
    • accessLightAttribs

      protected boolean accessLightAttribs()
    • setVertexAttribute

      protected void setVertexAttribute(int vboId, int size, int type, int stride, int offset)
    • setColorAttribute

      protected void setColorAttribute(int vboId, int size, int type, int stride, int offset)
    • setNormalAttribute

      protected void setNormalAttribute(int vboId, int size, int type, int stride, int offset)
    • setTexcoordAttribute

      protected void setTexcoordAttribute(int vboId, int size, int type, int stride, int offset)
    • setAmbientAttribute

      protected void setAmbientAttribute(int vboId, int size, int type, int stride, int offset)
    • setSpecularAttribute

      protected void setSpecularAttribute(int vboId, int size, int type, int stride, int offset)
    • setEmissiveAttribute

      protected void setEmissiveAttribute(int vboId, int size, int type, int stride, int offset)
    • setShininessAttribute

      protected void setShininessAttribute(int vboId, int size, int type, int stride, int offset)
    • setLineAttribute

      protected void setLineAttribute(int vboId, int size, int type, int stride, int offset)
    • setPointAttribute

      protected void setPointAttribute(int vboId, int size, int type, int stride, int offset)