Alpha Blending

Just a few changes to get this tutorial up and running, we will also expand upon this tutorial to get our bullets looking more realistic.

XNA 4 gives us some handy state objects meaning that we instead of setting the three variables:

device.RenderState.AlphaBlendEnable = true;
device.RenderState.SourceBlend = Blend.One;
device.RenderState.DestinationBlend = Blend.One;

we can replace them with the one statement:

device.BlendState = BlendState.Additive;

Now you will have noticed that when you are shooting that the bullets just don’t look right from some angles, its obvious that they are just a flat square and if you turned off culling that sometimes you wont see the bullets at all! So to solve this we will use a technique called billboarding.

Billboarding is a pretty simple concept. A billboard is a flat object, usually a quad, that faces the camera. This direction changes constantly as the object and camera move, and the object needs to be rotated each frame to point in that direction.

This technique can also be used to face other objects as well. For example,  to have a character’s head follow an object, or to rotate a turret to face a target.

To achieve this we will create a billboard matrix using XNA’s handy function Matrix.CreateBillboard() so we need to change the line

Matrix worldMatrix = Matrix.CreateScale(0.05f, 0.05f, 0.05f) * Matrix.CreateTranslation(bulletList[i].position);


Matrix worldMatrix = Matrix.CreateScale(0.05f, 0.05f, 0.05f) * Matrix.CreateBillboard(bulletList[i].position, GetCamPos(), GetCamUp(), Vector3.Forward);

We also need to add two new functions:

private Vector3 GetCamPos()

Vector3 campos = new Vector3(0, 0.1f, 0.6f);
campos = Vector3.Transform(campos, Matrix.CreateFromQuaternion(xwingRotation));
campos += xwingPosition;
return campos;


private Vector3 GetCamUp()

Vector3 camup = new Vector3(0, 1, 0);
camup = Vector3.Transform(camup, Matrix.CreateFromQuaternion(xwingRotation));
return camup;


The two functions provide our CreateBillboard function with positions for the camera which are just extracted from the UpdateCamera() method!

Also in we will need to change the line:

device.RasterizerState = RasterizerState.CullNone;


device.RasterizerState = RasterizerState.CullClockwise;

Since the Matrix.CreateBillboard() function seems to flip the quad, I have asked questions about why this is the case but have yet to get an answer! If anyone can explain this I would be very grateful.

Lastly change this line:

device.RenderState.AlphaBlendEnable = false;

to this:

device.BlendState = BlendState.Opaque;

to reset the BlendState back to its default value so that when we render the rest of the scene it doesn’t all get the same alpha blending effect as the bullets!

And that’s all for Alpha blending, next up Skybox!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s