You are on page 1of 6

Lectures on DirectX 9.

0
3D Graphs and Game
Programming in C#

Delin Tan
Mathematics and Computer Science Department
University of Houston Victoria

Contents
Chapter 1 Mathematics Background
1.1 Point Coordinates ------------------------------------------------------------------------------- 1
1.2 Vector ---------------------------------------------------------------------------------------------- 2
1.3 Matrix --------------------------------------------------------------------------------------------- 4
1.4 Class CustomVertex ---------------- ---------------------------------------------------------- 7
1.5 Mathematics Functions ------------- ---------------------------------------------------------- 8
1.6 Quaternion ----------------------------- ---------------------------------------------------------- 9

Chapter 2 DirectX Start


2.1 Install Microsoft DirectX 9C SDK ---------------------------------------------------------- 10
2.2 Add DirectX References ---------------------------------------------------------------------- 11
2.3 Device Capability ------------------------------------------------------------------------------- 12
2.4 Create Device Interface Object ------------------------------------------------------------- 13
2.5 BackBuffer Drawing ---------------------------------------------------------------------------- 14
2.6 Animation Loop ---------------------------------------------------------------------------------- 15
2.7 The First DirectX 3D Program --------------------------------------------------------------- 15
2.8 Class VertexBuffer ----------------------------------------------------------------------------- 17
2.9 Draw Frame Rate ------------------------------------------------------------------------------ 19

Chapter 3 Some Simple Geometric Renderings


3.1 Virtual View Point ------------------------------------------------------------------------------- 21
3.2 Projective Plane -------------------------------------------------------------------------------- 21
3.3 World Coordinates ----------------------------------------------------------------------------- 22
3.4 Render State and DrawPrimitive Methods ----------------------------------------------- 23
3.5 Render Triangle Rotating Around Axis --- ------------------------------------------------ 24
3.6 Rotating Triangle with Two Side Colors -------------------------------------------------- 26
3.7 Rotating Triangle with More Controls ----------------------------------------------------- 26
3.8 Smooth Change between Different Rotations ----------------------------------------- 27
3.9 Triangle Rotation Around Other Line ------------------------------------------------------ 29
3.10 Render Circular Ring by TriangleStrip --------------------------------------------------- 30
3.11 Render Circular Disk by TriangleFan --------------------------------------------------- 31
3.12 Render Circular Cone by TriangleFan ------------ -------------------------------------- 32

Chapter 4 Cube Rendering


4.1 Draw Cube ---------------------------------------------------------------------------------------- 34
4.2 Z- Buffer ------------------------------------------------------------------------------------------- 36
II

4.3 IndexBuffer --------------------------------------------------------------------------------------- 37


4.4 User Defined System Memory ------------------------------------------------------------ 40
4.5 Use Mouse to Rotate Cube ----------------------------------------------------------------- 42
4.6 Anti-Aliasing ------------------------------------------------------------------------------------ 42
4.7 Draw Cylinder Lateral Surface --- -------------------------------------------------------- 43

Chapter 5 Surface VertexBuffer and IndexBuffer


5.1 Surface Partition ------------------------------------------------------------------------------- 45
5.2 Case for Rectangle ---------------------------------------------------------------------------- 46
5.3 Cylinder Surface ------------------------------------------------------------------------------ 47
5.4 Football Surface ---------------------- --------------------------------------------------------- 48
5.5 Sphere Surface -------------------------------------------------------------------------------- 49
5.6 Torus Surface ------------------------- --------------------------------------------------------- 50
5.7 Three Spheres System ---------------------------------------------------------------------- 51
5.8 Local Coordinates ------------------- --------------------------------------------------------- 54

Chapter 6 Texture Mapping


6.1 Texture Coordinates --------------------------------------------------------------------------- 58
6.2 Map Picture to Cube -------------------------------------------------------------------------- 58
6.3 Set Different Images for Cube Faces --------------------------------------------------- 61
6.4 Cylinder Texture ------------------------------------------------------------------------------ 62
6.5 Two Sides Image Card ------------- --------------------------------------------------------- 63
6.6 Map Rectangular Picture to Trapezoid Interior ---------------------------------------- 64
6.7 Map Rectangular Picture to Triangle Interior ------------------------------------------- 66
6.8 Map Rectangular Picture to Circle Interior ---------------------------------------------- 67
6.9 Map World Map to Inside of an Ellipse ------------------------------------------------- 68
6.10 Map World Map to a Sphere Surface --------------------------------------------------- 70
6.11 EImbedding Picture Data into Resource ----------------------------------------------- 72
6.12 Draw Image to BackBuffer----------------------------------------------------------------- 74

Chapter 7 Material and Lighting


7.1 Material and Lighting ---------------- --------------------------------------------------------- 77
7.2 Directional Lighting ------------------ --------------------------------------------------------- 78
7.3 Cylinder Lighting Example ------------------------------------------------------------------ 78
7.4 Card Lighting Example ---------------------------------------------------------------------- 81
7.5 Point Lighting ---------------------------------------------------------------------------------- 82

III

7.6 High Density Cube with Texture ----------------------------------------------------------- 83


7.7 Example: One Cube with Two Surrounding Point Lights---------------------------- 84
7.8 Spot Lighting ----------------------------------------------------------------------------------- 87
7.9 Example of Spot Light and Point Light---------------------------------------------------- 89

Chapter 8 Mesh and X File


8.1 Mesh Class -------------------------------------------------------------------------------------- 92
8.2 Mesh Object Creation ------------------------------------------------------------------------- 92
8.3 Mesh IndexBuffer Setup --------------------------------------------------------------------- 93
8.4 Save and Load Mesh Data -------- -------------------------------------------------------- 94
8.5 X Templates ----------------------------------------------------------------------------------- 95
8.6 Tiger.x ------------------------------------------------------------------------------------------- 96
8.7 Multiple Mesh Bodies X file ----------------------------------------------------------------- 98
8.8 Multiple Material X file ----------------------------------------------------------------------- 99
8.9 Use Mesh Attributes Table to draw mesh Subsets------------------------------------ 100
8.10 Create Cube.x with Texture Coordinates ----------------------------------------------- 101
8.11 Create Sphere.x with Texture Coordinates -------------------------------------------- 103
8.12 Create Torus.x with Texture Coordinates --------------------------------------------- 104
8.13 Progressive Mesh --------------------------------------------------------------------------- 105
8.14 Partition Mesh -------------------------------------------------------------------------------- 106
8.15 Patch Mesh ----------------------------------------------------------------------------------- 107
8.16 DirectX SDK built-in Meshes ------------------------------------------------------------- 106
8.17 Generate XFile From 3DS MAX----------------------------------------------------------- 111

Chapter 9 Texture Color Management


9.1 Set Texture Alpha Color ---------------------------------------------------------------------- 112
9.2 Texture Alpha Blending ---------------------------------------------------------------------- 114
9.3 Masking and Fading Images ---------------------------------------------------------------- 116
9.4 Texture Operations ---------------------------------------------------------------------------- 117
9.5 Create Fur Image Files ----------------------------------------------------------------------- 119
9.6 Create Fur Textures --------------------------------------------------------------------------- 121
9.7 Body Shade and Dot Product --------------------------------------------------------------- 123
9.8 Store Normal Vector Data in a Picture --------------------------------------------------- 125
9.9 Texture Dot3 Product Operation ----------------------------------------------------------- 126
9.10 Dot3 Product Bumping ------------ ---------------------------------------------------------- 128
IV

9.11 Render Image to Texture ------------------------------------------------------------------ 130


9.12 CubeTexture ---------------------------------------------------------------------------------- 131

Chapter 10 Sprite and Point System


10.1 Spirit Animation ------------------------------------------------------------------------------- 136
10.2 Sprite Manager ------------------------------------------------------------------------------- 138
10.3 Background Tiling --------------------------------------------------------------------------- 139
10.4 Single Frame Animation ---------- --------------------------------------------------------- 141
10.5 2D Points Color Effect ---------------------------------------------------------------------- 143
10.6 Point Spirit --------------------------------------------------------------------------------------146
10.7 Fountain Fireworks -------------------------------------------------------------------------- 148
10.8 Exploring Fireworks ------------------------------------------------------------------------- 151

Chapter 11 Mesh Data Management


11.1 Read and Write Data of Mesh ------------------------------------------------------------ 156
11.2 Teapot Shadow Example ------------------------------------------------------------------ 157
11.3 Weight Interpolation ------------------------------------------------------------------------- 159
11.4 3D Text Animation --------------------------------------------------------------------------- 161
11.5 Dolphin Animation by Interpolation ------------------------------------------------------ 162
11.6 Vertex Blending ------------------------------------------------------------------------------ 165
11.7 Vertex Tweening for Dolphin Animation ------------------------------------------------ 167
11.8 Intersecting with Mesh ------------ --------------------------------------------------------- 169
11.9 Convert MD2 Model to Meshes ---------------------------------------------------------- 171

Chapter 12 Assembly Shader Language


12.1 GPU and Shader ----------------------------------------------------------------------------- 177
12.2 ASM Pixel Variables and Some ASM Operations ----------------------------------- 177
12.3 ASM Pixel Shader Operations in Cube Rendering ---------------------------------- 179
12.4 Rotate Rectangle by ASM Vertex Shader --------------------------------------------- 161
12.5 Use ASM Shader to Calculate Specular Lighting ------------------------------------ 162
12.6 Use ASM Shader for Dolphin Animation------------------------------------------------ 188
12.7 Use ASM Shader for Bumping Torus --------------------------------------------------- 190
12.8 Get Tangent and Binormal Vectors from Texture Coordinates ------------------- 193
12.9 Mix Pixel Shader and Vertex Shade ----------------------------------------------------- 196

Chapter 13 High Level Shader Language (HLSL)


13.1 HLSL Basic ------------------------------------------------------------------------------------ 198
V

13.2 HLSL Data Type and Formations -------------------------------------------------------- 199


13.3 HLSL Code and Compiling ----------------------------------------------------------------- 201
13.4 Cube Texture HLSL Example ------------------------------------------------------------ 203
13.5 HLSL Animation Example ---------------------------------------------------------------- 207
13.6 Reflection Specular Lighting Example ------------------------------------------------- 210
13.7 Sobel Filter Example ----------------------------------------------------------------------- 212
13.8 Wood Texture Affect Example ----------------------------------------------------------- 214
13.9 Add Fur to Mesh Surface ----------------------------------------------------------------- 217
13.10 Metal Flakes Texture Affect HLSL Example ---------------------------------------- 220

Chapter 14 Shadow Volume and Shadow Mapping


14.1 Stencil Buffer Basics ------------------------------------------------------------------------ 226
14.2 Stencil Buffer Tests -------------------------------------------------------------------------- 226
14.3 Shadow and Shadow Volume ------------------------------------------------------------- 228
14.4 Shadow Volume Creation Class -------------------------------------------------------- 229
14.5 Create Shadow Volume and Draw Shadow------------------------------------------- 230
14.6 Moving Object Shadow -------------------------------------------------------------------- 232
14.7 Shadow Mapping Principle --------------------------------------------------------------- 234
14.8 Shadow Mapping Example --------------------------------------------------------------- 235

Chapter 15 Terrain Creation


15.1 Terrain Height Map -------------------------------------------------------------------------- 239
15.2 Make Vertices and Normals --------------------------------------------------------------- 240
15.3 Texture Blending ----------------------------------------------------------------------------- 241
15.4 Make Terrain VertexBuffer and IndexBuffer ----------------------------------------- 242
15.5 Setting for HLSL ---------------------------------------------------------------------------- 243
15.6 HLSL Code ------------------------------------------------------------------------------------ 244
15.7 Add Sky Box ---------------------------------------------------------------------------------- 245
15.8 Use VolumeTexture to Mix Images ---------------------------------------------------- 246

Chapter 16 Bone and Skinning Mesh Animation


16.1 Bone Animation Example ------------------------------------------------------------------ 248
16.2 Skinning Mesh File Structure ------------------------------------------------------------- 251
16.3 Skinning Mesh Data Loading ------------------------------------------------------------- 254
16.4 Skinning Mesh Animation Rendering --------------------------------------------------- 257
16.5 Multi Animation Skinning Mesh -------------------------------------------------------- 259
VI

You might also like