DirectX9.0游戏开发编程基础pdf (高清可复制内容版)
希望对大家有所帮助 《DirectX 9 3D游戏设计入门》 .............................................................................................. 1 第一部分 必备的数学知识 (Mathematical Prerequisites) .......................................................... 10 目标 ........................................................................................................................................................ 10 三维空间中的向量 ................................................................................................................................. 11 向 量相等 ................................................................................................................................................ 13 计算向量大小(向量的模) ................................................................................................................. 14 标准化向量 ............................................................................................................................................ 14 向量相加 ................................................................................................................................................ 15 向量相减 ................................................................................................................................................ 16 标量与向量的乘积 ................................................................................................................................ 16 点积 ........................................................................................................................................................ 17 叉积 ........................................................................................................................................................ 17 矩阵 ........................................................................................................................................................ 18 相等、数乘矩阵以及相加 ..................................................................................................................... 19 乘法 ........................................................................................................................................................ 19 单位矩阵 ................................................................................................................................................ 20 逆转 ........................................................................................................................................................ 21 矩阵的转置 ............................................................................................................................................ 21 D3DX 矩阵 ........................................................................................................................................... 22 基本变换 ................................................................................................................................................ 24 矩阵平移 ................................................................................................................................................ 25 矩阵旋转 ................................................................................................................................................ 26 矩阵缩放 ................................................................................................................................................ 28 综合变换 ................................................................................................................................................ 29 一些向量变换函数 ................................................................................................................................ 30 平面 ........................................................................................................................................................ 31 D3DX平面 ............................................................................................................................................ 31 点和平面的空间关系 ............................................................................................................................. 32 创建平面 ................................................................................................................................................ 32 标准化平面 ............................................................................................................................................ 33 变换平面 ................................................................................................................................................ 34 点到平面上最近的点 ............................................................................................................................. 34 射线(可选的) .................................................................................................................................... 35 射线 ........................................................................................................................................................ 35 线/面相交 ............................................................................................................................................... 35 摘要(略) ............................................................................................................................................ 36 第二部分 Direct3D基础 (Direct3D Fundamentals) .................................................................. 37 第一章 初始化Direct3D (Direct3D Initialization) .................................................... 37 目标 ........................................................................................................................................................ 37 1.1 Direct3D概述 .................................................................................................................................. 37 1.1.1 REF设备 ............................................................................................................................... 38 1.1.2 D3DDEVTYPE ...................................................................................................................... 38 1.2 COM ................................................................................................................................................. 38 1.3 一些准备工作 ................................................................................................................................. 38 1.3.1 表面 ...................................................................................................................................... 39 1.3.2 Multisampling ........................................................................................................................ 40 1.3.3像素格式 ............................................................................................................................... 41 1.3.4 内存池 .................................................................................................................................. 41 1.3.5 交换链和页面切换 ............................................................................................................... 41 1.3.6 深度缓冲 ............................................................................................................................... 42 1.3.7 顶点处理 ............................................................................................................................... 43 1.3.8 设备能力 ............................................................................................................................... 43 1.4 初始化Direct3D ............................................................................................................................. 44 1.4.1获得IDirect3D9接口 ............................................................................................................ 44 1.4.2 检测硬件顶点处理 ............................................................................................................... 45 1.4.3 填充D3DPRESENT_PARAMETERS结构 ........................................................................ 46 1.4.4 创建IDirect3DDevice9对象................................................................................................ 47 1.5 初始化Direct3D实例 ..................................................................................................................... 48 1.5.1 d3dUtility.h/cpp ...................................................................................................................... 49 1.5.2 实例框架 ............................................................................................................................... 50 1.5.3 D3D Init实例 ........................................................................................................................ 51 1. 6摘要(略) ................................................................................................................................. 53 第二章 渲染管线 (The Rendering Pipeline) .......................................................... 54 目标 ........................................................................................................................................................ 54 2.1表现模型 .......................................................................................................................................... 54 2.1.1 顶点格式 ............................................................................................................................... 55 2.1.2 三角形 .................................................................................................................................. 56 2.1.3 索引 ...................................................................................................................................... 57 2.2虚拟照相机 ...................................................................................................................................... 57 2.3 渲染管线 ......................................................................................................................................... 58 2.3.1自身坐标系(Local Space) ................................................................................................ 59 2.3.2世界坐标系(World Space) ................................................................................................ 59 2.3.3视图坐标系(View Space) ................................................................................................. 60 2.3.4背面拣选(Backface Culling) ............................................................................................ 61 2.3.5光源(Lighting) .................................................................................................................. 62 2.3.6裁剪(Clipping) .................................................................................................................. 63 2.3.7投影(Projection)................................................................................................................ 63 2.3.8视口变换(Viewport Transform) ........................................................................................ 65 2.3.9光栅化(Rasterization) ....................................................................................................... 66 2.4 摘要(略) .......................................................................................................................................... 66 第三章 在Direct3D中绘制 (Drawing in Direct3D) .................................................. 67 目标 ........................................................................................................................................................ 67 3.1顶点/索引缓存 ................................................................................................................................. 67 3.1.1创建一个顶点和索引缓存 .................................................................................................... 67 3.1.2 访问缓冲内存 ....................................................................................................................... 69 3.1.3 找回顶点和索引缓存信息 ................................................................................................... 70 3.2 渲染状态 ......................................................................................................................................... 70 3.3 绘制准备 ......................................................................................................................................... 71 3.4用顶点/索引缓存绘制 ...................................................................................................................... 71 3.4.1 IDirect3DDevice9::DrawPrimitive ......................................................................................... 72 3.4.2 IDirect3DDevice9::DrawIndexedPrimitive ............................................................................ 72 3.4.3 开始/结束场景 ...................................................................................................................... 73 3.5 D3DX几何物体 ............................................................................................................................... 73 3.6 实例程序:三角形、立方体、茶壶、D3DXCreate* ................................................................... 75 3.7 摘要(略) ..................................................................................................................................... 79 第四章 色彩 (Color) ....................................................................................................... 80 目标 ........................................................................................................................................................ 80 4.1 颜色表示法 ............................................................................................................................. 80 4.2 顶点颜色 ......................................................................................................................................... 82 4.3 着色处理 ......................................................................................................................................... 83 4.4 实例程序:彩色三角形 .................................................................................................................. 83 4.5 摘要(略) .......................................................................................................................................... 85 第五章 灯光 (Lighting) ................................................................................................. 86 目标 ........................................................................................................................................................ 86 5.1灯光的组成 ...................................................................................................................................... 86 5.2材质 .................................................................................................................................................. 87 5.3顶点法线 .......................................................................................................................................... 88 5.4光源 .................................................................................................................................................. 90 5.5实例程序:灯光 ............................................................................................................................... 93 5.6附加实例 .......................................................................................................................................... 95 5.7摘要(略) ........................................................................................................................................... 96 第六章 纹理 (Texturing) .............................................................................................. 97 目标 ........................................................................................................................................................ 97 6.1 纹理坐标 ......................................................................................................................................... 97 6.2创建并赋予材质 ............................................................................................................................... 99 6.3过滤器 .............................................................................................................................................. 99 6.4 Mipmaps ..........................................................................................................................................100 6.4.1 Mipmaps过滤器 ...................................................................................................................101 6.4.2 Direct3D中使用Mipmaps ...................................................................................................101 6.5 寻址模式 ........................................................................................................................................101 6.6实例程序:有纹理的方块 ..............................................................................................................103 5. 7摘要(略) .....................................................................................................................................105 第七章 混合 (Blending) .............................................................................................. 106 目标 .......................................................................................................................................................106 7.1混合因素 .........................................................................................................................................106 7.2混合要素 .........................................................................................................................................108 7.3透明度 .............................................................................................................................................108 7.3.1Alpha通道 .............................................................................................................................108 7.3.2指定Alpha资源 ...................................................................................................................109 7.4使用DirectX纹理工具创建Alpha通道 .......................................................................................109 7.5实例程序:透明度 .......................................................................................................................... 111 7.6摘要(略) .......................................................................................................................................... 113 第八章 模版 (Stenciling) ........................................................................................... 114 目标 ....................................................................................................................................................... 115 8.1使用模版缓存 ................................................................................................................................. 115 8.1.1请求一个模版缓存 ............................................................................................................... 115 8.1.2模版测试 ............................................................................................................................... 115 8.1.3控制模版测试 ....................................................................................................................... 116 8.1.3.1模版参考值(Reference Value) ..................................................................................... 116 8.1.3.2模版掩码 ............................................................................................................................ 116 8.1.3.3模版值(Stencil Value) .................................................................................................. 116 8.1.3.4比较运算 ............................................................................................................................ 116 8.1.3更新模版缓存 ....................................................................................................................... 117 8.1.4模版写掩码 ........................................................................................................................... 118 8.2实例程序:镜子 .............................................................................................................................. 118 8.2.1反射数学 ............................................................................................................................... 118 8.2.2镜面实现流程 .......................................................................................................................120 8.2.3代码和解释 ...........................................................................................................................121 8.2.3.1第一部分 ............................................................................................................................121 8.2.3.2第二部分 ............................................................................................................................122 8.2.3.3第三部分 ............................................................................................................................122 8.2.3.4第四部分 ............................................................................................................................123 8.2.3.5第五部分 ............................................................................................................................123 8.3实例程序:平面阴影 ......................................................................................................................124 8.3.1平行光阴影 ...........................................................................................................................125 8.3.2点光源阴影 ...........................................................................................................................126 8.3.3阴影矩阵 ...............................................................................................................................126 8.3.4用模版缓存防止双倍混合 ...................................................................................................127 8.3.5代码和解释 ...........................................................................................................................128 8.4摘要(略) ..........................................................................................................................................129 第三部分 实用的Direct3D (Applied Direct3D) ....................................................................... 130 第九章 字体 (Fonts) ..................................................................................................... 130 目标 .......................................................................................................................................................130 9.1 ID3DXFont......................................................................................................................................130 9.1.1创建一个ID3DXFont ...........................................................................................................130 9.1.2绘制文本 ...............................................................................................................................131 9.1.3计算每秒的渲染帧数 ...........................................................................................................131 9.2 CD3DFont .......................................................................................................................................132 9.2.1创建一个CD3DFont ............................................................................................................132 9.2.2绘制文本 ...............................................................................................................................133 9.2.3 清除 .....................................................................................................................................133 9.3 D3DXCreateText ..............................................................................................................................133 9.4摘要(略) ..........................................................................................................................................135 第十章 网格模型I (Meshes Part I) ........................................................................... 136 目标 .......................................................................................................................................................136 10.1 几何信息 ......................................................................................................................................136 10.2 子集和属性缓存 ...........................................................................................................................137 10.3 绘制 ..............................................................................................................................................138 10.4 优化 ..............................................................................................................................................138 10.5 属性表 ..........................................................................................................................................140 10.6 邻接信息 ......................................................................................................................................141 10.7 复制 ..............................................................................................................................................142 10.8 创建一个Mesh(D3DXCreateMeshFVF) ................................................................................143 10.9 实例程序:创建和渲染Mesh .....................................................................................................144 10.10 摘要(略) .....................................................................................................................................148 第十一章 网格模型II (Building a Flexible Camera Class) .............................. 149 目标 .......................................................................................................................................................149 11.1 ID3DXBuffer .................................................................................................................................149 11.2 X文件 ...........................................................................................................................................150 11.2.1读取X文件 ........................................................................................................................150 11.2.2 X文件的材质 .....................................................................................................................151 11.2.3 实例程序:X文件 ............................................................................................................151 11.2.4 产生顶点法线 ....................................................................................................................154 11.3渐进网格(Progressive Meshes) ................................................................................................155 11.3.1 产生一个渐进网格 ............................................................................................................156 11.3.2 顶点品质权重 ....................................................................................................................156 11.3.3 ID3DXPMesh方法 .............................................................................................................157 11.3.4实例程序:渐进网格 .........................................................................................................158 11.4 界线容积(Bounding Volumes) .................................................................................................160 11.4.1一些新的特殊常量 .............................................................................................................162 11.4.2界线容积类型 .....................................................................................................................162 11.4.3实例程序:界线容积 .........................................................................................................163 11.5 摘要(略) .......................................................................................................................................165 第十二章 创建灵活的摄像机类 (Building a Flexible Camera Class) ................ 166 目标 .......................................................................................................................................................166 12.1 摄像机设计 ..................................................................................................................................166 12.2 执行详细资料 ...............................................................................................................................168 12.2.1计算视图矩阵 .....................................................................................................................168 12.2.1.1 第一部分:平移 .............................................................................................................168 12.2.1.2 第二部分:旋转 .............................................................................................................169 12.2.1.3 将两部分合并 .................................................................................................................169 12.2.2围绕任意轴旋转 .................................................................................................................170 12.2.3 Pitch、Yaw和Roll ............................................................................................................171 12.2.4 Walking、Strafing和Flying ..............................................................................................173 12.3实例程序:摄像机 ........................................................................................................................174 12.4 摘要 ..............................................................................................................................................176 第十三章 地形渲染基础 (Basic Terrain Rendering) ............................................. 177 目标 .......................................................................................................................................................177 13.1 Heightmaps(高度图) .................................................................................................................177 13.1.1 创建高度图(Heightmap) ..............................................................................................178 13.1.2 读取RAW文件 .................................................................................................................179 13.1.3 访问与修改Heightmap .....................................................................................................180 13.2 生成地形几何数据 .......................................................................................................................180 13.2.1 计算顶点 ............................................................................................................................182 13.2.2 计算索引-定义三角形 .......................................................................................................184 13.3 纹理 ..............................................................................................................................................186 13.3.1 程序上的处理方法 ............................................................................................................186 13.4 光照 ..............................................................................................................................................189 13.4.1概览(OVERVIEW) .............................................................................................................189 13.4.2 计算方格的阴影(Shade) ..............................................................................................190 13.4.3 地形阴影(Shading) .......................................................................................................191 13.5 在地形上“行走” .......................................................................................................................192 13.6 例子程序: Terrain .........................................................................................................................194 13.7 一些改进 ......................................................................................................................................196 13.8 摘要 ..............................................................................................................................................197 第十四章 粒子系统 (Particle Systems) ................................................................... 198 目标: ...................................................................................................................................................198 14.1 粒子和点精灵(Point Sprite) ....................................................................................................198 14.1.1 结构的格式 ........................................................................................................................198 14.1.2点精灵(Point Sprite)渲染状态 ......................................................................................199 14.1.3 粒子和他们的属性 ............................................................................................................200 14.2 粒子系统的组成 ...........................................................................................................................201 14.2.1 绘制粒子系统 ....................................................................................................................205 14.2.2 随机 ...................................................................................................................................209 14.3 具体的粒子系统:雪、火、粒子枪 ...........................................................................................209 14.3.1 例子程序:雪 ....................................................................................................................210 14.3.2 例子程序:火 ....................................................................................................................212 14.3.3 例子程序:粒子枪 ............................................................................................................214 14.4 摘要 ..............................................................................................................................................216 第十五章 选取 (Picking) .............................................................................................. 217 概览(OVERVIEW) ...............................................................................................................................217 目标 .......................................................................................................................................................218 15.1 屏幕到投影窗口的转换 ...............................................................................................................218 15.2 计算射线 ......................................................................................................................................220 15.3 变换射线 ......................................................................................................................................220 15.4 射线-对象 交点 .........................................................................................................................221 15.5 例子程序:选取 ...........................................................................................................................223 15.6 摘要 ..............................................................................................................................................224 第四部分 着色器和特效 (Shaders and Effects) ..................................................................... 225 第十六章 高级着色器语言入门 (Introduction to the High-Level Shading Language)................................................................................................................. 225 目标 .......................................................................................................................................................225 16.1 编写HLSL 着色器 ......................................................................................................................226 16.1.1 全局变量 ............................................................................................................................227 16.1.2 输入和输出结构 ................................................................................................................227 16.1.3 函数的入口点 ....................................................................................................................228 16.2 编译HLSL 着色器 ......................................................................................................................229 16.2.1 常量表 ...............................................................................................................................229 16.3 变量类型 ......................................................................................................................................233 16.3.2 向量类型 ............................................................................................................................234 16.3.3 矩阵类型 ............................................................................................................................235 16.3.4 数组 ...................................................................................................................................236 16.3.5 结构 ...................................................................................................................................236 16.3.6 typedef关键字 ....................................................................................................................237 16.4关键字、语句和强制转换 ............................................................................................................238 16.4.1 关键字 ...............................................................................................................................238 16.4.2 基本程序流程 ....................................................................................................................238 16.4.3 强制转换(casting) .........................................................................................................239 16.5 操作符 ..........................................................................................................................................239 16.6 用户定义函数 ...............................................................................................................................241 16.7内建函数 .......................................................................................................................................242 16.8 摘要 ..............................................................................................................................................244 第十七章 顶点着色器入门 (Introduction to Vertex Shaders) .......................... 245 概览 .......................................................................................................................................................245 目标 .......................................................................................................................................................246 17.1顶点声明 .......................................................................................................................................246 17.1.1 描述顶点声明 ....................................................................................................................246 17.1.2 创建顶点声明 ....................................................................................................................248 17.1.3 使用一个顶点声明 ............................................................................................................249 17.2顶点数据用途 ...............................................................................................................................249 17.3使用顶点着色器的步骤 ................................................................................................................250 17.3.1 编写并编译顶点着色器 ....................................................................................................251 17.3.2 创建顶点着色器 ................................................................................................................251 17.3.3 建立顶点着色器 ................................................................................................................251 17.3.4 销毁顶点着色器 ................................................................................................................252 17.4样例应用程序:散射光照 ............................................................................................................252 17.5 卡通渲染 ......................................................................................................................................257 17.5.1 卡通着色 ............................................................................................................................258 17.5.2 卡通着色的顶点着色器代码 ............................................................................................259 17.5.3轮廓勾勒 .............................................................................................................................260 17.5.4 轮廓边顶点着色器代码 ....................................................................................................263 17.6 摘要 ..............................................................................................................................................265 第十八章 像素着色器入门 (Introduction to Pixel Shaders) .............................. 266 目标 .......................................................................................................................................................266 18.1多纹理化概览 ...............................................................................................................................266 18.1.1 允许多个纹理 ....................................................................................................................268 18.1.2 多纹理坐标 ........................................................................................................................269 18.2像素着色器输入和输出 ................................................................................................................269 18.3使用像素着色器的步骤 ................................................................................................................270 18.3.1 编写并编译像素着色器 ....................................................................................................270 18.3.2 创建像素着色器 ................................................................................................................271 18.3.3 建立像素着色器 ................................................................................................................271 18.3.4 销毁像素着色器 ................................................................................................................271 18.4 HLSL采样器对象 .........................................................................................................................272 18.5 例子程序:Multitexturing in a Pixel Shader ................................................................................273 18.6 摘要 ..............................................................................................................................................280 第十九章 效果架构 (The Effects Framework) ....................................................................... 281 概览 .......................................................................................................................................................281 目标 .......................................................................................................................................................281 19.1 技术与传递(Techniques and Passes) .......................................................................................281 19.2 更多HLSL内置对象( More HLSL Intrinsic Objects) ..........................................................282 19.2.1 纹理对象 ............................................................................................................................283 19.2.2 采样器对象与采样器状态 ................................................................................................283 19.2.3 顶点与像素着色器对象(Vertex and Pixel Shader Objects) .........................................283 19.2.4 字符串 ...............................................................................................................................284 19.2.5 注解 (Annotations) ............................................................................................................285 19.3 效果文件的设备状态( Device States in an Effect File) .........................................................285 19.4 创建效果 ......................................................................................................................................286 19.5 设置系数(Setting Constants) ...................................................................................................287 19.6 使用效果 ......................................................................................................................................289 19.6.1 获得效果句柄( Obtaining a Handle to an Effect) .......................................................289 19.6.2 激活一个效果( Activating an Effect) ..........................................................................289 19.6.3 启动效果 ............................................................................................................................290 19.6.4 设置当前的渲染传递(Setting the Current Rendering Pass) ........................................290 19.6.5 结束效果(Ending an Effect) .........................................................................................290 19.6.6 例子 ...................................................................................................................................290 19.7 例子程序: Lighting and Texturing in an Effect File .....................................................................291 19.8例子程序: Fog Effect .....................................................................................................................296 19.9例子程序: Cartoon Effect ..............................................................................................................298 19.10 效果编辑(EffectEdit) .............................................................................................................299 19.11摘要 .............................................................................................................................................300 略 ...........................................................................................................................................................300 相关文章: ............................................................................................... 301 AGP内存 .......................................................................................................................... 301 Direct3D中实现图元的鼠标拾取 .......................................................................................... 303 1、什么是拾取,拾取能做什么? ..............................................................................................303 2、拾取操作的步骤和实现 ..........................................................................................................303 3、结束及声明..............................................................................................................................309 4、参考文献.................................................................................................................................310 克莱姆(Cramer)法则 ....................................................................................................... 311 一、线性方程组 ................................................................................................................................... 311 三、齐次线性方程组 ............................................................................................................................313 四、例子 ...............................................................................................................................................314