structsubset{std::vector<float> Vertexs;//locationstd::vector<float> normals;//Law tostd::vector<float> texcoords;//Texturesstd::vector<unsignedint> indices;//Index Subscriptstd::vector<unsignedint> facemtrls;//Polygon Material Index};classGetsimplifystldatavisitor: Publicosg::nodevisitor{ Public: Getsimplifystldatavisitor (Subset&s):d Stsubset (s), Osg::nodevisitor (Osg::nodevisitor::traverse_all_children) {}Virtual voidApply (osg::geode&Geode) {unsignedintCount =Geode.getnumdrawables (); for(unsignedinti =0; I < count; i++) {Osg::geometry*geometry = geode.getdrawable (i)Asgeometry (); if( !geometry)Continue; //Vertex Dataosg::vec3array* vertices = dynamic_cast<osg::vec3array*> (geometry->Getvertexarray ()); intVertexlnum = vertices->size (); for(intI=0; i<vertexlnum; i++) {DstSubset.vertexs.push_back (vertices-At (i). x ()); DstSubset.vertexs.push_back (Vertices-At (i). Y ()); DstSubset.vertexs.push_back (Vertices-At (i). Z ()); } //normal vectorosg::vec3array* normals = dynamic_cast<osg::vec3array*> (geometry->Getnormalarray ()); intNormalnum = normals->size (); for(intI=0; i<normalnum; i++) {dstSubset.normals.push_back (normals-At (i). x ()); DstSubset.normals.push_back (normals-At (i). Y ()); DstSubset.normals.push_back (normals-At (i). Z ()); } normalbindkinds.insert (Geometry-getnormalbinding ()); if(Osg::geometry::bind_per_vertex! = geometry->getnormalbinding ()) {std::stringmsg="unhandled normal vector binding mode"; MessageBox (0, LPCTSTR (Msg.c_str ()),"Warning", MB_OK); } //indexed Array for(unsignedinti =0; I < geometry->getnumprimitivesets (); ++i) {osg::P rimitiveset* PS = geometry->Getprimitiveset (i); if(!PS)Continue; Pts.insert (PS-GetType ()); Modes.insert (PS-GetMode ()); Switch(ps->GetType ()) { CaseOSG::P rimitiveset::D rawelementsuintprimitivetype: {osg::D rawelementsuint< /c8>* Deui = dynamic_cast<osg::D rawelementsuint*>(PS); Constunsigned indexnum = deui->getnumindices (); Switch(deui->GetMode ()) { CaseOSG::P rimitiveset::triangles: { for(unsignedintI=0; i<indexnum; i++) {dstSubset.indices.push_back (Deui-At (i)); } Break; } default: {std::stringmsg="Unhandled Drawing Mode"; MessageBox (0, LPCTSTR (Msg.c_str ()),"Warning", MB_OK); } } Break; } CaseOSG::P rimitiveset::D rawelementsushortprimitivetype: {osg::D Rawelementsush ORT* de = DYNAMIC_CAST<OSG::D rawelementsushort*>(PS); Constunsigned indexnum = de->getnumindices (); Switch(de->GetMode ()) { CaseOSG::P rimitiveset::triangles: { for(unsignedintI=0; i<indexnum; i++) {dstSubset.indices.push_back (de-At (i)); } Break; } CaseOSG::P rimitiveset::triangle_strip: { for(unsignedintI=0; i<indexnum-2; i++) { //Here is why the index is related to the cardinality even, Baidu Gl_triangle_strip if(i%2==0) {dstSubset.indices.push_back (de-At (i)); DstSubset.indices.push_back (de->at (i+1) ); DstSubset.indices.push_back (de->at (i+2) ); } Else{dstSubset.indices.push_back (de-At (i)); DstSubset.indices.push_back (de->at (i+2) ); DstSubset.indices.push_back (de->at (i+1) ); } } Break; } default: {std::stringmsg="Unhandled Drawing Mode"; MessageBox (0, LPCTSTR (Msg.c_str ()),"Warning", MB_OK); } } Break; } CaseOSG::P rimitiveset::D rawarraysprimitivetype: {osg::D rawarrays* da = dynamic_cast<osg::D rawarrays*>(PS); intFirst=da->GetFirst (); intCount=da->GetCount (); Switch(da->GetMode ()) { CaseOSG::P rimitiveset::triangles: { for(intI=first; i<first+count; i++) {dstSubset.indices.push_back (i); } Break; } CaseOSG::P rimitiveset::triangle_strip: { for(intI=first; i<first+count-2; i++) { if(i%2==0) {dstSubset.indices.push_back (i); DstSubset.indices.push_back (i+1 ); DstSubset.indices.push_back (i+2 ); } Else{dstSubset.indices.push_back (i); DstSubset.indices.push_back (i+2 ); DstSubset.indices.push_back (i+1 ); } } Break; } default: {std::stringmsg="Unhandled Drawing Mode"; MessageBox (0, LPCTSTR (Msg.c_str ()),"Warning", MB_OK); } } Break; } default: {std::stringmsg="primitive types that are not processed"; MessageBox (0, LPCTSTR (Msg.c_str ()),"Warning", MB_OK); } } } } }Private: Subset&Dstsubset; //The following three arrays are used to testSTD::Set<osg::Geometry::AttributeBinding>Normalbindkinds; STD::Set<OSG::P rimitiveset::type>pts; STD::Set<GLenum>modes;};
Use of Nodevisitor-traverse the geometry under the Geode node and such as vertices, normals, etc.