《DirectX 9 3D 游戏设计入门》
《Introduction to 3D Game Programming with DirectX 9》
by Frank Luna ISBN:1556229135
Wordware Publishing © 2003 (388 pages)
This text provides an introduction to programming interactive 3D computer graphics using DirectX 9.0, with
an emphasis on game development.
随书代码下载地址:http://www.moon-labs.com/ml_book_samples.htm
翻译:
01 章 – 12 章 翁云兵 Email:WengYB@126.com
13 章 – 19 章 天兵 Email:zzprogram@21cn.com
首先感谢游戏开发资源网,让我发现了翁云兵老兄翻译的前 12 章,从此让我有信心从事 D3D 游戏的开发,再次
感谢翁云兵,还有在我学习中给予帮助的网友们,对初学者的帮助与指导。出于同样的目的,我将整理后的文档再次
发到网上,希望能帮助更多热爱游戏开发的朋友。
本教程由天兵收集、翻译并整理,最初是为方便自己学习,非专业翻译,如有不准确的地方请谅解。欢迎提出翻
译错误的章节,请联系:zzprogram@21cn.com。
本书没有取得作者授权,此翻译版本纯属个人爱好,不得用于传播以及其他商业目的,只供爱好者参考使用,希
望用户下载完毕后 24 小时之内自觉删除!如果喜欢请购买正版支持!
《 游戏设计入门》
第一部分 必备的数学知识
目标............................................................................................................................................................10
三维空间中的向量....................................................................................................................................10
向量相等....................................................................................................................................................13
计算向量大小(向量的模)....................................................................................................................14
标准化向量................................................................................................................................................15
向量相加....................................................................................................................................................15
向量相减....................................................................................................................................................16
标量与向量的乘积....................................................................................................................................17
点积............................................................................................................................................................17
叉积............................................................................................................................................................18
矩阵............................................................................................................................................................19
相等、数乘矩阵以及相加........................................................................................................................19
乘法............................................................................................................................................................20
单位矩阵....................................................................................................................................................21
逆转............................................................................................................................................................22
矩阵的转置................................................................................................................................................22
D3DX 矩阵................................................................................................................................................23
基本变换....................................................................................................................................................25
矩阵平移....................................................................................................................................................26
矩阵旋转....................................................................................................................................................27
矩阵缩放....................................................................................................................................................29
综合变换....................................................................................................................................................30
一些向量变换函数....................................................................................................................................31
平面............................................................................................................................................................32
D3DX 平面.................................................................................................................................................32
点和平面的空间关系................................................................................................................................33
创建平面....................................................................................................................................................34
标准化平面................................................................................................................................................34
变换平面....................................................................................................................................................35
点到平面上最近的点................................................................................................................................35
射线(可选的)........................................................................................................................................36
射线............................................................................................................................................................36
线/面相交...................................................................................................................................................37
摘要(略)................................................................................................................................................37
第二部分 基础
第一章 初始化
目标............................................................................................................................................................38
1.1 Direct3D 概述......................................................................................................................................38
1.1.1 REF 设备...................................................................................................................................39
1.1.2 D3DDEVTYPE.........................................................................................................................39
1.2 COM.....................................................................................................................................................39
1.3 一些准备工作......................................................................................................................................40
1.3.1 表面...........................................................................................................................................40
1.3.2 Multisampling...........................................................................................................................42
1.3.3 像素格式...................................................................................................................................42
1.3.4 内存池.......................................................................................................................................43
1.3.5 交换链和页面切换...................................................................................................................43
1.3.6 深度缓冲...................................................................................................................................44
1.3.7 顶点处理...................................................................................................................................45
1.3.8 设备能力...................................................................................................................................45
1.4 初始化 Direct3D..................................................................................................................................46
1.4.1 获得 IDirect3D9 接口...............................................................................................................46
1.4.2 检测硬件顶点处理...................................................................................................................47
1.4.3 填充 D3DPRESENT_PARAMETERS 结构............................................................................48
1.4.4 创建 IDirect3DDevice9 对象....................................................................................................49
1.5 初始化 Direct3D 实例.........................................................................................................................50
1.5.1 d3dUtility.h/cpp.........................................................................................................................51
1.5.2 实例框架...................................................................................................................................52
1.5.3 D3D Init 实例............................................................................................................................53
1. 6 摘要(略).........................................................................................................................................55
第二章 渲染管线 !"#$%
目标............................................................................................................................................................56
2.1 表现模型..............................................................................................................................................56
2.1.1 顶点格式...................................................................................................................................57
2.1.2 三角形.......................................................................................................................................58
2.1.3 索引...........................................................................................................................................59
2.2 虚拟照相机..........................................................................................................................................60
2.3 渲染管线..............................................................................................................................................60
2.3.1 自身坐标系(Local Space)...................................................................................................61
2.3.2 世界坐标系(World Space)..................................................................................................62
2.3.3 视图坐标系(View Space)....................................................................................................63
2.3.4 背面拣选(Backface Culling)...............................................................................................64
2.3.5 光源(Lighting).....................................................................................................................65
2.3.6 裁剪(Clipping).....................................................................................................................65
2.3.7 投影(Projection)...................................................................................................................65
2.3.8 视口变换(Viewport Transform)..........................................................................................67
2.3.9 光栅化(Rasterization)..........................................................................................................68
2.4 摘要(略)...............................................................................................................................................69
第三章 在 中绘制 &"'
目标............................................................................................................................................................70
3.1 顶点/索引缓存.....................................................................................................................................70
3.1.1 创建一个顶点和索引缓存.......................................................................................................70
3.1.2 访问缓冲内存...........................................................................................................................72
3.1.3 找回顶点和索引缓存信息.......................................................................................................73
3.2 渲染状态..............................................................................................................................................74
3.3 绘制准备..............................................................................................................................................74
3.4 用顶点/索引缓存绘制.........................................................................................................................75
3.4.1 IDirect3DDevice9::DrawPrimitive...........................................................................................75
3.4.2 IDirect3DDevice9::DrawIndexedPrimitive..............................................................................75
3.4.3 开始/结束场景..........................................................................................................................77
3.5 D3DX 几何物体...................................................................................................................................77
3.6 实例程序:三角形、立方体、茶壶、D3DXCreate*......................................................................78
3.7 摘要(略)..........................................................................................................................................83
第四章 色彩 ()
目标............................................................................................................................................................84
颜色表示法...............................................................................................................................................84
4.2 顶点颜色..............................................................................................................................................87
4.3 着色处理..............................................................................................................................................87
4.4 实例程序:彩色三角形......................................................................................................................88
4.5 摘要(略)...............................................................................................................................................90
第五章 灯光 *""
目标............................................................................................................................................................91
5.1 灯光的组成..........................................................................................................................................91
5.2 材质......................................................................................................................................................92
5.3 顶点法线..............................................................................................................................................94
5.4 光源......................................................................................................................................................96
5.5 实例程序:灯光..................................................................................................................................98
5.6 附加实例............................................................................................................................................101
5.7 摘要(略)..............................................................................................................................................101
第六章 纹理 +",
目标..........................................................................................................................................................102
6.1 纹理坐标............................................................................................................................................103
6.2 创建并赋予材质................................................................................................................................104
6.3 过滤器................................................................................................................................................105
6.4 Mipmaps.............................................................................................................................................105
6.4.1 Mipmaps 过滤器.....................................................................................................................106
6.4.2 Direct3D 中使用 Mipmaps.....................................................................................................106
6.5 寻址模式............................................................................................................................................106
6.6 实例程序:有纹理的方块................................................................................................................108
5. 7 摘要(略).............................................................................................................................................111
第七章 混合 -",
目标..........................................................................................................................................................112
7.1 混合因素............................................................................................................................................112
7.2 混合要素............................................................................................................................................114
7.3 透明度.................................................................................................................................................114
7.3.1Alpha 通道................................................................................................................................114
7.3.2 指定 Alpha 资源......................................................................................................................115
7.4 使用 DirectX 纹理工具创建 Alpha 通道..........................................................................................116
7.5 实例程序:透明度............................................................................................................................117
7.6 摘要(略)..............................................................................................................................................119
第八章 模版 .",
目标..........................................................................................................................................................121
8.1 使用模版缓存....................................................................................................................................121
8.1.1 请求一个模版缓存.................................................................................................................121
8.1.2 模版测试.................................................................................................................................122
8.1.3 控制模版测试.........................................................................................................................122
8.1.3.1 模版参考值(Reference Value)........................................................................................122
8.1.3.2 模版掩码..............................................................................................................................122
8.1.3.3 模版值(Stencil Value).....................................................................................................123
8.1.3.4 比较运算..............................................................................................................................123
8.1.3 更新模版缓存.........................................................................................................................123
8.1.4 模版写掩码.............................................................................................................................124
8.2 实例程序:镜子................................................................................................................................124
8.2.1 反射数学.................................................................................................................................125
8.2.2 镜面实现流程.........................................................................................................................126
8.2.3 代码和解释.............................................................................................................................127
8.2.3.1 第一部分..............................................................................................................................128
8.2.3.2 第二部分..............................................................................................................................128
8.2.3.3 第三部分..............................................................................................................................129
8.2.3.4 第四部分..............................................................................................................................129
8.2.3.5 第五部分..............................................................................................................................130
8.3 实例程序:平面阴影........................................................................................................................130
8.3.1 平行光阴影.............................................................................................................................132
8.3.2 点光源阴影.............................................................................................................................133
8.3.3 阴影矩阵.................................................................................................................................133
8.3.4 用模版缓存防止双倍混合.....................................................................................................134
8.3.5 代码和解释.............................................................................................................................135
8.4 摘要(略)..............................................................................................................................................137
第三部分 实用的 /##
第九章 字体
目标..........................................................................................................................................................138
9.1 ID3DXFont.........................................................................................................................................138
9.1.1 创建一个 ID3DXFont.............................................................................................................139
9.1.2 绘制文本.................................................................................................................................139
9.1.3 计算每秒的渲染帧数.............................................................................................................140
9.2 CD3DFont..........................................................................................................................................141
9.2.1 创建一个 CD3DFont..............................................................................................................141
9.2.2 绘制文本.................................................................................................................................141
9.2.3 清除.........................................................................................................................................142
9.3 D3DXCreateText................................................................................................................................142
9.4 摘要(略)..............................................................................................................................................144
第十章 网格模型 )$
目标..........................................................................................................................................................145
10.1 几何信息..........................................................................................................................................145