⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vertexextractor.h.bak

📁 处理网格数据点的KDTREE数据结构程序
💻 BAK
字号:
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/NodeVisitor>
#include <osg/Vec3>
#include <osg/PrimitiveSet>
#include <osgUtil/SmoothingVisitor>
#include <osgDB/WriteFile>
#include <osg/Material>
#include <osg/LightSource>
#include <osg/StateSet>
#include <osg/StateAttribute>
#include <osg/BoundingSphere>

//class VertexExtractor,从OSG文件中取出点坐标,法向量,存储在osg::Vec3Array*数组中
class VertexExtractor : public osg::NodeVisitor
{
public:
	osg::Vec3Array* extracted_verts;
	osg::Vec3Array* extracted_normal;
	 unsigned int i;
     VertexExtractor() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
     {
                extracted_verts   = new osg::Vec3Array;
                extracted_normal = new osg::Vec3Array;
     }
  
     void apply( osg::Geode& geode )
     {
		 for(i=0; i<geode.getNumDrawables(); i++ )
         {
            osg::Geometry* geom = dynamic_cast<osg::Geometry*>( geode.getDrawable(i) );
			if( geom &&(geom->getPrimitiveSet(0)->getMode()==osg::PrimitiveSet::TRIANGLES||geom->getPrimitiveSet(0)->getMode()==osg::PrimitiveSet::TRIANGLE_STRIP ))
			{
			osg::Vec3Array* verts = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
            if( verts )  
				extracted_verts->insert( extracted_verts->end(), verts->begin(), verts->end() );
			  
			
			osg::Vec3Array* norm = dynamic_cast<osg::Vec3Array*>( geom->getNormalArray() );          
            if( norm )
				extracted_normal->insert( extracted_normal->end(), norm->begin(), norm->end() );
			}
                             
           }
      }

};


//osg::ref_ptr<osg::Node> getpoints()
//{
//	VertexExtractor ex_vert;
//    osg::ref_ptr<osg::Node> first_node = osgDB::readNodeFile("D:/ziliao/dixingbianhua2/weibianhuanei-.osg") ;
//    first_node->accept(ex_vert); 
///*osg::ref_ptr<osg::Geometry>*/
//	osg::Geometry* tgeom= new osg::Geometry;
//	osg::ref_ptr<osg::Geode>tgeode= new osg::Geode;
//     tgeom->setVertexArray(ex_vert.extracted_verts.get());
//     tgeom->setNormalArray(ex_vert.extracted_normal.get());
//     tgeom->addPrimitiveSet(
//		 new osg::DrawArrays( osg::PrimitiveSet::TRIANGLES, 0, 1203 ) );
//	 osgUtil::SmoothingVisitor  *smooth;
//	 smooth->smooth(*tgeom);
//	 tgeode->addDrawable( tgeom );
//	 //bool ss= osgDB::writeNodeFile(*tgeode,"D:/ziliao/dixingbianhua222/dixingbianhua/Second.osg");
//     return tgeode.get();
//
//
//}
//// ///添加光源
//void SetLightOn(osg::Group* m_Root,osg::Node* m_pCurNode)
//	{
//		osg::ref_ptr<osg::Material>  pMat = new osg::Material;
//		pMat->setSpecular( osg::Material::FRONT,osg::Vec4(1.0,1.0,1.0,1.0));
//		pMat->setShininess(osg::Material::FRONT,100.0);
//		osg::ref_ptr<osg::Light> mLight = new osg::Light;
//
//		osg::ref_ptr<osg::LightSource> m_pLightSource = new osg::LightSource;
//			m_pLightSource->setLight(mLight.get());
//			m_Root->addChild(m_pLightSource.get());
//		osg::BoundingSphere bx = m_pCurNode->getBound();
//		float modelSize = bx.radius()*10.0;
//		float NodeZ,NodeY,NodeX;
//		NodeZ = bx.center().z() + bx.radius();
//		NodeX =  bx.center().x();
//		NodeY = bx.center().y();
//		mLight->setPosition(osg::Vec4(NodeX,NodeY,NodeZ,1.0));
//		//衰减
//		mLight->setAmbient(osg::Vec4(0.8,0.8,0.8,1.0));
//		mLight->setSpecular(osg::Vec4(0.8,0.8,0.8,1.0));
//		mLight->setConstantAttenuation(0.8f);
//		mLight->setLinearAttenuation(1.0f/modelSize); 
//		mLight->setQuadraticAttenuation(2.0f/osg::square(modelSize));
//
//		m_pLightSource->setLocalStateSetModes(osg::StateAttribute::ON);
//		osg::StateSet *rootset = m_Root->getOrCreateStateSet();
//		m_pLightSource->setStateSetModes(*rootset, osg::StateAttribute::ON);
//		osg::StateSet *Stateset = new osg::StateSet;
//		Stateset->setAttributeAndModes(pMat.get(),osg::StateAttribute::MATERIAL);
//	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -