Your problem is that LightPosition isn't declared as a uniform. It should be declared like this:
uniform vec3 LightPosition;
Add this to the GLSL class:
void uniform3f(int location, float v0, float v1, float v2)
{
gl.glUniform3fARB(location, v0, v1, v2);
}
You will now need to actually initialize the LightPosition parameter. After startShader()
, but before you call glutSolid()
, do this:
glsl.uniform3f(glsl.getUniformLocation("LightPosition"), 10.0, 10.0, 20.0);
That should work (it worked for me).
One option is to pass information via uniform variables.
After
glUseProgram(myShaderProgram);
you can use
GLint myUniformLocation = glGetUniformLocation(myShaderProgram, "myUniform");
and for example
glUniform1f(myUniformLocation, /* some floating point value here */);
In your vertex or fragment shader, you need to add the following declaration:
uniform float myUniform;
That's it, in your shader you can now access (read-only) the value you passed earlier on via glUniform1f
.
Of course uniform variables can be any valid GLSL type including complex types such
as arrays, structures or matrices. OpenGL provides a glUniform
function with the usual suffixes,
appropriate for each type. For example, to assign to a variable of type vec3
, one would
use glUniform3f
or glUniform3fv
.
Note: the value can't be modified during execution of the shader, i.e. in a glBegin
/glEnd
block. It is read-only and the same for every fragment/vertex processed.
There are also several tutorials using uniforms, you can find them by googling "glsl uniform variable".
I hope that helps.
Best Answer
Here is the main sketch:
Here is the GLSL class used:
And the GLSL code, the vertex shader: toon.vs
And the fragment shader: toon.fs
If it helps, here is the zipped Processing project. Once you've installed Processing, unzip the file into the default Processing folder(~/Documents/Processing) and run Processing > it should show under File > Sketchbook
And here's a screenshot:
HTH
Update
Processing now provides a nice PShader class and comprehensive tutorial. It incluses a Toon shader:
ToonVert.glsl:
ToonFrag.glsl: