ofShader with ofFbo, ofVideoGrabber

Hello.
I want to PostEffect with shader (#version 150) , but I can’t.

ofApp.cpp

void ofApp::setup(){
    fbo.allocate(ofGetWidth(), ofGetHeight(), GL_RGBA);
    grabber.initGrabber(ofGetWidth(), ofGetHeight());
}

void ofApp::update(){
    grabber.update();
}

void ofApp::draw(){
        if(grabber.isFrameNew()){
        fbo.begin();
        ofClear(0);
        grabber.draw(0, 0, ofGetWidth(), ofGetHeight());
        fbo.end();
        }
    
    shader.begin();
    shader.setUniformTexture("image", fbo, 0);
    shader.setUniform1f("time", ofGetElapsedTimef());
    shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight());
    ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight());
    shader.end();
}

shader.vert

#version 150

uniform mat4 modelViewProjectionMatrix; // provide by openFrameworks

in vec4 position; // provide by openFrameworks
in vec2 texcoord; // provide by openFrameworks

out vec2 vTexCoord; // for fragment shader

void main() {
    vTexCoord = texcoord;
    gl_Position = modelViewProjectionMatrix * position;
}

shader.frag

#version 150
precision mediump float;

uniform sampler2DRect image;
uniform float time;
uniform vec2 resolution;

in vec2 vTexCoord;

out vec4 fragColor;

void main() {
    vec2 pos = vTexCoord;
    vec4 color = texture(image, pos);
    fragColor = color;
}

When it runs…

Please help me.
Thank you.

I’m not quite sure, but maybe the problem has something to do with texture coordinates. The sampler2DRect type has texture coordinates with a range that matches the width and height of the image.

I’m also not quite sure about this either, but the vTexCoord that has been passed from the vertex shader may have a normalized range of values.

So, I would recommend sending additional vec2 uniform that contains the width and height of the texture, and then when you use it in shader, multiply it to vTexCoord.

so in ofApp.cpp, and the following line
shader.setUniform2f("texture_resolution", fbo.getWidth(), fbo.getHeight());

and in fragment shader, change your code like below.

#version 150
precision mediump float;

uniform sampler2DRect image;
uniform float time;
uniform vec2 resolution;
uniform vec2 texture_resolution;

in vec2 vTexCoord;

out vec4 fragColor;

void main() {
    vec2 pos = vTexCoord;
    vec4 color = texture(image, pos * texture_resolution);
    fragColor = color;
}

Hope this helps, but I’m quite new to openGL and probably wrong!