Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach

This page contains some general information about the book Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach, published by Jones and Bartlett Learning, that will help you decide if this book if for you.

Supplement Files:
Demo Project Setup in Visual Studio 2005
Detailed Table of Contents
Source Code Part I
Source Code Part II
Source Code Part III
Source Code Appendix A
Part I Solutions

Quick Navigation


This book presents an introduction to programming interactive computer graphics, with an emphasis on game development, using real-time shaders with DirectX 9.0. It teaches the fundamentals of Direct3D and shader programming, after which the reader will be prepared to go on and learn more advanced techniques. The book is divided into three main parts. Part I explains the mathematical tools that will be used throughout this book. Part II shows how to implement, using shaders and the HLSL, elementary 3D techniques, such as defining 3D geometry, lighting, texturing, alpha blending, and stenciling. Part III is largely about applying Direct3D to implement a variety of interesting techniques and special effects, such as working with meshes, character animation, terrain rendering, picking, particle systems, environment mapping, normal mapping, and rendering to textures.

Changes from the First Edition

This second edition has been rewritten almost from scratch. This new edition largely abandons the fixed pipeline used in the first edition to give a complete programmable, or shader, approach. From Chapter 7 onwards, we are transforming, lighting, and coloring vertices and pixels in vertex and pixel shaders. This significant revision in approach reflects the fact that the fixed pipeline will not exist in DirectX 10.

Another key feature of the second edition is the inclusion of end of chapter exercises. The exercises serve three purposes: First, to test the reader's understanding of each chapter's contents; second, to provide the reader with Direct3D programming experience; and third, to extend the chapter's topics by asking the reader to investigate new ideas on his/her own. In many cases, the exercises are not of the "plug and chug" type, but require the reader to reason about how to apply the chapter's topics to the particular assignment. Readers are encouraged to complete chapter exercises before moving on to the next chapter.

Intended Audience

This book was designed with the following three audiences in mind:

  • Intermediate level C++ programmers who would like an introduction to 3D programming using the latest iteration of Direct3D
  • 3D programmers experienced with an API other than DirectX (e.g., OpenGL) who would like an introduction to Direct3D 9.0.
  • Experienced fixed function Direct3D programmers who like a Direct3D book taking a shader approach.


It should be emphasized that this is an introduction to Direct3D, shader, and game programming; it is not an introduction to general computer programming. The reader should satisfy the following prerequisites:

  • High School mathematics: algebra, trigonometry, and (mathematical) functions, for example.
  • Competent with Visual Studio: should know how to create projects, add files, and specify external libraries to link, for example.
  • Intermediate C++ and data structure skills: comfortable with pointers, arrays, operator overloading, linked lists, inheritance and polymorphism, for example.
  • Familiarity with Windows programming with the Win32 API is helpful, but not required; we provide a Win32 primer in Appendix A.

Required Development Tools and Recommended Hardware

To program Direct3D 9 applications, you will need the DirectX 9 SDK; the latest version can be downloaded here. Once downloaded, follow the instructions given by the installation wizard. As of the March 2008 DirectX SDK release, the SDK will only officially support versions of Visual Studio 2005 and Visual Studio 2008. This book's demo programs require DirectX 9.0 capable hardware that supports vertex and pixel shader versions 2.0. At the time of this book, low-end video cards supporting these requirements are available for about $50 (e.g., Radeon 9600, Geforce FX 5500).

Brief Table of Contents

  • Introduction
  • Part I Mathematical Prerequisites
    • Chapter 1 Vector Algebra
    • Chapter 2 Matrix Algebra
    • Chapter 3 Transformations; Planes
  • Part II Direct3D Foundations
    • Chapter 4 Direct3D Initialization
    • Chapter 5 Timing; Direct Input; Animation and Sprites
    • Chapter 6 The Rendering Pipeline
    • Chapter 7 Drawing in Direct3D
    • Chapter 8 Color
    • Chapter 9 Lighting
    • Chapter 10 Texturing
    • Chapter 11 Blending
    • Chapter 12 Stenciling
  • Part III Applied Direct3D and the D3DX Library
    • Chapter 13 Meshes
    • Chapter 14 Mesh Hierarchy Animation Part I: Rigid Meshes
    • Chapter 15 Mesh Hierarchy Animation Part II: Skinned Meshes
    • Chapter 16 Terrain Rendering Part I
    • Chapter 17 Terrain Rendering Part II
    • Chapter 18 Particle Systems
    • Chapter 19 Picking
    • Chapter 20 Advanced Texturing Part I
    • Chapter 21 Advanced Texturing Part II
  • Appendix A Introduction to Windows Programming
  • Appendix B High Level Shader Language Reference

Chapter Descriptions

Chapter 1 Vector Algebra: Vectors are, perhaps, the most fundamental mathematical objects used in computer games. We use vectors to represent positions, displacements, directions, velocities, and forces, for example. In this chapter, we study vectors and the operations used to manipulate them.
Chapter 2 Matrix Algebra: Matrices provide an efficient and compact way of representing transformations. In this chapter, we become familiar with matrices and the operations defined on them.
Chapter 3 Transformations; Planes: This chapter examines three fundamental geometric transformations: scaling, rotation, and translation. We use these transformations to manipulate 3D objects in space. In addition, a brief primer on 3D planes is provided; planes are useful for dividing and sorting 3D space.
Chapter 4 Direct3D Initialization: In this chapter, we learn what Direct3D is about and how to initialize it in preparation for 3D drawing. Basic Direct3D topics are also introduced, such as surfaces, pixel formats, double buffering, and multisampling. In addition, we show how to output 2D text, and give some tips on debugging Direct3D applications. We develop and use our own application framework--not the SDK's framework.
Chapter 5 Timing; Direct Input; Sprites and Animation: This chapter is a warm up before diving into 3D programming. We learn how to measure time with the performance counter, which we use to compute the frames rendered per second. In addition, we learn how to initialize and use Direct Input for keyboard and mouse input. Finally, we give an introduction to 2D graphics programming with the ID3DXSprite interface to give a flavor for graphics programming.
Chapter 6 The Rendering Pipeline: The first theme of this chapter is to learn, mathematically, how to describe a 3D world and represent a virtual camera that describes the volume of space the camera "sees." The second theme is to learn the steps necessary to take a 2D "picture" that corresponds to the 3D space the camera sees; these steps as a whole are referred to as the rendering pipeline.
Chapter 7 Drawing in Direct3D: This chapter shows how to draw 3D geometry in Direct3D. We learn how to store geometric data in a form usable by Direct3D, and we learn the Direct3D drawing commands. In addition, we also write our first simple vertex and pixel shaders using the HLSL (high level shading language) and effects framework (i.e., the ID3DXEffect interface). We also show how to configure the way in which Direct3D draws geometry using render states. We conclude by examining the D3DX functions used to create simple geometric meshes like cylinders, spheres, boxes, and teapots.
Chapter 8 Color: In this chapter, we learn how color is represented in Direct3D and how to apply color to solid 3D geometric primitives. We also show how we can animate the position and color of vertices completely in a vertex shader with the colored wave demo. Finally, we describe two render states that control how colors, specified per vertex, can be shaded across a primitive.
Chapter 9 Lighting: In this chapter, we learn how to create light sources and define the interaction between light and surfaces. In particular, we show how to implement directional lights, point lights, and spotlights with vertex and pixel shaders, which mimic the traditional lighting fixed function pipeline lighting model. We also show how to do per pixel lighting with interpolated per pixel normal vectors (i.e., Phong shading).
Chapter 10 Texturing: This chapter describes texture mapping, which is a technique used to increase the realism of the scene by mapping 2D image data onto a 3D primitive. For example, using texture mapping, we can model a brick wall by applying a 2D brick wall image onto a 3D rectangle. Other key texturing topics covered include multi-texturing, and animated texture transformations. As always, all of our work is done in the programmable pipeline with vertex and pixel shaders.
Chapter 11 Blending: In this chapter, we look at a technique called blending, which allows us to implement a number of special effects like transparency. In addition, we discuss the alpha test, which enables us to mask out certain parts of an image from showing up; this can be used to implement gates, for example. An exercise gets you started on implementing y-axis aligned billboards, which, when combined with the alpha test, enable us to simulate trees.
Chapter 12 Stenciling: This chapter describes the stencil buffer, which, like a stencil, allows us to block pixels from being drawn. To illustrate the ideas of this chapter, we include a thorough discussion on implementing reflections and planar shadows using the stencil buffer. An exercise describes an algorithm for using the stencil buffer to render the depth complexity of a scene, and asks you to implement the algorithm.
Chapter 13 Meshes: This chapter shows how to work with .X files and the ID3DXMesh interface. In addition, many of the D3DX mesh functions are surveyed. We also show how to compute the bounding box and sphere of a mesh.
Chapter 14 Mesh Hierarchy Animation Part I: In this chapter, we learn the relationship between a child mesh and its parent mesh, and how to model this hierarchy relationship in code. To demonstrate the ideas, we create and animate some simple mesh hierarchies, namely a robot arm and solar system.
Chapter 15 Mesh Hierarchy Animation Part II: With the theory of mesh hierarchies covered in the previous chapter, we focus now on implementing the vertex blending algorithm to model elastic skin. We demonstrate this technique by loading the animation and skinning data from a .X file, and then animating it with vertex blending. We use the D3DX animation interfaces.
Chapter 16 Terrain Rendering Part I: This chapter shows how to create, texture, light, and render 3D terrains using heightmaps and a multi-texturing technique. Furthermore, we show how to smoothly "walk" the camera over the terrain. We also learn how to divide the terrain mesh into sub-grid chunks in preparation for frustum culling.
Chapter 17 Terrain Rendering Part II: In this chapter, we show how to implement frustum culling. In addition, we embellish our terrain scene by adding trees, a castle, fog, and animated grass. As always, all of our work is done in the programmable pipeline with vertex and pixel shaders.
Chapter 18 Particle Systems: In this chapter, we learn how to model systems that consist of many small particles that all behave in a similar manner. For example, particle systems can be used to model falling snow and rain, fire and smoke, rocket trails and particle guns, and sprinklers and fountains.
Chapter 19 Picking: This chapter shows how to determine the particular 3D object (or 3D primitive) that the user has selected with the mouse. Picking is often a necessity in 3D games and applications where the user interacts with the 3D world with the mouse.
Chapter 20, Advanced Texturing Part I: In this chapter, we show how to reflect environments onto arbitrary meshes with environment mapping; in addition, we use an environment map to texture a sky-sphere. We also show how to get detailed real-time lighting using normal maps, which can also be used to simulate water waves. Finally, we implement a radar map by rendering the scene to a texture from a bird's eye view directly above the camera.
Chapter 21, Advanced Texturing Part II: We conclude the book by examining some more advanced texture methods. In particular, we discuss projective texturing, shadow mapping, and displacement mapping. Projective texturing is a technique that enables us to simulate a slide projector and project a texture onto arbitrary geometry within the projector’s frustum. Shadow mapping is a real-time shadowing technique, which shadows arbitrary geometry (it is not limited to planar shadows). Finally, displacement mapping is a technique where each vertex has an associated displacement vector, which specifies how a vertex should be displaced in a vertex shader.