osg 在场景中绘制坐标轴(xyz)

Posted herd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了osg 在场景中绘制坐标轴(xyz)相关的知识,希望对你有一定的参考价值。

//x y z font_size
osg::Geode* makeCoordinate(float a_x,float a_y,float a_z,float font_size)

    osg::ref_ptr<osg::Sphere> pSphereShape = new osg::Sphere(osg::Vec3(0, 0, 0), 1.0f);
    osg::ref_ptr<osg::ShapeDrawable> pShapeDrawable = new osg::ShapeDrawable(pSphereShape.get());
    pShapeDrawable->setColor(osg::Vec4(0.0, 0.0, 0.0, 1.0));

    //创建保存几何信息的对象
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();

    //创建四个顶点
    osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array();
    v->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
    v->push_back(osg::Vec3(a_x, 0.0f, 0.0f));
    
    v->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
    v->push_back(osg::Vec3(0.0f, a_y, 0.0f));
    v->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
    
    v->push_back(osg::Vec3(0.0f, 0.0f, a_z));
    geom->setVertexArray(v.get());


    osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array();
    c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)); 
    c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)); 
    
    c->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)); 
    c->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)); 
    c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); 
    
    c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); 
    geom->setColorArray(c.get());
    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);


    //xyz
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2));
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 2, 2));
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 4, 2));


    osg::ref_ptr<osgText::Text> pTextXAuxis1 = new osgText::Text;
    pTextXAuxis1->setText(L"X");
    pTextXAuxis1->setFont("Fonts/simhei.ttf");
    
    pTextXAuxis1->setAxisAlignment(osgText::Text::SCREEN);
    pTextXAuxis1->setCharacterSize(font_size);
    pTextXAuxis1->setPosition(osg::Vec3(a_x, 0.0f, 0.0f));

    osg::ref_ptr<osgText::Text> pTextYAuxis1 = new osgText::Text;
    pTextYAuxis1->setText(L"Y");
    pTextYAuxis1->setFont("Fonts/simhei.ttf");
    
    pTextYAuxis1->setAxisAlignment(osgText::Text::SCREEN);
    pTextYAuxis1->setCharacterSize(font_size);
    pTextYAuxis1->setPosition(osg::Vec3(0.0f, a_y, 0.0f));

    osg::ref_ptr<osgText::Text> pTextZAuxis1 = new osgText::Text;
    pTextZAuxis1->setText(L"Z");
    pTextZAuxis1->setFont("Fonts/simhei.ttf");
    
    pTextZAuxis1->setAxisAlignment(osgText::Text::SCREEN);
    pTextZAuxis1->setCharacterSize(font_size);
    pTextZAuxis1->setPosition(osg::Vec3(0.0f, 0.0f, a_z));

    osg::ref_ptr<osg::Geode> geode = new osg::Geode();
    geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    geode->getOrCreateStateSet()->setAttribute(new osg::LineWidth(3.0), osg::StateAttribute::ON);

    geode->addDrawable(pShapeDrawable.get());
    geode->addDrawable(geom.get());
    geode->addDrawable(pTextXAuxis1.get());
    
    geode->addDrawable(pTextYAuxis1.get());
    geode->addDrawable(pTextZAuxis1.get());

    return geode.release();


 

效果图片:

 

技术图片

 

 

技术图片

参考:https://blog.csdn.net/sun222555888/article/details/52083413

 

以上是关于osg 在场景中绘制坐标轴(xyz)的主要内容,如果未能解决你的问题,请参考以下文章

OSG坐标系统

OpenGL的视图变换与OSG漫游器

[osg]OSG相机添加动画路径

OpenGL坐标转换场景到世界

OSG世界坐标转屏幕坐标(转载)

C++学习(三二九)osg::Geometry绘制到窗口的过程