You have to specify the LINE type when calling shader(), it might sound superflous but the reason was to being able to do something like:
- shader(pointSh, POINT);
- shader(lineSh, LINE);
- shader(texSh); // shader(texSh, TRIANGLE) is also valid
- ...
- draw your geometry, including lines and points...
- ...
- resetShader(LINE); // only resets the LINE shader
Although, now I feel that the LINE/POINT argument might be redundant after all...
In any case, as things stand right now the fog sketch should look like:
- PShader fogColor;
- PShader fogLines;
- PShader fogTex;
- void setup() {
- size(640, 360, P3D);
- fogColor = loadShader("fogColor.glsl");
- fogColor.set("fogNear", 0.0);
- fogColor.set("fogFar", 500.0);
- fogLines = loadShader("fogLines.glsl");
- fogLines.set("fogNear", 0.0);
- fogLines.set("fogFar", 500.0);
- fogTex = loadShader("fogTex.glsl");
- fogTex.set("fogNear", 0.0);
- fogTex.set("fogFar", 500.0);
- hint(DISABLE_DEPTH_TEST);
- }
- void draw() {
- background(0);
- fill(255,0,0);
- shader(fogColor);
- noStroke();
- translate(mouseX, mouseY, -100);
- box(200);
- box(100);
- shader(fogLines, LINES);
- stroke(255, 0, 0);
- strokeWeight(10);
- line(0,0,0, width/2, height/2, 100);
- line(0,0,0, -width/2, height/2, 100);
- shader(fogTex);
- text("shader", 100, 0, 100);
- }
with the following shaders:
fogColor.glsl
- #define PROCESSING_COLOR_SHADER
- varying vec4 vertColor;
- uniform float fogNear;
- uniform float fogFar;
- void main(){
- gl_FragColor = vertColor;
- vec3 fogColor = vec3(1.0,1.0,1.0);
- float depth = gl_FragCoord.z / gl_FragCoord.w;
- float fogFactor = smoothstep(fogNear, fogFar, depth);
- gl_FragColor = mix(gl_FragColor, vec4(fogColor, gl_FragColor.w), fogFactor);
- }
fogLines.glsl:
- #define PROCESSING_LINE_SHADER
- varying vec4 vertColor;
- uniform float fogNear;
- uniform float fogFar;
- void main(){
- gl_FragColor = vertColor;
- vec3 fogColor = vec3(1.0,1.0,1.0);
- float depth = gl_FragCoord.z / gl_FragCoord.w;
- float fogFactor = smoothstep(fogNear, fogFar, depth);
- gl_FragColor = mix(gl_FragColor, vec4(fogColor, gl_FragColor.w), fogFactor);
- }
fogTex.glsl
- #define PROCESSING_TEXTURE_SHADER
- uniform sampler2D texture;
- varying vec4 vertColor;
- varying vec4 vertTexCoord;
- uniform float fogNear;
- uniform float fogFar;
- void main() {
- gl_FragColor = texture2D(texture, vertTexCoord.st) * vertColor;
- vec3 fogColor = vec3(1.0,1.0,1.0);
- float depth = gl_FragCoord.z / gl_FragCoord.w;
- float fogFactor = smoothstep(fogNear, fogFar, depth);
- gl_FragColor = mix(gl_FragColor, vec4(fogColor, gl_FragColor.w), fogFactor);
- }