Solved

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

Posted on 2004-09-08
9
1,895 Views
Last Modified: 2013-12-08
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.


0
Comment
Question by:thrawn80
  • 4
  • 3
9 Comments
 
LVL 17

Expert Comment

by:davebytes
ID: 12007749
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
0
 

Author Comment

by:thrawn80
ID: 12012576
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.

0
 
LVL 17

Expert Comment

by:davebytes
ID: 12012738
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:thrawn80
ID: 12157828
Any suggestions on other ideas or methods?
0
 

Author Comment

by:thrawn80
ID: 12157851
Btw, regarding Hardware rastering... is it possible?

Can anyone shed some light on this topic?
0
 
LVL 17

Accepted Solution

by:
davebytes earned 125 total points
ID: 12170375
I seem to recall that DX5-7 came with DirectDraw-based 'sprite game' sample(s).  Have you tried running those on your machine?  Do they work?  They should have used accelerated methods, but in hindsight maybe they didn't...

The same (well, similar) samples should be available with the DX9 SDK, but they'll use the 'DirectX Graphics' (d3d merged with dd) approach to do it.

It is certainly worth taking a look at the DX9 SDK to see if you can 'grok' the basics of the equivalent sprite samples -- it's gotten much easier to do cool tricks, but yes you need to make the leap to understand just a little bit about the setup and organization of 3D data.  Old DirectDraw methods are being maintained for backwards-compatibility, but the 'top features' may not necessarily all be there.

-d
0
 

Author Comment

by:thrawn80
ID: 12176088
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question