com.threed.jpct
Class GLSLShader

java.lang.Object
  extended by com.threed.jpct.GLSLShader

public class GLSLShader
extends java.lang.Object

A class for GLSL-based shaders. This isn't a full blown shader framework and it isn't meant to be one. This is useful only when using jPCT-AE's OpenGL ES 2.0 pipeline. If you aren't using this class to write your own shaders, jPCT-AE will fall back to its default shaders. Note: If you want to assign additional vertex attributes to your shader, you can't do this here but in the Mesh class.
While this class is similar to the one in desktop jPCT, it works a little different. Especially, it's not an implementation of IRenderHook, so if you want to set uniforms per Object3D on a shared shader, you have to write an additional IRenderHook to do so. However, it's ensured that IRenderHook's beforeRendering()-method will be called before the shader will be initialized, so you can safely set your uniforms there.
jPCT-AE will look out for some uniforms/attributes in the shader to inject it's data into.

uniform mat4 modelViewMatrix; - The model view matrix, i.e. the matrix that makes the transformation from object into camera space
uniform mat4 modelViewProjectionMatrix; - The model view projection matrix, i.e. the model view matrix * projection matrix
uniform mat4 textureMatrix; - The texture matrix
uniform mat4 projectionMatrix - The projection matrix


uniform vec4 additionalColor; - An object's additional color (stored in .rgb of the uniform).
uniform vec4 ambientColor; - The world's ambient color (stored in .rgb of the uniform).


uniform float alpha; - The alpha value of the object.
uniform float shininess; - The shininess if specular lighting is being used.
uniform bool useColors; - true, if the object contains additional vertex colors.


uniform float fogStart; - The depth coordinate, at which the fog starts. -1.0 if there is no fog.
uniform float fogEnd; - The depth coordinate of maximum fog.
uniform vec3 fogColor; - The fog color.


uniform int lightCount; - The number of lights that light this object.


uniform vec3 lightPositions[8]; - The light positions in camera space.
uniform vec3 diffuseColors[8]; - The diffuse color of each light source.
uniform vec3 specularColors[8]; - The specular color of each light source.
uniform float attenuation[8]; - The attenuation of each light source.


uniform sampler2D textureUnit0; - The texture sampler for the first texture layer.
uniform sampler2D textureUnit1; - The texture sampler for the second texture layer.
uniform sampler2D textureUnit2; - The texture sampler for the third texture layer.
uniform sampler2D textureUnit3; - The texture sampler for the forth texture layer.


uniform int textureCount; - The number of texture layers.
uniform int blendingMode[4]; - The blending modes between these layers (0==MODULATE, 1==ADD, 2==REPLACE, 3==BLEND).


attribute vec4 position; - The vertex positions in object space.
attribute vec3 normal; - The vertex normals in object space.
attribute vec4 color; - The additional vertex colors (if any).
attribute vec4 tangent; - The tangent vectors. The presence of this attribute in a shader's source code will trigger the tangent vector calculation when calling build().
attribute vec2 texture0; - The texture coordinates for the first stage.
attribute vec2 texture1; - The texture coordinates for the second stage.
attribute vec2 texture2; - The texture coordinates for the third stage.
attribute vec2 texture3; - The texture coordinates for the forth stage.


Constructor Summary
GLSLShader(java.lang.String vertexShaderSource, java.lang.String fragmentShaderSource)
          Creates a new instance.
 
Method Summary
static void addReplacement(java.lang.String key, java.lang.String shaderSrc)
          With this, you can override the default shaders by making jPCT-AE use your own shader code instead.
 void dispose()
          Disposes the shader.
 void finalize()
           
 int getProgram()
          Returns the id of the shader program.
static ShaderLocator getShaderLocator()
          Returns the shader locator or a new instance of jar-locator if none other has been set.
 boolean isDepthShader()
          Returns if this shader is a depth shader.
 void setDepthShader(boolean depthShader)
          Marks this shader as a depth shader.
 void setFloatArrayUniform(java.lang.String name, float[] val)
          Sets a new float[] uniform.
static void setShaderLocator(ShaderLocator locator)
          Sets a new shader locator.
 void setStaticFloatArrayUniform(java.lang.String name, float[] val)
          Sets a new float[] uniform.
 void setStaticUniform(java.lang.String name, float val)
          Sets a new float uniform.
 void setStaticUniform(java.lang.String name, float[] val)
          Sets a new float|vecX uniform.
 void setStaticUniform(java.lang.String name, int val)
          Sets a new integer uniform.
 void setStaticUniform(java.lang.String name, Matrix m)
          Sets a new Matrix uniform.
 void setStaticUniform(java.lang.String name, Matrix[] ms)
          Sets a new array of Matrix uniform.
 void setStaticUniform(java.lang.String name, SimpleVector val)
          Sets a new SimpleVector uniform.
 void setStaticUniform(java.lang.String name, SimpleVector[] vals)
          Sets a new array of SimpleVector uniform.
 void setUniform(java.lang.String name, float val)
          Sets a new float uniform.
 void setUniform(java.lang.String name, float[] val)
          Sets a new float|vecX uniform.
 void setUniform(java.lang.String name, int val)
          Sets a new integer uniform.
 void setUniform(java.lang.String name, Matrix m)
          Sets a new Matrix uniform.
 void setUniform(java.lang.String name, Matrix[] vals)
          Sets a new array of Matrix uniform.
 void setUniform(java.lang.String name, SimpleVector val)
          Sets a new SimpleVector uniform.
 void setUniform(java.lang.String name, SimpleVector[] vals)
          Sets a new array of SimpleVectors uniform.
 void setUniformCache(boolean cacheEnabled)
          Sets if uniform values can be cached or not.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GLSLShader

public GLSLShader(java.lang.String vertexShaderSource,
                  java.lang.String fragmentShaderSource)
Creates a new instance. This instance can then be passed to any Object3D that should use this shader. You may pass one instance to multiple Object3D's.
Please note that both parameters are not the paths to the shader sources but the shader sources themselves.
The shaders won't be compiled before their first usage, so you may pass bogus shader code here, but you'll not notice it, until you are trying to render the object using that shader.

Parameters:
vertexShaderSource - the source of the vertex shader
fragmentShaderSource - the source of the fragment shader
Method Detail

addReplacement

public static void addReplacement(java.lang.String key,
                                  java.lang.String shaderSrc)
With this, you can override the default shaders by making jPCT-AE use your own shader code instead. Valid names for key are:

defaultFragmentShader.src
defaultFragmentShaderDepth.src
defaultFragmentShaderFog.src
defaultFragmentShaderFogLight0.src
defaultFragmentShaderTex0.src
defaultFragmentShaderTex0Amb.src
defaultFragmentShaderTex0Light0.src
defaultFragmentShaderTex1.src
defaultVertextShader.src
defaultVertexShaderDepth.src
defaultVertexShaderFog.src
defaultVertexShaderFogLight0.src
defaultVertexShaderTex0.src
defaultVertexShaderTex0Amb.src
defaultVertexShaderTex0Light0.src
defaultVertexShaderTex1.src

Parameters:
key -
shaderSrc -

setShaderLocator

public static void setShaderLocator(ShaderLocator locator)
Sets a new shader locator. If this is needed, it has to be done before instantiating the frame buffer. Please refer to the documention of the ShaderLocator for more information.

Parameters:
locator - the new locator

getShaderLocator

public static ShaderLocator getShaderLocator()
Returns the shader locator or a new instance of jar-locator if none other has been set.

Returns:
the locator

setUniformCache

public void setUniformCache(boolean cacheEnabled)
Sets if uniform values can be cached or not. If they can, the shader is allowed to skip a setUniform if the value hasn't changed. If they can't, they will be set in every frame. Default is false.

Parameters:
cacheEnabled - can we cache the uniforms?

getProgram

public int getProgram()
Returns the id of the shader program. Can be useful if you want to fiddle around with the shader behind jPCT-AE's back.

Returns:
the shader

setStaticUniform

public void setStaticUniform(java.lang.String name,
                             int val)
Sets a new integer uniform. Static means, that this value will be set only once. If you want to change the value at runtime, use setUniform(...) instead.

Parameters:
name - the name of the uniform
val - the value

setStaticUniform

public void setStaticUniform(java.lang.String name,
                             float val)
Sets a new float uniform. Static means, that this value will be set only once. If you want to change the value at runtime, use setUniform(...) instead.

Parameters:
name - the name of the uniform
val - the value

setStaticUniform

public void setStaticUniform(java.lang.String name,
                             float[] val)
Sets a new float|vecX uniform. Static means, that this value will be set only once. If you want to change the value at runtime, use setUniform(...) instead.

Parameters:
name - the name of the uniform
val - the value. Supports array-lengths from 1 to 4.

setStaticFloatArrayUniform

public void setStaticFloatArrayUniform(java.lang.String name,
                                       float[] val)
Sets a new float[] uniform. Static means, that this value will be set only once. If you want to change the value at runtime, use setFloatArrayUniform(...) instead.

Parameters:
name - the name of the uniform
val - the value.

setStaticUniform

public void setStaticUniform(java.lang.String name,
                             SimpleVector[] vals)
Sets a new array of SimpleVector uniform. Static means, that this value will be set only once. If you want to change the value at runtime, use setUniform(...) instead.

Parameters:
name - the name of the uniform
vals - the values

setStaticUniform

public void setStaticUniform(java.lang.String name,
                             SimpleVector val)
Sets a new SimpleVector uniform. Static means, that this value will be set only once. If you want to change the value at runtime, use setUniform(...) instead.

Parameters:
name - the name of the uniform
val - the value

setStaticUniform

public void setStaticUniform(java.lang.String name,
                             Matrix m)
Sets a new Matrix uniform. Static means, that this value will be set only once. If you want to change the value at runtime, use setUniform(...) instead.

Parameters:
name - the name of the uniform
m - the matrix. It won't be transposed.

setStaticUniform

public void setStaticUniform(java.lang.String name,
                             Matrix[] ms)
Sets a new array of Matrix uniform. Static means, that this value will be set only once.

Parameters:
name - the name of the uniform
vals - the values

setUniform

public void setUniform(java.lang.String name,
                       Matrix[] vals)
Sets a new array of Matrix uniform. This value will be set each time the shader is executed. You may change it at runtime.

Parameters:
name - the name of the uniform
vals - the values

setUniform

public void setUniform(java.lang.String name,
                       int val)
Sets a new integer uniform. This value will be set each time the shader is executed. You may change it at runtime.

Parameters:
name - the name of the uniform
val - the value

setUniform

public void setUniform(java.lang.String name,
                       float val)
Sets a new float uniform. This value will be set each time the shader is executed. You may change it at runtime.

Parameters:
name - the name of the uniform
val - the value

setUniform

public void setUniform(java.lang.String name,
                       SimpleVector val)
Sets a new SimpleVector uniform. This value will be set each time the shader is executed. You may change it at runtime.

Parameters:
name - the name of the uniform
val - the value

setUniform

public void setUniform(java.lang.String name,
                       SimpleVector[] vals)
Sets a new array of SimpleVectors uniform. This value will be set each time the shader is executed. You may change it at runtime.

Parameters:
name - the name of the uniform
vals - the values

setUniform

public void setUniform(java.lang.String name,
                       float[] val)
Sets a new float|vecX uniform. This value will be set each time the shader is executed. You may change it at runtime.

Parameters:
name - the name of the uniform
val - the value. Supports array-lengths from 1 to 4.

setFloatArrayUniform

public void setFloatArrayUniform(java.lang.String name,
                                 float[] val)
Sets a new float[] uniform. This value will be set each time the shader is executed. You may change it at runtime.

Parameters:
name - the name of the uniform
val - the value.

setUniform

public void setUniform(java.lang.String name,
                       Matrix m)
Sets a new Matrix uniform. This value will be set each time the shader is executed. You may change it at runtime.

Parameters:
name - the name of the uniform
m - the matrix. It won't be transposed.

isDepthShader

public boolean isDepthShader()
Returns if this shader is a depth shader.

Returns:
is it?

setDepthShader

public void setDepthShader(boolean depthShader)
Marks this shader as a depth shader. Depth shaders won't be touched by shadow code injection.

Parameters:
depthShader - true, if it's a depth shader. False otherwise.

dispose

public void dispose()
Disposes the shader. Usually, there's no need to call this.


finalize

public void finalize()
Overrides:
finalize in class java.lang.Object