??? 終于寫好了供練習用的.x文件,只是靜態Mesh內容,以后要在這個基礎上添加Animation內容的。顯示如下:

??? 這里是.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。

??? 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的“
網格模型高級技術”,寫的很好。