通過前面的介紹,可以建立起一個網格模型,但這個網格模型是渾然一體的,而在現實生活中,為了能獨立對一個物體的不同部分進行變換,必須將模型分割成不同的模塊,在.x文件中使用框架(frame)對一個網格模型的不同部分進行組織和管理。框架僅僅是一個外殼,在框架中通常需要包含具體的網格和一個框架變換矩陣,其中框架變換矩陣用于指定該框架包含的部分在整個模型中的初始位置。
模板Frame和FrameTransformMatrix的定義如下:
Defines a coordinate frame, or "frame of reference."
The Frame template is open and can contain any object. The D3DX mesh-loading
functions recognize Mesh, FrameTransformMatrix, and Frame template
instances as child objects when loading a Frame instance.
template Frame
{
< 3D82AB46-62DA-11CF-AB39-0020AF71E433 >
[...]
}
The frame template recognizes child Frame and
Mesh nodes inside a frame and can recognize user-defined templates through a
callback function.
Defines a local transform for a frame (and all its
child objects).
template FrameTransformMatrix
{
< F6F23F41-7686-11cf-8F52-0040333594A3 >
Matrix4x4 frameMatrix;
}
Where:
- frameMatrix - A Matrix4x4 template.
Defines a 4 x 4 matrix. This is used as a frame
transformation matrix.
template Matrix4x4
{
< F6F23F45-7686-11cf-8F52-0040333594A3 >
array float matrix[16];
}
Where:
- array float matrix[16] - Array of 16 floats.
框架可以嵌套,即一個框架可以由許多子框架構成。例如為了模擬一個人的網格模型,整體可以由頭部、胸部、左臂、右臂、左腿、右腿等框架組成,而左、右臂又可以由上臂、下臂和手三個框架組成,而手又可以由五指和手掌6個框架組成,甚至每個手指還可以繼續細分。
我們在cube_2.x的基礎上添加框架構成cube_3.x:
xof 0302txt 0064
Header {
1;
0;
1;
}
Material RedMaterial { //第一塊材料
1.000000;0.000000;0.000000;1.000000;; // R = 1.0, G = 0.0, B = 0.0
0.000000;
0.000000;0.000000;0.000000;;
0.000000;0.000000;0.000000;;
TextureFilename
{
"Tex1.jpg"; //紋理文件名
}
}
Material GreenMaterial { //第二塊材料
0.000000;1.000000;0.000000;1.000000;; // R = 0.0, G = 1.0, B = 0.0
0.000000;
0.000000;0.000000;0.000000;;
0.000000;0.000000;0.000000;;
TextureFilename
{
"Tex2.jpg"; //紋理文件名
}
}
Frame CubeFrame { //框架
FrameTransformMatrix { //初始位置矩陣
1.000000, 0.000000, 0.000000, 0.000000,
0.000000, 1.000000, 0.000000, 0.000000,
0.000000, 0.000000, 1.000000, 0.000000,
0.001000, 0.001000, 0.001000, 1.000000;;
}
Mesh Cube { //網格
8; //8個頂點,以下為8個頂點的坐標
1.000000;1.000000;-1.000000;,
-1.000000;1.000000;-1.000000;,
-1.000000;1.000000;1.000000;,
1.000000;1.000000;1.000000;,
1.000000;-1.000000;-1.000000;,
-1.000000;-1.000000;-1.000000;,
-1.000000;-1.000000;1.000000;,
1.000000;-1.000000;1.000000;;
12; // 12個面, 以下為每個面三個頂點的索引
3;0,1,2;,
3;0,2,3;,
3;0,4,5;,
3;0,5,1;,
3;1,5,6;,
3;1,6,2;,
3;2,6,7;,
3;2,7,3;,
3;3,7,4;,
3;3,4,0;,
3;4,7,6;,
3;4,6,5;;
//網格材質列表
MeshMaterialList {
2; //使用材質的數量:2塊材質
12; //為12個面指定材質
0, //為前6個面使用第一塊材質
0,
0,
0,
0,
0,
1, //為后面的6個面使用第二塊材質
1,
1,
1,
1,
1;;
{RedMaterial} //第一塊材質,引用前面定義的RedMaterial材質
{GreenMaterial} //第二塊材質,引用前面定義的GreenMaterial材質
}
//頂點法線
MeshNormals {
8; //定義8個法線向量
0.333333;0.666667;-0.666667;,
-0.816497;0.408248;-0.408248;,
-0.333333;0.666667;0.666667;,
0.816497;0.408248;0.408248;,
0.666667;-0.666667;-0.333333;,
-0.408248;-0.408248;-0.816497;,
-0.666667;-0.666667;0.333333;,
0.408248;-0.408248;0.816497;;
12; //為12個面的每個頂點指定法線
3;0,1,2;,
3;0,2,3;,
3;0,4,5;,
3;0,5,1;,
3;1,5,6;,
3;1,6,2;,
3;2,6,7;,
3;2,7,3;,
3;3,7,4;,
3;3,4,0;,
3;4,7,6;,
3;4,6,5;;
}
//紋理坐標
MeshTextureCoords {
8; //定義8對紋理坐標
0.000000;1.000000;
1.000000;1.000000;
0.000000;1.000000;
1.000000;1.000000;
0.000000;0.000000;
1.000000;0.000000;
0.000000;0.000000;
1.000000;0.000000;;
}
}
}