Blur shader effect is displaced by camera movement

edited May 2014 in GLSL / Shaders

I was trying to run the blur.glsl shader included with the IDE as an example, together with PeasyCam, but when the camera rotates, so does the blurred image.

Do shader effects like blur.glsl have to be rewritten so they work with a moving camera?

Source code of my test: https://github.com/jdf/peasycam/issues/17

Thanks!

screenshot

Answers

  • Answer ✓

    I found the solution. Just call camera() before filtering the image. It was too simple for me to understand :P

    PShader myShader;
    
    public void setup() {
      size(600, 600, P3D);
    
      // This would be your fancy shader effect, 
      // in this case represented by green pixels.
    
      // Inline shader code is a bad idea, but makes sharing
      // this program easier.
    
      String[] vertSource = new String[] {
        "uniform mat4 transform; attribute vec4 vertex;", 
        "attribute vec4 color; varying vec4 vertColor;", 
        "void main() { gl_Position = transform * vertex;", 
        "vertColor = color; }"
      };
      String[] fragSource = new String[] {
        "void main() { gl_FragColor = vec4(0.0,1.0,0.0,1.0); }"
      };
      myShader = new PShader(this, vertSource, fragSource);
    }
    
    public void draw() {
      background(80);
      float a = frameCount * 0.02;
    
      // Problem: moving the camera displaces the filter results
      camera(100 * cos(a), 100 * sin(a) + 200, 500, 0, 0, 0, 0, 1, 0);
      box(300);
    
      // Solution: resetting the camera() gets rid of the displacement. doh! 
      //camera();
      filter(myShader);
    }
    
Sign In or Register to comment.