LWJGL Forum

Programming => OpenGL => Topic started by: Daslee on November 07, 2013, 18:12:15

Title: Alpha blending
Post by: Daslee on November 07, 2013, 18:12:15
Hello. I have problem with alpha blending and I tried many suggestions how to fix that, but nothing worked. Here is picture how I want my alpha:

http://s10.postimg.org/pcvdlnsex/screenshot_201311071.png (http://s10.postimg.org/pcvdlnsex/screenshot_201311071.png)

And how it looks, when it shouldn't be like that:
http://s10.postimg.org/5jj9syf15/screenshot_201311072.png (http://s10.postimg.org/5jj9syf15/screenshot_201311072.png)
http://s10.postimg.org/547zzxt3t/screenshot_201311076.png (http://s10.postimg.org/547zzxt3t/screenshot_201311076.png)

Here is my code, how I init alpha blending when initializing opengl:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glAlphaFunc(GL_GREATER, 0.1f);


Where could be the problem?

Title: Re: Alpha blending
Post by: Cornix on November 07, 2013, 18:39:08
You have to draw your translucent geometry in the reverse order of what you actually see.
You have to know what geometry is opaque and what has translucency, and then sort the translucent objects depending on the camera rotation.
Title: Re: Alpha blending
Post by: Mickelukas on November 08, 2013, 09:12:32
You have two options to handle alpha values (not counting shaders), alpha testing and blending. The first two rows of the code you posted is to enable blending, and the last row is to set the threshold for alpha testing.

Either enable blending, for example:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


Or enable alpha testing, for example:
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1f);


The main difference between them is that blending can have semi transparency while alpha testing is either true or false. So why use alpha testing when it also can be slower? The most common answer is simplicity, as you can render your objects in any order, while blending requires you to (in most cases) sort all draw calls to draw everything back to front.

Mike
Title: Re: Alpha blending
Post by: Daslee on November 08, 2013, 10:49:37
Quote from: Mickelukas on November 08, 2013, 09:12:32
You have two options to handle alpha values (not counting shaders), alpha testing and blending. The first two rows of the code you posted is to enable blending, and the last row is to set the threshold for alpha testing.

Either enable blending, for example:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


Or enable alpha testing, for example:
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1f);


The main difference between them is that blending can have semi transparency while alpha testing is either true or false. So why use alpha testing when it also can be slower? The most common answer is simplicity, as you can render your objects in any order, while blending requires you to (in most cases) sort all draw calls to draw everything back to front.

Mike

Never knew about GL_ALPHA_TEST, so now I enabled this and it works as it should. Thanks Mike!
Title: Re: Alpha blending
Post by: Estraven on November 13, 2013, 09:56:08

Some times, it might also be usefull to disable depth write. Light rays and so on can thus be cumulative. the trick is :

1- render solid geometry
2- disable depth write (but keep depth test !)
3- optionnal : Switch to additive rendering
4- render transparent non solid stuff, just like light beams.

that way, you don't need to sort anything, and transparent stuff that are not occluded by solid geometry will be all visible