| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/ArgumentParser> |
|---|
| 20 | #include <osg/Geode> |
|---|
| 21 | #include <osg/Geometry> |
|---|
| 22 | #include <osg/CullFace> |
|---|
| 23 | #include <osg/TriangleIndexFunctor> |
|---|
| 24 | #include <osg/PositionAttitudeTransform> |
|---|
| 25 | #include <osgUtil/SmoothingVisitor> |
|---|
| 26 | #include <osgText/Font3D> |
|---|
| 27 | #include <osgDB/WriteFile> |
|---|
| 28 | #include <osgGA/StateSetManipulator> |
|---|
| 29 | #include <osgUtil/Tessellator> |
|---|
| 30 | #include <osgViewer/Viewer> |
|---|
| 31 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 32 | #include <osg/io_utils> |
|---|
| 33 | |
|---|
| 34 | #include "GlyphGeometry.h" |
|---|
| 35 | #include "TextNode.h" |
|---|
| 36 | |
|---|
| 37 | extern int main_orig(int, char**); |
|---|
| 38 | extern int main_test(int, char**); |
|---|
| 39 | |
|---|
| 40 | int main_experimental(osg::ArgumentParser& arguments) |
|---|
| 41 | { |
|---|
| 42 | std::string fontFile("arial.ttf"); |
|---|
| 43 | while(arguments.read("-f",fontFile)) {} |
|---|
| 44 | |
|---|
| 45 | std::string word("This is a simple test"); |
|---|
| 46 | |
|---|
| 47 | while(arguments.read("--ascii")) |
|---|
| 48 | { |
|---|
| 49 | word.clear(); |
|---|
| 50 | for(unsigned int c=' '; c<=127;++c) |
|---|
| 51 | { |
|---|
| 52 | word.push_back(c); |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | while(arguments.read("-w",word)) {} |
|---|
| 57 | |
|---|
| 58 | osg::ref_ptr<osgText::Font> font = osgText::readFontFile(fontFile); |
|---|
| 59 | if (!font) return 1; |
|---|
| 60 | OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl; |
|---|
| 61 | |
|---|
| 62 | bool useTessellator = true; |
|---|
| 63 | while(arguments.read("-t") || arguments.read("--tessellate")) { useTessellator = true; } |
|---|
| 64 | while(arguments.read("--no-tessellate")) { useTessellator = false; } |
|---|
| 65 | |
|---|
| 66 | float thickness = 5.0; |
|---|
| 67 | while(arguments.read("--thickness",thickness)) {} |
|---|
| 68 | |
|---|
| 69 | float width = 20.0; |
|---|
| 70 | while(arguments.read("--width",width)) {} |
|---|
| 71 | |
|---|
| 72 | float creaseAngle = 30.0f; |
|---|
| 73 | while(arguments.read("--crease-angle",creaseAngle)) {} |
|---|
| 74 | |
|---|
| 75 | OSG_NOTICE<<"creaseAngle="<<creaseAngle<<std::endl; |
|---|
| 76 | |
|---|
| 77 | osgText::BevelProfile profile; |
|---|
| 78 | float ratio = 0.5; |
|---|
| 79 | while(arguments.read("--rounded",ratio)) { profile.roundedBevel(ratio); } |
|---|
| 80 | while(arguments.read("--rounded2",ratio)) { profile.roundedBevel2(ratio); } |
|---|
| 81 | while(arguments.read("--flat",ratio)) { profile.flatBevel(ratio); } |
|---|
| 82 | |
|---|
| 83 | bool outline = false; |
|---|
| 84 | while(arguments.read("--outline")) { outline = true; } |
|---|
| 85 | while(arguments.read("--no-outline")) { outline = false; } |
|---|
| 86 | |
|---|
| 87 | bool smooth = true; |
|---|
| 88 | while(arguments.read("--flat-shaded")) { smooth = false; } |
|---|
| 89 | while(arguments.read("--smooth")) { smooth = false; } |
|---|
| 90 | |
|---|
| 91 | unsigned int numSamples = 10; |
|---|
| 92 | while(arguments.read("--samples", numSamples)) {} |
|---|
| 93 | font->setNumberCurveSamples(numSamples); |
|---|
| 94 | |
|---|
| 95 | profile.print(std::cout); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | osg::ref_ptr<osg::Group> group = new osg::Group; |
|---|
| 99 | osg::Vec3 position; |
|---|
| 100 | |
|---|
| 101 | for(unsigned int i=0; i<word.size(); ++i) |
|---|
| 102 | { |
|---|
| 103 | osg::ref_ptr<osgText::Glyph3D> glyph = font->getGlyph3D(word[i]); |
|---|
| 104 | if (!glyph) return 1; |
|---|
| 105 | |
|---|
| 106 | osg::ref_ptr<osg::PositionAttitudeTransform> transform = new osg::PositionAttitudeTransform; |
|---|
| 107 | transform->setPosition(position); |
|---|
| 108 | transform->setAttitude(osg::Quat(osg::inDegrees(90.0),osg::Vec3d(1.0,0.0,0.0))); |
|---|
| 109 | |
|---|
| 110 | position.x() += glyph->getHorizontalWidth(); |
|---|
| 111 | |
|---|
| 112 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 113 | |
|---|
| 114 | osg::ref_ptr<osg::Geometry> glyphGeometry = osgText::computeGlyphGeometry(glyph.get(), thickness, width); |
|---|
| 115 | osg::ref_ptr<osg::Geometry> textGeometry = osgText::computeTextGeometry(glyphGeometry.get(), profile, width); |
|---|
| 116 | osg::ref_ptr<osg::Geometry> shellGeometry = outline ? osgText::computeShellGeometry(glyphGeometry.get(), profile, width) : 0; |
|---|
| 117 | if (textGeometry.valid()) geode->addDrawable(textGeometry.get()); |
|---|
| 118 | if (shellGeometry.valid()) geode->addDrawable(shellGeometry.get()); |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | if (smooth && textGeometry.valid()) |
|---|
| 122 | { |
|---|
| 123 | osgUtil::SmoothingVisitor::smooth(*textGeometry, osg::DegreesToRadians(creaseAngle)); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | transform->addChild(geode.get()); |
|---|
| 127 | |
|---|
| 128 | group->addChild(transform.get()); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | std::string filename; |
|---|
| 132 | if (arguments.read("-o", filename)) osgDB::writeNodeFile(*group, filename); |
|---|
| 133 | |
|---|
| 134 | osgViewer::Viewer viewer(arguments); |
|---|
| 135 | viewer.setSceneData(group.get()); |
|---|
| 136 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 137 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 138 | return viewer.run(); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | int main(int argc, char** argv) |
|---|
| 142 | { |
|---|
| 143 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 144 | |
|---|
| 145 | if (arguments.read("--test")) |
|---|
| 146 | { |
|---|
| 147 | return main_test(argc,argv); |
|---|
| 148 | } |
|---|
| 149 | else if (arguments.read("--original") || arguments.read("--orig")) |
|---|
| 150 | { |
|---|
| 151 | return main_orig(argc,argv); |
|---|
| 152 | } |
|---|
| 153 | else if (arguments.read("--exp")) |
|---|
| 154 | { |
|---|
| 155 | return main_experimental(arguments); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | osgViewer::Viewer viewer(arguments); |
|---|
| 159 | |
|---|
| 160 | std::string fontFile("arial.ttf"); |
|---|
| 161 | while(arguments.read("-f",fontFile)) {} |
|---|
| 162 | |
|---|
| 163 | osg::ref_ptr<osgText::Font> font = osgText::readFontFile(fontFile); |
|---|
| 164 | if (!font) return 1; |
|---|
| 165 | OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl; |
|---|
| 166 | |
|---|
| 167 | std::string word("This is a new test."); |
|---|
| 168 | while (arguments.read("-w",word)) {} |
|---|
| 169 | |
|---|
| 170 | osg::ref_ptr<osgText::Style> style = new osgText::Style; |
|---|
| 171 | |
|---|
| 172 | float thickness = 0.0f; |
|---|
| 173 | while(arguments.read("--thickness",thickness)) {} |
|---|
| 174 | style->setThicknessRatio(thickness); |
|---|
| 175 | |
|---|
| 176 | osgText::TextNode* text = new osgText::TextNode; |
|---|
| 177 | text->setText(word); |
|---|
| 178 | text->setFont(font.get()); |
|---|
| 179 | text->setStyle(style.get()); |
|---|
| 180 | text->setTextTechnique(new osgText::TextTechnique); |
|---|
| 181 | text->update(); |
|---|
| 182 | |
|---|
| 183 | viewer.setSceneData(text); |
|---|
| 184 | |
|---|
| 185 | return viewer.run(); |
|---|
| 186 | } |
|---|