天行健 君子当自强而不息

网格模型高级技术(2)

我们在cube_1.x的基础上添加材质、法线和纹理,构成cube_2.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"; //纹理文件名
}
}
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;;
}
}
 

效果图如下:


 

可以看到在Mesh模板中嵌套着一个子模板MeshMaterialList,它是Mesh模板的一部分,用来将每个面与材质相关联,其定义如下:

Used in a mesh object to specify which material applies to which faces. The nMaterials member specifies how many materials are present, and materials specify which material to apply.

template MeshMaterialList
{
< F6F23F42-7686-11CF-8F52-0040333594A3 >
DWORD nMaterials;
DWORD nFaceIndexes;
array DWORD faceIndexes[nFaceIndexes];
[Material <3D82AB4D-62DA-11CF-AB39-0020AF71E433>]
}

Where:

  • nMaterials - A DWORD. The number of materials.
  • nFaceIndexes - A DWORD. The number of indices.
  • faceIndexes[nFaceIndexes] - An arrray of DWORDs containing the face indices.

MeshMaterialList是一个受限的模板,它只能包含Material模板,其定义如下:

Defines a basic material color that can be applied to either a complete mesh or a mesh's individual faces. The power is the specular exponent of the material.

Note     The ambient color requires an alpha component.

TextureFilename is an optional data object. If this object is not present, the face is untextured.

template Material
{
< 3D82AB4D-62DA-11CF-AB39-0020AF71E433 >
ColorRGBA faceColor;
FLOAT power;
ColorRGB specularColor;
ColorRGB emissiveColor;
[...]
}

Where:

  • faceColor - Face color. A ColorRGBA template.
  • power - Material specular color exponent.
  • specularColor - Material specular color. A ColorRGB template.
  • emissiveColor - Material emissive color. A ColorRGB template.

 

Defines a color object with an alpha component. This is used for the face color in the material template definition.

template ColorRGBA
{
< 35FF44E0-6C7C-11cf-8F52-0040333594A3 >
float red;
float green;
float blue;
float alpha;
}

Defines the basic RGB color object.

template ColorRGB
{
< D3E16E81-7835-11cf-8F52-0040333594A3 >
float red;
float green;
float blue;
}

在cube_2.x中,首先定义了两个材质RedMaterial和GreenMaterial:

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"; //纹理文件名
}
}

在模板MeshMaterialList中则给出了各个面与材质的关联信息:

//网格材质列表
MeshMaterialList {
2; //使用材质的数量:2块材质
12; //为12个面指定材质
0, //为前6个面使用第一块材质
0,
0,
0,
0,
0,
1, //为后面的6个面使用第二块材质
1,
1,
1,
1,
1;;
	{RedMaterial}       //第一块材质,引用前面定义的RedMaterial材质
{GreenMaterial} //第二块材质,引用前面定义的GreenMaterial材质
}

其中,{RedMaterial}和{GreenMaterial}是对上面定义的材质模板对象的引用。

在光照模型运算时需要用到法向量,法向量分为面法向量和顶点法向量。在基于逐顶点计算的光照模型中,需要使用顶点法向量。通常顶点法向量的计算过程是:先将共享该顶点的几个面的面法向量相加并除以共享该顶点的面的个数,接着归一化这个结果。模板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.
  • nFaceNormals - Number of face normals.
  • array MeshFace faceNormals[nFaceNormals] - Array of mesh face normals.

在文件cube_2.x中,法向量的定义以及为面指定法向量的内容如下:

//顶点法线
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;;
}

模板TextureFilename用于引用纹理,它通常作为Material模板对象的子对象出现,其定义如下:

Enables you to specify the file name of a texture to apply to a mesh or a face. This template should appear within a material object.

template TextureFilename 
{
< A42790E1-7810-11cf-8F52-0040333594A3 >
string filename;
}

在使用TextureFilename模板时,只需要使用字符串filename指定一个纹理文件名即可,但要将这幅纹理映射到网格模型中,还需要指定纹理坐标:

Defines a mesh's texture coordinates.

template MeshTextureCoords
{
< F6F23F40-7686-11cf-8F52-0040333594A3 >
DWORD nTextureCoords;
array Coords2d textureCoords[nTextureCoords] ;
}

Where:

  • nTextureCoords - Number of texture coordinates.
  • array Coords2d textureCoords[nTextureCoords] - Array of 2D texture coordinates.

Defines a two dimensional vector used to define a mesh's (u, v) texture coordinates.

template Coords2d
{
< F6F23F44-7686-11cf-8F52-0040333594A3 >
float u;
float v;
}
  • u - u coordinate value.
  • v - v coordinate value.

在cube_2.x中,定义纹理坐标的代码如下:

//纹理坐标
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 14:49 lovedday 阅读(1745) 评论(0)  编辑 收藏 引用


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


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论