Dark Overlord of Chaos Edition
Forum    Blog    FAQ    About    
 
     
     
Login
Username(*):
Password(*):
 
 
Control Panel
 
 
Blog Entry
XNA and Render Surfaces
(jlucard, 22:17:54 - 21 May 2011 - )

C#Previously on GD chaos, our hero demonstrated how he can kill his frame rates by drawing simple (yet expensive in terms of processing) 2D primitives. Today the hero cheats. Imagine a world way more complicated to draw, a world of 2D and 3D shapes of great complexity. Oh the terror of having to calculate everything from scratch for every single frame. No need to panic. Render surfaces are your friends. Render surfaces love you. You see dear reader, not every graphic artefact changes all the time. So if something has not really changed, why re-create it from scratch again and again. A much better idea is to “render” this artefact on a texture, a buffer in the graphic card’s memory, and display that texture instead. The good news is XNA makes this technique extremely easy to use. The bad news is your texture size is limited by the graphics card’s capabilities – 4096x4096 is the maximum for most decent ones. No matter, still useful. So with no further ado let me introduce you to your new best friend, RenderTarget2D.

// Create a render surface
RenderTarget2D surface;
surface = new RenderTarget2D(graphicsDevice, sizeX, sizeY);

// Set the surface as the render target and clear it
graphicsDevice.SetRenderTarget(surface);
graphicsDevice.Clear(Color.Black);

// Add your code to draw anything you wish here, 2D or 3D

// Set the render target back to screen
graphicsDevice.SetRenderTarget(null);

Note that the code example above assumes the presence of C# express 2010 and XNA 4.0. As you can see Mr RenderTarget2D is quite easy to setup. Now typically the content of such a surface would either be created during initialisation (assuming it never needs to be changed again), or through an update method provided by a class that wraps such an object (the class update method can be called inside the game.Update() method). The latter can be more useful as wrapping such a surface in a class can allow the rendering code to be invoked or not depending if any of the parameters used to control what is to be drawn has changed. Our hero usually uses a “modified” flag to see if the content needs to be re-created or not. Assuming this is done, one way or another, displaying the surface comes next. This is easily done as RenderTarget2D can be used as a texture. A simple call to the SpriteBatch Draw() method is all that is required. This also means that certain basic effects such as flipping, scaling and rotating the image can be accomplished simply by changing the parameters passed to the draw method.

// Depending on how many parameters are needed
// it might look a bit like this
sprites.Draw(surface, position, clipping, tint, 
    rotate, origin, scale, flipeffect, 0);

To see how all this can be useful let as consider a simple scenario: assume you wanted to draw a filled ellipse – normally you would either create a bitmap of one in something like Photoshop, or you would draw one using basic 2D primitives (plotting points and so on). The first is a bad idea as it requires more graphics to be included in you project and the second one inefficient as you would waste a lot of processing power. So here is an idea. Draw a circle instead (we seen how this can be done in a previous tutorial) save it on a render surface and draw it using different scale values for the X and Y Axis (thus stretching our circle so that it looks like an ellipse). We can even rotate it as needed since the angle is one of the parameters Draw expects.

Normally you will want to do far more complicated things. Mini-maps, reflections and GUI elements are only some of the things this technique is typically employed for. When dealing with 2D shapes, displaying a single texture is much faster than displaying lots of smaller textures/sprites, especially if those overlap each other. When dealing with 3D scenes, saving a certain camera view as a texture opens up some possibilities for performing a number of interesting effects. Our friend, the render surface, is a very useful thing indeed. This is particularly true if we are dealing with things that can’t be pre-created using a drawing application.

 
 
 
Friends
Links to external sites
 
 
             
 
 
Who's online
In the last 15 minutes, 1 user(s) have been active: 1 guest(s) + 0 registered.
User list (first 32):
 
There are 19 registered users in total.
Most users ever online was 291 on 03:43:14 - 17 Nov 2011
 
 
The art for this website is a copyright © of dark overlord jlucard.
 
Website powered by Drupal
This website is powered by Drupal