Use of Nodevisitor-traverse the geometry under the Geode node and obtain data such as vertices, normals, etc.

Source: Internet
Author: User


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&LT;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&LT;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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.