HI! I am AFJ (Away From Java) for some undetermined time. Back of couple of months i had written some code and text about GLSLShaderObjectsState, and would not want that go to waste. So here is my version of GLSL/LWJGLGLSLShaderObjectsState:
package com.jme.scene.state;
import com.jme.util.ShaderAttributeStore;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import com.jme.math.Matrix3f;
import com.jme.math.Matrix4f;
import com.jme.util.ShaderAttribute;
import com.jme.util.ShaderUniform;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
/**
* Implementation of the GL_ARB_shader_objects extension.
*
* @author Thomas Hourdel
*/
public abstract class GLSLShaderObjectsState extends RenderState {
public ShaderAttributeStore store = new ShaderAttributeStore();
/**
* <code>isSupported</code> determines if the ARB_shader_objects extension
* is supported by current graphics configuration.
*
* @return if ARB shader objects are supported
*/
public abstract boolean isSupported();
/**
* <code>relinkProgram</code> instructs openGL to relink the associated
* program and sets the attributes. This should be used after setting
* ShaderAttributes.
*/
public abstract void relinkProgram();
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value
* the new value
*/
public void setUniform(String var, int value) {
store.setUniform(var, value);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value
* the new value
*/
public void setUniform(String var, float value) {
store.setUniform(var, value);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value1
* the new value
* @param value2
* the new value
*/
public void setUniform(String var, int value1, int value2) {
store.setUniform(var, value1, value2);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value1
* the new value
* @param value2
* the new value
*/
public void setUniform(String var, float value1, float value2) {
store.setUniform(var, value1, value2);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value1
* the new value
* @param value2
* the new value
* @param value3
* the new value
*/
public void setUniform(String var, int value1, int value2, int value3) {
store.setUniform(var, value1, value2, value3);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value1
* the new value
* @param value2
* the new value
* @param value3
* the new value
*/
public void setUniform(String var, float value1, float value2, float value3) {
store.setUniform(var, value1, value2, value3);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value1
* the new value
* @param value2
* the new value
* @param value3
* the new value
* @param value4
* the new value
*/
public void setUniform(String var, int value1, int value2, int value3,
int value4) {
store.setUniform(var, value1, value2, value3, value4);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value1
* the new value
* @param value2
* the new value
* @param value3
* the new value
* @param value4
* the new value
*/
public void setUniform(String var, float value1, float value2,
float value3, float value4) {
store.setUniform(var, value1, value2, value3, value4);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value
* the new value (a float buffer of size 4)
* @param transpose
* transpose the matrix ?
*/
public void setUniform(String var, float value[], boolean transpose) {
store.setUniform(var, value, transpose);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value
* the new value
* @param transpose
* transpose the matrix ?
*/
public void setUniform(String var, Matrix3f value, boolean transpose) {
store.setUniform(var, value, transpose);
}
/**
* Set an uniform value for this shader object.
*
* @param var
* uniform variable to change
* @param value
* the new value
* @param transpose
* transpose the matrix ?
*/
public void setUniform(String var, Matrix4f value, boolean transpose) {
store.setUniform(var, value, transpose);
}
/**
* <code>clearUniforms</code> clears all uniform values from this state.
*
*/
public void clearUniforms() {
store.clearUniforms();
}
/**
* Set an attribute pointer value for this shader object.
*
* @param var
* attribute variable to change
*/
public void setAttributePointer(String var, int size, boolean normalized,
int stride, FloatBuffer data) {
store.setAttributePointer(var, size, normalized, stride, data);
}
/**
* Set an attribute pointer value for this shader object.
*
* @param var
* attribute variable to change
*/
public void setAttributePointer(String var, int size, boolean normalized,
boolean unsigned, int stride, ByteBuffer data) {
store.setAttributePointer(var, size, normalized, unsigned, stride, data);
}
/**
* Set an attribute pointer value for this shader object.
*
* @param var
* attribute variable to change
*/
public void setAttributePointer(String var, int size, boolean normalized,
boolean unsigned, int stride, IntBuffer data) {
store.setAttributePointer(var, size, normalized, unsigned, stride, data);
}
/**
* Set an attribute pointer value for this shader object.
*
* @param var
* attribute variable to change
*/
public void setAttributePointer(String var, int size, boolean normalized,
boolean unsigned, int stride, ShortBuffer data) {
store.setAttributePointer(var, size, normalized, unsigned, stride, data);
}
/**
* <code>clearAttributes</code> clears all attribute values from this state.
*
*/
public void clearAttributes() {
store.clearAttributes();
}
/**
* @return RS_SHADER_OBJECTS
* @see com.jme.scene.state.RenderState#getType()
*/
public int getType() {
return RS_GLSL_SHADER_OBJECTS;
}
/**
* <code>load</code> loads the shader object from the specified file. The
* program must be in ASCII format. We delegate the loading to each
* implementation because we do not know in what format the underlying API
* wants the data.
*
* @param vert
* text file containing the vertex shader object
* @param frag
* text file containing the fragment shader object
*/
public abstract void load(URL vert, URL frag);
public abstract void load(String vert, String frag);
public void release() {
this.setEnabled(false);
}
public void write(JMEExporter e) throws IOException {
super.write(e);
OutputCapsule capsule = e.getCapsule(this);
capsule.writeSavableArrayList(store.uniforms,"uniforms", new ArrayList<ShaderUniform>());
capsule.writeSavableArrayList(store.attribs,"attribs", new ArrayList<ShaderAttribute>());
}
@SuppressWarnings("unchecked")
public void read(JMEImporter e) throws IOException {
super.read(e);
InputCapsule capsule = e.getCapsule(this);
store.uniforms = capsule.readSavableArrayList("uniforms", new ArrayList<ShaderUniform>());
store.attribs = capsule.readSavableArrayList("attribs", new ArrayList<ShaderAttribute>());
}
public Class getClassTag() {
return GLSLShaderObjectsState.class;
}
}
The change in GLSLShaderObjectsState is the usage of ShaderAttributeStore instead of ArrayList-s. More on why this change, later on.