Managed DirectX 9 (Part 4) - Transparent Sprites


Senior Contributor
May 21, 2003
Boston, Massachusetts
I will be making a set of 5 or 6 tutorials, each of which include source for Visual Basic.NET 2002 and Visual Basic.NET 2003. C# tutorials may or may not follow. These tutorials are to give you guys a jumpstart to some Direct3D. I hope you enjoy these.

This tutorial will teach you how to remove that green background.
Note: This tutorial is based directly off of my other tutorial:
I have posted a tutorial here for people who do not know my site.

This tutorial will be based off of the previous tutorial (Matrices and Transformations). So open up the project from that (you can download the source code at the forums if you wish, or you can just get it from the thread " Managed DirectX 9 (Part 3) - Matrices and Transformations ", in the Tutors Corner of this site.

This tutorial will be very brief, and well only be working with clsSprite.

Getting Started
Open up the source code from the Matrices and Transformations tutorial. Ill be very brief in this tutorial as most of it is pretty simple.

In clsSprites globals:
This variable will store if the image is Transparent or not
Dim IsTransparent As Boolean

Change the arguments for Public Sub New of clsSprite:
Public Sub New(ByVal lDevice As Device, ByVal ImageName As String, ByVal TopLeft As Point, ByVal Width As Integer, ByVal Height As Integer, Optional ByVal Transparent As Boolean = True)

Just in case you dont know what an Optional Argument is:
There are some arguments for a function which you type frequently and you dont want to bother wasting time typing. Specifying an Optional argument means, when you call your function - you dont have to use that argument, a default value is provided there. Most of our sprites will be transparent, so we might as well make them transparent by default without having to specify True for the last argument New Sub... understand? Its easier than you think :).

Now add this line to the beginning of the New sub:
IsTransparent = Transparent

Pretty easy so far, it just stores the variable IsTransparent. Now lets modify the Load sub. Add this line after declaring Vertices():

This value will store our color that is to be made transparent.
That color is: Fully Visible (indicated by the first argument)
and fully green (third argument).
Basically, our color is LimeGreen (255,0,255,0).

Dim ColorKeyVal As Color = Color.FromArgb(255, 0, 255, 0)

Now replace this block with the line that says SpriteImage = TextureLoader.FromFile(dxDevice, ImageName):
If our image is transparent Then.
If IsTransparent Then
The following arguments require a very advanced knowledge of Direct3D. So I really dont
know what most of them do.
SpriteImage = TextureLoader.FromFile(dxDevice, ImageName, 32, 32, D3DX.Default, 0, Format.Unknown, Pool.Default, Filter.Point, Filter.Point, ColorKeyVal.ToArgb)
Arguments from left to right:
Device, ImageName, Width, Height,

MipLevels (Honestly I dont know what this is... so I use default).
Usage (this is something advanced, so we leave it alone and say 0; use 0 when unsure of anything in Direct3D).
Format: Heh, instead of guessing the image format (8-bit, 16-bit, 24-bit...etc) and hardcoding it for each image that you have,
just use Format.Unknown and let d3d figure it out.
Memory Pool: Let d3d handle the memory stuff, so say defualt.
Filter & Mip Filter - They affect how the image is rendered. Filter.Point is the clearest and is NOT good if your image is pixilated.
Try changing both the Filter.Points to Filter.Triangle, youll get a slightly more blurrier image,
but it adds a nice "warm" effect. Just mess around with the Filter arguments until you get one that works for you. I use Points for
clearer images, and Triangle for images that appear to be pixilated and require blending
Finally, the Color to be made transparent

Set our SpriteImage, non transparent
SpriteImage = TextureLoader.FromFile(dxDevice, ImageName)
End If

Lots of green ^^. Everything that I know about that line is written right there in green.

Now lets jump to the Render sub. At the very beginning, do this:

If the sprite is Transparent, then
If IsTransparent Then
Enable AlphaBlending. The process of mixing a pixels color + its inverse color.
dxDevice.RenderState.AlphaBlendEnable = True
A Pixels color + its inverse color is a "transparent color"
Not fully understanding how this works, all I can really tell you is
that whenever it sees the transparent color (LimeGreen), it
overlays it with its inverse color, and makes the LimeGreen transparent.

dxDevice.RenderState.SourceBlend = Blend.SourceAlpha
dxDevice.RenderState.DestinationBlend = Blend.InvSourceAlpha
End If

The process of Alphablending makes a pixel transparent. Youre simply making LimeGreen transparent by using those flags. I honestly do not understand what they mean. If anyone has any information whatsoever, please post on the forums. Thank you.

Ok, alphablending takes up a lot of memory, so, disable it when youre done. At the very end of the Render sub:
Alphablending takes up a LOT of memory, so just disable it when you dont need it.
dxDevice.RenderState.AlphaBlendEnable = False

Run the program, the (2) sprites should be transparent! Sorry for the lack of explanation, this is something which I dont understand a lot about. Next up: "World Space vs Model Space." Now that is something which I do understand pretty well! So youll have a lot of explanation going on there.

Edit: Sorry, the link for the source code is here:
I dont think you mods have to worry about the source code containing an exe, however, since its not hosted on your forum.

-The Pentium Guy
Last edited by a moderator: