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,876 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now