net.java.joglutils.msg.misc
Class Shader

java.lang.Object
  extended by net.java.joglutils.msg.misc.Shader

public class Shader
extends Object

Represents an OpenGL shader program object, which can be constructed from the source code for a vertex shader, a fragment shader, or both. Contains convenience methods for enabling/disabling shader state.

Usage example:

     String source =
         "uniform sampler2D myTex;" +
         "void main(void)" +
         "{" +
         "    vec4 src = texture2D(myTex, gl_TexCoord[0].st);" +
         "    gl_FragColor = src.bgra;" + // swizzle!
         "}";
     Shader shader = new Shader(source);
     shader.setUniform("myTex", 0); // myTex will be on texture unit 0
     ...
     shader.enable();
     texture.enable();
     texture.bind();
     ...
     texture.disable();
     shader.disable();
 };
 

Author:
Chris Campbell

Constructor Summary
Shader(String fragmentCode)
          Creates a new shader program object and compiles/links the provided fragment shader code into that object.
Shader(String vertexCode, String fragmentCode)
          Creates a new shader program object and compiles/links the provided vertex shader and fragment shader code into that object.
 
Method Summary
 void disable()
          Disables this shader program in the current GL context's state.
 void dispose()
          Disposes the native resources used by this program object.
 void enable()
          Enables this shader program in the current GL context's state.
 int getProgramObject()
          Returns the underlying OpenGL program object handle for this fragment shader.
 void setUniform(String name, float f0)
          Sets the uniform variable of the given name with the provided float value.
 void setUniform(String name, float f0, float f1)
          Sets the uniform variable of the given name with the provided float values.
 void setUniform(String name, float f0, float f1, float f2)
          Sets the uniform variable of the given name with the provided float values.
 void setUniform(String name, float f0, float f1, float f2, float f3)
          Sets the uniform variable of the given name with the provided float values.
 void setUniform(String name, int i0)
          Sets the uniform variable of the given name with the provided integer value.
 void setUniform(String name, int i0, int i1)
          Sets the uniform variable of the given name with the provided integer values.
 void setUniform(String name, int i0, int i1, int i2)
          Sets the uniform variable of the given name with the provided integer values.
 void setUniform(String name, int i0, int i1, int i2, int i3)
          Sets the uniform variable of the given name with the provided integer values.
 void setUniformArray1f(String name, int count, float[] vals, int off)
          Sets the uniform array variable of the given name with the provided float array values.
 void setUniformArray1i(String name, int count, int[] vals, int off)
          Sets the uniform array variable of the given name with the provided int array values.
 void setUniformArray2f(String name, int count, float[] vals, int off)
          Sets the uniform array variable of the given name with the provided float array values.
 void setUniformArray2i(String name, int count, int[] vals, int off)
          Sets the uniform array variable of the given name with the provided int array values.
 void setUniformArray3f(String name, int count, float[] vals, int off)
          Sets the uniform array variable of the given name with the provided float array values.
 void setUniformArray3i(String name, int count, int[] vals, int off)
          Sets the uniform array variable of the given name with the provided int array values.
 void setUniformArray4f(String name, int count, float[] vals, int off)
          Sets the uniform array variable of the given name with the provided float array values.
 void setUniformArray4i(String name, int count, int[] vals, int off)
          Sets the uniform array variable of the given name with the provided int array values.
 void setUniformMatrices2f(String name, int count, boolean transpose, float[] vals, int off)
          Sets the uniform matrix (or matrix array) variable of the given name with the provided matrix values.
 void setUniformMatrices3f(String name, int count, boolean transpose, float[] vals, int off)
          Sets the uniform matrix (or matrix array) variable of the given name with the provided matrix values.
 void setUniformMatrices4f(String name, int count, boolean transpose, float[] vals, int off)
          Sets the uniform matrix (or matrix array) variable of the given name with the provided matrix values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Shader

public Shader(String fragmentCode)
       throws javax.media.opengl.GLException
Creates a new shader program object and compiles/links the provided fragment shader code into that object.

Parameters:
fragmentCode - a String representing the fragment shader source code to be compiled and linked
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

Shader

public Shader(String vertexCode,
              String fragmentCode)
       throws javax.media.opengl.GLException
Creates a new shader program object and compiles/links the provided vertex shader and fragment shader code into that object.

Parameters:
vertexCode - a String representing the vertex shader source code to be compiled and linked; this may be null if only a fragment shader is going to be needed
fragmentCode - a String representing the fragment shader source code to be compiled and linked; this may be null if only a vertex shader is going to be needed
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred
Method Detail

getProgramObject

public int getProgramObject()
Returns the underlying OpenGL program object handle for this fragment shader. Most applications will not need to access this, since it is handled automatically by the enable() and dispose() methods.

Returns:
the OpenGL program object handle for this fragment shader

enable

public void enable()
            throws javax.media.opengl.GLException
Enables this shader program in the current GL context's state.

Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

disable

public void disable()
             throws javax.media.opengl.GLException
Disables this shader program in the current GL context's state.

Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

dispose

public void dispose()
             throws javax.media.opengl.GLException
Disposes the native resources used by this program object.

Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniform

public void setUniform(String name,
                       int i0)
                throws javax.media.opengl.GLException
Sets the uniform variable of the given name with the provided integer value.

Parameters:
name - the name of the uniform variable to be set
i0 - the first uniform parameter
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniform

public void setUniform(String name,
                       int i0,
                       int i1)
                throws javax.media.opengl.GLException
Sets the uniform variable of the given name with the provided integer values.

Parameters:
name - the name of the uniform variable to be set
i0 - the first uniform parameter
i1 - the second uniform parameter
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniform

public void setUniform(String name,
                       int i0,
                       int i1,
                       int i2)
                throws javax.media.opengl.GLException
Sets the uniform variable of the given name with the provided integer values.

Parameters:
name - the name of the uniform variable to be set
i0 - the first uniform parameter
i1 - the second uniform parameter
i2 - the third uniform parameter
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniform

public void setUniform(String name,
                       int i0,
                       int i1,
                       int i2,
                       int i3)
                throws javax.media.opengl.GLException
Sets the uniform variable of the given name with the provided integer values.

Parameters:
name - the name of the uniform variable to be set
i0 - the first uniform parameter
i1 - the second uniform parameter
i2 - the third uniform parameter
i3 - the fourth uniform parameter
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniform

public void setUniform(String name,
                       float f0)
                throws javax.media.opengl.GLException
Sets the uniform variable of the given name with the provided float value.

Parameters:
name - the name of the uniform variable to be set
f0 - the first uniform parameter
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniform

public void setUniform(String name,
                       float f0,
                       float f1)
                throws javax.media.opengl.GLException
Sets the uniform variable of the given name with the provided float values.

Parameters:
name - the name of the uniform variable to be set
f0 - the first uniform parameter
f1 - the second uniform parameter
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniform

public void setUniform(String name,
                       float f0,
                       float f1,
                       float f2)
                throws javax.media.opengl.GLException
Sets the uniform variable of the given name with the provided float values.

Parameters:
name - the name of the uniform variable to be set
f0 - the first uniform parameter
f1 - the second uniform parameter
f2 - the third uniform parameter
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniform

public void setUniform(String name,
                       float f0,
                       float f1,
                       float f2,
                       float f3)
                throws javax.media.opengl.GLException
Sets the uniform variable of the given name with the provided float values.

Parameters:
name - the name of the uniform variable to be set
f0 - the first uniform parameter
f1 - the second uniform parameter
f2 - the third uniform parameter
f3 - the fourth uniform parameter
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformArray1i

public void setUniformArray1i(String name,
                              int count,
                              int[] vals,
                              int off)
                       throws javax.media.opengl.GLException
Sets the uniform array variable of the given name with the provided int array values.

Parameters:
name - the name of the uniform variable to be set
count - the number of int elements in the array
vals - the array values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformArray2i

public void setUniformArray2i(String name,
                              int count,
                              int[] vals,
                              int off)
                       throws javax.media.opengl.GLException
Sets the uniform array variable of the given name with the provided int array values.

Parameters:
name - the name of the uniform variable to be set
count - the number of ivec2 elements in the array
vals - the array values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformArray3i

public void setUniformArray3i(String name,
                              int count,
                              int[] vals,
                              int off)
                       throws javax.media.opengl.GLException
Sets the uniform array variable of the given name with the provided int array values.

Parameters:
name - the name of the uniform variable to be set
count - the number of ivec3 elements in the array
vals - the array values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformArray4i

public void setUniformArray4i(String name,
                              int count,
                              int[] vals,
                              int off)
                       throws javax.media.opengl.GLException
Sets the uniform array variable of the given name with the provided int array values.

Parameters:
name - the name of the uniform variable to be set
count - the number of ivec4 elements in the array
vals - the array values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformArray1f

public void setUniformArray1f(String name,
                              int count,
                              float[] vals,
                              int off)
                       throws javax.media.opengl.GLException
Sets the uniform array variable of the given name with the provided float array values.

Parameters:
name - the name of the uniform variable to be set
count - the number of float elements in the array
vals - the array values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformArray2f

public void setUniformArray2f(String name,
                              int count,
                              float[] vals,
                              int off)
                       throws javax.media.opengl.GLException
Sets the uniform array variable of the given name with the provided float array values.

Parameters:
name - the name of the uniform variable to be set
count - the number of vec2 elements in the array
vals - the array values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformArray3f

public void setUniformArray3f(String name,
                              int count,
                              float[] vals,
                              int off)
                       throws javax.media.opengl.GLException
Sets the uniform array variable of the given name with the provided float array values.

Parameters:
name - the name of the uniform variable to be set
count - the number of vec3 elements in the array
vals - the array values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformArray4f

public void setUniformArray4f(String name,
                              int count,
                              float[] vals,
                              int off)
                       throws javax.media.opengl.GLException
Sets the uniform array variable of the given name with the provided float array values.

Parameters:
name - the name of the uniform variable to be set
count - the number of vec4 elements in the array
vals - the array values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformMatrices2f

public void setUniformMatrices2f(String name,
                                 int count,
                                 boolean transpose,
                                 float[] vals,
                                 int off)
                          throws javax.media.opengl.GLException
Sets the uniform matrix (or matrix array) variable of the given name with the provided matrix values.

Parameters:
name - the name of the uniform variable to be set
count - the number of 2x2 matrices (mat2 elements) in the array
transpose - if false, each matrix is assumed to be suppplied in column major order; otherwise assumed to be supplied in row major order
vals - the matrix values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformMatrices3f

public void setUniformMatrices3f(String name,
                                 int count,
                                 boolean transpose,
                                 float[] vals,
                                 int off)
                          throws javax.media.opengl.GLException
Sets the uniform matrix (or matrix array) variable of the given name with the provided matrix values.

Parameters:
name - the name of the uniform variable to be set
count - the number of 3x3 matrices (mat3 elements) in the array
transpose - if false, each matrix is assumed to be suppplied in column major order; otherwise assumed to be supplied in row major order
vals - the matrix values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred

setUniformMatrices4f

public void setUniformMatrices4f(String name,
                                 int count,
                                 boolean transpose,
                                 float[] vals,
                                 int off)
                          throws javax.media.opengl.GLException
Sets the uniform matrix (or matrix array) variable of the given name with the provided matrix values.

Parameters:
name - the name of the uniform variable to be set
count - the number of 4x4 matrices (mat4 elements) in the array
transpose - if false, each matrix is assumed to be suppplied in column major order; otherwise assumed to be supplied in row major order
vals - the matrix values to be set
off - the offset into the vals array
Throws:
javax.media.opengl.GLException - if no OpenGL context was current or if any OpenGL-related errors occurred