"Unknow Error" for simple Fragment shader - reaction diffusion

edited January 2015 in GLSL / Shaders

Hey, I'm porting pmneila's really nice web reaction diffusion project (https://pmneila.github.io/jsexp/grayscott/) to Processing with shaders. However, I can't, for the life of me, get the fragment shader to compile. The loadShader() call works, but as soon as I try to set my first uniform, the program crashes with "Cannot link shader program: Unknow error". Yes, with the typo and all. Here's my shader:

https://github.com/abigpotostew/reaction_diffusion/blob/master/reaction_diffusion_shader/data/grey_scott.glsl *Edit, changed code to link to github since formatting was all jacked up.

I can't figure it out, if you know what's going on, let me know. Thanks.

Here's the Processing sketch which crashes on line 18 https://github.com/abigpotostew/reaction_diffusion/blob/master/reaction_diffusion_shader/reaction_diffusion_shader.pde

Answers

  • edited January 2015 Answer ✓

    Ok, there are a bunch of things wrong, both in your sketch and your shader code. You should take a look at the shader examples that come with Processing. Also, shader debugging sucks, so it's best to do a lot of incremental development. Your sketch is overcomplicated and you use a PVector before it's initialized. Your shader uses vec2 vertTexCoord, where it should be vec4 vertTexCoord and doesn't use texOffset.

    Adapted Sketch

    PShader shader;
    float feed = 0.037;
    float kill = 0.06;
    float delta = 1.0;
    
    void setup() {
      size(640, 360, P2D);
      shader = loadShader("shader.glsl"); 
      shader.set("screen", float(width), float(height));
      shader.set("delta", delta);
      shader.set("feed", feed);
      shader.set("kill", kill);
    }
    
    void draw() {
      shader.set("mouse", float(mouseX), float(height-mouseY));
      filter(shader);  
    }
    

    Adapted Shader

    #ifdef GL_ES
    precision mediump float;
    precision mediump int;
    #endif
    
    #define PROCESSING_TEXTURE_SHADER
    
    uniform sampler2D texture;
    uniform vec2 texOffset;
    
    uniform vec2 screen;
    uniform vec2 mouse;
    uniform float delta;
    uniform float feed;
    uniform float kill;
    
    varying vec4 vertColor;
    varying vec4 vertTexCoord;
    
    void main() {
        vec2 uv = texture2D(texture, vertTexCoord.st).rg;
        vec2 uv0 = texture2D(texture, vertTexCoord.st + vec2(-texOffset.s, 0.0)).rg;
        vec2 uv1 = texture2D(texture, vertTexCoord.st + vec2(+texOffset.s, 0.0)).rg;
        vec2 uv2 = texture2D(texture, vertTexCoord.st + vec2(0.0, -texOffset.t)).rg;
        vec2 uv3 = texture2D(texture, vertTexCoord.st + vec2(0.0, +texOffset.t)).rg;
    
        vec2 lapl = uv0 + uv1 + uv2 + uv3 - 4.0 * uv;
        float du = 0.2097 * lapl.r - uv.r * uv.g * uv.g + feed * (1.0 - uv.r);
        float dv = 0.105 * lapl.g + uv.r * uv.g * uv.g - (feed+kill) * uv.g;
        vec2 dst = uv + delta * vec2(du, dv);
    
        vec2 diff = vertTexCoord.st * screen - mouse;
        float dist = dot(diff, diff);
        if(dist < 50.0) {
            dst.g = 0.9;
        }
    
        gl_FragColor = vec4(dst.r, dst.g, 0.0, 1.0);
    }
    
  • Thanks so much amnon! I overlooked the texOffset uniform while looking over the shader tutorial. And I didn't know filter() existed at all. I am new to working with shaders in processing. Thanks for parting some wisdom on me.

Sign In or Register to comment.