DirectX 11 Tutorials

The following tutorials are based on my exploration of Frank Luna's Introduction to 3D Game Programming with Direct3D 11.0, adapting the C++ original code into C#, using SlimDX as a managed wrapper for the DirectX libraries.
The code for all of these examples can be found on my GitHub public repository, at https://github.com/ericrrichards/dx11.
To compile and run the examples, you will need Visual Studio 2012.  I have the Professional edition, but I would imagine that you should be able to use the C# Express edition as well.  You will also need SlimDX, which is available either through NuGet, or from http://slimdx.org/, as well as the DirectX SDK, which can be obtained at http://www.microsoft.com/en-us/download/details.aspx?id=6812.  If you are running Windows 8 or greater, DirectX is now included in the Windows SDK, so you will want to grab that for the most updated version of DirectX, at http://msdn.microsoft.com/en-US/windows/desktop/hh852363.
Some of the examples use Assimp.Net to load 3D models.  You can download this from Google Code at https://code.google.com/p/assimp-net/.
If you find these tutorials helpful, please consider donating a couple bucks. While my primary goal with this site is simply learning new things and sharing that knowledge with others, I do put a lot of time and energy into these tutorials, and web hosting and domain registration have some costs. Alternatively, you could turn off AdBlock and endure a couple of banner ads...

Tutorials

Initializing DirectX 11

DirectX Initialization and sample framework

Introduces the demo application framework that will be used going forwards, and discusses DirectX intialization and integration with Windows Forms.
A colored cube

Rendering a Colored Cube

Draw a colored cube using simple vertex and pixel shaders.
A simple terrain

Rendering a simple terrain with DirectX 11

Creating simple geometric shapes with DirectX11

Creating simple geometric shapes with DirectX11

Loading a mesh from file using a simple text format

Loading a mesh from file using a simple text format

Rendering a dynamic vertex buffer with DirectX 11

Rendering a dynamic vertex buffer with DirectX 11

Phong Lighting Theory

Phong Lighting Theory

Adding Lighting to the simple terrain

Adding Lighting to the simple terrain

Creating a basic reusable lighting shader

Creating a basic reusable lighting shader

Texture Mapping in DirectX11

Texture Mapping in DirectX11

Adding textures to the simple terrain

Adding textures to the simple terrain

Sprite animation using a texture atlas

Sprite animation using a texture atlas

Alpha Blending in DirectX 11

Alpha Blending in DirectX 11

Using Alpha Blending on the simple terrain

Using Alpha Blending on the simple terrain

Planar Reflections and Shadows using the Stencil Buffer

Planar Reflections and Shadows using the Stencil Buffer

Rendering Billboards using the Geometry Shader in DirectX 11

Rendering Billboards using the Geometry Shader in DirectX 11

Creating a reusable FPS-style Camera class for DirectX11

Creating a reusable FPS-style Camera class for DirectX11

Creating a reusable RTS-style Camera class for DirectX11

Creating a reusable RTS-style Camera class for DirectX11

Frustum Culling and Hardware mesh instancing

Frustum Culling and Hardware mesh instancing

Screen-World Picking with DirectX11

Screen-World Picking with DirectX11

Skyboxes and environmental reflections with CubeMaps

Skyboxes and environmental reflections with CubeMaps

Dynamic Cube-mapped reflections

Dynamic Cube-mapped reflections

Bump and Displacement mapping

Bump and Displacement mapping

Examining the Tessellation Stages of DirectX11: Hull & Domain shaders

Examining the Tessellation Stages of DirectX11: Hull & Domain shaders

DirectX11 Terrain rendering with LOD

DirectX11 Terrain rendering with LOD

DirectX10-compatible Terrain rendering with LOD

DirectX10-compatible Terrain rendering with LOD

A Simple Loading Screen using Direct2D

A Simple Loading Screen using Direct2D

Generating Random Terrain with Perlin Noise Generating Random Terrain with Perlin Noise

Generating Random Terrain with Perlin Noise

Loading 3D models with Assimp.Net and SlimDX

Loading 3D models with Assimp.Net and SlimDX

Skeletal Animation with Assimp.Net and SlimDX

Skeletal Animation with Assimp.Net and SlimDX

Hardware Particle Systems with DirectX 11

Hardware Particle Systems with DirectX 11

Expanding the BasicModel Class

Expanding the BasicModel Class

Shadow Mapping

Shadow Mapping

SSAO

SSAO

Displacement Mapped Water

Displacement Mapped Water

Shadow Mapping and Ambient Occlusion on Terrain

Terrain with Shadow Mapping and Ambient Occlusion

A Terrain Minimap

A Terrain Minimap

Terrain Picking

Terrain Mouse Picking

Pathfinding1

Pathfinding 1: Map Representation and Preprocessing

Pathfinding2

Pathfinding II: A* and Heuristics

Pathfinding3

Pathfinding III: Putting it All Together

11 comments :

  1. Awesome tutorials, thx for the work u have done.

    ReplyDelete
  2. This is a great resource! How about you write a book like the one you used, only based on C# - I would surely buy it!

    ReplyDelete
  3. My name is Tom. I would buy your book as
    well. I have a question. In referring to
    AssImpModelDemo.cs code I am having an
    error. At runtime
    Effects.NormalMapFX.SetDirLights(_dirLights);
    gives a System.NullReferenceException.
    Would you please tell me why? I am running
    Windows 8.1 with the Windows8.1 SDK.
    I am using Visual Studio Pro 2013.
    Any help would be much appreciated.

    Thank you,

    Tom

    ReplyDelete
  4. Most likely you have an error in the post-build step to compile the HLSX shader code. Often times you'll need to add the directory that fxc lives in to your PATH environmental variable. Check for any warnings messages on build.

    ReplyDelete
    Replies
    1. Sorry for the thread necro, but I'm having the same issue, in particular with the TerrainDemo (so far).
      TexturedHills runs just fine.
      Win8.1, VS2012. There's a deprecation warning for every compiled shader (fxc path is set, shaders compile).

      For additional info:
      These lines are all null:
      [code]
      _view = FX.GetVariableByName("gView").AsMatrix();
      _ambientMap = FX.GetVariableByName("gSsaoMap").AsResource();
      _viewProjTex = FX.GetVariableByName("gViewProjTex").AsMatrix();
      _shadowTransform = FX.GetVariableByName("gShadowTransform").AsMatrix();
      _shadowMap = FX.GetVariableByName("gShadowMap").AsResource();
      _walkMap = FX.GetVariableByName("gWalkMap").AsResource();
      _unwalkableSRV = FX.GetVariableByName("gUnwalkable").AsResource();
      [/code]

      They do not exist in the Terrain.fx file, yet the common code tries to access them.

      Also, when launching the TerrainDemo, after loading the effect files, the following error is printed (does not throw an exception):
      "E_INVALIDARG: An invalid paramter was passed to the returning function (-2147024809)"
      "E_FAIL: An undetermined error occurred (-2147467259)

      -Corey

      Delete
    2. Hi Corey,

      It has been quite some time since I wrote that post, so it's highly possible there is some code rot. I may have some time this weekend to look through that code and see if I can reproduce it.

      By the way, this blogspot site is pretty much dead, I moved off of Blogger and onto my own custom site this summer. The new site (with all of the same content as this one): http://richardssoftware.net/Home/DirectX11Tutorials

      Delete
  5. This is Tom again. I converted all the fx
    files to fxo using fxc.exe. I then set the
    property to copy if newer. I am getting
    this error:
    An undetermined error occurred. In the
    console window I get this...
    Loading effects from ... (DIR)
    When I click ok I get...
    Object reference not set to an instance
    of an object.
    Am I missing an effect file?
    I have already copied all the effect
    files over to the FX folder. (eg Sky.fxo)
    It originally gave me an error and said
    it was missing.
    What do you think is the error?
    Any help would be much appreciated.

    Thank you,

    Tom

    ReplyDelete
  6. Hi,
    I found a error that was causing trouble. In core.util.cs the static variable_unmanagedStaging is always at least 1024 long and that's not in some projects.
    So I had to change :
    public static byte[] GetArray(object o) {
    var len = Marshal.SizeOf(o);
    _unmanagedStaging = new byte[len];
    Array.Clear(_unmanagedStaging, 0, _unmanagedStaging.Length);
    //if (len >= _unmanagedStaging.Length) {
    // _unmanagedStaging = new byte[len];
    //}
    var ptr = Marshal.AllocHGlobal(len);
    Marshal.StructureToPtr(o, ptr, true);
    Marshal.Copy(ptr, _unmanagedStaging, 0, len);
    Marshal.FreeHGlobal(ptr);
    return _unmanagedStaging;

    }
    Regards,
    Michel G

    ReplyDelete
  7. Hi,
    A change that has to be done for every country that use comma instead of dot.
    Every Convert.ToSingle(vals[nn].Trim()) MUST be changed to : Convert.ToSingle(vals[0].Trim(), new CultureInfo("en-US"))
    Regard,
    Michel G

    ReplyDelete