|
一直在网上寻找相关信息,发现都是推荐用更新回调来写这个最好,于是写了个很简单的,但总是报错,希望各位大神能指点一下,基础确实薄弱,求指教~
Code:
(1)更新回调部分
class LabelCallBack: public osg::NodeCallback
{
public:
LabelCallBack(){}
virtual void operator()(osg::Node* text, osg::NodeVisitor* nv)
{
float q=_start.x()*1.05;
float w=_start.y()*1.05;
float e=_start.z()*1.15;
float _value=sqrt( (tempPoint.x()-_start.x())*(tempPoint.x()-_start.x())+(tempPoint.y()-_start.y())*(tempPoint.y()-_start.y()) + (tempPoint.z()-_start.z())*(tempPoint.z()-_start.z()) );
text = createSceneGraph(q,w,e,_value);
traverse(text, nv);
}
};
(2)handle()中的“case (osgGA::GUIEventAdapter::MOVE)”下:
osg::ref_ptr<osgUtil:ineSegmentIntersector> intersector =new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
osgUtil::IntersectionVisitor iv( intersector.get() );
viewer->getCamera()->accept( iv );
if ( intersector->containsIntersections() )
{
osgUtil::LineSegmentIntersector::Intersection result = *(intersector->getIntersections().begin());
tempPoint=result.getWorldIntersectPoint();
osg::Vec3Array * vertexArray=new osg::Vec3Array(2);
(*vertexArray)[0]=_start;
(*vertexArray)[1]= tempPoint;
pGeometry->setVertexArray(vertexArray);
viewer->getSceneData()->asGroup()->addUpdateCallback(new LabelCallBack());
}
break;
(3)标注部分
osg::ref_ptr<osg::Node> createSceneGraph(float a,float b, float c,float _value)
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osgText::Font> font = osgText::readFontFile( "fonts/arial.ttf" );
osg::ref_ptr<osgText::Text> text=new osgText::Text;
osg::Vec4 white( 1.f, 1.f, 1.f, 1.f );
{
char txt[30];
sprintf(txt,"%.2f",_value);
string sText = std::string(txt);
text->setFont( font.get() );
text->setColor( white );
text->setCharacterSize( 25.f );
text->setPosition( osg::Vec3( a, b, c ) );
text->setAxisAlignment( osgText::Text::SCREEN );
text->setText(sText);//设置显示的文字
text->setDataVariance(osg::Object:YNAMIC);//
geode->addDrawable( text.get() );
}
return geode.get();
}
注:代码中唯一涉及的两个变量_start和tempPoint,我将它们放到了整个代码的开头,作为全局。
操作流程:
(1)点击地面后,鼠标移动,实时绘出一条线段,起终点为_start和tempPoint
(2)绘画的同时,会有线段的长度标注(osgText),实时更新
问题:
当我点击完一个点后,只要移动一点儿,立马弹出错误
点击“继续”,始终是这几个地方
|
|