problem with transparency

I have two bitmaps that I edited in Photoshop. One is an image of clouds with a blue background and white clouds. The other is an image of a white sun with a black background. I created alpha channels for each bitmap. The bitmap of the clouds has the alpha for the blue background as 0 and the clouds as 1. The bitmap of the sun has the black background as 0 and the sun as 1.

I get the desired effect of the clouds obscuring the sun when they go over the sun and the sun being more visible when the clouds are not over the sun. However the clouds show up with a black background instead of the blue background. (ie. they appear to just have alpha color and not rgb color)

I am using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) to do the blending. How can I do this while still retaining the blue background of the clouds?

The effect you want is the clouds obscuring the sun right? Make the background in the sun image blue and skip the alpha on this image. Now when you draw the clouds with alpha the sun and blue sky should be visible.

Simplest is what WhatEver says:

A1) draw blue sky with sun, opaque (which means you don’t have to clear the screen)
A2) draw clouds with blending (or just cut-out)

Other mechanisms would require that you either use destination alpha, or possibly the stencil buffer, or an ordered draw. Here’s some ideas:

B1) Draw cloudy sky with destination alpha
B2) Draw sun with blending mode GL_ONE_MINUS_DST_ALPHA, GL_ONE

C1) Clear screen to sky color
C2) Draw sun
C3) Draw individual clouds with blending, sorted far-to-near

another, more advanced method:

  1. draw sky (blue + clouds)
  2. create a vector: sun.pos - camera.pos
  3. creata a line with this vector and a startpoint at camera.pos
  4. compute the 3d-intersection with the sky-plane.
  5. compute the mapping-coords of this intersection point and lookup in the texture.
    based on the value in the texture, adjust the intensity of the sun based on this value(you could also adjust the size of the sun)
  6. draw sun-billboard with blending GL_ONE,GL_ONE (additive) with a color which is set to the intesity of the sun.

btw. this method can be easly extended to be used for lensflares.

Thanks for all the help, should be able to get what I want now.