hmi.graphics.opengl
Class GLShaderProgram

java.lang.Object
  extended by hmi.graphics.opengl.GLShaderProgram
All Implemented Interfaces:
GLRenderObject

public class GLShaderProgram
extends Object
implements GLRenderObject

A GLSL shader program wrapper, in the form of a GLRenderObject. A GLShaderProgram encapsulates a GLSL shader program, but it does not handle particular settings for its uniform variables. The latter aspect is delegated to the GLShader class. GLShaderProgram does contain a few utility methods for obtaining uniform locations, and for extracting uniform declarations from shader code. Basically, GLShaderProgram deals with the shader *texts*, up to and including compilation and linking, and unform declarations within these shader texts.


Field Summary
(package private) static Pattern declPattern
           
private  String fstext
           
private  int shaderId
           
private  String shaderName
           
(package private) static Pattern uniformLinePattern
           
(package private) static Pattern varPattern
           
private  String vstext
           
 
Constructor Summary
GLShaderProgram()
          Default constructor
GLShaderProgram(String shaderName, String vstext, String fstext)
          Creates a new GLShaderProgram, with specified vertex shader and fragment shader texts, and specified uniform names
 
Method Summary
private  int compileShader(GLRenderContext gl, String shaderText, int shaderType)
          Compiles a shader program text, and returns the shader handle.
private  int createShaderProgram(GLRenderContext gl, String vertexShaderText, String fragmentShaderText)
          Compiles vertex/frament program texts, allocates a shader program, attaches the compiles vertex and fragment shaders of the program, and links the program.
 int getProgramId()
          Returns the OpenGL id for the shader program.
 int getShaderUniforms(List<String> types, List<String> names)
           
static List<String> getUniformLines(String shader)
          Detects lines with uniform declarations in shader text Returns a List of Strings, where each String contains the text after the uniform specified, up to (not including) the ; at the end Eaxh of these has the form: type var-0, var-1, ... var-n where each var has optionally an array postfix of the form [nn]
 int[] getUniformLocations(GLRenderContext gl, String... uniformNames)
          Returns the array with OpenGL uniform locations
static int getUniforms(List<String> decls, List<String> types, List<String> names)
          Parses a list of declarations decls.
 void glInit(GLRenderContext gl)
          OpenGL initialization: an OpenGL id is created for this shader program, The shader is compiled and linked, and finally, OpenGL uniform locations are obtained.
 void glRender(GLRenderContext gl)
          Activates thsi shader program as the current shader
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

vstext

private String vstext

fstext

private String fstext

shaderId

private int shaderId

shaderName

private String shaderName

uniformLinePattern

static Pattern uniformLinePattern

declPattern

static Pattern declPattern

varPattern

static Pattern varPattern
Constructor Detail

GLShaderProgram

public GLShaderProgram()
Default constructor


GLShaderProgram

public GLShaderProgram(String shaderName,
                       String vstext,
                       String fstext)
Creates a new GLShaderProgram, with specified vertex shader and fragment shader texts, and specified uniform names

Method Detail

getProgramId

public int getProgramId()
Returns the OpenGL id for the shader program.


getUniformLocations

public int[] getUniformLocations(GLRenderContext gl,
                                 String... uniformNames)
Returns the array with OpenGL uniform locations


glInit

public void glInit(GLRenderContext gl)
OpenGL initialization: an OpenGL id is created for this shader program, The shader is compiled and linked, and finally, OpenGL uniform locations are obtained.

Specified by:
glInit in interface GLRenderObject

glRender

public final void glRender(GLRenderContext gl)
Activates thsi shader program as the current shader

Specified by:
glRender in interface GLRenderObject

createShaderProgram

private int createShaderProgram(GLRenderContext gl,
                                String vertexShaderText,
                                String fragmentShaderText)
Compiles vertex/frament program texts, allocates a shader program, attaches the compiles vertex and fragment shaders of the program, and links the program. The program handle is returned. If an error occurs, -1 is returned.


compileShader

private int compileShader(GLRenderContext gl,
                          String shaderText,
                          int shaderType)
Compiles a shader program text, and returns the shader handle. If an error occurs, -1 is returned.


getShaderUniforms

public int getShaderUniforms(List<String> types,
                             List<String> names)

getUniformLines

public static List<String> getUniformLines(String shader)
Detects lines with uniform declarations in shader text Returns a List of Strings, where each String contains the text after the uniform specified, up to (not including) the ; at the end Eaxh of these has the form: type var-0, var-1, ... var-n where each var has optionally an array postfix of the form [nn]


getUniforms

public static int getUniforms(List<String> decls,
                              List<String> types,
                              List<String> names)
Parses a list of declarations decls. Each String is assumed to be like type var-0, var-1, ... var-n Returns the results in two Lists (types and name) The type String is the type used within the shader code, except that an array type is added to the base type, For instance, a declation like uniform vec3 lightPos[1]; within the shader text ends up in the type List as "vec3[1]"and in the names List as "lightPos". The number of elements added to each of the two Lists is equal to the number of variable names encountered in tghe declarations. This number is returned as result