gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Alpha Blending Issue

#46362 - AkumaATR - Thu Jun 23, 2005 5:46 pm

Hey guys. I am trying to alpha blend bg2 (common starfield) (target layer) with some sprites (source layer) in Mode 4. Sprites are set up to be transparent. Also, further below I set:

REG_BLDMOD = 0x450;
//10001010000b

REG_COLEV = 0x808;
//100000001000b for 1/2 target color, 1/2 source color

I end up being able to see the stars throught the sprites (some sort of alpha blending is taking place, just doesn't appear to be right), but the stars are black dots. When they reemerge on the other side they go back to being white. The majority of the sprite doesn't get darker (as I want it to) as well -- I'm not sure if this is due to the same issue, or because first entry palette colors (black is behind the starfield and it's bg palette entry 0) are ignored in the alpha blend because first entry palette colors are deemded transparent by the hardware (I think this only applies to sprites... not sure yet... can anyone clarify?).

I am also wondering if alpha blending like that can even be accomplished in palettized modes -- my palettes are quite small -- I don't have palette colors defined for the resulting color that would come from an alpha blend -- is this an issue? I'm guessing it's not an issue since the result color is computed for each relative pixel for each frame... so it prob. doesn't need to have associated colors in the palette...

Thanks,
Jason

#46371 - Quirky - Thu Jun 23, 2005 8:00 pm

I had this problem - it's as if it "masks" rather than "blends" would be my description - and I seem to remember solving it by adding the "BD" background in there. I use transparent sprites over a couple of mode 1 backgrounds and have this as my function:

Code:

#define MODE_TRANSPARENT        0x400

#define BLDMOD_FIRST_BG0  BIT00
#define BLDMOD_FIRST_BG1  BIT01
#define BLDMOD_FIRST_BG2  BIT02
#define BLDMOD_FIRST_BG3  BIT03
#define BLDMOD_FIRST_OBJ  BIT04
#define BLDMOD_FIRST_BD   BIT05
#define BLDMOD_MODE(n)    (n<<6)
#define BLDMOD_SECOND_BG0 BIT08
#define BLDMOD_SECOND_BG1 BIT09
#define BLDMOD_SECOND_BG2 BIT10
#define BLDMOD_SECOND_BG3 BIT11
#define BLDMOD_SECOND_OBJ BIT12
#define BLDMOD_SECOND_BD  BIT13

void fade_bg_sprites(int factor)
{
  // fade by "factor" the sprites to the bg
  // bldmod contains the layers to blend with, blend mode 0 means only
  // blend objects that have transparency set.
  REG_BLDMOD = BLDMOD_SECOND_BG0 |
               BLDMOD_SECOND_BG1 | BLDMOD_SECOND_BG2 | BLDMOD_SECOND_BD |
               BLDMOD_MODE(0);
  if (factor > 16) {
      REG_COLEV = 16;  // all sprites, no background
  } else {
    REG_COLEV = factor | ((16-factor)<<8); // fade from all sprite (16|0) to all bg (0|16)
  }
}


I know it has extra bgs, but the idea should help. I set sprites to be blendy individually by setting the MODE_TRANSPARENT flag on sprite attribute0.

Note:I'm not too sure about mode 4, the principal should be the same as for other modes though.