Creating Render Targets and Surfaces


Now that you have a track and multiple karts being rendered, you can get your scene ready to create a rear-view mirror effect. To do this task, you need to create a texture that is a render target. As the name implies, a render target is the target of your rendering. Before you begin with that, though, you need to ensure that your device is capable of rendering to a render target of the appropriate type. Find the IsDeviceAcceptable method and before the final return true; line, add the following:

 // Skip back buffer formats that don't support render targets format we want if (!Manager.CheckDeviceFormat(caps.AdapterOrdinal, caps.DeviceType,     adapterFormat, Usage.RenderTarget, ResourceType.Textures, Format.X8R8G8B8))     return false; 

Here you're checking whether the device can accept render target textures of type Format.X8R8G8B8, and if it cannot, you return false, signifying that this device isn't acceptable. Now, you need to declare these variables to continue:

 // Rearview mirror private const int RenderTargetSize = 256; private RenderToSurface rearSurface = null; private Texture rearTexture = null; private Surface rearTextureSurface = null; 

First, you have a constant that is the size of the render target you will be creating. (You will almost always want this number to be a power of 2, such as it is here.) Next, you declare the RenderToSurface object, which is a helper class that allows you to render your scene to a surface later. Next, you declare the texture that will be the render target and the surface that will be the underlying surface of that texture.

Because you're not entirely sure where you'll be using this texture and surface, you want to create these objects in the OnResetDevice method. While you're there, you might as well also add the support code for the RenderToSurface object. Add the following code to that method:

 // Re-create the render target texture rearTexture = new Texture(sampleFramework.Device, RenderTargetSize,     RenderTargetSize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default); rearTextureSurface = rearTexture.GetSurfaceLevel(0); if (rearSurface != null) {     rearSurface.OnResetDevice(); } 

Here you'll notice that the texture is created first, using the render target size and a single mip level. The usage will be for a render target, and the format is what you checked earlier in your IsDeviceAcceptable callback. After the texture is created, you store the surface for that texture as well. Because there is only a single mip level, getting the first surface level is always correct for your scenario. For the rearSurface helper class, you simply call the OnResetDevice method if it has been instantiated so far.

Notice that the texture was created in the Pool.Default memory pool, which normally signifies video memory. For a render target texture, this must be the memory pool specified. You must also clean up the texture when the device is lost to ensure proper reset. Find the OnLostDevice method and add this code to the end:

 // Clean up rear view mirror texture if (rearTexture != null) {     rearTexture.Dispose();     rearTextureSurface.Dispose();     rearTexture = null;     rearTextureSurface = null; } if (rearSurface != null) {     rearSurface.OnLostDevice(); } 

Notice that only the rearTexture variable is checked for null here. Because the surface is created from the texture, it is assumed that if the texture is valid, the surface must be. Because these objects are in the default memory pool, they are required to be disposed now. They are also set to null so they cannot be used again (until the device is reset). Also notice that the appropriate helper method is called on the rearSurface member. Because you're on a cleanup kick anyway, add this code to the OnDestroyDevice method, even though you haven't written the code to create the surface yet:

 if (rearSurface != null) {     rearSurface.Dispose();     rearSurface = null; } 

Finally, the last thing you want to do is create your helper object. In the OnNewGame method, add this creation code to the end:

 // Create rear-view mirror surface and texture rearSurface = new RenderToSurface(sampleFramework.Device, RenderTargetSize,     RenderTargetSize, Format.X8R8G8B8, true, DepthFormat.D16); 

Notice that you've used the same render target size and format here.



Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net