From 4f881b9a094143dc89941249703564aeb6ef5ab7 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 7 May 2008 13:46:24 +0000 Subject: [PATCH] From Jeremy Moles,"Here's a small example I us to test text rendering in osgWidget; I figured it might be helpful to folks in OSG who need to do the same. " --- examples/CMakeLists.txt | 3 +- examples/osgfont/CMakeLists.txt | 4 + examples/osgfont/osgfont.cpp | 140 ++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 examples/osgfont/CMakeLists.txt create mode 100644 examples/osgfont/osgfont.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e3f6cd6af..ca89a7a9d 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -32,6 +32,7 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osgdepthpeeling) ADD_SUBDIRECTORY(osgdistortion) ADD_SUBDIRECTORY(osgfadetext) + ADD_SUBDIRECTORY(osgfont) ADD_SUBDIRECTORY(osgforest) ADD_SUBDIRECTORY(osgfxbrowser) ADD_SUBDIRECTORY(osggeodemo) @@ -41,7 +42,6 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osghud) ADD_SUBDIRECTORY(osgimpostor) ADD_SUBDIRECTORY(osgintersection) - ADD_SUBDIRECTORY(osgmultiplerendertargets) ADD_SUBDIRECTORY(osgkeyboard) ADD_SUBDIRECTORY(osgkeyboardmouse) ADD_SUBDIRECTORY(osglauncher) @@ -52,6 +52,7 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osgmanipulator) ADD_SUBDIRECTORY(osgmotionblur) ADD_SUBDIRECTORY(osgmovie) + ADD_SUBDIRECTORY(osgmultiplerendertargets) ADD_SUBDIRECTORY(osgmultitexture) ADD_SUBDIRECTORY(osgmultitexturecontrol) ADD_SUBDIRECTORY(osgoccluder) diff --git a/examples/osgfont/CMakeLists.txt b/examples/osgfont/CMakeLists.txt new file mode 100644 index 000000000..28172568a --- /dev/null +++ b/examples/osgfont/CMakeLists.txt @@ -0,0 +1,4 @@ +SET(TARGET_SRC osgfont.cpp) +SET(TARGET_ADDED_LIBRARIES osgText) + +SETUP_EXAMPLE(osgfont) diff --git a/examples/osgfont/osgfont.cpp b/examples/osgfont/osgfont.cpp new file mode 100644 index 000000000..319457ab6 --- /dev/null +++ b/examples/osgfont/osgfont.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include +#include + +void textInfo(osgText::Text* text) +{ + const osgText::Text::TextureGlyphQuadMap& tgqm = text->getTextureGlyphQuadMap(); + + const osgText::Text::TextureGlyphQuadMap::const_iterator tgqmi = tgqm.begin(); + + const osgText::Text::GlyphQuads& gq = tgqmi->second; + + osgText::String& s = text->getText(); + + for(unsigned int i = 0; i < s.size(); i++) + { + osg::Vec2 ul = gq.getCoords()[0 + (i * 4)]; // upperLeft + osg::Vec2 ll = gq.getCoords()[1 + (i * 4)]; // lowerLeft + osg::Vec2 lr = gq.getCoords()[2 + (i * 4)]; // lowerRight + osg::Vec2 ur = gq.getCoords()[3 + (i * 4)]; // upperRight + + /* + osg::Vec3 ul = gq.getTransformedCoords(0)[0 + (i * 4)]; + osg::Vec3 ll = gq.getTransformedCoords(0)[1 + (i * 4)]; + osg::Vec3 lr = gq.getTransformedCoords(0)[2 + (i * 4)]; + osg::Vec3 ur = gq.getTransformedCoords(0)[3 + (i * 4)]; + */ + + osg::notify(osg::NOTICE) + << "'" << static_cast(s[i]) << "':" + << " width(" << lr.x() - ll.x() << ")" + << " height(" << ul.y() - ll.y() << ")" << std::endl << "\t" + << "ul(" << ul << "), " + << "ll(" << ll << "), " + << "lr(" << lr << "), " + << "ur(" << ur << ")" + << std::endl + ; + } +} + +osg::Camera* createOrthoCamera(double width, double height) +{ + osg::Camera* camera = new osg::Camera(); + + camera->getOrCreateStateSet()->setMode( + GL_LIGHTING, + osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF + ); + + osg::Matrix m = osg::Matrix::ortho2D(0.0f, width, 0.0f, height); + + camera->setProjectionMatrix(m); + camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); + camera->setViewMatrix(osg::Matrix::identity()); + camera->setClearMask(GL_DEPTH_BUFFER_BIT); + camera->setRenderOrder(osg::Camera::POST_RENDER); + + return camera; +} + +osgText::Text* createLabel(const std::string& l, const char* f, unsigned int size) +{ + static osg::Vec3 pos(10.0f, 10.0f, 0.0f); + + osgText::Text* label = new osgText::Text(); + osgText::Font* font = osgText::readFontFile(f); + + label->setFont(font); + label->setCharacterSize(size); + label->setFontResolution(size, size); + label->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)); + label->setPosition(pos); + label->setAlignment(osgText::Text::LEFT_BOTTOM); + + // It seems to be important we do this last to get best results? + label->setText(l); + + textInfo(label); + + pos.y() += size + 10.0f; + + return label; +} + +typedef std::list Sizes; + +int main(int argc, char** argv) +{ + osgViewer::Viewer viewer; + osg::ArgumentParser args(&argc, argv); + + // Make sure we have the minimum args... + if(argc <= 2) + { + osg::notify(osg::FATAL) << "usage: " << args[0] << " FONTFILE [sizes...]" << std::endl; + + return 1; + } + + viewer.addEventHandler(new osgViewer::StatsHandler()); + viewer.addEventHandler(new osgViewer::WindowSizeHandler()); + + osg::Group* group = new osg::Group(); + osg::Camera* camera = createOrthoCamera(1280.0f, 1024.0f); + + // Create the list of desired sizes. + Sizes sizes; + + for(int i = 2; i < argc; i++) + { + if(!args.isNumber(i)) continue; + + sizes.push_back(std::atoi(args[i])); + } + + osg::Geode* geode = new osg::Geode(); + + // Add all of our osgText drawables. + for(Sizes::const_iterator i = sizes.begin(); i != sizes.end(); i++) + { + std::stringstream ss; + + ss << *i << " 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + geode->addDrawable(createLabel(ss.str(), args[1], *i)); + } + + camera->addChild(geode); + + group->addChild(camera); + + viewer.setSceneData(group); + + return viewer.run(); +}