Sign in to follow this  
silverphyre673

Getting alpha channel in GL ortho mode

Recommended Posts

silverphyre673    454
Well, I'm back to playing with 3D graphics again (I'm still very new to this particular area), and I was wondering if there is any way to get alpha blending in orthographic mode. When in ortho mode, this is my code to draw a box filled with color:
    glBegin(GL_QUADS);
        glColor4ub(color.r, color.g, color.b, color.a); //"color stores the color of the quad
        glVertex2d(r.x, r.y); //r stores the location and size of the box
        glVertex2d(r.x + r.w, r.y);
        glVertex2d(r.x + r.w, r.y + r.h);
        glVertex2d(r.x, r.y + r.h);
    glEnd();

I was hoping that the function glColor4ub would work for this, but my boxes don't alpha blend with each other. Any ideas? I would appreciate it. Also, in ortho mode, the boxes I render using the above code do the opposite of what I would expect them to, coming from the 2D graphics world. The first ones that I render always appear on top, while the ones I render last get drawn behind. I would find it more intuitive if the opposite were true. If there is a way to get this working, I would appreciate it. Thanks!

Share this post


Link to post
Share on other sites
bpoint    464
There is nothing special with getting alpha to work in orthographic mode. You just need to ensure blending is enabled with glEnable(GL_BLEND).

As for the improper ordering, you most likely have the Z buffer enabled. This is causing clipping between each quad because the default Z compare function is set to "less than". You can either set the function to "less than or equal" (via glDepthFunc), which would allow multiple primitives at the same Z to overlap each other, or simply disable Z compare entirely when doing 2D rendering (via glDiable(GL_DEPTH_TEST)). The latter tends to be better, as it avoids problems with Z-fighting and rendering does pretty much what you expect it to.

Share this post


Link to post
Share on other sites
silverphyre673    454
Well, your solution to the improper ordering worked fine - thanks! However, after enabling blending via GL_BLEND, the quads still aren't being blended. Is there any specific time when I should be enabling GL_BLEND?

Share this post


Link to post
Share on other sites
bpoint    464
As long as you enable blending before calling glBegin, the primitive will be drawn blended. The other possibility is your alpha value is set to 255 (full-opaque), where there will be no visible blending, even if GL_BLEND is enabled.

I suppose it's possible your blend function (glBlendFunc) could also be incorrect, but this _should_ default to:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


I'm betting your alpha is probably incorrect, though. [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this