Link to home
Start Free TrialLog in
Avatar of thrawn80
thrawn80Flag for Singapore

asked on

Alpha Blending using BitBlt's raster features and, is there such as thing as Hardware Rastering in DirectDraw?

Hi all.

I've read up on methods for alpha blending on DirectDraw in MSDN. The only methods they offer is to do it "by hand" which means to lock the surface, do a pixel blending directly onto the back buffer, before unlocking.

So therefore, my understanding is,
Alpha Blending is a mixture of 1 or more surfaces with less than 100% opacity.

So for example, I have a magenta surface RGB(255, 0, 255) at 50% opacity. RGB(127, 0, 127)
Then I have a white surface RGB(255, 255, 255) at 50% opacity. RGB(127, 127, 127)
Logically when I blend these 2 surface together I'll get BRIGHT MAGENTA right?
Technically it should be RGB(254, 127, 254) - a bit of pink I believe.

I was thinking, could I have used a simple raster operation to actually perform this blending?
which is, SRCAND? The only raster ops I can perform is by using the WIN32's bitBlt method which is rather ... slow...

Secondly, I often find Hardware Rastering in DirectDraw a pain. It doesn't seem to work at all. Can anybody explain why?

Lastly, please do not advise me to use OpenGL 3D or Direct3D for Alpha Blending. My level of games programming is still relatively low and I'm still at DirectDraw.

Thanks for your attention and guidance.


Avatar of davebytes
davebytes
Flag of United States of America image

You'll find that the reason people push to the 3D solutions is that you don't do anything 'by hand' -- the hardware can do alpha blending intrinsically.

Additionally, DirectDraw interfaces while still maintained for backwards compatability, have basically been 'dead' since DX8 landed on the scene.  So there's less and less active sample code, sprite libraries, etc., that you can learn  from.

Alpha blending is mixing surfaces where alpha is a component of the mixing.  Unfortunately, the best guides to blending, and the algorithms, are all in 3D land.  They extensively cover the different types of blending operations provided natively in hardware.

What you've described is an ADD operation.  That's one approach.  You can also use multiply, subtract/diff, etc.  Also, typically it is the SOURCE ALPHA (the top layer) that is important to the blend calculation, though it depends upon what you want the result to be.  'Alpha blending' is a set of techniques and math/algorithms, but is open and flexible (for the most part)... If you want the result to be half of the two, then you take:
(SRC-RGB * SRC-ALPHA) + (DST-RGB * DEST-ALPHA)
If you want a standard textured-alpha-blend used to apply sprites over a background, you want a neat trick:
(SRC-RGB * SRC-ALPHA) + (DST-RGB * (1 - SRC-ALPHA))
Which uses the source alpha to determine how much of the destination color to 'allow through'.  When source alpha approaches zero, 1-SA approaches 1, thus the sprite pixels stop showing and the destination shows fully.

That help at all?

-d
Avatar of thrawn80

ASKER

Hmmm.... my approach to blending my surfaces would be

(SRC-RGB * SRC-ALPHA) + (DST-RGB)
SRC = blending surface
DST = back buffer
BitBlt seem to solve these problems in a simplier way.

if(any of the R, G or B exceeds 255, it'll remain at 255)

would that be simplier enough?

Yes, you are right. Nowadays most if not all of the computer users are using AGP, which would imply the availability of 3d acceleration. Therefore, writting a game in 2d using a 3d engine is quite a norm nowadays I believe.

that's a "saturated add".  and a fine solution if it looks the way you want it to.  If BitBlt takes care of it using that, go for it. ;)

Yes, you'll find dozens of sprite engines (or starter kits) popping up now, since it's really trivial to do certain classes of 2D/sprite coding with 3D hardware.  Some scrolling systems don't work quite as well as old dedicated virtual video pages did, but then you get the advantage of lighting, blending, rotation, effects, all for 'free'. ;)

-d
Any suggestions on other ideas or methods?
Btw, regarding Hardware rastering... is it possible?

Can anyone shed some light on this topic?
ASKER CERTIFIED SOLUTION
Avatar of davebytes
davebytes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
icic.. thanks.

The author of the book, Ernest Pazera once said that the Hardware Rastering features on DirectX 7 seemed a little sketchy and eccentric...

But then again, I'm seriously considering using a Direct3d to write 2d games....

Thanks again.