How to bind ofFbo texture to a 3d object?

Hi, I’m trying to draw a red circle into a fbo and then use it as a texture for a box shape.

I tried the below code and fbo.draw() works fine but I could not bind the texture into a box.

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

    fbo.allocate(200, 200, GL_RGBA);
    
    fbo.begin();
    ofClear(255, 255, 255, 150);
    ofSetColor(ofColor::red);
    ofDrawCircle(100, 100, 100);
    fbo.end();
    
    ofDisableArbTex();
    ofEnableDepthTest();
    
    mesh = ofMesh::box(200, 200, 200);
}

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

}

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

    fbo.draw(0, 0);
    
    ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
    
    float degrees = ofGetElapsedTimef()*10;
    
    ofRotateX(degrees);
    ofRotateY(degrees);
    ofRotateZ(degrees);
    
    fbo.getTexture().bind();
    
    mesh.draw();
    
    fbo.getTexture().unbind();
}

What is wrong with my code?
Thanks in advance.

try this:

 class ofApp: public ofBaseApp{
        public:
        ofFbo fbo;
        ofMesh mesh;
        ofEasyCam cam;

        void setup(){

            ofDisableArbTex(); //<-- above allocate fbo
            fbo.allocate(200, 200);
            fbo.begin();
            ofClear(255, 255, 255, 150);
            ofSetColor(ofColor::red);
            ofDrawCircle(100, 100, 100);
            fbo.end();

            ofEnableDepthTest();
            mesh = ofMesh::box(200, 200, 200);
        }

        void draw(){

            fbo.draw(0, 0);

            ofTranslate(ofGetWidth()/2, ofGetHeight()/2);

            float degrees = ofGetElapsedTimef()*10;

            ofRotateX(degrees);
            ofRotateY(degrees);
            ofRotateZ(degrees);

            fbo.getTexture().bind();
            mesh.draw();
            fbo.getTexture().unbind();
        }
};

int main( ){
        ofSetupOpenGL(1024,768, OF_WINDOW);
        ofRunApp( new ofApp());
}
2 Likes

Hey @kashim Thanks for your help! it works now. :slight_smile:

By the way, Is it normal that some sides of the box are transparent but some sides are not?

if the ofFbo uses alpha, shouldn’t all sides of the box be equally transparent? Can this be a bug? Thanks in advance!

That has to do with depth test being enabled. If you disable the depth test, all faces will be drawn… but the order may (will) be wrong.

Check on blend modes for the fbo, also this example may help.

bonus

1 Like

@chuckleplant Thank you so much. that makes sense!
Also, setting the blend mode to OF_BLENDMODE_ADD or OF_BLENDMODE_SCREEN could make all sides look transparent.