Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Developing for Windows Phone and Xbox Live : Avatars Using Render Targets

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/10/2011 6:39:04 PM
Although the avatars render in 3D, it is possible to use them within your 2D game easily. One method to do this is to draw the animating avatars to a render target and then use the texture from the render target as you would any other 2D texture in your game. You can even build sprite sheets from the avatar animations.

Let’s create a sample that shows how to render an avatar to a RenderTarget and then display the texture as a sprite. Start with the existing sample that displays a random avatar animation. For an additional member variable to store the RenderTarget, add the following member variable to your game’s class.

RenderTarget2D renderTarget;

To initialize the RenderTaget so it is ready to be set on the device, add the following to the LoadContent method in your game:

// Create new render target to draw the avatar to it
renderTarget = new RenderTarget2D(GraphicsDevice, 512, 512, false,
SurfaceFormat.Color, DepthFormat.Depth16);


You also want to change the projection matrix to use an aspect ratio of 1 because the width and height of the texture are equal.

projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
1, 0.01f, 200.0f);

The last portion of code changes the way you render the avatar in the game’s Draw method. Update the game’s Draw method to contain the following code:

// Set the render target
GraphicsDevice.SetRenderTarget(renderTarget);

// Set the render target to transparent
GraphicsDevice.Clear(Color.Transparent);

// Set the world, view, and projection matrices
avatarRenderer.World = world;
avatarRenderer.View = view;
avatarRenderer.Projection = projection;

// Draw the avatar to the current render target
avatarRenderer.Draw(avatarAnimation);

// Set back to the backbuffer
GraphicsDevice.SetRenderTarget(null);

// Clear the backbuffer
GraphicsDevice.Clear(Color.CornflowerBlue);

// Draw the render target as a sprite
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
spriteBatch.Draw(renderTarget, Vector2.Zero, Color.White);
spriteBatch.End();

First, set your render target and clear the render target to transparent. You clear to transparent to be able to composite the texture with other textures in your game. Then, draw the avatar as you would normally by setting the World, View, and Projection transforms before calling the Draw method. This draws the avatar to the render target.

The render target is then set to null to switch back to the backbuffer. The backbuffer is then cleared to CornflowerBlue before the SpriteBatch is used to draw the render target to the screen.

If you run the sample now, notice that the the texture with the avatar appears in the upper right-hand corner of the screen. Because the rest of the texture is transparent, it is difficult to tell you are using a render target. You can change the clear color when clearing the render target to show that you are in fact drawing the avatar to the screen using a texture that is created using a render target. Figure 1 shows the avatar drawn to the screen using a render target.

Figure 1. Avatar drawn to the screen using a render target

Other -----------------
- Developing for Windows Phone and Xbox Live : Interacting with Objects
- Windows Phone 7 : Resetting a form by shaking the phone!
- Developing for Windows Phone and Xbox Live : Blending Between Animations
- Managing Gestures from the Silverlight for Windows Phone 7 Toolkit
- Windows Phone 7 : Handling Gestures in a Graphical Context Such as a Game Menu
- Developing for Windows Phone and Xbox Live : Modifying Avatar Lighting & Playing Multiple Animations
- Windows Phone 7 : Adding Gestures Management to Click-less Silverlight Controls
- Managing Gestures in a Silverlight Windows Phone 7 Application
- Developing for Windows Phone and Xbox Live : Introduction to Avatars (part 2) - Loading Avatar Animations with AvatarAnimation & Drawing the Avatar Using AvatarRenderer
- Developing for Windows Phone and Xbox Live : Introduction to Avatars (part 1) - Accessing Avatar Information Using AvatarDescription
 
 
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
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server