IT tutorials
 
Mobile
 

XNA Game Studio 3.0 : Creating Fake 3-D - Creating Shadows Using Transparent Colors

11/29/2011 4:48:11 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
Lots of the graphics in games are faked. Rather than make something 3-D, a game programmer makes something that looks 3-D but turns out to be much easier to program. In this section, you make some 3-D text, but without using any complicated rendering or models (although you can do this kind of thing if required). You use only two principles:
  • Things that are 3-D have shadows.

  • Things that have the light shining directly on them look the brightest.

This means that you need to draw your text in three stages. First, you draw the shadows, then the "sides" of the text, and finally the top layer of the text. This seems like a lot of work, but, as Figure 1 shows, I think it’s worth the effort.

Figure 1. 3-D text that "jumps" out of the screen


1. Creating Shadows Using Transparent Colors

The first part of the text that you want to draw is the shadow at the back. You draw your picture from the back forwards and use the fact that each time you draw, you add to what’s already there. You use another feature of XNA drawing: colors that cause things to be drawn slightly transparent (that is, with part of the background showing through). By drawing transparent colors on top of each other, you can get a nice blurry effect, as done in the following code:

Color nowColor = new Color(0,0,0,20);
for (layer = 0; layer < 10 ; layer++)
{
spriteBatch.DrawString(font, nowString, nowVector, nowColor);
nowVector.X++;
nowVector.Y++;
}

This code is very similar to the previous code that draws the 3-D text except that it creates the value for nowColor in a slightly different way. The Color is constructed from four values rather than three:

Color nowColor = new Color(0,0,0,20);

The first three values give the intensity of red, green, and blue, which you’ve set to 0 because you’re drawing in black. The fourth gives the transparency of the color. In graphical terms, this is often called the alpha channel value. The bigger the number, the less the background shows through. Just like your color intensity values, the transparency value can range from 0 (completely transparent) to 255 (solid color). If you don’t give a transparency value, the Color is created as solid.

A value of 20 means that a lot of the background shows through the color that you draw. Figure 2 shows the display produced by drawing 10 times using a transparent black value. Note that because each of the drawing positions is slightly different, you get a blurring effect.

Figure 2. Creating a shadow using transparent colors


This works rather well in that the text is nicely blurred around the edges, as a shadow would be. Now you know one way video games achieve blur. They can do it by repeatedly drawing the same scene in slightly different positions.

The next part of the drawing process uses the same technique you’ve used before, except that you use slightly different colors. The complete drawing method is as follows:

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

DateTime nowDateTime = DateTime.Now;
string nowString = nowDateTime.ToLongTimeString();
Vector2 nowVector = new Vector2(50, 400);
int layer;

spriteBatch.Begin();

// Draw the shadow
Color nowColor = new Color(0, 0, 0, 20);
for (layer = 0; layer < 10; layer++)
{
spriteBatch.DrawString(font, nowString, nowVector, nowColor);
nowVector.X++;
nowVector.Y++;
}

// Draw the solid part of the characters
nowColor = Color.Gray;
for (layer = 0; layer < 5; layer++)
{
spriteBatch.DrawString(font, nowString, nowVector, nowColor);
nowVector.X++;
nowVector.Y++;
}

// Draw the top of the characters
spriteBatch.DrawString(font, nowString, nowVector, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}


This produces the display shown in Figure 2.


2. Drawing Images with Transparency

Something else that’s useful is that if you draw an image using a color that has a transparency value, the image is drawn transparently. This is how game programmers get pictures to fade slowly onto the screen. The image is repeatedly drawn with different levels of transparency to make it slowly appear over a background.

 
Others
 
- Android Application Development : ViewGroups (part 2) - ListView, ListActivity, ScrollView & TabHost
- Android Application Development : ViewGroups (part 1) - Gallery and GridView
- Java ME on Symbian OS : MIDP 2.0 Game API Core Concepts
- Java ME on Symbian OS : Building a Simple MIDP Game
- jQuery 1.3 : Compound effects & Creating custom animations
- jQuery 1.3 : Effects and speed
- Mobile Web Apps : Quick Wins (part 2) - Form Field Attributes
- Mobile Web Apps : Quick Wins (part 1) - Nifty Links
- iPhone Developer : Using Creative Popping Options
- iPhone Developer : Navigating Between View Controllers
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us