从简单开始 邂逅Geometry
Download
几何体的导出是相当容易的,有了上次获得Node的基础,获得几何体的信息就显得顺其自然了。
几何体关心的主要信息就是顶点和三角面,自然还有纹理坐标,法线,顶点颜色,切线,BinNormal等等,不过这些都是后话,首先我们只关心顶点和面。
同时另外一个重要的信息就是矩阵信息,需要注意的是,在不同的Frame中矩阵信息很可能会发生变化,这点需要注意下。
在第一个例子的基础上 只需要添加为数不多的代码即可实现。
首先我们需要判断结点是不是包含几何体信息:
BOOL OnlyGeometry::nodeEnum(INode* node,int indent)
{
……
ObjectState os = node->EvalWorldState(m_ip->GetTime());
if (os.obj)
{
if(os.obj->SuperClassID()==GEOMOBJECT_CLASS_ID)
{
ExportGeomObject(node,indent);
}
}
for (int c = 0; c < node->NumberOfChildren(); c++) {
if (!nodeEnum(node->GetChildNode(c),indent+1))
return FALSE;
}
return TRUE;
}
然后在具体的导出几何体的函数中,我们还需要再判断一次:
void OnlyGeometry::ExportGeomObject(INode* node,int indent)
{
TimeValue time= m_ip->GetTime();
ObjectState os=node->EvalWorldState(time);
if(!os.obj)
return;
if (os.obj->ClassID() == Class_ID(TARGET_CLASS_ID, 0))
return;
…………………..
这是因为在3DS MAX中还有很多其他的物体也包含了几何体信息,例如相机和灯光的Target部分,所以需要过滤一下。
然后就直接获取我们需要的信息,首先是矩阵:我们使用
INode:: GetObjectTMAfterWSM()来获得。这里WSM是World Space Modifier的缩写。意思是在使用了World Space Modifier之后的矩阵。关于World Space Modifier的详细信息可以参看SDK的文档,因为本例中没有用到任何的World Space Modifier。所以这里如果使用INode::GetObjectTM()的效果也是一样的。
Matrix3 tm=node->GetObjTMAfterWSM(time);
Point3 row=tm.GetRow(0);
fprintf(m_fileStream,"%s%s<%f %f %f>\n",GetIndent(indent),_T("Matrix Row 0:"),row.x,row.y,row.z);
row=tm.GetRow(1);
fprintf(m_fileStream,"%s%s<%f %f %f>\n",GetIndent(indent),_T("Matrix Row 1:"),row.x,row.y,row.z);
row=tm.GetRow(2);
fprintf(m_fileStream,"%s%s<%f %f %f>\n",GetIndent(indent),_T("Matrix Row 2:"),row.x,row.y,row.z);
row=tm.GetRow(3);
fprintf(m_fileStream,"%s%s<%f %f %f>\n",GetIndent(indent),_T("Matrix Row 3:"),row.x,row.y,row.z);
接下来就是顶点和三角面的导出,在Max中的结点并没有直接保存了Mesh的信息,我们如果要获得Mesh的信息需要首先将Object转化为Mesh,然后再从Mesh获得具体的信息:
TriObject* triobj=0;
triobj=(TriObject*)os.obj->ConvertToType(time,Class_ID(TRIOBJ_CLASS_ID,0));
这里Max给我们执行的其实是一个Deep Copy,所以我们可以等会转换完成后把内存释放。
if(triobj!=os.obj) //deep copy
{
Mesh * mesh = &(triobj->GetMesh());
if(mesh)
{
mesh->buildNormals();
fprintf(m_fileStream,"%s%s\n",GetIndent(indent),_T("Geometry Data:"));
fprintf(m_fileStream,"%s%s%d%s%d\n",GetIndent(indent+1),_T("Vertex Number:"),mesh->getNumVerts(),_T("Face Number:"),mesh->getNumFaces());
//顶点信息
for(int i=0;i<mesh->getNumVerts();++i)
{
Point3 vert = tm * mesh->verts[i];
fprintf(m_fileStream, "%s vertex %d:<%f,%f,%f>\n", GetIndent(indent),i, vert.x, vert.y, vert.z);
}
//面信息
for (int i = 0; i < mesh->getNumFaces(); i++)
{
fprintf(m_fileStream, "%s face %d -- verts:<%d,%d,%d> edgevis:<%d,%d,%d> smoothgrp:<0x%x> matid:<%d>\n",
GetIndent(indent),
i,
mesh->faces[i].v[0],
mesh->faces[i].v[1],
mesh->faces[i].v[2],
mesh->faces[i].getEdgeVis(0) ? 1 : 0,
mesh->faces[i].getEdgeVis(1) ? 1 : 0,
mesh->faces[i].getEdgeVis(2) ? 1 : 0,
mesh->faces[i].getSmGroup(),
mesh->faces[i].getMatID());
}
}
}
delete triobj;
fprintf(m_fileStream,"%s%s\n",GetIndent(indent),_T("Geometry End"));
fprintf(m_fileStream, "\n");
}
到此为止,我们的初步导出Geometry的工作就顺利完成了,可以打开Max试一试,输出的简单的文件如下:
Begin Do Header
Begin Do Nodes
Totoal Node Number:3
Root Name:Scene Root
Node Name:Box01
Matrix Row 0:<1.000000 0.000000 0.000000>
Matrix Row 1:<0.000000 1.000000 0.000000>
Matrix Row 2:<0.000000 0.000000 1.000000>
Matrix Row 3:<-19.448299 -3.523636 0.000000>
Geometry Data:
Vertex Number:8Face Number:12
vertex 0:<-40.204018,-28.128052,0.000000>
vertex 1:<1.307419,-28.128052,0.000000>
vertex 2:<-40.204018,21.080780,0.000000>
vertex 3:<1.307419,21.080780,0.000000>
vertex 4:<-40.204018,-28.128052,24.273842>
vertex 5:<1.307419,-28.128052,24.273842>
vertex 6:<-40.204018,21.080780,24.273842>
vertex 7:<1.307419,21.080780,24.273842>
face 0 -- verts:<0,2,3> edgevis:<1,1,0> smoothgrp:<0x2> matid:<1>
face 1 -- verts:<3,1,0> edgevis:<1,1,0> smoothgrp:<0x2> matid:<1>
face 2 -- verts:<4,5,7> edgevis:<1,1,0> smoothgrp:<0x4> matid:<0>
face 3 -- verts:<7,6,4> edgevis:<1,1,0> smoothgrp:<0x4> matid:<0>
face 4 -- verts:<0,1,5> edgevis:<1,1,0> smoothgrp:<0x8> matid:<4>
face 5 -- verts:<5,4,0> edgevis:<1,1,0> smoothgrp:<0x8> matid:<4>
face 6 -- verts:<1,3,7> edgevis:<1,1,0> smoothgrp:<0x10> matid:<3>
face 7 -- verts:<7,5,1> edgevis:<1,1,0> smoothgrp:<0x10> matid:<3>
face 8 -- verts:<3,2,6> edgevis:<1,1,0> smoothgrp:<0x20> matid:<5>
face 9 -- verts:<6,7,3> edgevis:<1,1,0> smoothgrp:<0x20> matid:<5>
face 10 -- verts:<2,0,4> edgevis:<1,1,0> smoothgrp:<0x40> matid:<2>
face 11 -- verts:<4,6,2> edgevis:<1,1,0> smoothgrp:<0x40> matid:<2>
Geometry End
Node Name:Pyramid01
Matrix Row 0:<1.000000 0.000000 0.000000>
Matrix Row 1:<0.000000 1.000000 0.000000>
Matrix Row 2:<0.000000 0.000000 1.000000>
Matrix Row 3:<24.578979 -52.777748 0.005000>
Geometry Data:
Vertex Number:6Face Number:8
vertex 0:<24.578979,-52.777748,43.850536>
vertex 1:<-1.593857,-72.522881,0.005000>
vertex 2:<50.751816,-72.522881,0.005000>
vertex 3:<50.751816,-33.032616,0.005000>
vertex 4:<-1.593857,-33.032616,0.005000>
vertex 5:<24.578979,-52.777748,0.005000>
face 0 -- verts:<0,1,2> edgevis:<1,1,1> smoothgrp:<0x4> matid:<0>
face 1 -- verts:<0,2,3> edgevis:<1,1,1> smoothgrp:<0x2> matid:<0>
face 2 -- verts:<0,3,4> edgevis:<1,1,1> smoothgrp:<0x10> matid:<0>
face 3 -- verts:<0,4,1> edgevis:<1,1,1> smoothgrp:<0x20> matid:<0>
face 4 -- verts:<1,5,2> edgevis:<1,1,1> smoothgrp:<0x8> matid:<0>
face 5 -- verts:<2,5,3> edgevis:<1,1,1> smoothgrp:<0x8> matid:<0>
face 6 -- verts:<3,5,4> edgevis:<1,1,1> smoothgrp:<0x8> matid:<0>
face 7 -- verts:<4,5,1> edgevis:<1,1,1> smoothgrp:<0x8> matid:<0>
Geometry End
Node Name:Box02
Matrix Row 0:<1.000000 0.000000 0.000000>
Matrix Row 1:<0.000000 1.000000 0.000000>
Matrix Row 2:<0.000000 0.000000 1.000000>
Matrix Row 3:<128.516830 57.409515 0.000000>
Geometry Data:
Vertex Number:8Face Number:12
vertex 0:<111.216064,37.396194,0.000000>
vertex 1:<145.817596,37.396194,0.000000>
vertex 2:<111.216064,77.422836,0.000000>
vertex 3:<145.817596,77.422836,0.000000>
vertex 4:<111.216064,37.396194,-40.238293>
vertex 5:<145.817596,37.396194,-40.238293>
vertex 6:<111.216064,77.422836,-40.238293>
vertex 7:<145.817596,77.422836,-40.238293>
face 0 -- verts:<2,0,3> edgevis:<1,0,1> smoothgrp:<0x2> matid:<0>
face 1 -- verts:<1,3,0> edgevis:<1,0,1> smoothgrp:<0x2> matid:<0>
face 2 -- verts:<5,4,7> edgevis:<1,0,1> smoothgrp:<0x4> matid:<1>
face 3 -- verts:<6,7,4> edgevis:<1,0,1> smoothgrp:<0x4> matid:<1>
face 4 -- verts:<1,0,5> edgevis:<1,0,1> smoothgrp:<0x8> matid:<4>
face 5 -- verts:<4,5,0> edgevis:<1,0,1> smoothgrp:<0x8> matid:<4>
face 6 -- verts:<3,1,7> edgevis:<1,0,1> smoothgrp:<0x10> matid:<3>
face 7 -- verts:<5,7,1> edgevis:<1,0,1> smoothgrp:<0x10> matid:<3>
face 8 -- verts:<2,3,6> edgevis:<1,0,1> smoothgrp:<0x20> matid:<5>
face 9 -- verts:<7,6,3> edgevis:<1,0,1> smoothgrp:<0x20> matid:<5>
face 10 -- verts:<0,2,4> edgevis:<1,0,1> smoothgrp:<0x40> matid:<2>
face 11 -- verts:<6,4,2> edgevis:<1,0,1> smoothgrp:<0x40> matid:<2>
Geometry End
END MYEXPORTER