For anyone that is interested here is John's glsl class which can interact with vec's in a glsl shader. In this example it sets the vec4 color of a quad set to the screen. Is getTextureByteBuffer function as fast (the same?) as the glgraphics lib?
This seems like a good place to start for those who want to learn some glsl coding and have access to all the possible variables.
Code:import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
import com.sun.opengl.util.BufferUtil;
import java.nio.*;
import processing.opengl.PGraphicsOpenGL;
GLSL glsl;
PGraphicsOpenGL pgl;
GL gl;
GLU glu = new GLU();
int texture;
PImage img;
void setup(){
size(640, 480, OPENGL);
glsl=new GLSL();
glsl.loadFragmentShader("data/Paint.glsl");
glsl.useShaders();
img = createImage(width, height, ARGB);
pgl = (PGraphicsOpenGL) g;
gl = pgl.gl;
gl.glShadeModel(GL.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
}
void draw(){
pgl.beginGL();
glsl.startShader();
glsl.setUniformValue4f(glsl.getUniformLocation("Color"), 0.0,1.0,1.0,1.0);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, img.width, img.height, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, getTextureByteBuffer( false ));
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glBegin(GL.GL_QUADS);
gl.glNormal3f(0.0f,0.0f,1.0f);
gl.glTexCoord2f(0, 0); gl.glVertex2f(0, height);
gl.glTexCoord2f(1, 0); gl.glVertex2f(width,height);
gl.glTexCoord2f(1, 1); gl.glVertex2f(width, 0);
gl.glTexCoord2f(0, 1); gl.glVertex2f(0, 0);
gl.glEnd();
glsl.endShader();
pgl.endGL();
}
ByteBuffer getTextureByteBuffer ( boolean useAlphaChannel )
{
int bytesPerPixel = useAlphaChannel ? 4 : 3;
ByteBuffer unpackedPixels = BufferUtil.newByteBuffer( img.pixels.length * bytesPerPixel );
for (int row = img.height - 1; row >= 0; row--) {
for (int col = 0; col < img.width; col++) {
int packedPixel = img.pixels[row * img.width + col];
unpackedPixels.put((byte) ((packedPixel >> 16) & 0xFF));
unpackedPixels.put((byte) ((packedPixel >> 8) & 0xFF));
unpackedPixels.put((byte) ((packedPixel >> 0) & 0xFF));
if ( useAlphaChannel ) {
unpackedPixels.put((byte) ((packedPixel >> 24) & 0xFF));
}
}
}
unpackedPixels.flip();
return unpackedPixels;
}
GLSL Class by John
Code:class GLSL
{
int programObject;
GL gl;
boolean vertexShaderEnabled;
boolean vertexShaderSupported;
int vs;
int fs;
GLSL()
{
gl=((PGraphicsOpenGL)g).gl;
String extensions = gl.glGetString(GL.GL_EXTENSIONS);
vertexShaderSupported = extensions.indexOf("GL_ARB_vertex_shader") != -1;
vertexShaderEnabled = true;
programObject = gl.glCreateProgramObjectARB();
vs=-1;
fs=-1;
}
void loadVertexShader(String file)
{
String shaderSource=join(loadStrings(file),"\n");
vs = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);
gl.glShaderSourceARB(vs, 1, new String[]{shaderSource},(int[]) null, 0);
gl.glCompileShaderARB(vs);
checkLogInfo(gl, vs);
gl.glAttachObjectARB(programObject, vs);
}
void loadFragmentShader(String file)
{
String shaderSource=join(loadStrings(file),"\n");
fs = gl.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB);
gl.glShaderSourceARB(fs, 1, new String[]{shaderSource},(int[]) null, 0);
gl.glCompileShaderARB(fs);
checkLogInfo(gl, fs);
gl.glAttachObjectARB(programObject, fs);
}
int getAttribLocation(String name)
{
return(gl.glGetAttribLocationARB(programObject,name));
}
int getUniformLocation(String name)
{
int uniformLocation = gl.glGetUniformLocationARB(programObject,name);
// println(uniformLocation);
return(uniformLocation);
}
void setUniformValue2f(int uniformLocation, float v0, float v1) {
gl.glUniform2f(uniformLocation, v0, v1);
}
void setUniformValue4f(int uniformLocation, float v0, float v1, float v2, float v3) {
gl.glUniform4f(uniformLocation, v0,v1,v2,v3);
}
void useShaders()
{
gl.glLinkProgramARB(programObject);
gl.glValidateProgramARB(programObject);
checkLogInfo(gl, programObject);
}
void startShader()
{
gl.glUseProgramObjectARB(programObject);
}
void endShader()
{
gl.glUseProgramObjectARB(0);
}
void checkLogInfo(GL gl, int obj) {
IntBuffer iVal = BufferUtil.newIntBuffer(1);
gl.glGetObjectParameterivARB(obj, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
int length = iVal.get();
if (length <= 1) {
return;
}
ByteBuffer infoLog = BufferUtil.newByteBuffer(length);
iVal.flip();
gl.glGetInfoLogARB(obj, length, iVal, infoLog);
byte[] infoBytes = new byte[length];
infoLog.get(infoBytes);
println("GLSL Validation >> " + new String(infoBytes));
}
}
GLSL Frag Shader
Code:uniform vec4 Color;
void main()
{
gl_FragColor = Color;
}