com.threed.jpct
Class GLSLShader

java.lang.Object
  extended bycom.threed.jpct.GLSLShader
All Implemented Interfaces:
IRenderHook

public class GLSLShader
extends java.lang.Object
implements IRenderHook

A simple helper class for GLSL-based shaders. The purpose of this is to ease shader handling somewhat, but it isn't a full blown shader framework and it isn't meant to be one. You can always write your own shaders by hand using the IRenderHook interface (which is what this class implements too).
If the shader fails to compile or any other problem arises, the shader won't be used and an error message will be given.
Note: If you want to assign additional vertex attributes to your shader, you can't do this here but in the Mesh class.
Keep in mind that assigning a shader disables the fixed function pipeline completely for this object. Everything the fixed function pipeline does has to be rebuild in the shader if needed.
Albeit an implementation of IRenderHook and mainly meant to be used in combination with Object3Ds, you may use this class on its own to ease shader management for shaders that aren't applied on Object3Ds, for example in an IPostProcessor of yours. To do this, call beforeRendering() to activate the shader, afterRendering() to disable it and onDispose() if you don't need it anymore.


Constructor Summary
GLSLShader(java.lang.String vertexShaderSrc, java.lang.String fragmentShaderSrc)
          Creates a new instance.
 
Method Summary
 void afterRendering(int polyId)
          Disables the shader.
 void beforeRendering(int polyID)
          Activates the shader.
 void clear()
          Disables the shader.
 void compile(java.util.List attribs)
          For compatibility with jPCT-AE.
 void excludeShadowMap(boolean exclude)
          If set to true (default), no shaders will be applied when rendering into a depth map.
 int getProgram()
          Returns the id of the shader program.
static int guessShaderModel()
          Guesses the shader model.
 void onDispose()
          Releases resourced used by the shader.
 boolean repeatRendering()
          Always returns false.
 void setCurrentObject3D(Object3D obj)
          Does nothing in the default implementation.
 void setCurrentShader(GLSLShader shader)
          Does nothing in the default implemention.
 void setDelayedDisabling(boolean delayed)
          If set to true, the engine can optimize shader usage better but it's not guaranteed, that the shader will be disabled after usage.
 void setFloatArrayUniform(java.lang.String name, float[] val)
          Sets a new float[] uniform.
 void setMinShaderModel(int min)
          Sets the minimum shader model that this shader needs.
 void setShadowHelper(ShadowHelper helper)
          Sets the ShadowHelper instance.
 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[] vals)
          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 setTransparency(float transparency)
          Does nothing in the default implementation.
 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)
          Does nothing in desktop jPCT.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GLSLShader

public GLSLShader(java.lang.String vertexShaderSrc,
                  java.lang.String fragmentShaderSrc)
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:
vertexShaderSrc - the source of the vertex shader
fragmentShaderSrc - the source of the fragment shader
Method Detail

setMinShaderModel

public void setMinShaderModel(int min)
Sets the minimum shader model that this shader needs. There is no way to ask OpenGL about the supported shader model, so this implementation has to guess it. Depending on the hardware and the drivers, this may work or may not. If it can't determine the shader model, any shader will pass regardless of the value set here.

Parameters:
min - the minimum shader model. Default is -1, which means: All are fine.

setCurrentObject3D

public void setCurrentObject3D(Object3D obj)
Does nothing in the default implementation. You can extend this class and override this method if you need the information, which object is currently using this shader.

Specified by:
setCurrentObject3D in interface IRenderHook
Parameters:
obj - the current Object3D

setCurrentShader

public void setCurrentShader(GLSLShader shader)
Does nothing in the default implemention. This method mainly exists to make jPCT more compatible with jPCT-AE. In the context of jPCT, it serves no real purpose.

Specified by:
setCurrentShader in interface IRenderHook
Parameters:
shader - the shader or null if none is used

setTransparency

public void setTransparency(float transparency)
Does nothing in the default implementation. You can extend this class and override this method if you need the information, which transparency is used for this object. This is the value OpenGL uses, not the one assigned to the object, i.e. it has already been processed. This will be called before setCurrentObject3D().

Specified by:
setTransparency in interface IRenderHook

beforeRendering

public void beforeRendering(int polyID)
Activates the shader. Compiles and initializes it, if not yet done. When using the shader on an Object3D, there's no need to call this method directly.

Specified by:
beforeRendering in interface IRenderHook
Parameters:
polyID - the ID of the polygon that is first in the current list that is about to render.

setUniformCache

public void setUniformCache(boolean cacheEnabled)
Does nothing in desktop jPCT. This method is here only to match jPCT-AE's method signatures.

Parameters:
cacheEnabled - does nothing...

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's back.

Returns:
the shader

compile

public void compile(java.util.List attribs)
For compatibility with jPCT-AE. Does nothing here.

Parameters:
attribs -

setDelayedDisabling

public void setDelayedDisabling(boolean delayed)
If set to true, the engine can optimize shader usage better but it's not guaranteed, that the shader will be disabled after usage. If set to false, it's guaranteed that the shader will be disabled right after usage, but performance may suffer.

Parameters:
delayed - should it be delayed?

afterRendering

public void afterRendering(int polyId)
Disables the shader. When using the shader on an Object3D, there's no need to call this method directly.

Specified by:
afterRendering in interface IRenderHook
Parameters:
polyId - the ID of the polygon that is first in the current list that is about to render.

clear

public void clear()
Disables the shader. When using the shader on an Object3D, there's no need to call this method directly.

Specified by:
clear in interface IRenderHook

onDispose

public void onDispose()
Releases resourced used by the shader. When using the shader on an Object3D, there's no need to call this method directly.

Specified by:
onDispose in interface IRenderHook

repeatRendering

public boolean repeatRendering()
Always returns false.

Specified by:
repeatRendering in interface IRenderHook
Returns:
false

setShadowHelper

public void setShadowHelper(ShadowHelper helper)
Sets the ShadowHelper instance. This is useful only in combination with the excludeShadowMap() method below.

Parameters:
helper - the ShadowHelper

excludeShadowMap

public void excludeShadowMap(boolean exclude)
If set to true (default), no shaders will be applied when rendering into a depth map. If set to false, they will.
The ShadowHelper has to be set to activate this option.

Parameters:
exclude - exclude it or not?
See Also:
setShadowHelper(com.threed.jpct.util.ShadowHelper)

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 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,
                             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,
                             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[] vals)
Sets a new array of 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
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,
                       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,
                       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.

guessShaderModel

public static int guessShaderModel()
Guesses the shader model. Return value is somewhere between 1 and 4 or 9999, if no valid guess is possible.

Returns:
the shader model or 9999