Creating a Direct3D renderer class
In this recipe, we will look at the final component of our sample rendering framework, the renderer. These are classes that implement specific rendering logic, such as drawing a mesh or utility classes that wish to participate within the Direct3D resource lifecycle management of the rendering framework.
Getting ready
We continue on from where we left off in the Using the sample rendering framework recipe.
How to do it…
We will first look at the steps necessary to create a Common.RendererBase
descendent and then how this class would be created, initialized, and finally execute its Direct3D draw commands.
- Creating a renderer class within the sample framework requires the following minimal code:
public class MyRenderer : Common.RendererBase { // Create device dependent resources protected override void CreateDeviceDependentResources() { ... } // Create size dependent resources protected override void CreateSizeDependentResources() { ... } // Perform rendering commands protected override void DoRender() { ... } }
- Within a class that is ultimately descending from
D3DApplicationBase
, you would create and initialize an instance of this renderer class as follows:var myRenderer = ToDispose(new MyRenderer()); myRenderer.Initialize(this);
- Finally, within a render loop, we can tell the renderer instance to perform its draw commands as shown in the following line of code:
myRenderer.Render();
How it works…
The Common.RendererBase
abstract class is similar to the D3DApplicationBase
class. It follows the same approach for managing the lifecycle of Direct3D resources by using the SharpDX.Component
class and the CreateDeviceDependentResources
and CreateSizeDependentResources
methods. In addition, a public Render
method is available for executing the renderer's Direct3D commands.
The following class diagram shows the available methods and properties:
Implementations of the RendererBase
class included in the rendering framework sample code includes the TextRenderer
and the FpsRenderer
classes. The text renderer uses the Direct2D device and context to render text of the specified font, size, and color at the desired screen location. The FPS renderer descends from the text renderer and displays the current frame rate and frame time (100 frames simple moving average).
See also
- The recipes in Chapter 9, Rendering on Multiple Threads and Deferred Contexts, demonstrate how to control the current device context for a renderer instance