Introduction to 3D Game Programming with DirectX 11

Frank D. Luna

Mentioned 8

Presents instructions on programming interactive video and computer games using DirectX 11.

More on

Mentioned in questions and answers.

So far the only tutorials I've been able to find are on, which are essentially inferred from the existing samples. Does anyone know where to find other tutorials, or better yet open source projects using DirectX 11? (Extra points for project code using DirectX 11 :) )

One book that I think is very useful for DirectX11 even if you not doing game programming is Beginning DirectX 11 Game Programming book . Excellent source of information.

I think if you need a really in-depth understanding of concepts, Frank Luna's book Introduction to 3D Game Programming with DirectX 11 is by far the best book on DirectX 11.

Also there is a good beginner level easy to follow video tutorial series on YouTube here.

My question for you guys are really about more resources. Ive gotten stuck in my homework and my school wants me to do stuff with shaders but has frank d luna as a resource which is all written in effect files. Which source code I might add, does not seem to work for most people, including myself. Im trying to learn more about 3d programming and trying to create a simple grid/terrain area but it is super hard to find simple and educative source code.

I got the book of Frank D luna which to be honest havent entirely helpful. Everything is in effect files which is to my understanding obsolete and is still slightly different compared to vertex/pixel shaders. Also, any source code from that book is not able to run properly. But the structure of the code is fantastic.

Rastertek is a very unhelpful resource as in that he goes through everything at once in everything. Like asking for a page and getting handed a 500 page book. It is very hard to follow and jump back to use as a resource when it consistantly writes codes as a preparation for another tutorial or to complete a past tutorial.

Is there any resources that anyone recommend that would be worth spending money on or even better, free? It is so much information at times that it is hard to summerize everything so I would love to see short source code examples. Much like Frank d luna, except you know.. effect files and actually working.

I have a spatial structure where I keep a few objects. All the positions of the objects are global.

I'm now trying to create a parent/child system but I'm having trouble with the math. What I tried at first was, everytime I move an object, I also move all of its children by the same amount. That works but I also need rotation, so I tried using matrixes. I built a model matrix for the child. It was built using the relative position/rotation/scale to the parent:

glm::mat4 child_model;
//"this" is the parent
child_model = glm::translate(child_model, child_spatial.position - this->m_position);
child_model = child_model * glm::toMat4(glm::inverse(this->m_rotation) * child_spatial.rotation);
child_model = glm::scale(child_model, child_spatial.scale - this->m_scale);

I would then rotate/translate/scale the child matrix by the amount the parent was rotated/moved/scaled and then I would decompose the resulting matrix back to the global child:

child_model = child_model * glm::toMat4(this->m_rotation * rotation);
child_model = glm::translate(child_model, this->m_position + position);
child_model = glm::scale(child_model, this->m_scale * scale);

where position/rotation/scale are defined as:

//How much the parent changed
position = this->position - m_position;
rotation = glm::inverse(m_rotation) * this->rotation;
scale = this->scale - m_scale;


glm::decompose(child_model, d_scale, d_rotation, d_translation, d_skew, d_perspective);
child_spatial.position = d_translation;
child_spatial.rotation = d_rotation;
child_spatial.scale = d_scale;

But it doesn't work and I'm not sure what is wrong. Everything just spins/moves out of control. What am I missing here?

This problem is similar to joint animation used for computer animations. First you have to use or construct transformation matrices for each object. However, the transformation matrix of each child object should be with respect to their parent's coordinate system (i.e., child coordinate should be in parent's coordinate space). We'll call this matrix a ToParentMatrix. In addition, each object needs to have another matrix which transforms it all the way to the root (which is in world space). We'll call this matrix aToRootMatrix. When we multiply these two matrices we get a 'ToWorldMatrix' which contains the position and orientation in world space. So the transformation of each object to world space is as follows in a 2-joint hierarchie (root, child1 (of root) and child2 (of child1)):

RootTransform = ToWorldMatrix; // The root object is allready in world space;
Child1.ToRootMatrix = RootTransform;
Child1.ToWorldMatrix = Child1.ToRootMatrix*Child1.ToParentMatrix;

Child2.ToRootMatrix = Child1.ToWorldMatrix;
Child2.ToWorldMatrix = Child2.ToRootMatrix*Child2.ToParentMatrix;

For more information, search for joint (or skeletal) animation and forward kinematics. Also Frank Luna's book has two nice chapters about skeletal animation.

I'm writing a small rendering engine using C++ and DirectX 11. I've managed to render models using indexed vertices. Now I want to support textures and vertex normals as well. I'm using Wavefront OBJ files, which uses indices to reference to the correct texture and normal coordinates (much like indexed vertices). This is the first time I'm doing something like this with vertices, textures and normals combined, so this is all a bit new to me.

The problem I'm facing is that the number of indices for vertices, normals and texture coordinates are not the same and I'm trying to find a right way to use all these indices in a vertex shader. To illustrate the problem more clearly, I've made some images.

The left image is a wireframe of a simple piramid object and the right image is its UV coordinate layout (with the bottom of the piramid in the center). In the left image you can see that the piramid has 4 vertices, so there are 4 vertex indices. The right UV layout has 6 UV coordinates so there are 6 texture indices. For each face of the piramid has a normal of its own, so there are only 4 normal indices.

I've found this question while searching on SO and it seems to work in theory (I haven't tried it yet). Is this a common way to solve a problem like this or are there better ways? What would you recommend I do?

Thanks :)

P.S.: my vertex, normal and texture data is stored in separate arrays on the cpu side.

I would recommend you to analyze the problem narrowing down to the simplest case; to me it helped using only two squares (flat surface, 4 triangles) and try to correctly map the texture on both squares as shown in the image.

enter image description here

Using this simple case you have this data:

  • 6 vertices
  • 6 UVs
  • 6 normals (for each vertex, even though some vertex might share same normal)
  • 12 indices

In theory this will suffice to describe your whole data, but if you test it will find out that one of the mapped textures won't be fully mapped onto one of the squares. That's because for the vertex 2 and 3, you have two different UV coord sets, it's impossible to describe using so few vertices, you need more vertices!

To fix this problem, the data you gather from whatever exporter you chose, will duplicate vertices when clashes like this occur. For the example above, two more vertices will be needed, it will produce a duplication of data for the normals, 2 more elements to the indices but now it will describe correctly map the UV coords.

Finally, for your example those 4 vertices can't fully describe all UV coords, then duplicating some vertices will be needed to complete the UV layout, the indices array will grow.

For the part am not sure is if the OBJ format exporter actually throws the data ready to read, either you can try to figure it out with an exporter or you can try another format (which I better recommend you) that can export data that is already in the right format ready to be pushed into the graphics pipeline of DirectX11.

Hope the example that helped me understanding this problem, will help to you as well. Take a look to some basic DirectX11 tutorials, or if you can this book.

I have managed to get this example to work:

My problem is that I am trying to structure the code there into something more understandable...

My first step is to remove all the stuff that's not absolutely necessary to lanching a window with a direct x surface... but as soon as I start messing with the code, I get massively confused at which components are core, and which are not...

Do you know of a kind of boilerplate project with literally the utmost basics in it for a directx surface in a window.

D2D is fine unless you suggest I just code straight in D3D... even though i'm only going to be drawing 2d stuff.

I've read so many resources and tutorials for directX but every blooming one has different bits and bobs placed in and it's so difficult to just learn what the "must" bits are!...

Just to help you with the question.

I have started by trying to create a Gfx object that will start and create surfaces and resources for direct X, I also tried to separate my windows code but ofcourse got a bit lost there too... Finally I wrapped up my Game class so that the windows loop just ran begin draw, render and end draw. in the tutorial above you have 2 brushes which as i'm not familiar with directX, I'm unsure if I can just whip them out completely or what?

I guess a step in the right direction is really what i'm looking for.

I very strongly suggest that you get hold of a decent book that can guide you through the steps:

For example - try to get a copy of this

Is there a learning resource that goes over the ins and outs of game programming in C++? I'm looking for something thorough that details the industry standards, tricks, and common approaches rather than just a "build a game in 10 steps" type material - something like the C++ Primer for games. What would a good book or tutorial be?

The book that I used to learn and would recommend is Introduction to 3D Game Programming with DirectX 11

Pro OGRE 3D Programming (Expert's Voice in Open Source) was what we used at vizible (

I want to understand DXGI Desktop Duplication. I have read a lot and this is the code I copied from parts of the DesktopDuplication sample on the Microsoft Website. My plan is to get the Buffer or Array from the DesktopImage because I want to make a new Texture for an other program. I hope somebody can explain me what I can do to get it. Greetings Boke

void DesktopDublication::GetFrame(_Out_ FRAME_DATA* Data, _Out_ bool* Timeout)
    IDXGIResource* DesktopResource = nullptr;

    // Get new frame
    HRESULT hr = m_DeskDupl->AcquireNextFrame(500, &FrameInfo, &DesktopResource);
        *Timeout = true;

    *Timeout = false;

    if (FAILED(hr))


    // If still holding old frame, destroy it
    if (m_AcquiredDesktopImage)
        m_AcquiredDesktopImage = nullptr;

    // QI for IDXGIResource
    hr = DesktopResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void **>(&m_AcquiredDesktopImage));
    DesktopResource = nullptr;
    if (FAILED(hr))


    // Get metadata
    if (FrameInfo.TotalMetadataBufferSize)
        // Old buffer too small
        if (FrameInfo.TotalMetadataBufferSize > m_MetaDataSize)
            if (m_MetaDataBuffer)
                delete[] m_MetaDataBuffer;
                m_MetaDataBuffer = nullptr;
            m_MetaDataBuffer = new (std::nothrow) BYTE[FrameInfo.TotalMetadataBufferSize];
            if (!m_MetaDataBuffer)
                m_MetaDataSize = 0;
                Data->MoveCount = 0;
                Data->DirtyCount = 0;

            m_MetaDataSize = FrameInfo.TotalMetadataBufferSize;

        UINT BufSize = FrameInfo.TotalMetadataBufferSize;

        // Get move rectangles
        hr = m_DeskDupl->GetFrameMoveRects(BufSize, reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*>(m_MetaDataBuffer), &BufSize);
        if (FAILED(hr))
            Data->MoveCount = 0;
            Data->DirtyCount = 0;

        Data->MoveCount = BufSize / sizeof(DXGI_OUTDUPL_MOVE_RECT);

        BYTE* DirtyRects = m_MetaDataBuffer + BufSize;
        BufSize = FrameInfo.TotalMetadataBufferSize - BufSize;

        // Get dirty rectangles
        hr = m_DeskDupl->GetFrameDirtyRects(BufSize, reinterpret_cast<RECT*>(DirtyRects), &BufSize);
        if (FAILED(hr))
            Data->MoveCount = 0;
            Data->DirtyCount = 0;

        Data->DirtyCount = BufSize / sizeof(RECT);

        Data->MetaData = m_MetaDataBuffer;

    Data->Frame = m_AcquiredDesktopImage;
    Data->FrameInfo = FrameInfo;


If I'm understanding you correctly, you want to get the current desktop image, duplicate it into a private texture, and then render that private texture onto your window. I would start by reading up on Direct3D 11 and learning how to render a scene, as you will need D3D to do anything with the texture object you get from DXGI. This, this, and this can get you started on D3D11. I would also spend some time reading through the source of the sample you copied your code from, as it completely explains how to do this. Here is the link to the full source code for that sample.

To actually get the texture data and render it out, you need to do the following:

1). Create a D3D11 Device object and a Device Context.

2). Write and compile a Vertex and Pixel shader for the graphics card, then load them into your application.

3). Create an Input Layout object and set it to the device.

4). Initialize the required Blend, Depth-Stencil, and Rasterizer states for the device.

5). Create a Texture object and a Shader Resource View object.

6). Acquire the Desktop Duplication texture using the above code.

7). Use CopyResource to copy the data into your texture.

8). Render that texture to the screen.

This will capture all data displayed on one of the desktops to your texture. It does not do processing on the dirty rects of the desktop. It does not do processing on moved regions. This is bare bones 'capture the desktop and display it elsewhere' code.

If you want to get more in depth, read the linked resources and study the sample code, as the sample basically does what you're asking for.

LATER EDIT: Please mention not only game programming books, but also more scientific/simulation oriented books written for C++ programmers. As mentioned in one comment, this is why I chose to post the question on a more general SO site. Thank you all!

please, no specialized books presenting algorithms and techniques with C++ snippets - aka any Gems series, etc. Please expand on Scott Meyer's Effective C++ with similar, more complete or better book examples, or take into account that I mentioned a book for scientists and engineers.

Although similar questions have been asked, what I strictly need to know as a collection of opinions is which books are relevant for a person that has intermediate C++ skills, but wants to learn tips, tricks, good practices and polish up on their coding style to develop an edge in writing efficient number crunching applications (these applications should be dedicated to perform mathematical computations with customized structures, rendering/graphics and communicate with the user).

What I would like to see in an answer is a mention of a truly relevant fundamental C++ book, then 2 to 3 books (series also accepted - e.g. Scott Meyers' Effective C++) focused on intermediate to advanced programming tips, one of these 2-3 dealing exactly with tips and tricks for efficient computations within numerical scenarios/simulation/graphics/etc. I think there was a book called C++ for Scientists and Engineers, I'm not sure though how well this is rated as I haven't finished reading it :(. Perhaps a book that illustrates what design patterns are relevant for scientists/engineers/game programmers?

Thank you all. (hope this will help programmers that aren't keen on learning how to manipulate data just for the sake of making a living out of programming, as most average programmers tend to do nowadays).

I found C++ For Game Programmers (2nd Ed) to be a good intermediate-level C++ primer focused on performance-oriented C++.

Video Game Optimization provides good coverage on C++ optimization (at high and low level), profiling tools, and general approaches to optimization (both CPU and GPU).

Before you venture into a specialization, be sure you have a solid grasp of C++. With that being said, here are some books I would personally recommend.

Game Engine Architecture has become the book you want to read for game development. Having read the book from front to cover, I can say the author does a wonderful job of presenting a large amount of material.

Game Engine Gems 1 and Game Engine Gems 2 present excellent topics that for game development also.

If you are new to graphics programming, and you are on Windows, you may appreciate Introduction to 3D Game Programming with DirectX 11. I have read the previous two books in the "series" by Frank Luna and while I am still working on finishing this one, in my opinion they present the most correct material.