osg点云加载与渲染

目录

效果

laslib 关键代码

完整代码


效果

osg点云加载与渲染_第1张图片

las点云读取使用了laslib这个库。

laslib 关键代码

{
		// 这里演示读取一个 .txt 点云文件

		const char* lasfile = path.c_str();

		std::ifstream ifs;
		ifs.open(lasfile, std::ios::in | std::ios::binary);

		liblas::ReaderFactory f;
		liblas::Reader reader = f.CreateWithStream(ifs);
		liblas::Header const & header = reader.GetHeader();
		int count = header.GetPointRecordsCount();

		int i = 0;
		while (reader.ReadNextPoint() && i < 10)
		{
			liblas::Point const& p = reader.GetPoint();
			double time = p.GetTime();
			double x = p.GetX();
			double y = p.GetY();
			double z = p.GetZ();
			float intensity = p.GetIntensity();
			liblas::Color color = p.GetColor();
			coords->push_back(osg::Vec3(x, y, z));
			colors->push_back(osg::Vec4(color.GetRed(), color.GetGreen(), color.GetBlue(), 1.0f));
			cloudNum++;
		}


		ifs.close();
	}

完整代码

osg::ref_ptr coords = new osg::Vec3Array();
	// 创建颜色
	osg::ref_ptr colors = new osg::Vec4Array();

	/读取点云文件//
	int cloudNum = 0;
	{
		// 这里演示读取一个 .txt 点云文件

		const char* lasfile = path.c_str();

		std::ifstream ifs;
		ifs.open(lasfile, std::ios::in | std::ios::binary);

		liblas::ReaderFactory f;
		liblas::Reader reader = f.CreateWithStream(ifs);
		liblas::Header const & header = reader.GetHeader();
		int count = header.GetPointRecordsCount();

		int i = 0;
		while (reader.ReadNextPoint() && i < 10)
		{
			liblas::Point const& p = reader.GetPoint();
			double time = p.GetTime();
			double x = p.GetX();
			double y = p.GetY();
			double z = p.GetZ();
			float intensity = p.GetIntensity();
			liblas::Color color = p.GetColor();
			coords->push_back(osg::Vec3(x, y, z));
			colors->push_back(osg::Vec4(color.GetRed(), color.GetGreen(), color.GetBlue(), 1.0f));
			cloudNum++;
		}


		ifs.close();
	}
	/读取点云文件//

	//创建几何体
	osg::ref_ptr geometry = new osg::Geometry();
	// 设置顶点数组
	geometry->setVertexArray(coords.get());
	geometry->setColorArray(colors.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, cloudNum));

	// 添加到叶节点
	osg::ref_ptr geode = new osg::Geode();
	osg::ref_ptr root = new osg::Group();
	geode->addDrawable(geometry.get());
	root->addChild(geode.get());

你可能感兴趣的:(osg学习记录,c++)