com.threed.jpct.util
Class GLSLShader

java.lang.Object
  extended bycom.threed.jpct.util.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.
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.
Note: Because jPCT doesn't support vertex attributes ATM, there are no setters for these.


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 excludeShadowMap(boolean exclude)
          If set to true (default), no shaders will be applied when rendering into a depth map.
static int guessShaderModel()
          Guesses the shader model.
 void onDispose()
          Releases resourced used by the shader.
 boolean repeatRendering()
          Always returns false.
 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 setMinShaderModel(int min)
          Sets the minimum shader model that this shader needs.
 void setShadowHelper(ShadowHelper helper)
           
 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[] 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, SimpleVector val)
          Sets a new 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 SimpleVector 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, SimpleVector val)
          Sets a new SimpleVector uniform.
 
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.

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.

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)

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[] 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.

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.

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,
                       float[] 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. Supports array-lengths from 1 to 4.

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