OpenVR+OSG_成功取得VR手柄模型

IVRRenderModels::GetRenderModelName

用這個函數取Steam提供的模型, Steam還提供generic遊戲手柄模型oculus的手柄模型,等...數組

可是, 所跟蹤設備的ID與模型ID並無一一對應, 兩個手柄的模型對應3與4,而HTC VIVE手柄模型的ID函數

這個ID值超過了k_unMaxTrackedDeviceCount , 因此以前一直找不到.ui

在hellovr_opengl例子中不是這樣取的. 沒看清是怎麼取的.spa

代碼:code

// 直接取VR手柄模型:
	if(!m_bController3Ready || !m_bController4Ready)
	{
		vr::RenderModel_t *pModel=nullptr;
		vr::EVRRenderModelError err= m_vrRenderModels->LoadRenderModel_Async("vr_controller_vive_1_5",&pModel);// 一但讀取成功, err的結果爲vr::VRRenderModelError_None
		if(pModel && err!=vr::VRRenderModelError_Loading)
		{
			m_vController3 = pModel;
			m_vController4 = pModel;
			m_bController3Ready=true;
			m_bController4Ready=true;
			cout<<"取模型完畢 vr_controller_vive_1_5"<<endl;
		}
	}

這裏直接用模型的名稱去取兩個手柄共用一個模型orm

 

關於如何把取到的模型畫出來. 目前暫時用多邊型代替, 沒能用網格物體, 這樣是無法貼圖的.遊戲

有去看OSG裏的基本物體是怎麼畫的, 比想像中的難, 因此還沒深刻去看.get

 

繪畫的代碼:it

if(m_bController3Ready && !m_bController3DrawDone){
			osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
			//首先定義四個點:
			osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
			geom->setVertexArray(v.get());
			//v->push_back(osg::Vec3(0005.753f,0011.188f,0050.148f));
			//v->push_back(osg::Vec3(0005.27f,0012.092f,0050.272f));
			//v->push_back(osg::Vec3(0004.707f,0011.718f,0051.579f));

			for(uint32_t i=0;i<m_vController3->unVertexCount;++i){
				v->push_back(osg::Vec3(p1->vPosition.v[0] , p1->vPosition.v[1] , p1->vPosition.v[2] ));
				++p1;
			}

			// 定義顏色數組:
			osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
			geom->setColorArray(c.get());
			geom->setColorBinding(osg::Geometry::BIND_OVERALL);
			c->push_back(osg::Vec4(1.f,1.f,1.f,.5f));
			//c->push_back(osg::Vec4(0.f,1.f,0.f,1.f));
			//c->push_back(osg::Vec4(0.f,0.f,1.f,1.f));
			//c->push_back(osg::Vec4(1.f,1.f,1.f,1.f));

			//定義法線:
			osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
			geom->setNormalArray(n.get());
			geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
			n->push_back(osg::Vec3(0.f,-1.f,0.f));

			cout<<"頂點數:"<<m_vController3->unVertexCount<<endl;
			//設置頂點關聯方式
			geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,m_vController3->unVertexCount));

			//幾何組節點
			osg::ref_ptr<osg::Geode> geode = new osg::Geode;
			geode->addDrawable(geom);

			// 放大一千倍			
			osg::Matrix cc = osg::Matrix::scale(99,99,99);
			osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform;
			trans->setMatrix(cc);		
			trans->addChild(geode);
			//gScene->addChild(trans);
			
			//cout<<"改變場景:"<<endl;
			//viewer->setSceneData(trans);

			osg::Group *gScene = new osg::Group;// 子場景

			cout <<"叄:把手柄放大"<<endl;
			gScene->addChild(trans);
			m_mtController3 = trans;
			viewer->addChild(gScene);
			m_bController3DrawDone=true;
		}/**/
相關文章
相關標籤/搜索