Tutorial 1: Create a Window

This tutorial is the first one in a series about using Direct3D. Although we want to create a fullscreen Direct3D application a window is still necessary. Direct3D relies on this window to communicate with the Windows operating system and therefore we have to start with that. The window is created using the Win32 API, which provides the methods to create and handle it. Short explanations about the used Winapi functions will be provided in this tutorial. For further information visit msdn.microsoft.com or take a look at other resources concerning this topic.

Online Tutorial

Tutorial 2: Init Direct3D

Till now we created a window, which handles the messages from Windows and started programming the application class. In this tutorial we finish the application class. We will init Direct3D with the appropriate parameters. There are two ways of doing this. The easy and the hard way. In this tutorial we are concentrating on the easy one. Usually we have to enumerate all adapters, devices and modes, which are available on the system, but here we start from knowing the capabilities of the system. We use the default adapter, so when you have two or more graphic cards in your system the standard will be used. We will set the screen resolution, the color bits, the depth buffer bits and the stencil buffer bits.

Online Tutorial

Tutorial 3: Draw Polygons

In this tutorial we will draw the first polygons. There will be two objects, a triangle with three different and a quad with one color. Before we can do this we set the needed render states and create some more requirements for drawing. We must create our own specific vertex format and structure with the needed variables included. Then we will draw the polygons with the help of vertex buffers.

Online Tutorial

Tutorial 4: Solid Objects, Rotation, Translation

The contents of this tutorial are solid objects and the rotation and translation of them. We do these operations using the Direct3DX utility library. Solid objects are nothing more than many single polygons, which lie next to each other in 3D space. We will build a pyramide out of four triangles and one quad for the basis. Then we rotate it and translate it into the screen so that we can see it. Using the Direct3DX utility library for that makes it easy. For each rotation and translation we need one matrix. We create these matrices with the Direct3DX functions. Then we have to multiply all matrices together and send it to the device. That's all what we do in this tutorial. We need no more files or functions.

Online Tutorial

Tutorial 5: Texture Mapping

Now where we have done all the basic stuff, like the initializationof Direct3D and creation simple objects, we can make something new. In this tutorial we will start using texture mapping. Textures are a simple 3D effect, but have an enormous result and are very easy to use. The first thing we have to do is to change the custom vertex format. Then we only need to load the texture with help of the Direct3DX utility library. Two important things are the texture coordinates, which we need to establish how the texture is displayed on the triangle, and the filters, which smooth the texture. The filters are already set in the application class. You can choose the filter type in the settings dialog.

Online Tutorial

Tutorial 6: Lighting, Materials

In this tutorial we will handle one of the most important topics in 3D graphics programming. Lighting helps us creating realistic scenes or different atmospheres in a game. To use lighting we have to create a light source, of course. In the fixed-function pipeline DirectX supports simultaneous light sources, but they are rarely needed. The number of supported active lights is specified in the D3DCAPS9 structure. The light source for this tutorial is a directional light, which means that the direction of the light is equal for the whole scene. To use lighting we need to know in which direction the triangles of our scene face. This information is stored in the normal, which is perpendicular to the triangle. The light intensity for each vertex is the dot product between the light vector and the normal vector. The material for the object is either given by the diffuse color value or a material structure, which we will use here.

Online Tutorial

Tutorial 7: Vertex Alpha

This tutorial covers a nice effect. We want to make the polygons of the pyramide transparent with alpha blending. We will use the alpha value of the vertex color. To do this we have to add a diffuse color value to the vertex structure again. Then we set the alpha value to 50%, so that the texture will be half transparent. Before we can use alpha blending we have to change the render states. We enable alpha blending and deactivate the depth buffer, because the hidden surfaces are now also visible and they wont be drawn with an activated depth buffer.

Online Tutorial

Tutorial 8: Index Buffer

This tutorial covers an easy way to optimize a program or make it faster. Index buffers are used to reduce the number of vertices needed to draw an object. You only need each vertex one time. In the index buffer is stored, which vertices of a vertex buffer are used to draw a triangle. So there are always three indices that describe one triangle. The result is, that there must be stored only a small part of the vertices, which were needed without this buffer. Now you have to store three times the indices of the vertices you want to use, but that is not so much memory, because the indices are only short values.

Online Tutorial

Tutorial 9: Text with D3DXFont

The topic of this tutorial is something that is needed in nearly every program. Fonts are used for menus, stats, briefings, etc. The Direct3DX library includes a class, which makes it possible for us to use the Windows true type fonts, but also others. The class is called D3DXFont and is very easy to use. The only thing we have to do is to create the font object and then draw the text. For that we fill a so called LOGFONT structure with the information about name, color, size, etc. of the font. The alignment of the text is stored in a RECT structure. I don't create a new class for this, because it is not much and you better add it to classes where you need it. We only add some code to the main.cpp.

Online Tutorial

Tutorial 10: Color Key Transparency

In the seventh tutorial we already had the topic of alpha blending with the alpha value of the vertex color. This time we will cover a new way of using transparency. We will load a texture, which shows a leaf. As you can see there is a black border around the leaf, but we don't want the black area to be displayed. Here we come to the color key. The color key defines one specific color, in this case black, which shouldn't be drawn to the render surface. The Direct3DX utility library includes a function, which can be used for that. On the image below you can see the leaf.

Online Tutorial

Tutorial 11: Alpha Channel Transparency

This is another nice tutorial about alpha blending. It covers alpha blending with the alpha channel of a texture loaded from a bitmap. We just load a texture, which includes an alpha channel, after we have enabled alpha blending. We use the same texture as in the last tutorial, but this time we have the alpha channel included. On the image below you can see the alpha channel of this texture.

Online Tutorial

Tutorial 12: Single Pass Multitexturing

With DirectX 6.1 Microsoft introduced multitexturing, also known as texture blending, to his API. Direct3D supports the blending of up to eight textures on a primitive in a single pass through the use of texture stages. You can apply one texture to all of these stages. Each texture stage takes two arguments and performs a blending operation between them, both defined by the application. There are several texture arguments and texture operation flags usable. For a complete list please take a look in the SDK documentation. With the help of these arguments you can access results of a previous texture stage, which is used to perform multitexturing. All texture operations are possible for color and alpha values.

Online Tutorial

Tutorial 13: Multi Pass Multitexturing

There are often people, who have graphic cards that don't support single pass multitexturing or want to use more than the supported textures. These people can use a technic called multi pass multitexturing. In this tutorial we try to replicate the result of the last tutorial with multi pass multitexturing. Multi pass multitexturing is done as follows. We set a texture in the first texture stage and define all arguments needed for rendering. Then we simply render the primitive. Now we set the second texture in the first texture stage and define color or alpha blending render states to adjust the blending factors as needed. Now we render the same primitive again with the new texture and blending factors. The system now blends the already rendered colors and the new colors of the second texture according to the blending factors.

Online Tutorial

Tutorial 14: Dot3 Bump Mapping

In the last tutorials with lighting we always used it per-vertex with the help of the normal stored in each vertex. This leads us to a problem when the light comes close to the triangle, which is lit. Though the triangle should appear bright it is not, because the angle between the normal and the light vector is high. That is where dot3 bump mapping, also known as per-pixel lighting, is used to solve this problem. But that is not the only reason to use dot3 bump mapping. It also allows you to add high detail to surfaces or objects without increasing the number of polygons.

Online Tutorial

Tutorial 15: Vertex Fog

This tutorial covers an easy way of creating fog in Direct3D. Sometimes it is also used to hide distant objects needed to achieve high framerates in realtime applications. Direct3D consists of two types of fog called vertex and pixel fog. Vertex fog is faster, as it is only computed per vertex and then interpolated over the triangle. We simply can activate vertex fog through the device's render states. There are several parameters that can be set such as color, start and end distance and the fog formula.

Online Tutorial

Tutorial 16: Render-To-Texture

Rendering to a texture is one of the advanced techniques in Direct3D. On the one hand it is simple, on the other hand it is powerful and enables numerous special effects. Examples are the glow effect, first introduced in Tron 2.0, environment mapping, shadow mapping, and many more. Rendering to a texture is simply an extension to rendering to a surface. There are just some additions needed. First the texture itself has to be created. Furthermore some other precautions have to be made. The second step is to render the appropriate scene to the texture. Finally the texture is used to render the final scene.

Online Tutorial

Tutorial 17: Point Sprites

With point sprites Direct3D offers an often underestimated feature. Though most people share the opinion that point sprites are too unflexible, especially when used for particle systems, they have advantages in memory usage and thus performance compared to usual approaches. This tutorial will just cover the basics of point sprites, but it's still a good base to work with. The name by itself already suggests some of the characteristics. All in all a point sprite is simply a screen-aligned element of variable size that is defined by a single point. This already leads to the advantages of point sprites since all you need is a single vertex for each point sprite instead of four for a textured quad. Using only one vertex, whose overall size is less than the size of usual textured verices, means the among of memory that needs to be transfered over the AGP bus decreases tremendously.

Online Tutorial

Tutorial 18: Simple Vertex and Pixel Shaders

This tutorial will explain the basic implementation of Vertex and Pixel Shaders without using the ID3DXEffect interface. In this example, we will draw a quad with a texture applied to it and we will add a simple sepia effect.

Online Tutorial