Now that you have your variables created, you are ready to render the scene into this render target texture you've created. Add the method from Listing 22.4 to your code now. Listing 22.4. Rendering a Scene into the Rear View/// <summary>Renders the scene into the rear-view mirror texture</summary> private void RenderRearViewMirror(Device device) { if (currentState != GameState.Gameplay) return; // Nothing to do here // Render the scene into the surface for the rear-view mirror Viewport view = new Viewport(); view.Width = RenderTargetSize; view.Height = RenderTargetSize; view.MaxZ = 1.0f; rearSurface.BeginScene(rearTextureSurface, view); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, 0, 1.0f, 0); RenderScene(Matrix.Identity, Matrix.LookAtLH( new Vector3(0, 10, -8), new Vector3(0, 0, -30.0f), ModelViewerCamera.UpDirection), camera.ProjectionMatrix); rearSurface.EndScene(Filter.None); } Here you check to make sure that the scene is in the GamePlay state, and if it isn't, you simply exit out of the method because you have nothing to render. You'll notice that the BeginScene method takes the surface that will be rendered to as an argument. Because you are using the surface you retrieved from the texture, any updates to this surface are reflected in this texture. The EndScene method allows you to pass in a mipmap filter that you want to apply to the surface. Rather than worry about the device capabilities, simply use no filtering for this example. One last thing to notice is that the clear color for the scene was changed. You do so to show the obvious difference between the "real" scene and the rear-view mirror scene. Aside from that, this rendering is basically the same as the rendering of the normal scene. You call Clear on the device, and then you call RenderScene with a different set of matrices. This step renders the scene (still using your shaders) to the texture. However, you haven't added a call to this method anywhere yet. Add a call to this method as the first line of the OnFrameRender method (before the Clear call): // Before you do anything else, render the rear-view mirror RenderRearViewMirror(device); Construction Cue
|