[osg-users] Osg Text issues

212 views
Skip to first unread message

Dan johansson

unread,
Nov 19, 2019, 4:49:02 AM11/19/19
to osg-...@lists.openscenegraph.org
Hi,

I am creating a simple coordinate system and trying to use the osg text for axis labelling and tick information. This is the settings i use for creating the text



Code:

OsgText2D::OsgText2D() :
textGeode_(new osg::Geode),
osgText_(new osgText::Text)

{
osgText_->setFont("media/fonts/Roboto-Light.ttf");
osgText_->setText("Sphere");

textNode_ = new osg::PositionAttitudeTransform;
textGeode_->setDataVariance(osg::Object::DYNAMIC);
osgText_->setDataVariance(osg::Object::DYNAMIC);
textNode_->addChild(textGeode_);
textNode_->setPosition(osg::Vec3f(0.f,0.f,0.f));

textType_ = GI::TextType::TEXT_3D;

osgText_->setFontResolution(40, 40);
osgText_->setEnableDepthWrites(false);
osgText_->setDrawMode(osgText::Text::TEXT);
textGeode_->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);

osg::DisplaySettings::instance()->setTextShaderTechnique("NO_TEXT_SHADER");
osgText_->setCharacterSize(30);
osgText_->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
osgText_->setAutoRotateToScreen(true);

osgText_->setText("xyz");

textGeode_->getStateSet()->setRenderBinToInherit();

osgText_->setColor(osg::Vec4(0.f,0.f,0.f,1.f));
//osgText_->setBackdropType(osgText::Text::OUTLINE);
//osgText_->setBackdropColor(osg::Vec4(0.0, 0.0, 0.0, 0.0));
osgText_->setBoundingBoxColor(osg::Vec4(0.0,0.0,0.0,0.0));

textGeode_->addDrawable(osgText_);
}




And this is the output i recieve

https://imgur.com/a/MgeXSB4

As you can see the quality is very poor. I've been trying all settings i can find and the only solution i've found is to to activate the backdrop with outline in black, but its very ugly to have a black outline on the text so i would like to solve it better. Any tips on how to improve this?

Thanks in advance

Best regards Dan

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=76914#76914





_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Dan johansson

unread,
Nov 19, 2019, 4:52:19 AM11/19/19
to osg-...@lists.openscenegraph.org
Don't mind the
Code:
textType_ = GI::TextType::TEXT_3D;

line, its a typo. Sorry for that confusion

//Dan

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=76915#76915

Robert Osfield

unread,
Nov 19, 2019, 9:34:07 AM11/19/19
to OpenSceneGraph Users
Hi Dan,

From the screenshot and your tiny segment of code there is no way for us to know what might be amiss.

What happens when you run the osgtext example?

Robert.

Dan johansson

unread,
Nov 20, 2019, 8:35:32 AM11/20/19
to osg-...@lists.openscenegraph.org
Hi Robert

I understand its difficult to help with such little information, unfortunately that is all there is so i cannot provide more code regarding this specific case. The osgtext example looks splendid, i copied and replaced my code with an example there and it now looks much cleaner. I'm still clueless as to what caused the issue. I can add i have built OSG with core profile and disabled the shader pipeline, but i am still wondering what actual renders the text as i haven't added a shader program to it in the way i would with a geode/geometry structure.

Thanks for the reply

Regards Dan

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=76917#76917

Robert Osfield

unread,
Nov 20, 2019, 10:08:46 AM11/20/19
to OpenSceneGraph Users
Hi Dan,

On Wed, 20 Nov 2019 at 14:54, Dan johansson <johans...@hotmail.com> wrote:
I understand its difficult to help with such little information, unfortunately that is all there is so i cannot provide more code regarding this specific case. The osgtext example looks splendid, i copied and replaced my code with an example there and it now looks much cleaner. I'm still clueless as to what caused the issue. I can add i have built OSG with core profile and disabled the shader pipeline, but i am still wondering what actual renders the text as i haven't added a shader program to it in the way i would with a geode/geometry structure.

Are you using OSG from github master or the stable 3.6.x or other release?  If you are using master I'd recommend using the latest 3.6.4 release as it doesn't include the experimental shader pipeline work.

From 3.6.x onwards osgText provides it's own shaders so you don't need to provide them yourself.  I can't provide any guesses as to what is wrong with your original code, the best thing I can do is to recommend comparing the example code to your own to see what differences there are.

Cheers,
Robert.

Dan johansson

unread,
Jan 27, 2020, 10:56:55 AM1/27/20
to OpenSceneGraph Users
Hello, sorry for a late reply. I have digged further into this problem and figured out the source of the issue. I am using 3.6.4. release which was the latest at the time. The problem comes when launching OpenSceneGraph through a QOpenGlWIdget, i have used a simple example from https://vicrucann.github.io/tutorials/qt-osg-navigation/ , i will paste my code below so it is easier to follow(although most is copy pasted)


QtOSGWidget(QWidget* parent = 0)

      : QOpenGLWidget(parent)

        , _mGraphicsWindow(new osgViewer::GraphicsWindowEmbedded( this->x(), this->y(),

                                                                  this->width(), this->height() ) )

        , _mViewer(new osgViewer::Viewer)

      // take care of HDPI screen, e.g. Retina display on Mac

      , m_scale(QApplication::desktop()->devicePixelRatio())

      {

        _mGraphicsWindow->init();



        osg::Group* root = new osg::Group;



        osg::StateSet* rootStateSet = root->getOrCreateStateSet();

        rootStateSet->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON );



        osgText::Text* text = new osgText::Text();

        text->setText("ABRAKADABRA");

        text->setCharacterSize(14);

        text->setCharacterSizeMode(osgText::Text::CharacterSizeMode::SCREEN_COORDS);

        text->setAutoRotateToScreen(true);

        text->setColor(osg::Vec4(0,0,0,1));



        osg::Geode* drawableText = new osg::Geode;

        drawableText->addDrawable(text);

        root->addChild(drawableText);



        osg::Camera* camera = new osg::Camera;

        camera->setViewport( 0, 0, this->width(), this->height() );

        camera->setClearColor( osg::Vec4( 0.9f, 0.9f, 1.f, 1.f ) );

        float aspectRatio = static_cast<float>( this->width()) / static_cast<float>( this->height() );

        camera->setProjectionMatrixAsPerspective( 30.f, aspectRatio, 1.f, 1000.f );

        camera->setGraphicsContext( _mGraphicsWindow );



        _mViewer->setCamera(camera);

        _mViewer->setSceneData(root);

        osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator;

        manipulator->setAllowThrow( false );

        this->setMouseTracking(true);

        _mViewer->setCameraManipulator(manipulator);

        _mViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);

        _mViewer->realize();

        



      }


 virtual void paintGL() {

    _mViewer->frame();

  }


int main(int argc, char** argv)

{

    QSurfaceFormat glFormat;

    glFormat.setVersion(3, 3);

    glFormat.setProfile(QSurfaceFormat::CoreProfile);

    glFormat.setSamples(4); //anti aliasing

    QSurfaceFormat::setDefaultFormat(glFormat);



    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);



    QApplication qapp(argc, argv);

    QMainWindow window;

    QtOSGWidget* widget = new QtOSGWidget(&window);



    window.setCentralWidget(widget);

    window.show();



    return qapp.exec();

}

When running directly through OpenSceneGraph i get the result i want, i post the code for clarity


    QSurfaceFormat glFormat;

    glFormat.setVersion(3, 3);

    glFormat.setProfile(QSurfaceFormat::CoreProfile);

    glFormat.setSamples(4); //anti aliasing

    QSurfaceFormat::setDefaultFormat(glFormat);



    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);



    osg::Group* root = new osg::Group;

    osgViewer::Viewer viewer;



    osg::Camera* camera = new osg::Camera;

    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);

    camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);

    camera->setViewMatrix(osg::Matrix::identity());



    camera->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);

    camera->setClearColor(osg::Vec4(1.0,1.0,1.0,1.0));

    camera->setClearMask(

    GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT

    );



    camera->setCullingMode(osg::CullStack::SMALL_FEATURE_CULLING);

    camera->addChild(root);

    osgText::Text* text = new osgText::Text();

    text->setText("ABRAKADABRA");

    text->setCharacterSize(14);

    text->setCharacterSizeMode(osgText::Text::CharacterSizeMode::SCREEN_COORDS);

    text->setAutoRotateToScreen(true);

    text->setColor(osg::Vec4(0, 0, 0, 1));

    text->setPosition(osg::Vec3(500,500,0));


    osg::Geode* drawableText = new osg::Geode;

    drawableText->addDrawable(text);

    root->addChild(drawableText);

    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);

    viewer.setSceneData(camera);

    viewer.realize();

    return viewer.run();
Ange koden här...

The result can be compared in this image https://imgur.com/a/S7IsZs5. Now i have tried to fiddle with millions of parameters but i cannot understand how to solve this behaviour. Does anybody have an idea what causes it and how to prevent it?

I am relying on integration with Qt in my application so it is a necessity that it works together.
Thanks 

//Regards Dan

Robert Osfield

unread,
Jan 29, 2020, 4:39:00 AM1/29/20
to OpenSceneGraph Users
Hi Dan,

Thanks for all the details.  Seeing a difference between the same scene graph in our Qt based viewer vs OSG suggests that the Qt side is changing OpenGL state, or setting up the graphics context+frame buffers in a different way to the OSG does.

I don't have any Qt expertise so can't help with that side of things, perhaps other OSG/Qt users can help, or perhaps the Qt community can provide guidance on what OpenGL state it sets.  The OSG can't change OpenGL state that it isn't aware of, but if you can tell the OSG about this state via the osg::State::haveApplied*() calls:


       /** Mode has been set externally, update state to reflect this setting.*/
        void haveAppliedMode(StateAttribute::GLMode mode,StateAttribute::GLModeValue value);

        /** Mode has been set externally, therefore dirty the associated mode in osg::State
          * so it is applied on next call to osg::State::apply(..)*/
        void haveAppliedMode(StateAttribute::GLMode mode);

        /** Attribute has been applied externally, update state to reflect this setting.*/
        void haveAppliedAttribute(const StateAttribute* attribute);

        /** Attribute has been applied externally,
          * and therefore this attribute type has been dirtied
          * and will need to be re-applied on next osg::State.apply(..).
          * note, if you have an osg::StateAttribute which you have applied externally
          * then use the have_applied(attribute) method as this will cause the osg::State to
          * track the current state more accurately and enable lazy state updating such
          * that only changed state will be applied.*/
        void haveAppliedAttribute(StateAttribute::Type type, unsigned int member=0);

At a guess I'd suggest it's the blending or texture filter that could amiss.

Robert.

Dan johansson

unread,
Jun 9, 2020, 4:07:32 AM6/9/20
to OpenSceneGraph Users
Hi Robert !

I just want to bump this to tell the issue have been resolved. On the last code snippet i posted you can see i create a new camera, set the graphics context and set the viewers camera to this one. This was what caused the issue and when i instead extracted the associated camera directly from the viewer and set the graphics context it solved the issue. I'm not exactly sure why this is happening, maybe you understand that better then me :) But its definitely the reason causing the bug

//Regards Dan  

OpenSceneGraph Users

unread,
Jun 10, 2020, 1:30:24 PM6/10/20
to OpenSceneGraph Users
Hi Dan,

On Wed, 10 Jun 2020 at 01:37, OpenSceneGraph Users <osg-...@lists.openscenegraph.org> wrote:
I just want to bump this to tell the issue have been resolved. On the last code snippet i posted you can see i create a new camera, set the graphics context and set the viewers camera to this one. This was what caused the issue and when i instead extracted the associated camera directly from the viewer and set the graphics context it solved the issue. I'm not exactly sure why this is happening, maybe you understand that better then me :) But its definitely the reason causing the bug

I vaguely recall something about global state not being set when you create your own osg::StateSet.  Basically a bug fix to the core OSG several years ago meant that global state needed to be set, and normal usage of the viewer this is done, but with custom usage it's possible to miss this step and leave some of the state without a default.  The change to the OSG was required as it was unintentionally overriding global defaults.

Robert.
 
Reply all
Reply to author
Forward
0 new messages