Here is the latest rough first draft of what we are thinking in terms of programmable shader support in Java 3DTM for 1.4. We plan to formalize the 1.4 API specification under the auspices of the Java Community Process (JCP). However, we very much want to involve the larger community in API discussions for programmable shaders, so please join in the discussion.
We have created a thread on the Java 3D discussion forum for discussing Java 3D Programmable Shaders. Feel free to post your comments on our ideas, or post some ideas of your own.
The proposed class hierarchy for programmable shaders in Java 3D is:
public abstract class ShaderProgram extends NodeComponent public abstract class Shader extends NodeComponent
public class GLSLShaderProgram extends ShaderProgram method: {add/remove/get}Shader(GLSLShader) // set of shader objs method: {add/remove}ErrorListener(GLSLErrorListener) method: validate(Canvas3D) ??? public abstract class GLSLShader extends Shader public class GLSLVertexShader extends GLSLShader method: set/getShaderSource(String) method: validate(Canvas3D) ??? public class GLSLFragmentShader extends GLSLShader method: set/getShaderSource(String) method: validate(Canvas3D) ???
public class CgShaderProgram extends ShaderProgram method: {set/get}VertexShader(CgVertexShader) method: {set/get}FragmentShader(CgFramentShader) method: {add/remove}ErrorListener(CgErrorListener) method: validate(Canvas3D) ??? public abstract class CgShader extends Shader public class CgVertexShader extends CgShader method: set/getShaderSource(String) method: validate(Canvas3D) ??? public class CgFragmentShader extends CgShader method: set/getShaderSource(String) method: validate(Canvas3D) ???
public class ShaderAppearance extends Appearance method: set/getShaderProgram(ShaderProgram) method: set/getShaderAttributeSet(ShaderAttributeSet)
public class ShaderAttributeSet extends NodeComponent method: put/get(ShaderAttribute) ...
Click on the following link for a current look at the javadoc-generated API definitions for the proposed 1.4 API.
This is an example code excerpt showing how one might use the new programmable shader API in a Java 3D program.
String vertexShaderFile = "my-vertex-shader-file-name"; String fragmentShaderFile = "my-fragment-shader-file-name"; String vertexShaderSource; String fragmentShaderSource; // Read GLSL vertex and fragment shader source code from text files vertexShaderSource = StringIO.readFully(vertexShaderFile); fragmentShaderSource = StringIO.readFully(fragmentShaderSource); // Create GLSL vertex and fragment shader objects using the given source code GLSLVertexShader vertexShader = new GLSLVertexShader(vertexShaderSource); GLSLFragmentShader fragmentShader = new GLSLFragmentShader(fragmentShaderSource); // Create the GLSL shader program object and attach the vertex and // fragment shader objects; add an error listener GLSLShaderProgram shaderProgram = new GLSLShaderProgram(); shaderProgram.setVertexShader(vertexShader); shaderProgram.setFragmentShader(fragmentShader); shaderProgram.addErrorListener(myGLSLErrorListener); // Use GLSL shader program object in appearance shaderAppearance.setShaderProgram(shaderProgram);
Programmable shaders define two types of parameters: uniform and varying. As the names imply, uniform parameters are constant (within a primitive), while varying parameters can vary on per-vertex or per-fragment basis.
Java 3D
Attribute |
Cg
shader variable |
GLSL
shader variable |
ModelViewProjection |
glstate.matrix.mvp |
gl_ModelViewProjectionMatrix |
Light[n] pos |
glstate.light[n].position | gl_LightSource[n].position |
... |
... |
... |
TODO: more info here.
Page last updated — $Date$