Class Shader

java.lang.Object
arc.graphics.gl.Shader
All Implemented Interfaces:
Disposable
Direct Known Subclasses:
Shaders.LoadShader, Shaders.SurfaceShader

public class Shader extends Object implements Disposable

A shader program encapsulates a vertex and fragment shader pair linked to form a shader program.

After construction a Shader can be used to draw Mesh. To make the GPU use a specific Shader the programs bind() method must be used which effectively binds the program.

When a Shader is bound one can set uniforms, vertex attributes and attributes as needed via the respective methods.

A Shader must be disposed via a call to dispose() when it is no longer needed

ShaderPrograms are managed. In case the OpenGL context is lost all shaders get invalidated and have to be reloaded. This happens on Android when a user switches to another application or receives an incoming call. Managed ShaderPrograms are automatically reloaded when the OpenGL context is recreated so you don't have to do this manually.

  • Field Details

    • positionAttribute

      public static final String positionAttribute
      default name for position attributes
      See Also:
    • normalAttribute

      public static final String normalAttribute
      default name for normal attributes
      See Also:
    • colorAttribute

      public static final String colorAttribute
      default name for color attributes
      See Also:
    • mixColorAttribute

      public static final String mixColorAttribute
      default name for mix color attributes
      See Also:
    • texcoordAttribute

      public static final String texcoordAttribute
      default name for texcoords attributes, append texture unit number
      See Also:
    • pedantic

      public static boolean pedantic
      flag indicating whether attributes & uniforms must be present at all times
    • prependVertexCode

      public static String prependVertexCode
      code that is always added to the vertex shader code, typically used to inject a #version line. Note that this is added as-is, you should include a newline (`\n`) if needed.
    • prependFragmentCode

      public static String prependFragmentCode
      code that is always added to every fragment shader code, typically used to inject a #version line. Note that this is added as-is, you should include a newline (`\n`) if needed.
  • Constructor Details

    • Shader

      public Shader(String vertexShader, String fragmentShader)
      Constructs a new Shader and immediately compiles it.
      Parameters:
      vertexShader - the vertex shader
      fragmentShader - the fragment shader
    • Shader

      public Shader(Fi vertexShader, Fi fragmentShader)
  • Method Details

    • apply

      public void apply()
      Applies all relevant uniforms, if applicable. Should be overridden.
    • preprocess

      protected String preprocess(String source, boolean fragment)
    • createProgram

      protected int createProgram()
    • getLog

      public String getLog()
      Returns:
      the log info for the shader compilation and program linking stage. The shader needs to be bound for this method to have an effect.
    • isCompiled

      public boolean isCompiled()
      Returns:
      whether this Shader compiled successfully.
    • fetchUniformLocation

      public int fetchUniformLocation(String name, boolean pedantic)
    • setUniformi

      public void setUniformi(String name, int value)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      value - the value
    • setUniformi

      public void setUniformi(int location, int value)
    • setUniformi

      public void setUniformi(String name, int value1, int value2)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      value1 - the first value
      value2 - the second value
    • setUniformi

      public void setUniformi(int location, int value1, int value2)
    • setUniformi

      public void setUniformi(String name, int value1, int value2, int value3)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      value1 - the first value
      value2 - the second value
      value3 - the third value
    • setUniformi

      public void setUniformi(int location, int value1, int value2, int value3)
    • setUniformi

      public void setUniformi(String name, int value1, int value2, int value3, int value4)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      value1 - the first value
      value2 - the second value
      value3 - the third value
      value4 - the fourth value
    • setUniformi

      public void setUniformi(int location, int value1, int value2, int value3, int value4)
    • setUniformf

      public void setUniformf(String name, float value)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      value - the value
    • setUniformf

      public void setUniformf(int location, float value)
    • setUniformf

      public void setUniformf(String name, float value1, float value2)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      value1 - the first value
      value2 - the second value
    • setUniformf

      public void setUniformf(int location, float value1, float value2)
    • setUniformf

      public void setUniformf(String name, float value1, float value2, float value3)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      value1 - the first value
      value2 - the second value
      value3 - the third value
    • setUniformf

      public void setUniformf(int location, float value1, float value2, float value3)
    • setUniformf

      public void setUniformf(String name, float value1, float value2, float value3, float value4)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      value1 - the first value
      value2 - the second value
      value3 - the third value
      value4 - the fourth value
    • setUniformf

      public void setUniformf(int location, float value1, float value2, float value3, float value4)
    • setUniform1fv

      public void setUniform1fv(String name, float[] values, int offset, int length)
    • setUniform1fv

      public void setUniform1fv(int location, float[] values, int offset, int length)
    • setUniform2fv

      public void setUniform2fv(String name, float[] values, int offset, int length)
    • setUniform2fv

      public void setUniform2fv(int location, float[] values, int offset, int length)
    • setUniform3fv

      public void setUniform3fv(String name, float[] values, int offset, int length)
    • setUniform3fv

      public void setUniform3fv(int location, float[] values, int offset, int length)
    • setUniform4fv

      public void setUniform4fv(String name, float[] values, int offset, int length)
    • setUniform4fv

      public void setUniform4fv(int location, float[] values, int offset, int length)
    • setUniformMatrix

      public void setUniformMatrix(String name, Mat matrix)
      Sets the uniform matrix with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      matrix - the matrix
    • setUniformMatrix

      public void setUniformMatrix(String name, Mat matrix, boolean transpose)
      Sets the uniform matrix with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      matrix - the matrix
      transpose - whether the uniform matrix should be transposed
    • setUniformMatrix

      public void setUniformMatrix(int location, Mat matrix)
    • setUniformMatrix

      public void setUniformMatrix(int location, Mat matrix, boolean transpose)
    • setUniformMatrix4

      public void setUniformMatrix4(String name, float[] val)
    • setUniformMatrix4

      public void setUniformMatrix4(String name, Mat mat)
    • setUniformMatrix4

      public void setUniformMatrix4(String name, Mat mat, float near, float far)
    • setUniformMatrix3fv

      public void setUniformMatrix3fv(String name, FloatBuffer buffer, int count, boolean transpose)
      Sets an array of uniform matrices with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      buffer - buffer containing the matrix data
      transpose - whether the uniform matrix should be transposed
    • setUniformMatrix4fv

      public void setUniformMatrix4fv(String name, FloatBuffer buffer, int count, boolean transpose)
      Sets an array of uniform matrices with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      buffer - buffer containing the matrix data
      transpose - whether the uniform matrix should be transposed
    • setUniformMatrix4fv

      public void setUniformMatrix4fv(int location, float[] values, int offset, int length)
    • setUniformMatrix4fv

      public void setUniformMatrix4fv(String name, float[] values, int offset, int length)
    • setUniformf

      public void setUniformf(String name, Vec2 values)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      values - x and y as the first and second values respectively
    • setUniformf

      public void setUniformf(int location, Vec2 values)
    • setUniformf

      public void setUniformf(String name, Vec3 values)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      values - x, y and z as the first, second and third values respectively
    • setUniformf

      public void setUniformf(int location, Vec3 values)
    • setUniformf

      public void setUniformf(String name, Color values)
      Sets the uniform with the given name. The Shader must be bound for this to work.
      Parameters:
      name - the name of the uniform
      values - r, g, b and a as the first through fourth values respectively
    • setUniformf

      public void setUniformf(int location, Color values)
    • bind

      public void bind()
      Makes OpenGL ES 2.0 use this vertex and fragment shader pair.
    • dispose

      public void dispose()
      Disposes all resources associated with this shader. Must be called when the shader is no longer used.
      Specified by:
      dispose in interface Disposable
    • isDisposed

      public boolean isDisposed()
      Specified by:
      isDisposed in interface Disposable
    • disableVertexAttribute

      public void disableVertexAttribute(String name)
      Disables the vertex attribute with the given name
      Parameters:
      name - the vertex attribute name
    • hasAttribute

      public boolean hasAttribute(String name)
      Parameters:
      name - the name of the attribute
      Returns:
      whether the attribute is available in the shader
    • getAttributeType

      public int getAttributeType(String name)
      Parameters:
      name - the name of the attribute
      Returns:
      the type of the attribute, one of GL20.GL_FLOAT, GL20.GL_FLOAT_VEC2 etc.
    • getAttributeLocation

      public int getAttributeLocation(String name)
      Parameters:
      name - the name of the attribute
      Returns:
      the location of the attribute or -1.
    • getAttributeSize

      public int getAttributeSize(String name)
      Parameters:
      name - the name of the attribute
      Returns:
      the size of the attribute or 0.
    • hasUniform

      public boolean hasUniform(String name)
      Parameters:
      name - the name of the uniform
      Returns:
      whether the uniform is available in the shader
    • getUniformType

      public int getUniformType(String name)
      Parameters:
      name - the name of the uniform
      Returns:
      the type of the uniform, one of GL20.GL_FLOAT, GL20.GL_FLOAT_VEC2 etc.
    • getUniformLocation

      public int getUniformLocation(String name)
      Parameters:
      name - the name of the uniform
      Returns:
      the location of the uniform or -1.
    • getUniformSize

      public int getUniformSize(String name)
      Parameters:
      name - the name of the uniform
      Returns:
      the size of the uniform or 0.
    • getAttributes

      public String[] getAttributes()
      Returns:
      the attributes
    • getUniforms

      public String[] getUniforms()
      Returns:
      the uniforms
    • getVertexShaderSource

      public String getVertexShaderSource()
      Returns:
      the source of the vertex shader
    • getFragmentShaderSource

      public String getFragmentShaderSource()
      Returns:
      the source of the fragment shader
    • copyTransform

      public static float[] copyTransform(Mat matrix)
    • copyTransform

      public static float[] copyTransform(Mat matrix, float near, float far)