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,919 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

728 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