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.

DS development > Alpha blending a single sprite

#170119 - headspin - Tue Sep 01, 2009 12:58 am

How do I get per-sprite alpha blending working? I have setup a sprite in 16-bit bitmap mode and have the alpha value set to 0x7. Sprites appear completely opaque. What else do I need to do?

Code:
oamSet(&oamSub,                  // sub graphics engine context
   m_oamIndex,                  // oam index (0 to 127)
   m_x, m_y,                  // x and y pixel location of the sprite
   1,                        // priority, lower renders last (on top)
   0x7,                     // this is the palette index if multiple palettes or the alpha value if bmp sprite   
   SpriteSize_32x32,
   SpriteColorFormat_Bmp,
   m_gfxSub,                  // pointer to the loaded graphics
   -1,                        // sprite rotation data 
   false,                     // double the size when rotating?
   false,                     // hide the sprite?
   false,                     // horizontal flip?
   false,                     // vertical flip?
   false);                     // mosaic?

_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#170121 - sverx - Tue Sep 01, 2009 8:49 am

if you're using a bitmap object (a 32k colors sprite) then check this:

http://nocash.emubase.de/gbatek.htm#dsvideoobjs
Quote:
OBJ Attribute 0 and 2
Setting the OBJ Mode bits (Attr 0, Bit10-11) to a value of 3 has been prohibited in GBA, however, in NDS it selects the the new Bitmap OBJ mode; in that mode, the Color depth bit (Attr 0, Bit13) should be set to zero; also in that mode, the color bits (Attr 2, Bit 12-15) are used as Alpha-OAM value (instead of as palette setting).

#170123 - headspin - Tue Sep 01, 2009 12:51 pm

I did read that on GBATek but nothing seems to be working. I am using libnds so I want to do things the "libnds" way and that is use their helper functions as much as possible rather than hitting the registers directly.

I've looked at the bitmap_sprite example from libnds and although it seems to be setting an alpha value the sprites do not appear alpha blended. When I can't even get the example working (both in No$GBA and hardware although No$GBA fails to rotate the sprite as well) I don't have much hope for it working in my own game.

Please can someone show me a working example of alpha blending a single sprite using the latest libnds.
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#170126 - sverx - Tue Sep 01, 2009 2:14 pm

I never used bitmap objects before, I'm coding an example that uses them with alpha blending, no luck on no$gba at the moment... :|

btw blending a regular sprite (256 colors) works...

#170127 - headspin - Tue Sep 01, 2009 2:41 pm

sverx wrote:
btw blending a regular sprite (256 colors) works...


Really? I can get all the sprites to alpha blend using the blend registers (REG_BLDCNT / REG_BLDALPHA) but not a single sprite. I'm happy to use 256 color sprites if I can have one blended out. If you can show me an example of how to do it that would be great.

Appreciate your help :)
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#170128 - headspin - Tue Sep 01, 2009 3:52 pm

Turns out there is a bug in the No$GBA emulator. When I ran this on hardware it works fine. Just a few tips for others who want to achive per sprite alpha blending.

1. You have to turn on alpha blending for REG_BLDCNT (BLEND_ALPHA)
2. You must use bitmap sprites for per sprite alpha blending
3. BLEND_SRC_SPRITE is not required and REG_BLDALPHA is ignored when using bitmap sprites. Use the alpha value for each oam entry instead
4. Overlapping two alpha blended sprites only shows the background
5. You won't see the result in No$ so run it on hardware
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game


Last edited by headspin on Tue Sep 01, 2009 4:14 pm; edited 1 time in total

#170129 - gauauu - Tue Sep 01, 2009 4:01 pm

Heh, I wish I had remembered that I had had that same trouble also about a year ago, I could've saved you some time. But I didn't remember till you had said so...(I mention it at the bottom of this post http://anguna-dev.blogspot.com/2008/09/update-on-bugs.html)

#170130 - headspin - Tue Sep 01, 2009 4:24 pm

I rely on No$ quite alot and this demonstrates you should always test on hardware every so often. No$ does a damn good job though.

My only problem now is that my fading in and out of black between screens the sprites are not effected by the fading.

I am using

Code:
REG_BLDCNT_SUB = BLEND_FADE_BLACK | BLEND_ALPHA | BLEND_SRC_BG2 | BLEND_SRC_BG3 | BLEND_SRC_SPRITE | BLEND_DST_BG2 | BLEND_DST_BG3;


But it seems like the alpha value for each oam entry overrides the REG_BLDY_SUB value so sprites won't fade in and out of black.

The only solution to this I can see is to loop through oam and set each alpha value to be the fade value and they will fade in and out of black like everything else.
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#170137 - sverx - Wed Sep 02, 2009 8:35 am

I did some more tests yesterday evening, and I'm quite confident now I've found how it works. I'm posting it here for completeness :)

- You have to set the ATTR0_TYPE_BLENDED in the OAM for the sprite(s) you want to alpha blend
- You have to set in the REG_BLDCNT[_SUB] the flags of the DST backgrounds and/or backdrop you want your sprite(s) to blend with (ex: REG_BLDCNT = BLEND_DST_BG1 | BLEND_DST_BG2 | BLEND_DST_BG0 | BLEND_DST_BACKDROP; )
- You have to set REG_BLDALPHA[_SUB] according to 'how' you would like to 'add' the components.
- You don't need to activate a special effect (ex: BLEND_ALPHA). But you can do it, of course. But you also should be able to use another special effect and still have semi-transparent sprites. (There are some limitations, btw...)
- You don't need to activate BLEND_SRC_SPRITE flag in the REG_BLDCNT[_SUB]. If you do this and if you activate also the BLEND_ALPHA mode then you'll have ALL your sprites blending, not only those marked with ATTR0_TYPE_BLENDED.

no$gba fails in showing this behaviour, but it works perfectly on hardware. This morning I tried with some other emulators and I found that iDeaS (I'm using ver. 1.0.3.0) does render that feature correctly. My tests were done with 256 colors sprites, but I believe the effect is the same with 16 color sprites and also with bitmap objects (but in this case I think the value in REG_BLDALPHA will be -at least partially- ignored)

Hope it helps.

#170138 - sverx - Wed Sep 02, 2009 8:39 am

headspin wrote:
My only problem now is that my fading in and out of black between screens the sprites are not effected by the fading.


Maybe you could remove the ATTR0_TYPE_BLENDED attribute from the sprites when fading in/out. This way those sprites won't be blending and then they would fade just like everything else. Don't know if it fits your needs, just an idea...

#171181 - grapeape - Wed Nov 04, 2009 6:42 pm

Hey guys. I'm trying to blend some 256 color paletted (not bmp) sprites with bg3. I set up the blend with:

Code:

REG_BLDCNT_SUB = BLEND_ALPHA | BLEND_DST_BG3 | BLEND_SRC_SPRITE;
REG_BLDALPHA = 0x1010;


oamSet() doesn't seem to allow me to set the attribute field, so just before oamSet, I call:

Code:

oamMain.oamMemory[id].attribute[0] = ATTR0_TYPE_BLENDED;


Where id is the sprite index. Still no joy. I'm using ideas v1.0.3.0 to test. Sverx, do you have an example that defintely works with this emu that you could post?

Cheers,
J

#171182 - Exophase - Wed Nov 04, 2009 8:56 pm

headspin wrote:
My only problem now is that my fading in and out of black between screens the sprites are not effected by the fading.

I am using

Code:
REG_BLDCNT_SUB = BLEND_FADE_BLACK | BLEND_ALPHA | BLEND_SRC_BG2 | BLEND_SRC_BG3 | BLEND_SRC_SPRITE | BLEND_DST_BG2 | BLEND_DST_BG3;


But it seems like the alpha value for each oam entry overrides the REG_BLDY_SUB value so sprites won't fade in and out of black.

The only solution to this I can see is to loop through oam and set each alpha value to be the fade value and they will fade in and out of black like everything else.


Indeed bitmap sprites with alpha < 0xF and > 0x0 or translucent mode will always blend regardless of blend mode. I think that also applies to 3D with final alpha < 0x1F and > 0x0.

Try using the master brightness control to fade to/from black instead, that's what it's there for.

#171188 - sverx - Thu Nov 05, 2009 9:09 am

grapeape wrote:
Sverx, do you have an example that defintely works with this emu that you could post?


I'm doing this (I'm using Sub engine and ext palettes)

The video mode:
Code:
videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG2_ACTIVE | DISPLAY_BG1_ACTIVE | DISPLAY_BG_EXT_PALETTE | DISPLAY_SPR_ACTIVE | DISPLAY_SPR_2D | DISPLAY_SPR_EXT_PALETTE);


The blending:
Code:
   // blending
   REG_BLDCNT_SUB = BLEND_DST_BG1 | BLEND_DST_BG2 | BLEND_DST_BG0 | BLEND_DST_BACKDROP;
    REG_BLDALPHA_SUB = 0x04 | ( 0x04 <<  8 );   // valori in sedicesimi...


The blended sprite:
Code:
   SubSpriteCTRL->Sprite[6].y_attrib_0= ATTR0_TYPE_BLENDED | ATTR0_SQUARE | ATTR0_COLOR_256 | OBJ_Y(60);   // 256 colori
   SubSpriteCTRL->Sprite[6].x_attrib_1= ATTR1_SIZE_64 | OBJ_X(10);                     // normale 8*8
   SubSpriteCTRL->Sprite[6].attrib_2 = TILE256(48) | ATTR2_PALETTE(2) | ATTR2_PRIORITY(2); // tile, priority, palette


sorry the code is quite messy, it's taken directly from my code. Sorry I can't post the whole code or a demo at the moment :|

Hope it helps :)