Why does this shader make the result shift to the right?

edited November 2015 in GLSL / Shaders

I want to add up colors. The code below is a work in progress. I'm only stuck at some unwanted behavior, the thing shifts to the right.

So to begin, why has result.background(0, 255, 0); no effect?

Same for the code below that where I use a rectangle instead of a background.

I also tried clear() but that seems to have a really serious bug in processing which I will isolate now to report.

To be more clear let's say I have those values for red on the first row:

[10][0][12][6][18]

then I want the result to become:

[10][10][22][28][46]

so the total seen from the x=0. To do this I want to sweep over the image. Using an input image and the context we draw on to get the result of the previous sweep line.

    PImage img;

    PShader shader;

    PGraphics result;

    @ Override
    public void settings() {
        size(512, 512, P2D);

    }

    @ Override
    public void setup() {
        img = loadImage("http:"+"//icons.iconarchive.com/icons/mattahan/ultrabuuf/512/Comics-Rulk-Fist-icon.png");

        shader = new PShader(this, "IntegralImage_vert.glsl", "IntegralImage_frag.glsl");

        result = createGraphics(img.width, img.height, P2D);

        frameRate(999);
    }

    @ Override
    public void draw() {
        //resetShader();
        background(255);

        shader.set("texInp", img);
        shader.set("contextTexture", result);
        shader.set("width", width);
        //noinspection SuspiciousNameCombination
        shader.set("height", height);

        shader.set("channel", 0); // red = 0, green = 1, blue = 2



        result.beginDraw();

        result.resetShader();
        result.background(0, 255, 0); // no effect...

        // same shit, different code... 
        result.noStroke();
        result.fill(255);
        result.rect(0, 0, result.width, result.height);


        result.shader(shader);
        //image(img, 0, 0);

        result.stroke(0); // color doesn't matter, just something so it does draw
        result.strokeWeight(1);
        for (int x = 0; x < img.width; x++) {
            result.line(x, 0, x, height);
        }

        result.endDraw();

        image(result, 0, 0);


        surface.setTitle("fps: "+frameRate);

    }

vert:

#version 410

uniform mat4 transform;

in vec4 vertex;

void main() {
    gl_Position = transform * vertex;
}

frag:

out vec4 fragColor;

uniform sampler2D texInp;

uniform sampler2D contextTexture;

uniform int width;
uniform int height;

uniform int channel;




void main() {

    // value previous (-1 in x)
    float vp = texture(contextTexture, vec2((gl_FragCoord.x-1) / width, 1.0 - (gl_FragCoord.y / height)))[channel];
    // value current
    float vc = texture(texInp, vec2(gl_FragCoord.x / width, 1.0 - (gl_FragCoord.y / height)))[channel];


    fragColor = vec4(0,0,0,1);
    fragColor[channel] = vp+vc;

}
Sign In or Register to comment.