unc.cdva.graphics.models
Class ShaderProgram

java.lang.Object
  extended by unc.cdva.graphics.models.ShaderProgram

public class ShaderProgram
extends java.lang.Object

Encapsulate a shader program. Load the source files of the shaders, compile them, link them, and activate them to alter the T&L of the rendering pipeline Do not use if you don't have a video card that supports vertex programs

Author:
Guillaume Conte

Field Summary
 int fragmentObject
          Fragment program id
 javax.media.opengl.GL gl
          Open GL context
 int shaderObject
          Program id
 java.lang.String[] sourceFragment
          Source of the fragment program
 int[] sourceFragmentLength
          Lengths of lines of the fragment program source
 java.lang.String[] sourceVertex
          Source of the vertex program
 int[] sourceVertexLength
          Lengths of lines of the vertex program source
 java.util.Hashtable<java.lang.String,float[]> uniformVariables
          List of the uniform variables used by the program
 int vertexObject
          Vertex program id
 
Constructor Summary
ShaderProgram(javax.media.opengl.GL gl)
          Init an empty shader program
ShaderProgram(java.lang.String vertexShader, java.lang.String fragmentShader, javax.media.opengl.GL gl)
          Init a shader program, and load the source of the vertex and fragment program
 
Method Summary
 void addUniformVariable(java.lang.String name, float[] values)
          Add a uniform variable to the program
 void compileShader()
          Compile the vertex and fragment program, then link the shader program
 int[] computeLengths(java.lang.String[] source)
          Compute the length of the lines of a source code
 void destroy()
          Remove the shader program from the GPU memory
 java.lang.String[] loadSource(java.lang.String file)
          Load a source file from the hard drive
 void printShaderLog(int object)
          Display the compilation/linking log
 void removeUniformVariable(java.lang.String name)
          Remove a uniform variable
 void setSourceFragment(java.lang.String fragmentFile)
           
 void setSourceVertex(java.lang.String vertexFile)
           
 void setUniformVariables()
          Send the uniform variables to the GPU
 void stopUsing()
          Deactivate the shader program
 java.lang.String toString()
           
 void use()
          Activate the shader program
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

sourceVertex

public java.lang.String[] sourceVertex
Source of the vertex program


sourceVertexLength

public int[] sourceVertexLength
Lengths of lines of the vertex program source


sourceFragment

public java.lang.String[] sourceFragment
Source of the fragment program


sourceFragmentLength

public int[] sourceFragmentLength
Lengths of lines of the fragment program source


uniformVariables

public java.util.Hashtable<java.lang.String,float[]> uniformVariables
List of the uniform variables used by the program


gl

public javax.media.opengl.GL gl
Open GL context


shaderObject

public int shaderObject
Program id


vertexObject

public int vertexObject
Vertex program id


fragmentObject

public int fragmentObject
Fragment program id

Constructor Detail

ShaderProgram

public ShaderProgram(javax.media.opengl.GL gl)
Init an empty shader program

Parameters:
gl - OpenGL context

ShaderProgram

public ShaderProgram(java.lang.String vertexShader,
                     java.lang.String fragmentShader,
                     javax.media.opengl.GL gl)
Init a shader program, and load the source of the vertex and fragment program

Parameters:
vertexShader - vertex program source code path
fragmentShader - fragmen program source code path
gl - OpenGL context
Method Detail

compileShader

public void compileShader()
Compile the vertex and fragment program, then link the shader program


use

public void use()
Activate the shader program


stopUsing

public void stopUsing()
Deactivate the shader program


destroy

public void destroy()
Remove the shader program from the GPU memory


printShaderLog

public void printShaderLog(int object)
Display the compilation/linking log

Parameters:
object - id of the object you want to display the log

computeLengths

public int[] computeLengths(java.lang.String[] source)
Compute the length of the lines of a source code

Parameters:
source - source code
Returns:
length of the lines

loadSource

public java.lang.String[] loadSource(java.lang.String file)
Load a source file from the hard drive

Parameters:
file - path of the source file
Returns:
the source code as an array of strings

setSourceFragment

public void setSourceFragment(java.lang.String fragmentFile)
Parameters:
fragmentFile - Path of the fragment file to load.

setSourceVertex

public void setSourceVertex(java.lang.String vertexFile)
Parameters:
vertexFile - Path of the vertex file to load.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()

addUniformVariable

public void addUniformVariable(java.lang.String name,
                               float[] values)
Add a uniform variable to the program

Parameters:
name - name of the variale
values - values of the variable

removeUniformVariable

public void removeUniformVariable(java.lang.String name)
Remove a uniform variable

Parameters:
name - name of the variable

setUniformVariables

public void setUniformVariables()
Send the uniform variables to the GPU