标签:
1 osg::Vec3 center; // osg中的坐标是三维的,所以我们需要定义一个三维向量center用来存储获得的坐标 2 int NodeNum; 3 center = mRoot->getChild(NodeNum)->computeBound()._center; //获得鼠标点击模型的包围盒的中心处坐标,NodeNum是我加载模型的时候模型在节点中的序号
osg::Vec3 wordMark = mRoot->getChild(NodeNum)->getBound().center()*osg::computeLocalToWorld(mGroup->getChild(NodeNum)->getParentalNodePaths()[0]);//获得节点的世界坐标
1 void CreatText(osgText::Text& textObject,osgText::Font* font,float size,const osg::Vec3& pos ) 2 { 3 textObject.setFont(font);// 读入字体 4 textObject.setCharacterSize(size);//字体大小 5 textObject.setPosition(pos); 6 textObject.setColor(osg::Vec4(0.0,1.0,0.0,1.0)); 7 textObject.setAlignment(osgText::Text::LEFT_BOTTOM);//文字对齐方向 8 textObject.setAutoRotateToScreen(true);//跟随视角不断变化,但离物体越远,文字越小 9 textObject.setAxisAlignment(osgText::Text::XZ_PLANE);//获取文字对称成方式 10 11 }//在这个函数中也可以根据自己的需要使用text的其他方法,如文字描边:textObject.setBackdropType(),给文字加上边框:textObject.setDrawMode()等 12 13 void createContent(osgText::Text& textObject,const char* string) 14 { 15 int requiredSize=mbstowcs(NULL,string,0); //如果mbstowcs第一参数为NULL那么返回字符串的数目 16 wchar_t* wText=new wchar_t[requiredSize+1]; 17 mbstowcs(wText,string,requiredSize+1) ;//由char转换成wchar类型 18 textObject.setText(wText); 19 delete wText; 20 }
1 const char* textString={ 2 "我选中的节点是这个;\n" 3 "先画一个文字面板;\n" 4 "再添加文字;\n" 5 "然后把文字和面板都添加到geode中;\n" 6 "最后把geode加载到mRoot中" 7 }; 8 const char* titleString={ 9 "文字面板" 10 }; 11 osgText::Font* fontHei = osgText::readFontFile("simhei.ttf"); //读入字体 12 osg::ref_ptr<osgText::Text> text = new osgText::Text; //新建一个text对象 13 CreatText(*text,fontHei,350.0,wordMark); //调用CreatText()方法,创建一个文字对象,wordMark就是我们上步计算的选择节点的世界坐标 14 createContent(*text,textString); 15 16 osg::ref_ptr<osgText::Text> title = new osgText::Text; 17 setupProperties(*title,fontHei,350.0,osg::Vec3(wordMark.x()+1100,wordMark.y()-2508,wordMark.z()+1600)); 18 createContent(*title,titleString); 19 title->setColor(osg::Vec4(1.0,0.0,0.0,1.0)); 20 21 osg::ref_ptr<osg::Geode> geode3 = new osg::Geode; //创建一个geode对象,用来加载text和Gemotery对象 22 //给文字添加底板 23 osg::ref_ptr<osg::Geometry> geo = new osg::Geometry; //创建一个Gemotery对象 24 osg::Vec4Array* colorArray = new osg::Vec4Array; //创建一个Vec4Array对象,存放颜色信息 25 geo = osg::createTexturedQuadGeometry(osg::Vec3(wordMark.x(),wordMark.y()+6,wordMark.z()+1400),osg::Vec3(4500,0.0,0.0),osg::Vec3(0.0,0.0,200),1.0,1.0); //画一个小矩形,用来摆放title,后面会对createTexturedQuadGeometry里面的参数做介绍 26 colorArray->push_back(osg::Vec4(0.0,1.0,0.0,1.0)); //给colorArray赋值,0101是绿色 27 geo->setColorArray(colorArray); //给画的矩形设置颜色 28 geode3->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(wordMark.x(),wordMark.y()+8,wordMark.z()-130.0),osg::Vec3(4500,0.0,0.0),osg::Vec3(0.0,0.0,1600),1.0,1.0)); //再画一个大矩形,用来放其它文字信息,并且把矩形加进geode对象中 29 geode3->addDrawable(geo.get()); //把小矩形也加载进geode 30 geode3->addDrawable(text.get()); 31 geode3->addDrawable(title.get()); 32 mRoot->addChild(geode3); //把文字面板加进mRoot中
widthVec=osg::Vec3(num1,0,0); //这个num1就是矩形的长 heightVec=osg::Vec3(0,0,num2); //这个num2就是矩形的高
colarray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0)); //Red colarray->push_back(osg::Vec4(0.0, 1.0, 0.0, 1.0)); //Green colarray->push_back(osg::Vec4(0.0, 0.0, 1.0, 1.0)); //Blue colarray->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0)); //White
标签:
原文地址:http://www.cnblogs.com/xiaocainiao2hao/p/4544312.html