OpenGL blending? layering?


I don’t know if this is the right forum for this or if this is an OpenGL specific question but hopefully someone can help.

So I made this really nice looking OpenGL sphere with lighting and everything and I’m trying to draw some stuff over it but it’s not layering properly. Basically, I want to draw things in the following order: the sphere on the bottom, an ofCvGrayscaleImage in the middle, and a red circle on the top.

Here’s the draw() method:

void testApp::draw() {  
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);  
    // draw the mask  
        glTranslatef(ofGetWidth()/2, ofGetHeight()/2, 0);  
        glScalef(ofGetHeight()/2-MARGIN, ofGetHeight()/2-MARGIN, 1.0);  
        glutSolidSphere(1.0, 50, 50);  
    // draw the frame first  
        glTranslatef((WIDTH-HEIGHT+MARGIN)/2, MARGIN, 0);  
        glScalef((ofGetHeight()-MARGIN*2.0)/HEIGHT, (ofGetHeight()-MARGIN*2.0)/HEIGHT, 1.0);  
        frameChannel.draw(0, 0);  
    // draw the mouse  
    ofCircle(mx, my, 10);  

What’s happening though is that the red circle is getting drawn under the cvImage. If I remove the call to glBlendFunc(…) then the red circle is on top of the cvImage, but the 3D sphere doesn’t get displayed anymore. From what I read (and understand), the glBlendFunc(…) is probably the problem but I’ve tried a bunch of different parameters and nothing works. I also tried enabling and disabling the lighting at different points in the code but that doesn’t do it either.

Is the problem that I’m mixing lit elements with non-lit elements?
Is it that I’m mixing 2D with 3D?

Any help would be appreciated as I’m not even sure what the source of the problem is. If anyone feels like messing with the code, you can download the files here.


hi –

the mouse is getting drawn on top, but because of the blend mode, you don’t see it…

If you change the code, like this:

frameChannel.draw(0, 0);  
// add this:  
// (or try glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); )   
// draw the mouse  

you’ll see the red dot.

when your have GL_ONE as the destination factor for the glBlendFunc, you will typically head to white, meaning, you can’t draw a red dot on top of a white rectangle.

some good info on blending options is here:…-ending.php

hope that helps!

take care,

Hi Zach,

Thanks, it worked. And the link was helpful to understand all the blend functions as well. Actually, I keep coming across good links on the forum and I think it would be a good idea to consolidate everything on a reference page. Just a list of external sites or books, organized by topic. It would be really useful.

that’s a very good idea -

I will start a sub-forum about resources, because I think it would be good for everyone to contribute links / etc that they think is helpful and we will find a way to work them into the site.

also perhaps a delicious tag might be useful too?
(openframeworks_resource) ??