blending colors


I’m implementing a fog simulation with different colored fogs.
Therefore I’ve got three three-dimensional grids that contain the density of either red, green or blue fog per voxel.
I’m rendering the fog by an volume visualization that displays a set of square plains in front of each other blended by opengl. (Each of these plains has a texture with the density of the according layer in my density grid (by now I’m using one grid in a black and white version)


Ok, now there’s the problem:
I want to blend my three colors into one texture.
The problem is, that I’ve got an alpha value per color. My resulting color should be the result of the source colors (r, g, b) weighted by their alpha value.

So, i’ve got three rgba colors:
[1, 0, 0, a_r]
[0, 1, 0, a_g]
[0, 0, 1, a_b]

my result should be:
[a_r/(a_r+a_g+a_b), a_g/(a_r+a_g+a_b), a_b/(a_r+a_g+a_b)]

Why? Some examples:

  1. only red fog with 20% alpha
    -> [1, 0, 0, 0.2]
  2. red fog 20% alpha, blue fog 40% alpha
    -> [0.33, 0, 0.66, 0.6]
  3. red fog 20% alpha, green fog 90% alpha, blue fog 40% alpha
    -> [0,1333, 0,6, 0,333, 1]

Is there a possibility to get this using the gpu? The grids have 64^3=262144 Voxels * 3 Divisions = too much for computing by software.

Thanks a lot!

I think your problem is using separate textures for red, green and blue colors in the first place. Just define your fog by one texture - each texel will define the color and density of fog and you’re done.

But if for some reason you want to mix these 3 textures in realtime (if you want red fog to go one way and green the other way for example), then you will have to use shaders to do exactly the calculations you described above (weighted average on RGB and sum of alpha channels).