From 8c575c0cea62bb0ab813acffad5bf330a93550d3 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 26 Sep 2017 15:32:41 +0100 Subject: [PATCH] UPdated from OpenSceneGraph-Data with handling of non textured text decoration --- examples/osgfont/osgfont.cpp | 93 +++++++++++++++++++++------ src/osgText/Font.cpp | 10 ++- src/osgText/shaders/text_sdf_frag.cpp | 7 ++ 3 files changed, 90 insertions(+), 20 deletions(-) diff --git a/examples/osgfont/osgfont.cpp b/examples/osgfont/osgfont.cpp index 460b6280d..b063325cf 100644 --- a/examples/osgfont/osgfont.cpp +++ b/examples/osgfont/osgfont.cpp @@ -74,7 +74,7 @@ struct TextSettings glyphTextureFeatures(osgText::GlyphTexture::GREYSCALE), textColor(1.0f, 1.0f, 1.0f, 1.0f), backdropType(osgText::Text::NONE), - backdropOffset(0.04f, 0.04f), + backdropOffset(0.07f, 0.07f), backdropColor(0.0f, 0.0f, 0.0f, 1.0f), scaleFontSizeToFontResolution(false) { @@ -194,10 +194,8 @@ struct TextSettings bool scaleFontSizeToFontResolution; }; -osgText::Text* createLabel(const std::string& l, TextSettings& settings, unsigned int size) +osgText::Text* createLabel(const std::string& l, TextSettings& settings, unsigned int size, osg::Vec3& pos) { - static osg::Vec3 pos(10.0f, 10.0f, 0.0f); - osgText::Text* label = new osgText::Text(); settings.setText(*label); @@ -302,21 +300,52 @@ int main(int argc, char** argv) osg::ref_ptr root = new osg::Group; - bool ortho = args.read("--ortho"); - if (ortho) + bool split_screen = args.read("--split"); + + if (split_screen) { - osg::ref_ptr camera = createOrthoCamera(1280.0f, 1024.0f); - root->addChild(camera.get()); - root = camera; - } - else - { - osg::ref_ptr transform = new osg::MatrixTransform; - transform->setMatrix(osg::Matrixd::rotate(osg::DegreesToRadians(90.0), 1.0, 0.0, 0.0)); - root->addChild(transform.get()); - root = transform; + viewer.realize(); + + // quite an dirty divusion of the master Camera's window if one is assigned. + if (viewer.getCamera()->getGraphicsContext()) + { + viewer.stopThreading(); + + osg::ref_ptr gc = viewer.getCamera()->getGraphicsContext(); + osg::ref_ptr traits = gc->getTraits(); + + // left half + { + osg::ref_ptr camera = new osg::Camera; + camera->setCullMask(0x1); + camera->setGraphicsContext(gc.get()); + camera->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height)); + viewer.addSlave(camera.get(), osg::Matrixd::translate(1.0,0.0,0.0), osg::Matrixd::scale(2.0, 1.0, 1.0)); + } + + { + osg::ref_ptr camera = new osg::Camera; + camera->setCullMask(0x2); + camera->setGraphicsContext(gc.get()); + camera->setViewport(new osg::Viewport(traits->width/2+2,0, traits->width/2, traits->height)); + viewer.addSlave(camera.get(), osg::Matrixd::translate(-1.0,0.0,0.0), osg::Matrixd::scale(2.0, 1.0, 1.0)); + } + + viewer.getCamera()->setGraphicsContext(0); + + viewer.startThreading(); + } + else + { + split_screen = false; + } } + osg::ref_ptr transform = new osg::MatrixTransform; + transform->setMatrix(osg::Matrixd::rotate(osg::DegreesToRadians(90.0), 1.0, 0.0, 0.0)); + root->addChild(transform.get()); + root = transform; + osg::ref_ptr program = new osg::Program; std::string shaderFilename; while(args.read("--shader", shaderFilename)) @@ -363,7 +392,9 @@ int main(int argc, char** argv) settings.sizes.push_back(64); } - osg::Geode* geode = new osg::Geode(); + osg::ref_ptr geode = new osg::Geode(); + + osg::Vec3 pos(0.0f, 0.0f, 0.0f); // Add all of our osgText drawables. for(Sizes::const_iterator i = settings.sizes.begin(); i != settings.sizes.end(); i++) @@ -372,10 +403,34 @@ int main(int argc, char** argv) ss << *i << " 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - geode->addDrawable(createLabel(ss.str(), settings, *i)); + geode->addDrawable(createLabel(ss.str(), settings, *i, pos)); + } + + root->addChild(geode.get()); + + if (split_screen) + { + geode->setNodeMask(0x1); + + osg::ref_ptr right_geode = new osg::Geode; + right_geode->setNodeMask(0x2); + + settings.glyphTextureFeatures = osgText::GlyphTexture::GREYSCALE; + + pos.set(0.0f, 0.0f, 0.0f); + + for(Sizes::const_iterator i = settings.sizes.begin(); i != settings.sizes.end(); i++) + { + std::stringstream ss; + + ss << *i << " 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + right_geode->addDrawable(createLabel(ss.str(), settings, *i, pos)); + } + + root->addChild(right_geode); } - root->addChild(geode); if (!outputFilename.empty()) { diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index c1aa0e077..b8ae6ef1d 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -243,7 +243,7 @@ Font::Font(FontImplementation* implementation): setImplementation(implementation); char *ptr; - if( (ptr = getenv("OSG_MAX_TEXTURE_SIZE")) != 0) + if ((ptr = getenv("OSG_MAX_TEXTURE_SIZE")) != 0) { unsigned int osg_max_size = atoi(ptr); @@ -251,6 +251,14 @@ Font::Font(FontImplementation* implementation): if (osg_max_size<_textureHeightHint) _textureHeightHint = osg_max_size; } + if ((ptr = getenv("OSG_SDF_TEXT")) != 0) + { + _glyphTextureFeatures = GlyphTexture::ALL_FEATURES; + } + else if ((ptr = getenv("OSG_GREYSCALE_TEXT")) != 0) + { + _glyphTextureFeatures = GlyphTexture::GREYSCALE; + } } Font::~Font() diff --git a/src/osgText/shaders/text_sdf_frag.cpp b/src/osgText/shaders/text_sdf_frag.cpp index ea4b675ae..fde9f406d 100644 --- a/src/osgText/shaders/text_sdf_frag.cpp +++ b/src/osgText/shaders/text_sdf_frag.cpp @@ -104,6 +104,13 @@ char text_sdf_frag[] = "$OSG_GLSL_VERSION\n" "\n" "void main(void)\n" "{\n" + " if (texCoord.x<0.0 && texCoord.y<0.0)\n" + " {\n" + " osg_FragColor = vertexColor;\n" + " return;\n" + " }\n" + "\n" + "\n" " float near_fade_away = 2.0;\n" " float far_fade_away = near_fade_away+5.0;\n" "\n"