Why doesn't alpha blending work in ortho?

时光总嘲笑我的痴心妄想 提交于 2019-12-08 05:23:00

问题


This is how I turn on ortho projection after drawing 3D objects:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Screen_Width,Screen_Height,0,0,1);

And this is how I turn on blending and draw textures in ortho after drawing 3D objects:

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); 
glColor4f(1,1,1,1);

glBindTexture(GL_TEXTURE_2D,Texture1);
glBegin(GL_QUADS);
//draw 1st quad
glEnd();

glBindTexture(GL_TEXTURE_2D,Texture2);
glBegin(GL_QUADS);
//draw 2nd quad
glEnd();

glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);

Quad1 is a bit larger than Quad2 and covers some parts of Quad2. Both textures have alpha channel as RGBA.

The problem is, Quad1 & Quad2 overlay the 3D objects with alpha correctly, but the alpha of Quad1 doesn't work when being on top of Quad2. It draws on top of Quad2 like RGB only.

How could I solve this problem?


回答1:


Is depth buffering enabled? If it is, that could cause fragments to be discarded (failing the Z test) instead of alpha-blended with what's already there.

If you're using a depth buffer, you need to draw transparent objects in back-to-front order.

For a purely 2D scene, you might not even need a depth buffer, and you can just turn it off:

glDisable(GL_DEPTH_TEST);


来源:https://stackoverflow.com/questions/7373277/why-doesnt-alpha-blending-work-in-ortho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!