# Getting alpha channel in GL ortho mode

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
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 on other sites
You could use something like:
glBlendFunc(GL_SRC_ALPHA,GL_ONE);

Check out the glBlendFunc here.

## 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

• ### Forum Statistics

• Total Topics
628345
• Total Posts
2982187

• 9
• 24
• 9
• 9
• 13