Initializing Direct3D

  CU_MDX_D3DInit.zip (11.1 KiB, 3,859 hits)

Before we go on creating Unreal 5, we first need to learn how to initialize Direct3D. This tutorial will initialize Direct3D with basic functionality. There’s a lot more settings to choose from when you initialize Direct3D, but this tutorial is meant to get you up and running with minimal effort.

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

The first thing we need to do is add a couple of namespaces to our program: Microsoft.DirectX and Microsoft.DirectX.Direct3D. While this isn’t necessary, it saves us some typing in the rest of the code.

/// 
/// The main entry point for the application.
/// 
[STAThread]
static void Main()
{
    using ( Form1 form = new Form1() )
    {
        if ( !form.InitializeGraphics() )
        {
            MessageBox.Show( "Unable to initialize DirectX." );
            form.Dispose();
            return;
        }
        Application.Idle += new EventHandler( form.OnApplicationIdle );
        Application.Run( form );
    }
}

Before we run the Application with Application.Run, we’ll call our application defined method, InitializeGraphics, to initialize Direct3D.

/// Initializes DirectX graphics
/// true on success, false on failure
public bool InitializeGraphics()
{
    PresentParameters pp = new PresentParameters();
    pp.SwapEffect = SwapEffect.Discard;
    pp.IsWindowed = true;
    try
    {
        m_device = new Device( 0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, pp );
        return true;
    }
    catch ( DirectXException )
    {
        return false;
    }
}

We initialize Direct3D by creating an instance of a Direct3D Device object. The m_device variable is a member variable of the Form1 class and is an instance of the Device object. But before we can create the device, we need to fill out the members of the PresentParameters class. The PresentParameters class is used to specify how DirectX is going to behave. Members include pixel formats, how backbuffers are displayed, how many backbuffers are used, etc. In this tutorial, we are doing the bare minimum, so we’ll just fill out a couple of the members of PresentParameters. If everything goes according to my master plan, Direct3D is now initialized and we can begin rendering a scene.

/// Renders the current frame
private void RenderFrame()
{
    m_device.Clear( ClearFlags.Target, Color.Navy, 1.0f, 0 );
    m_device.BeginScene();

    // Render frame here

    m_device.EndScene();
    m_device.Present();
}

To render a frame, we first need to erase our “canvas” by calling Device.Clear. This clears the window so we can render a new frame. Whenever we want to render anything, we first need to call Device.BeginScene. This just tells DirectX to listen up because we’re gonna start sending it geometry to render. Once we’re done rendering, we need to call Device.EndScene, which is the opposite of Device.BeginScene. And finally we call Device.Present. This displays what we just rendered to the monitor so you can see what’s going on. The junk that goes on between Device.BeginScene and Device.EndScene renders to a backbuffer, which is like a canvas of paper. When we’re done drawing on our canvas, we Present it to our audience.

Direct3D is now up and running, but not efficiently. In the next tutorial, we’ll learn how to query the user’s hardware to intialize a more capable device.