Chad Vernon
  • Home
  • Reel/Resume
  • Work
  • Resources
  • About
  • Contact
sideBar

Search

Categories

  • CG
  • cvxporter
  • Maya
    • API
    • Plug-ins
  • Personal

Archives

  • September 2011
  • June 2011
  • March 2011
  • December 2010
  • November 2010
  • September 2010
  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • December 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007

Rss

  • Main Entries RSS
  • Comments RSS
Home » Resources » Managed DirectX 2.0 » Initializing Direct3D

Initializing Direct3D

  CU_MDX_D3DInit.zip (11.1 KiB, 2,928 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.

/// <summary>
/// The main entry point for the application.
/// </summary>
[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.

/// <summary>Initializes DirectX graphics</summary>
/// <returns>true on success, false on failure</returns>
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.

/// <summary>Renders the current frame</summary>
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.

No Responses to “Initializing Direct3D”

Subscribes to this topic Comment RSS or TrackBack URL

Leave A Reply

Allowed tag : <blockquote>, <p>, <code>, <em>, <small>, <ul>, <li>, <ol>, <a href=>..

 Username

 Email Address

 Website

Sticky: Always double check your comment before posting Please Note: Comment Moderation Maybe Active So There Is No Need To Resubmit Your Comments
Home » Resources » Managed DirectX 2.0 » Initializing Direct3D

© 2011 Chad Vernon