Rendering to a 2D texture is a cool thing, and allows
you to create some amazing effects. Once such example is
rendering
your 3d world to a seperate texture, then using that texture to either
blur/stretch or modify your image, or even wrap your texture onto an
object! Of course where getting to the world of reflections and
shinny objects, but you can render the world from the perspective of an
object
to a texture. Then render your world with your object in it, but
mapping the texture on it, so it looks like its super reflective or
made of mirrors!
But where going to do something simple here, showing the principles of
rendering to a texture as the focus point, I'll create a 3d menu and
render it to a texture then display it :)
The main parts of the code, the meat and potatoes as
they say, is shown in snippet form below.
Code Snippet : Render Target
|
/// **1** PresentationParameters pp = graphicsDevice.PresentationParameters; RenderTarget2D renderTarget = new RenderTarget2D(graphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1, SurfaceFormat.Color); /// /|\ /// | ///>graphicsDevice.DisplayMode.Format< ALPHA PROBLEMS --+ /// /// NOTE! /// If you use the graphicDevices colour format 'graphicsDevice.DisplayMode.Format', then you /// might notice if you use alphas that your alphas dont work. /// /// **2** graphicsDevice.SetRenderTarget(0, renderTarget); /// Switch to our render target graphicsDevice.Clear(Color.TransparentBlack); /// Clear it /// **3** /// Draw anything we want here!! /// **4** graphicsDevice.SetRenderTarget(0, null); /// Set main render target back Texture2D renderTexture = renderTarget.GetTexture();/// Get our texture2d
|
Of course this is just to show you the main functions, you'd only
new the RenderTarget2D when you needed it once, and I've pointed out
that if you dont set the Color format correctly you'll have a problem
with your alpha's ;)
|