|
|

楼主 |
发表于 2009-2-5 17:15:00
|
显示全部楼层
Re: 3dmax sdk插件的纹理坐标问题,请教各位一下.
输出的代码
bool ExportMesh(NodeItem & Node, FILE *out)
{
INode * pNode = Node.pNode;
if ((!pNode)||(!IsMesh(pNode))) return false;
//写入材质信息
Mtl * pMtl = pNode->GetMtl();
if (!pMtl) // No material assigned
{
Node.Mesh.bUseMat = false;
}
else // Material assigned
{
Node.Mesh.bUseMat = true;
// See if it's a Standard material
if (pMtl->ClassID() == Class_ID(DMTL_CLASS_ID, 0))
{
// We're going to use the 'Diffuse' color as the object color for DirectX
Node.Mesh.Diff_r = pMtl->GetDiffuse().r;
Node.Mesh.Diff_g = pMtl->GetDiffuse().g;
Node.Mesh.Diff_b = pMtl->GetDiffuse().b;
//Alpha
Node.Mesh.Diff_a = 1.0f - pMtl->GetXParency();
// Access the Diffuse map and see if it's a Bitmap texture
Texmap *tmap = pMtl->GetSubTexmap(ID_DI);
if ( (tmap != NULL) && (tmap->ClassID() == Class_ID(BMTEX_CLASS_ID, 0)) )
{
FindTextureFilename texfilename;
Node.Mesh.Diff_r = 1.0f;
Node.Mesh.Diff_g = 1.0f;
Node.Mesh.Diff_b = 1.0f;
Node.Mesh.Diff_a = 1.0f;
tmap->EnumAuxFiles(texfilename, FILE_ENUM_ALL);
if (texfilename.m_szTextureName != NULL) // texture filename found
{
GetFileName(texfilename.m_szTextureName);
strcpy(Node.Mesh.TexFile,texfilename.m_szTextureName);
}
}
}
}
// Matrix3 tm=pNode->GetObjTMAfterWSM(0); lzp
Matrix3 tm = pNode->GetObjectTM(0);
ObjectState os = pNode->EvalWorldState(0);
int needDel;
Mesh& mesh =*(((GeomObject*) os.obj)->GetRenderMesh(0,pNode,nullView,needDel));
//===============================================================
// write the mesh vertices
Node.Mesh.vertCnt = mesh.getNumVerts();
Node.Mesh.vertOfs = ftell(out);
for(int i = 0; i < Node.Mesh.vertCnt; i++)
{
Point3 pnt = mesh.getVert(i) * tm; //premultiply in MAX
MAXtoD3D(pnt); //D3D useful coordinate transformation
fwrite(&pnt.x, sizeof(float), 3, out);
}
// write vertex normals
mesh.buildNormals();
Node.Mesh.normCnt = mesh.getNumVerts();
Node.Mesh.normOfs = ftell(out);
for(int i = 0; i < Node.Mesh.normCnt; i++)
{
// normals are taken from a unique smoothing group
Point3 norm = Normalize(mesh.getNormal(i));
MAXtoD3D(norm);
fwrite(&norm.x, sizeof(float), 3, out);
}
// write texture vertex
Node.Mesh.tvertCnt = mesh.getNumTVerts();
Node.Mesh.tvertOfs = ftell(out);
for(int i = 0; i < Node.Mesh.tvertCnt; i++)
{
UVVert uv = mesh.getTVert(i);
uv.y=1.0f-uv.y;
fwrite(&uv.x, sizeof(float), 2, out);
}
// build and write faces
Node.Mesh.faceCnt = mesh.getNumFaces();
Node.Mesh.faceOfs = ftell(out);
for(int i = 0; i < Node.Mesh.faceCnt; i++)
{
SGEFace_hdr fHdr;
memset(&fHdr, 0, sizeof(SGEFace_hdr));
fHdr.vert[0] = mesh.faces.v[0];
fHdr.vert[1] = mesh.faces.v[1];
fHdr.vert[2] = mesh.faces.v[2];
// TODO: insert normals
fwrite(&fHdr, sizeof(SGEFace_hdr), 1, out);
}
return true;
}
|
|