天行健 君子当自强而不息

网格模型高级技术(3)

通过前面的介绍,可以建立起一个网格模型,但这个网格模型是浑然一体的,而在现实生活中,为了能独立对一个物体的不同部分进行变换,必须将模型分割成不同的模块,在.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;;
}
}
}

posted on 2008-05-27 15:05 lovedday 阅读(1474) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论