osg之顯示點雲


            osg雖然不是很火,但是對於非計算機專業的人來說確實是一個神器,osg其實也是來自於opengl,它可以理解為一個高度封裝的opengl圖像庫,由於其沒有太多太多的技術門檻以及擴展性不高,導致其市場一直不溫不火,但是其封裝的LOD技術,多線程技術以及顯示還是為急需做平台又不想投入大量時間的人提供了便利。博主就是屬於其中之一,由於點雲數據越來越大,如今數據點不超過1個億都不好意思稱作大數據。由於opengl的LOD技術國內在這方面保密甚為嚴格,當然各行各業都是這樣,這也是國內技術一直遠遠落后於國外的原因之一吧!其實很多工具真的很有用,但是苦於沒有入門教程,所以只能望洋興嘆,這也是國內編程技術的現狀之一吧!目前博主也是盡自己的一點綿薄之力把一些有用的入門教程奉獻給大家,希望能給那些還在點雲顯示道路上掙扎的人一點點幫助!同時也呼吁大家能把一些並沒有什么核心競爭力的編程知識共享一下,因為每一篇有用的博客都有可能開啟一個人的編程之路,廢話不多說,直接上代碼,保證直接運行可用。

          bool rgbIsInt=false;

	/*osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
	viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));*/
	osg::ref_ptr<osg::Group> root = new osg::Group();
	
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
	{
		osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
		traits->x = 40;
		traits->y = 40;
		traits->width = 600;
		traits->height = 480;
		traits->windowDecoration = true;
		traits->doubleBuffer = true;
		traits->sharedContext = 0;


		osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());

		osg::ref_ptr<osg::Camera> camera = new osg::Camera;
		camera->setGraphicsContext(gc.get());
		camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
		GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
		camera->setDrawBuffer(buffer);
		camera->setReadBuffer(buffer);

		// add this slave camera to the viewer, with a shift left of the projection matrix
		viewer->addSlave(camera.get());
	}

	//創建頂點數組
	osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array();
	osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array();

	
	//快速讀取文件
	string filePath = "整數.txt";//mcolor_小數  ZSWZ2015111
	FILE* pfData = fopen(filePath.c_str(), "r");

	ifstream fileIn(filePath, ios::in);//讀取文件
	string line;//讀取了每一行的字符

	getline(fileIn, line);//獲取第一行
	stringstream sstr(line);//給他轉一下
	string token;

	int lineInNum=0;
	while (getline(sstr, token, ' '))
	{
		lineInNum ++ ;
		if (lineInNum >= 3)
		{
			if (stof(token)> 1)
			{
				rgbIsInt = true;
			}
	
		}
		
	}

	
	int num = 0;//數據點數量
	if (pfData == NULL)
	{
		cout << "DataFile does not exist!!!" << endl;
		return NULL;
	}
	else
	{
		if (rgbIsInt == false)//讀小數
		{ 
			while (!feof(pfData))
			{
				float fx, fy, fz, n, m, k;
				float fa, fb, fc;
				fscanf(pfData, "%f", &fx);
				fscanf(pfData, "%f", &fy);
				fscanf(pfData, "%f", &fz);
				fscanf(pfData, "%f", &fa);
				fscanf(pfData, "%f", &fb);
				fscanf(pfData, "%f", &fc);
				coords->push_back(osg::Vec3(fx, fy, fz));
				color->push_back(osg::Vec4(fa, fb, fc, 1.0f));
				num++;
			}
		}
		else//讀整數
		{
			while (!feof(pfData))
			{
				float fx, fy, fz;
				int fa, fb, fc;
				fscanf(pfData, "%f", &fx);
				fscanf(pfData, "%f", &fy);
				fscanf(pfData, "%f", &fz);
				fscanf(pfData, "%d", &fa);
				fscanf(pfData, "%d", &fb);
				fscanf(pfData, "%d", &fc);
				float aa = (float)fa / 255.00;
				float bb = (float)fb / 255.00;
				float cc = (float)fc / 255.00;
				coords->push_back(osg::Vec3(fx, fy, fz));
				color->push_back(osg::Vec4(aa, bb, cc, 1.0f));
				num++;
			}
		}
		fclose(pfData);
	}

	//創建幾何體
	osg::ref_ptr<osg::Geometry> 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, num)); //設置關聯方式

	//添加到葉節點
	osg::ref_ptr<osg::Geode> geode = new osg::Geode();
	geode->addDrawable(geometry.get());
	root->addChild(geode.get());

	root->getOrCreateStateSet()->setMode(GL_LIGHTING, StateAttribute::OFF | StateAttribute::OVERRIDE);
	
	//優化場景數據
	osgUtil::Optimizer optimizer;
	optimizer.optimize(root.get());
	viewer->setSceneData(root.get());

	viewer->realize();
	viewer->run();
	return viewer->run();

  這是運行效果:

                                                                                     

            后期會把osg嵌入到Qt的方法貢獻給大家,由於目前博主也在學習階段,感覺還是控制台學習較為方便。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM