您好,登錄后才能下訂單哦!
這篇文章主要介紹“OpenSceneGraph如何導(dǎo)出三角形數(shù)據(jù)”,在日常操作中,相信很多人在OpenSceneGraph如何導(dǎo)出三角形數(shù)據(jù)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”O(jiān)penSceneGraph如何導(dǎo)出三角形數(shù)據(jù)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
在OpenSceneGraph開發(fā)中,為了方便會(huì)經(jīng)常使用到一些不是三角形片的數(shù)據(jù),比如四邊形等數(shù)據(jù)。例如畫一個(gè)管子用四邊形帶比用三角形片好計(jì)算得多。比如現(xiàn)在我們要畫一個(gè)由兩個(gè)平面組成的面,我可以這樣做:
osg::Geode* geode=new osg::Geode; osg::Geometry* polyGeom = new osg::Geometry; osg::Vec3 myCoords[]= { osg::Vec3(0,1,0), osg::Vec3(0,0,0), osg::Vec3(1,1,0), osg::Vec3(1,0,0), osg::Vec3(2,1,0), osg::Vec3(2,0,0) }; int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); polyGeom->setVertexArray(vertices); polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,numCoords)); geode->addDrawable(polyGeom);
這樣就用6個(gè)點(diǎn),用OpenGL提供的QUAD_STRIP方式畫出了兩個(gè)平面。 但是如果要把這個(gè)平面用于碰撞檢測(cè)等技術(shù),那么就需要把這六個(gè)點(diǎn)所表示的四邊形帶轉(zhuǎn)換成三角形片才行。這些三角形定點(diǎn)如下:
0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 1 0 1 0 0 2 1 0 1 0 0 2 0 0 2 1 0
可以看出兩個(gè)平面由4個(gè)三角形組成,而且都是逆時(shí)針排列(朝向一致)。 以前我自己做過(guò)轉(zhuǎn)換,但是感覺(jué)很麻煩。OpenSceneGraph的Example osggeometry中提供了一個(gè)printTriangles函數(shù),它可以打印出一個(gè)drawable所有的三角形片,不管最初的數(shù)據(jù)結(jié)構(gòu)如何:
struct NormalPrint { void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool) const { osg::Vec3 normal = (v2-v1)^(v3-v2); normal.normalize(); std::cout << "t("<<<") ("<<<") ("<<<") "<<") normal ("<<<")"< } }; // decompose Drawable primtives into triangles, print out these triangles and computed normals. void printTriangles(const std::string& name, osg::Drawable& drawable) { std::cout<< osg::TriangleFunctor tf; drawable.accept(tf); std::cout< }
核心的思想就是利用osg::TriangleFunctor這個(gè)模版。這個(gè)模版會(huì)讓你重載()運(yùn)算符,然后讓Drawable去visit它。在這個(gè)過(guò)程中,所有原始的數(shù)據(jù)(不管是三角形片的,還是四邊形的)都轉(zhuǎn)換成了三角形片數(shù)據(jù)。
那么如何把三角形數(shù)據(jù)導(dǎo)出哪?只需要修改一下借助這個(gè)思路,將NormalPrint修改成我們需要的就對(duì)了。
struct GetVertex { void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool) const { vertexList->push_back(v1); vertexList->push_back(v2); vertexList->push_back(v3); } osg::Vec3Array* vertexList; }; void getTriangles(osg::Drawable& drawable) { osg::TriangleFunctor tf; tf.vertexList=new osg::Vec3Array; drawable.accept(tf); for(osg::Vec3Array::iterator itr=tf.vertexList->begin(); itr!=tf.vertexList->end(); itr++) { osg::Vec3 vertex=*itr; std::cout<< } std::cout< }
以下是完整的示例文件:
// PrimitiveSet.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 // #include "stdafx.h" #include struct GetVertex { void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool) const { vertexList->push_back(v1); vertexList->push_back(v2); vertexList->push_back(v3); } osg::Vec3Array* vertexList; }; void getTriangles(osg::Drawable& drawable) { osg::TriangleFunctor tf; tf.vertexList=new osg::Vec3Array; drawable.accept(tf); for(osg::Vec3Array::iterator itr=tf.vertexList->begin(); itr!=tf.vertexList->end(); itr++) { osg::Vec3 vertex=*itr; std::cout<< } std::cout< } osg::Node* createGeode() { osg::Geode* geode=new osg::Geode; osg::Geometry* polyGeom = new osg::Geometry; osg::Vec3 myCoords[]= { osg::Vec3(0,1,0), osg::Vec3(0,0,0), osg::Vec3(1,1,0), osg::Vec3(1,0,0), osg::Vec3(2,1,0), osg::Vec3(2,0,0) }; int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); polyGeom->setVertexArray(vertices); polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,numCoords)); geode->addDrawable(polyGeom); getTriangles(*polyGeom); return geode; } int _tmain(int argc, _TCHAR* argv[]) { //Set up viewer osgViewer::Viewer viewer; osg::ref_ptr traits=new osg::GraphicsContext::Traits; traits->x=200; traits->y=200; traits->width=800; traits->height=600; traits->windowDecoration=true; traits->doubleBuffer=true; traits->sharedContext=0; osg::ref_ptr gc=osg::GraphicsContext::createGraphicsContext(traits.get()); osg::ref_ptr camera=new osg::Camera; //osg::Camera camera=new osg::Camera; camera->setGraphicsContext(gc.get()); camera->setViewport(new osg::Viewport(0,0,traits->width,traits->height)); camera->setDrawBuffer(GL_BACK); camera->setReadBuffer(GL_BACK); osgGA::TrackballManipulator* tm=new osgGA::TrackballManipulator; viewer.setCameraManipulator(tm); viewer.addSlave(camera.get()); //Set up root node osg::ref_ptr root=new osg::Group; root->addChild(createGeode()); //Start show! viewer.setSceneData(root.get()); viewer.realize(); while(!viewer.done()) { viewer.frame(); } }
到此,關(guān)于“OpenSceneGraph如何導(dǎo)出三角形數(shù)據(jù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。