AlphaBlending with a mesh

#1

Hey,

I am stucked with the following problem:
I would like to fade a mesh in and out via alphaBlending or the overall brightness (?) of the mesh. Is there a way to do that without iterating through the whole mesh? Basically I would like to have the code below working for meshes. Any ideas/suggestions? :slight_smile:


void ofApp::draw(){
    easyCam.begin();
        ofPushMatrix();
            ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2);
   
            ofSetColor(255,255,255, alpha1); // so this doesnt work for meshes I guess
            mesh.draw();

        ofPopMatrix();
    easyCam.end();
}
#2

That should work for meshes

#3

Thanks for your response!
I tried it and it does not work. It works with images, but not with meshes…
Am I missing something?

//--------------------------------------------------------------
void ofApp::setup(){
    
    img.load("1.jpg");
    
    ofEnableAlphaBlending();
    mesh.setMode(OF_PRIMITIVE_POINTS);
    mesh.enableColors();
   
    float intensityThreshold = 50;
    int w = img.getWidth();
    int h = img.getHeight();

    for (int x=0; x<w; ++x) {
        for (int y=0; y<h; ++y) {
            ofColor c = img.getColor(x, y);
            ofVec3f pos(x, y, 0);
            mesh.addVertex(pos);
            mesh.addColor(c);
            }
        }
}

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

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

    ofSetColor(255, 255, 255, 100);
    mesh.draw();

 // img.draw(0, 0); //works
    
}
#4

oh i see, yeah it won’t work if you use color coordinates but it will if you use only vertices or a texture. if you want it to work with color coordinates you’ll need to create your own shader to make it work

#5

thank you! I will check that out!

#6

to use shaders in general with meshes I would need to store the mesh into an ofFbo, right? At least thats what I have learned throughout many other entries here.

It basically works, exept that I can not get the fbo into 3d. I tried out many things but the fbo stays 2d/flat

Any ideas?

void ofApp::setup(){
    
    ofEnableDepthTest();
    
    image.load("1.jpg");
    
    fbo.allocate( image.getWidth(), image.getHeight(), GL_RGBA, 4 );

    
    mesh.setMode(OF_PRIMITIVE_POINTS);
    mesh.enableColors();
    
    float intensityThreshold = 50;
    int w = image.getWidth();
    int h = image.getHeight();
    
    for (int x=0; x<w; ++x) {
        for (int y=0; y<h; ++y) {
            ofColor c = image.getColor(x, y);
            ofVec3f pos(x, y, ofRandom(200));  // change z position
            mesh.addVertex(pos);
            mesh.addColor(c);
        }
    }
}

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

}

//--------------------------------------------------------------
void ofApp::draw(){
    
    //store mesh in fbo
        fbo.begin();
            ofClear(0);
            mesh.draw();
        fbo.end();
    
        easyCam.begin();
            ofPushMatrix();
    
                fbo.draw( 0, 0 ); // doesnt show z axis (the image is 2d/flat)
                //mesh.draw();  --> shows z axis
    
            ofPopMatrix();
        easyCam.end();
}
#7

no you don’t need an fbo for this, an fbo just renders to a texture instead of directly to the screen so yes you’ll get a flat projected representation of the mesh.

you can just use a shader when drawing a mesh, no need for a fbo

1 Like
#8

If you know the alpha level while you are adding the colours to the mesh you can add a colour with alpha:

  for (int x=0; x<w; ++x) {
        for (int y=0; y<h; ++y) {
            ofColor c = img.getColor(x, y);
            ofVec3f pos(x, y, 0);
            mesh.addVertex(pos);
            mesh.addColor(c.r, c.g, c.b, alpha);
            }
        }
   }
1 Like