Basic fbo issue

Hi forum,

I am trying to implement a basic fbo techniques using ofFbo.

  1. I am rendering a rotating cube to a texture using the ofFbo as follows

    fbo.begin();
    
    shady1.begin();
       
    ofClear(green);
    
    ofPushMatrix();
    cube.setPosition(ofGetWidth() * 0.5, ofGetHeight() * 0.5 , 0);;
    cube.rotate(f, 1.0, 0.0, 0.0);
    cube.rotate(f, 0, 1.0, 0.0);   
    
    cube.draw();
    
    ofPopMatrix();
    
    shady1.end();
    
    fbo.end();
    

Here shady1 is of ofShader type. Then i get the reference to the attached texture to the fbo and pass the texture through another shader and render to the default fbo - which is the screen . In the long run , i want to get a rotating cube on each face of the cube that is rendered to the default buffer . But i am not getting get. Here goes more snippet

   //get the reference of the color texture
   ofTexture &color_texture = fbo.getTextureReference();

   //bind the texture
   color_texture.bind();

   shady2.begin();
   ofClear(green);
   
   shady2.setUniformTexture("tex",color_texture,1);
   
   cube.draw();

   shady2.end();

   color_texture.unbind();

All of the above snippets are inside the draw() function. By the way cube is of ofBoxPrimitive.

Some hints is requested to get the desired effect.

This is what i am getting now

http://imgur.com/NuwonST

Thanks

Hi forum,

I have not got any response over this issue.

Please refer me to any examples that will give me enough hint.

I want to have a rotating cube on each face of the cube on the default framebuffer.

The default tex coords for a box primitive are 0-1. Did you call ofDisableArbtex()? And are you using 0-1 tex coords in your shader? Does the texture show up on the cube if you don’t use the shader?

Hi @NickHardeman

Thanks for the hint.

I tried as follows:

 void ofApp::setup()
{
   ofDisableArbTex();
   
   model.loadModel("elephant.obj",true);

   ofFbo::Settings s;

   s.width = 512;
   s.height = 512;
   s.internalformat = GL_DEPTH_COMPONENT;
   s.depthStencilAsTexture = true;
   s.useDepth = true;
   s.useStencil = true;

   //allocate resources for the fbo
   fbo.allocate(s);

   fbo.begin();
   ofClear(ofFloatColor(0,0,0,1));  
   fbo.end();   
   
   //allocate the side of the box
   box.set(ofGetWidth()/2 * .5);

And inside the draw function i have the following snippet:

void ofApp::draw()
{
   float f = ofGetElapsedTimef() * 0.3;


   fbo.begin();
   //black back ground
   ofBackground(0,0,0,0);

   model.setPosition(ofGetWidth()/2,ofGetHeight()/2,0);
   model.setRotation(0,f,1,0,0);
   model.setRotation(1,f,0,1,0);
   model.setRotation(2,f,0,0,1);
   model.drawFaces();

   fbo.end();

   fbo.getDepthTexture().bind();   

   box.setPosition(ofGetWidth()/5,ofGetHeight()/5,0);
   box.rotate(f,1.0,0.0,0.0);
   box.rotate(f,0.0,1.0,0.0);

   fbo.getDepthTexture().unbind();   
}

I am trying the simplest probably , not passing through any shader yet. But i am getting a blank screen with the following error report:

 [ error ] ofFbo: FRAMEBUFFER_INCOMPLETE_ATTACHMENT

Any idea?

sorry

i forgot to mention the box.draw() function

Now i do see the the cube but the each face is not showing the model depth texture as it supposed to show. instead it shows the following

http://imgur.com/5Fa7wAs

And the fbo error prevails

Hi folks,

I am on my way to get the basic fbo right. Lets re-iterate what i want to simulate.

  1. I want to have a rotating model rendered to the attached texture of the fbo.
  2. Then i want to have that texture rendered on each face of the box primitive.

Check the following image

http://imgur.com/Y6IVF2J

As you can see that the back face culling is not working . How to enable it for ofBoxPrimitive ?

Can you see some other reason behind it that i do not see ?

And here goes some snippet:

//--------------------------------------------------------------
void ofApp::setup()
{
   ofSetVerticalSync(true);
   ofDisableArbTex();
   
   model.loadModel("teapot.obj",true);
   model.enableNormals();
   model.enableTextures();

   //scale the model
   model.setScale(0.7,0.7,0.7);
   
   //depth testing makes the teapot
   //rendered properly
   ofEnableDepthTest();
   
   //load the programmable shaders
   if(ofIsGLProgrammableRenderer())
   {
      perFragmentShader.load("shaders/shader-gauraud-fragment");
   }
   else
   {
      std::cout << "Programmable Shading is not supported. " << std::endl;
      exit();
   }

   texture_width = 512;
   texture_height = 512;

   //allocate buffer for the fbo
   fbo.allocate(texture_width,texture_height);

   box.set(ofGetWidth()/2);
   box.setUseVbo(true);
   box.enableNormals();
   box.enableTextures();
}

And inside the update() and draw() function

//--------------------------------------------------------------
void ofApp::update()
{
   float f  = ofGetElapsedTimef() * 60;


   fbo.begin();

   //clear the background
   //before the main rendering
   ofBackground(50,50,50,50);
      
   model.setPosition(texture_width/2,
		     texture_height/2,
		     0);

   model.setRotation(0,f,1,0,0);
   model.setRotation(1,f,0,1,0);
   

   perFragmentShader.begin();
   perFragmentShader.setUniform3f("specular_albedo",1.0f,1.0f,1.0f);
   perFragmentShader.setUniform1f("specular_power",30.0f);
   
   model.drawFaces();

   perFragmentShader.end();   
   
   fbo.end();

   box.mapTexCoordsFromTexture( fbo.getTextureReference() );
}

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

   float f  = ofGetElapsedTimef() * 0.3;

   ofBackground(ofFloatColor(1.0,1.0,1.0,1.0));

   camera.begin();

   fbo.getTextureReference().bind();   
   
   box.drawFaces();
   
   fbo.getTextureReference().unbind();
   
   
   camera.end();
}

Thanks

Hi forum,

No response so far. May be i was not clear enough. Lets come up with more screen shots of this tiny application.

http://imgur.com/B0R0rEO

As you can see that some of the back faces are visible and i failed to debug it. Here goes another one

http://imgur.com/1D6CSMk

And this image is fine enough. The rest of the faces are culled as belong to the back side of the ofBoxPrimitive.

As i rotate the box with the easycam mechanism i get this unwanted back face artifacts of teapot over some of the faces of the box.

Any explanation folks ?

face culling is not enabled by default in openGL you need to enable it using:

glEnable(GL_CULL);

I tried it and it did not work. Here goes the snippet ,

#include "ofApp.h"

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

   ofSetVerticalSync(true);

   ofDisableArbTex();

   glEnable(GL_CULL_FACE);

   ofEnableAntiAliasing();
   ofEnableSmoothing();
   
   box.set(ofGetWidth()/2);
   box.setUseVbo(true);


   model.loadModel("teapot.obj",true);
   model.enableNormals();
   model.enableTextures();

   //scale the model
   model.setScale(0.7,0.7,0.7);
   
   //depth testing makes the teapot
   //rendered properly
   ofEnableDepthTest();

   
   //load the programmable shaders
   if(ofIsGLProgrammableRenderer())
   {
      perFragmentShader.load("shaders/shader-gauraud-fragment");
   }
   else
   {
      std::cout << "Programmable Shading is not supported. " << std::endl;
      exit();
   }

   texture_width = 512;
   texture_height = 512;

   //allocate buffer for the fbo
   fbo.allocate(texture_width,texture_height);

   fbo.getTextureReference().setTextureWrap( GL_REPEAT, GL_REPEAT );
   
   box.mapTexCoordsFromTexture( fbo.getTextureReference() );
}

//--------------------------------------------------------------
void ofApp::update()
{
   float f  = ofGetElapsedTimef() * 60;


   //render starts on the attached texture of the fbo
   fbo.begin();

   //clear the background
   //before the main rendering
   ofBackground(50,50,50,50);

   //re-position the teapot model
   model.setPosition(texture_width/2,
		     texture_height/2,
		     0);

   //animate the model
   model.setRotation(0,f,1,0,0);
   model.setRotation(1,f,0,1,0);
   
   //the teapot goes through the shader
   perFragmentShader.begin();

   perFragmentShader.setUniform3f("specular_albedo",1.0f,1.0f,1.0f);
   perFragmentShader.setUniform1f("specular_power",30.0f);

   //draw the teapot
   model.drawFaces();

   perFragmentShader.end();   
   
   fbo.end();
}

//--------------------------------------------------------------
void ofApp::draw()
{
   ofBackground(ofFloatColor(0.5,0.50,0.5,1.0));

   ofFill();
   
   camera.begin();

   //bind the fbo texture we have rendered to
   fbo.getTextureReference().bind();   
   box.draw();
   fbo.getTextureReference().unbind();

   camera.end();
}

I need to know what i am missing in the process.

Thanks

The order in which you draw alpha pixels into the depth image can result in artifacts. So if you draw the images further away first, drawing the closest last, then it will probably yield the top image you showed. But if you draw the closest image first, then then the furthest, the alpha will clip in the depth image. More like the second image you posted.
If you don’t want to see the other sides, why not make them a solid color by drawing an ofRect after the ofBackground call to fill the alpha pixels with the color you want?

Hi @NickHardeman

I made it work as follows

BEFORE:

   ofBackground(50,50,50,50); 

AFTER:

ofBackground(50,50,50,255); 

And now i get the following output as i expected

http://imgur.com/I7K7EHS

As you can see each face of the box have the rotating model which is initially rendered to texture and then used the texture to wrap the box. i not see the faces from the front side of the cube as i used to have. The alpha value now made the texture opaque. Did i explain it right ? Anything better is always welcome.

Thanks