FBO Ping-Poing w/ programable renderer

Hey all,

Trying to start learning more about OpenGL 3.2+ and move away from fixed-function stuff. Starting of with FBO ping-ponging and I’m a bit stuck at the moment.

I thought I had it working, it all went to hell as soon as I tried to use actual geometry, meshes were deformed and appearing at random places and all sorts of other things.

The issue right now that I’m having is that the shaders don’t seem to be having any effect on altering the initial data loaded into the FBOs, if someone could take a quick peak(I believe all the relevant bits are included) and give me a nudge in the right direction, that would be greatly appreciated!

Mostly been following along with the gpuParticles example in spite of the fact that it still seems to be using fixed function stuff(theres a reference to the programable renderer but I don’t think the relevant shaders were included).

Thanks so much!

Setup

    fbos[0].allocate(1024,1024,GL_RGB );
    fbos[0].getTextureReference().setTextureMinMagFilter(GL_NEAREST, GL_NEAREST);
    fbos[1].allocate(1024,1024,GL_RGB );
    fbos[1].getTextureReference().setTextureMinMagFilter(GL_NEAREST, GL_NEAREST);
     
 //numParticles is just a little over a million. textureRes is 1024
        int test = numParticles * 3;
        float * pos = new float[test];
        for (int x = 0; x < textureRes; x++){
            for (int y = 0; y < textureRes; y++){
                int i = textureRes * y + x;
               //gpuParticles example has the r and g channels set to be random, that appeared to be the cause of 
//my meshes deforming.
                pos[i*3 + 0] = 1.0; //x*offset;
                pos[i*3 + 1] = 1.0; //y*offset;
                pos[i*3 + 2] = 0.0;
            }
            }  
             mesh.setMode(OF_PRIMITIVE_POINTS);
                for(int x = 0; x < 600; x++){
                    for(int y = 0; y < 600; y++){
                        mesh.addVertex(ofVec3f(x,y));
                        mesh.addTexCoord(ofVec2f(x, y));
                    }
                 }
     currentFbo = 0;
        fbos[0].getTextureReference().loadData(pos, textureRes, textureRes, GL_RGB);
        fbos[1].getTextureReference().loadData(pos, textureRes, textureRes, GL_RGB);

update

fbos[currentFbo].begin();
ofClear(0);
    position.begin(); //position is the shader being used to alter the texture
    position.setUniformTexture("position_texture", fbos[!currentFbo].getTextureReference(), 0);
    fbos[!currentFbo].draw(0, 0);
    position.end();
    
    fbos[currentFbo].end();
    swap(); // changes currentFbo to 1 and vice versa

draw

  render.begin(); //render is the shader being used to apply texture data to.
render.setUniformTexture("render_texture", fbos[!currentFbo].getTextureReference(), 0);
  mesh.draw();
render.end();

position fragment shader(ofShader position)

#version 150

uniform sampler2DRect position_texture;

out vec4 fragColor;
in vec2 tex_pos;
void main(){
	
    
    vec4 fbo = texture(position_texture,tex_pos);
    fbo.r += 300.0; // no matter what I try, it doesn't seem to have a affect during rendering
    fragColor = fbo;
}

rendering vertex(ofShader render)

#version 150

in vec4 position;
in vec2 texcoord;

uniform mat4 modelViewProjectionMatrix;
out vec2 texCoordVarying;

uniform sampler2DRect render_texture;

void main(){
    vec4 fbo = texture(render_texture,texcoord);

	
    //get our current vertex position so we can modify it
	vec4 pos = modelViewProjectionMatrix * position * fbo;
   
    texCoordVarying = texcoord;
    
	//finally set the pos to be that actual position rendered
	gl_Position = pos;
    }
fbo.r += 300.0; // no matter what I try, it doesn't seem to have a affect during rendering

These values are normalized so anything higher than 1.0 is ignored. Also, this:

vec4 pos = modelViewProjectionMatrix * position * fbo;

looks a little suspicious to me, since you’re using GL_RGB I think it’ll always have w=0, which might mess up other calculations.

Thanks for the response Joshua!

Re:normalizing values - ok, cool, thanks for confirming, I figured that was probably what was happening but wasn’t sure, I could have swore I’ve seen examples with much higher values.

Question while we’re on this, and I’m betting I’m misunderstanding something fundamental somewhere but if I need to keep values between 0 and 1, how would screen coordinates translate to the shader then?

Re: RGB - I switched the fbos and their textures to use RGBA but it doesn’t seem to have made any difference.

Will be exploring more when I get home later.

After noodling about last night, while I didn’t accomplish exactly what I had set out to do, At the very least, things are working otherwise as expected. Time to hit the books more this weekend.
Thanks again Josh!