终于写好了供练习用的.x文件,只是静态Mesh内容,以后要在这个基础上添加Animation内容的。显示如下:
![test.x_ok.jpg](/images/cppblog_com/codejie/test.x_ok.jpg)
这里是.x文件内容:
xof 0303txt 0032
template Frame {
<3d82ab46-62da-11cf-ab39-0020af71e433>
[
]
}
template Matrix4x4 {
<f6f23f45-7686-11cf-8f52-0040333594a3>
array FLOAT matrix[16];
}
template FrameTransformMatrix {
<f6f23f41-7686-11cf-8f52-0040333594a3>
Matrix4x4 frameMatrix;
}
template Vector {
<3d82ab5e-62da-11cf-ab39-0020af71e433>
FLOAT x;
FLOAT z;
FLOAT y;
}
template MeshFace {
<3d82ab5f-62da-11cf-ab39-0020af71e433>
DWORD nFaceVertexIndices;
array DWORD faceVertexIndices[nFaceVertexIndices];
}
template Mesh {
<3d82ab44-62da-11cf-ab39-0020af71e433>
DWORD nVertices;
array Vector vertices[nVertices];
DWORD nFaces;
array MeshFace faces[nFaces];
[
]
}
template MeshNormals {
<f6f23f43-7686-11cf-8f52-0040333594a3>
DWORD nNormals;
array Vector normals[nNormals];
DWORD nFaceNormals;
array MeshFace faceNormals[nFaceNormals];
}
template Coords2d {
<f6f23f44-7686-11cf-8f52-0040333594a3>
FLOAT u;
FLOAT v;
}
template MeshTextureCoords {
<f6f23f40-7686-11cf-8f52-0040333594a3>
DWORD nTextureCoords;
array Coords2d textureCoords[nTextureCoords];
}
template ColorRGBA {
<35ff44e0-6c7c-11cf-8f52-0040333594a3>
FLOAT red;
FLOAT green;
FLOAT blue;
FLOAT alpha;
}
template IndexedColor {
<1630b820-7842-11cf-8f52-0040333594a3>
DWORD index;
ColorRGBA indexColor;
}
template MeshVertexColors {
<1630b821-7842-11cf-8f52-0040333594a3>
DWORD nVertexColors;
array IndexedColor vertexColors[nVertexColors];
}
template VertexElement {
<f752461c-1e23-48f6-b9f8-8350850f336f>
DWORD Type;
DWORD Method;
DWORD Usage;
DWORD UsageIndex;
}
template DeclData {
<bf22e553-292c-4781-9fea-62bd554bdd93>
DWORD nElements;
array VertexElement Elements[nElements];
DWORD nDWords;
array DWORD data[nDWords];
}
Frame Root {
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.000000, 0.000000, 0.000000, 1.000000;;
}
Frame SubFrame {
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.000000, 0.000000, 0.000000, 1.000000;;
}
Mesh MeshObj {
12;
0.000000; 0.000000; 1.000000;,
1.000000; 0.000000; 1.000000;,
1.000000; 1.000000; 1.000000;,
0.000000; 1.000000; 1.000000;,
0.000000; 0.000000; 0.000000;,
1.000000; 0.000000; 0.000000;,
1.000000; 1.000000; 0.000000;,
0.000000; 1.000000; 0.000000;,
0.000000; 0.000000; -1.000000;,
1.000000; 0.000000; -1.000000;,
1.000000; 1.000000; -1.000000;,
0.000000; 1.000000; -1.000000;;
20;
3; 0, 4, 1;,
3; 4, 5, 1;,
3; 1, 2, 0;,
3; 2, 3, 0;,
3; 6, 2, 5;,
3; 2, 1, 5;,
3; 3, 2, 7;,
3; 2, 6, 7;,
3; 7, 4, 3;,
3; 4, 0, 3;,
3; 4, 8, 5;,
3; 5, 8, 9;,
3; 5, 9, 6;,
3; 9, 10, 6;,
3; 6, 10, 11;,
3; 7, 6, 11;,
3; 7, 11, 8;,
3; 4, 7, 8;,
3; 8, 10, 9;,
3;8, 11, 10;;
MeshNormals {
12;
-0.333333; -0.333333; 0.333333;,
0.333333; -0.333333; 0.333333;,
0.333333; 0.333333; 0.333333;,
-0.333333; 0.333333; 0.333333;,
-0.500000; -0.500000; 0.000000;,
0.500000; -0.500000; 0.000000;,
0.500000; 0.500000; 0.000000;,
-0.500000; 0.500000; 0.000000;,
-0.333333; -0.333333; -0.333333;,
0.333333; -0.333333; -0.333333;,
0.333333; 0.333333; -0.333333;,
-0.333333; 0.333333; -0.333333;,
20;
3; 0, 4, 1;,
3; 4, 5, 1;,
3; 1, 2, 0;,
3; 2, 3, 0;,
3; 6, 2, 5;,
3; 2, 1, 5;,
3; 3, 2, 7;,
3; 2, 6, 7;,
3; 7, 4, 3;,
3; 4, 0, 3;,
3; 4, 8, 5;,
3; 5, 8, 9;,
3; 5, 9, 6;,
3; 9, 10, 6;,
3; 6, 10, 11;,
3; 7, 6, 11;,
3; 7, 11, 8;,
3; 4, 7, 8;,
3; 8, 10, 9;,
3;8, 11, 10;;
}
}
}
} 这里需要说明的是:如何保证一个Mesh(或者叫做MeshFace)的面是“正面”朝上的。这个面的朝向是由MeshNormals定义的,其模版定义如下:
MeshNormals
Defines normals for a mesh. The first array of vectors is the normal vectors themselves, and the second array is an array of indexes specifying which normals should be applied to a given face. The value of the nFaceNormals member should be equal to the number of faces in a mesh.
template MeshNormals
{
< F6F23F43-7686-11cf-8F52-0040333594A3 >
DWORD nNormals;
array Vector normals[nNormals];
DWORD nFaceNormals;
array MeshFace faceNormals[nFaceNormals];
}
Where:
nNormals - Number of normals.
array Vector normals[nNormals] - Array of normals. See Vector.
nFaceNormals - Number of face normals.
array MeshFace faceNormals[nFaceNormals] - Array of mesh face normals. See MeshFace.
由其定义可以看出,Vertex的法线向量和Face的法线向量共同决定Triangle的面朝向。如何确定两个的法线向量呢?有个简单步骤:首先确定Vertex的法线向量,方法是将使用到指定Vertex的多个面的“正(或朝向)”方向的法线向量相加,再除以面个数,就得到了指定Vertex的法线向量;然后定义Mesh的三个Vertex顺序,方法是伸出右手,大拇指指向Mesh“正”方向,弯曲其他四指,则三个Vetex的定义顺序按照四指旋转方向确定,简单点就是绕大拇指,逆时针定义;最后将Mesh的Vertex索引定义,拷贝到MeshNormals模版中即可。下面说个实例,如下图所示,我们来定义MeshA和MeshB。
![sample_normals.JPG](/images/cppblog_com/codejie/sample_normals.JPG)
Mesh {
8;
0.000000; 0.000000; 1.000000;,//0
1.000000; 0.000000; 1.000000;,//1
1.000000; 1.000000; 1.000000;,//2
0.000000; 1.000000; 1.000000;,//3 ...
12;
3;0, 1, 2;,
3;0, 2, 3;,
...
}
则Vertex0的法线向量为((0,0,1) + ( -1,0,0) +(0,-1, 0))/3 = (-0.333333,-0.333333,0.333333)。说明下,由于A,B在同一水平面,因此将AB算作了一个面,因此这认为Vertex0被三个面使用,而不是标准的4个面。
具体可以参考
loveddy的“
网格模型高级技术”,写的很好。