Tutorial 7 - Render to Texture

[Download the source]
Have you ever seen water reflecting the environment? This can be done with a technique called render to texture. Let's look at what it involves.
What is render to texture?
Render to texture describes the operation of "saving/capturing" a render target (surface)to a texture.

Let's take an example:

The top left hand corner is a sprite rendered with the textured that we have "saved/captured" from a render target (surface).
The steps to follow the render to a texture.
The easiest way of capturing a render target (surface) to a texture is by taking the following steps.
1) Declare a Texture to capture to.
2) Declare a Surface that will be your new render target
3) Get the surface of the texture
4) Save the original back buffer (color buffer) in a temporary surface
5) Set the render target of the device to a
6 ) Render the objects that you want to save into the texture
7) Set the render target to the original render target.

7 easy steps to remember (I hope).

The code
Declaring your objects.

texture = new Texture(window.D3DDevice, 128, 128, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
surface = texture.GetSurfaceLevel(0);

Your RenderToTexture method that will render the scene that you want saved/captured into your texture
private void RenderToTexture()
{
    Viewport view = new Viewport();
    view.Width = 128;
    view.Height = 128;
    view.MaxZ = 1.0f;

    //Save the current render target so that we can reset it after use.
    Surface oldRenderTarget = window.D3DDevice.GetRenderTarget(0);
    //Set the render target to the surface we created
    window.D3DDevice.SetRenderTarget(0, surface);
    //Clear the render target (our surface and our depth buffer of the surface)
    window.D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black.ToArgb(), 1.0f, 0);
    window.D3DDevice.BeginScene();
    //Set the material and the world transform and render the cube
    window.D3DDevice.Material = material;
    window.D3DDevice.Transform.World = matWorld;
    cube.DrawSubset(0);
    window.D3DDevice.EndScene();
    //Reset the render target.
    window.D3DDevice.SetRenderTarget(0, oldRenderTarget);
}
Render your scene.
private void Render()
{
    //Invoke our method for Rendering to the texture
    RenderToTexture();
    //Set the material and the world transform and render the cube
    window.BeginRender(Color.Black);
    window.D3DDevice.Material = material;
    window.D3DDevice.Transform.World = matWorld;
    cube.DrawSubset(0);
    //Use the sprite class to easily render the texture that we saved/captured from our render target (surface)
    using(Sprite s = new Sprite(window.D3DDevice))
    {
        s.Begin(SpriteFlags.None);
        s.Draw(texture, new Rectangle(0, 0, 128, 128), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, 0.0f), Color.White);
        s.End();
    }
    window.FinishRender();
}
Published Saturday, August 06, 2005 4:23 PM by pieterg
Filed under:

Comments

# re: Tutorial 7 - Render to Texture

How does this work in full screen?

I have created a similar solution but it down't work in full screen.

Sunday, February 03, 2008 9:57 AM by Annoyed

# re: Tutorial 7 - Render to Texture

Yrxvqs Hi from Russia!

Sunday, March 16, 2008 11:07 PM by zxevil163

# re: Tutorial 7 - Render to Texture

IgbduQ  <a href="nsdwsydotfer.com/.../a>, [url=http://biacjxforllz.com/]biacjxforllz[/url], [link=http://kszwydjpdtkf.com/]kszwydjpdtkf[/link], http://rikfpbzkaapp.com/

Monday, April 14, 2008 11:21 PM by kmeipwybj

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: