从obj文件读入网格信息,组成网格。
首先看一下MeshLoader的代码。
1.Create
创建网格,首先是调用LoadGeometryFromOBJ,这个函数主要用来读入网格的几何信息,例如顶点、法线等等
for(; ; )
{
InFile >> strCommand;
if( !InFile )
break;
if( 0 == wcscmp( strCommand, L"#" ) )
{
// Comment
}
else if( 0 == wcscmp( strCommand, L"v" ) )
{
// Vertex Position
float x, y, z;
InFile >> x >> y >> z;
Positions.Add( D3DXVECTOR3( x, y, z ) );
}
else if( 0 == wcscmp( strCommand, L"vt" ) )
{
// Vertex TexCoord
float u, v;
InFile >> u >> v;
TexCoords.Add( D3DXVECTOR2( u, v ) );
}
else if( 0 == wcscmp( strCommand, L"vn" ) )
{
// Vertex Normal
float x, y, z;
InFile >> x >> y >> z;
Normals.Add( D3DXVECTOR3( x, y, z ) );
}
else if( 0 == wcscmp( strCommand, L"f" ) )
{
上面的是LoadGeometryFromOBJ的主体,可以看到该函数根据读到的字符来进行不同的操作。这里只是贴出了一小部分。
// If an associated material file was found, read that in as well.
if( strMaterialFilename[0] )
{
V_RETURN( LoadMaterialsFromMTL( strMaterialFilename ) );
}
函数的最后还把MTL文件(材质定义文件)载入了(有的话)。
下面回到Create函数。接下来就该读入MTL文件中网格用到的材质了。
读完材质就可以开始构造网格了。
// Create the encapsulated mesh
ID3DXMesh* pMesh = NULL;
V_RETURN( D3DXCreateMesh( m_Indices.GetSize() / 3, m_Vertices.GetSize(),
D3DXMESH_MANAGED | D3DXMESH_32BIT, VERTEX_DECL,
pd3dDevice, &pMesh ) );