Help with (hopefully dumb) ofShader problem?

Playing around with ofShader: I’ve been trying to just blend between multiple ofFbo’s. My idea initially was to have two Fbos with separate scenes, and then blend between them using a maskFbo. After failing at that miserably, I’m trying to simplify to ID the problems I’m having.

Right now I have two Fbos which I’m just drawing simple shapes to. The shader should draw brighter of the two values at each pixel.

The shader seems to be working… although only in the lower-right quadrant. Any thoughts?

My problem isn’t getting them to blend, but that the upper half and lower-left quarter of the “combine” Fbo aren’t updating / drawing at all.


Here’s my code:

main.cpp:

#include “ofMain.h”
#include “ofApp.h”
#include “ofGLProgrammableRenderer.h”

//====================================================================
int main( ){
ofSetCurrentRenderer(ofGLProgrammableRenderer::TYPE);
ofSetupOpenGL(640,430,OF_WINDOW);
ofRunApp(new ofApp());
}

ofApp.cpp:

#include “ofApp.h”

//--------------------------------------------------------------
void ofApp::setup(){

fa.allocate(200, 200);
fb.allocate(200, 200);
combine.allocate(200, 200);
b0 = ofVec2f(ofRandom(200), ofRandom(200));
b1 = ofVec2f(ofRandom(200), ofRandom(200));
v0 = ofVec2f(ofRandom(-1, 1), ofRandom(-1, 1));
v1 = ofVec2f(ofRandom(-1, 1), ofRandom(-1, 1));
fa.begin();
ofClear(ofColor::grey);
fa.end();
fb.begin();
ofClear(ofColor::black);
fb.end();
ofBackground(ofColor::lightGray);
shader.load("blendShader");

}

//--------------------------------------------------------------
void ofApp::update(){

if(b0.x > 200 || b0.x < 0) v0.x *= -1;
if(b0.y > 200 || b0.y < 0) v0.y *= -1;
b0 += v0;
if(b1.x > 200 || b1.x < 0) v1.x *= -1;
if(b1.y > 200 || b1.y < 0) v1.y *= -1;
b1 += v1;
fa.begin();
ofClear(ofColor::black);
ofFill();
ofSetColor(ofColor::white);
ofCircle(b0, 15);
fa.end();
fb.begin();
ofClear(ofColor::black);
ofFill();
ofSetColor(ofColor::white);
ofCircle(b1, 10);
fb.end();
combine.begin();
ofClear(ofColor::black);
shader.begin();
shader.setUniformTexture("tex0", fa, 0);
shader.setUniformTexture("tex1", fb, 1);
ofRect(0, 0, 200, 200);
shader.end();
combine.end();

}

//--------------------------------------------------------------
void ofApp::draw(){

ofSetColor(ofColor::white);
fa.draw(10, 10);
fb.draw(220, 10);
combine.draw(430, 10);

}

ofApp.h:

#pragma once

#include “ofMain.h”

class ofApp : public ofBaseApp{

public:
void setup();
void update();
void draw();

ofFbo fa;
ofFbo fb;
ofFbo combine;
ofShader shader;
ofVec2f b0;
ofVec2f b1;
ofVec2f v0, v1;

};

vert shader:

#version 410

uniform mat4 modelViewProjectionMatrix;

in vec4 position;

void main() {
gl_Position = position;
}

frag shader:

#version 410

uniform sampler2DRect tex0;
uniform sampler2DRect tex1;

out vec4 outputColor;

void main() {
vec4 c0 = texture(tex0, gl_FragCoord.xy);
vec4 c1 = texture(tex1, gl_FragCoord.xy);

float c0b = (c0.r + c0.g + c0.b) / 3.0;
float c1b = (c1.r + c1.g + c1.b) / 3.0;
if(c0b >= c1b) {
    outputColor = vec4(c0.rgb, 1.0);
} else if (c1b > c0b){
    outputColor = vec4(c1.rgb, 1.0);
} else {
    outputColor = vec4(0.0, 1.0, 0.0, 1.0);
}

}

Thanks!

Hi there!

You only have gl_Position = position, you need to do gl_Position = modelViewProjectionMatrix * position. OF is always drawing in 3D space, but since you are drawing in 2D, it just hides the camera for you.

Tried that!

Here’s a screen shot after i change that:

vert shader:

#version 410

uniform mat4 modelViewProjectionMatrix;

in vec4 position;

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

AhHH!!

Just figured it out… @hubris, you’re correct. I didn’t realize that ‘vec4 * mat4’ is different than ‘mat4 * vec4’ (? can someone explain to me?)

When i reversed it in the code all looks well… and this is probably one of the problems I’ve been having across the board.

Thanks!

Matrix-matrix and matrix-vector multiplications are non-commutative.

If you want to work with the GPU, I must recommend that you study a bit of linear algebra, integral calculus, and topics like eigenvalues and eigenvectors. Good work!