#include #include #include #include #include #include #include #include #include #include #include #include #include #include int main() { osg::ref_ptr viewer = new osgViewer::Viewer() ; viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet())) ; osg::ref_ptr root = new osg::Group() ; //创建顶点数组 osg::ref_ptr coords = new osg::Vec3Array() ; osg::ref_ptr color = new osg::Vec4Array() ; pcl::PointCloud::Ptr cloud(new pcl::PointCloud) ; if (pcl::io::loadPLYFile("3-1-2.ply" , *cloud) == -1) { std::cout<<"读取点云失败!\n" ; return -1 ; } int nums = cloud->size() ; std::cout<<"点云数据:"<push_back(osg::Vec3(cloud->points[i].x , cloud->points[i].y , cloud->points[i].z)) ; color->push_back(osg::Vec4(1.0f , 0.0f , 0.0f , 0.3f)) ; k++ ; } //创建几何体 osg::ref_ptr geometry = new osg::Geometry() ; //设置顶点数组 geometry->setVertexArray(coords.get()) ; geometry->setColorArray(color.get()) ; geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX) ; osg::Vec3Array *normals = new osg::Vec3Array ; normals->push_back(osg::Vec3(0.0f , 1.0f , 0.0f)) ; geometry->setNormalArray(normals) ; geometry->setNormalBinding(osg::Geometry::BIND_OVERALL) ; geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS , 0 , k)) ; //设置关联方式 //添加到叶节点 osg::ref_ptr geode = new osg::Geode() ; geode->addDrawable(geometry.get()) ; root->addChild(geode.get()) ; //优化场景数据 osgUtil::Optimizer optimizer ; optimizer.optimize(root.get()) ; viewer->setSceneData(root.get()) ; viewer->realize() ; viewer->run() ; return 1 ; }