2007-12-10 23:15:56 +08:00
|
|
|
/* OpenSceneGraph example, osgtext.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2010-07-15 02:50:41 +08:00
|
|
|
#include <osg/ArgumentParser>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Geometry>
|
2010-08-24 22:22:58 +08:00
|
|
|
#include <osg/CullFace>
|
2010-08-20 00:24:08 +08:00
|
|
|
#include <osg/TriangleIndexFunctor>
|
2010-07-15 02:50:41 +08:00
|
|
|
#include <osg/PositionAttitudeTransform>
|
2010-07-26 19:12:45 +08:00
|
|
|
#include <osgUtil/SmoothingVisitor>
|
2010-07-15 02:50:41 +08:00
|
|
|
#include <osgText/Font3D>
|
|
|
|
#include <osgDB/WriteFile>
|
|
|
|
#include <osgGA/StateSetManipulator>
|
|
|
|
#include <osgUtil/Tessellator>
|
2007-12-10 23:15:56 +08:00
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2010-07-15 02:50:41 +08:00
|
|
|
#include <osg/io_utils>
|
2007-12-10 23:15:56 +08:00
|
|
|
|
2010-08-24 22:22:58 +08:00
|
|
|
#include "GlyphGeometry.h"
|
|
|
|
|
2010-07-15 02:50:41 +08:00
|
|
|
extern int main_orig(int, char**);
|
|
|
|
extern int main_test(int, char**);
|
2007-12-10 23:15:56 +08:00
|
|
|
|
2010-07-15 02:50:41 +08:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
|
|
|
|
if (arguments.read("--test"))
|
|
|
|
{
|
|
|
|
return main_test(argc,argv);
|
|
|
|
}
|
|
|
|
else if (arguments.read("--original") || arguments.read("--orig"))
|
|
|
|
{
|
|
|
|
return main_orig(argc,argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string fontFile("arial.ttf");
|
|
|
|
while(arguments.read("-f",fontFile)) {}
|
|
|
|
|
|
|
|
std::string word("This is a simple test");
|
2010-07-20 18:46:27 +08:00
|
|
|
|
|
|
|
while(arguments.read("--ascii"))
|
|
|
|
{
|
|
|
|
word.clear();
|
|
|
|
for(unsigned int c=' '; c<=127;++c)
|
|
|
|
{
|
|
|
|
word.push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-15 02:50:41 +08:00
|
|
|
while(arguments.read("-w",word)) {}
|
|
|
|
|
|
|
|
osg::ref_ptr<osgText::Font3D> font = osgText::readFont3DFile(fontFile);
|
|
|
|
if (!font) return 1;
|
|
|
|
OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl;
|
|
|
|
|
2010-07-31 03:39:38 +08:00
|
|
|
bool useTessellator = true;
|
2010-07-15 02:50:41 +08:00
|
|
|
while(arguments.read("-t") || arguments.read("--tessellate")) { useTessellator = true; }
|
2010-07-31 03:39:38 +08:00
|
|
|
while(arguments.read("--no-tessellate")) { useTessellator = false; }
|
2010-07-15 02:50:41 +08:00
|
|
|
|
2010-07-20 04:34:15 +08:00
|
|
|
float thickness = 5.0;
|
|
|
|
while(arguments.read("--thickness",thickness)) {}
|
|
|
|
|
2010-08-20 00:24:08 +08:00
|
|
|
float width = 20.0;
|
|
|
|
while(arguments.read("--width",width)) {}
|
|
|
|
|
2010-08-25 00:08:50 +08:00
|
|
|
float creaseAngle = 30.0f;
|
2010-07-31 03:39:38 +08:00
|
|
|
while(arguments.read("--crease-angle",creaseAngle)) {}
|
|
|
|
|
|
|
|
OSG_NOTICE<<"creaseAngle="<<creaseAngle<<std::endl;
|
|
|
|
|
2010-08-24 22:22:58 +08:00
|
|
|
osgText::BevelProfile profile;
|
2010-08-20 00:24:08 +08:00
|
|
|
float ratio = 0.5;
|
|
|
|
while(arguments.read("--rounded",ratio)) { profile.roundedBevel(ratio); }
|
2010-08-24 22:22:58 +08:00
|
|
|
while(arguments.read("--rounded2",ratio)) { profile.roundedBevel2(ratio); }
|
2010-08-20 00:24:08 +08:00
|
|
|
while(arguments.read("--flat",ratio)) { profile.flatBevel(ratio); }
|
|
|
|
|
2010-08-25 19:07:30 +08:00
|
|
|
bool outline = false;
|
|
|
|
while(arguments.read("--outline")) { outline = true; }
|
|
|
|
while(arguments.read("--no-outline")) { outline = false; }
|
|
|
|
|
|
|
|
bool smooth = true;
|
2010-08-25 22:34:08 +08:00
|
|
|
while(arguments.read("--flat-shaded")) { smooth = false; }
|
2010-08-25 19:07:30 +08:00
|
|
|
while(arguments.read("--smooth")) { smooth = false; }
|
|
|
|
|
|
|
|
unsigned int numSamples = 10;
|
|
|
|
while(arguments.read("--samples", numSamples)) {}
|
|
|
|
font->setNumberCurveSamples(numSamples);
|
|
|
|
|
2010-08-20 00:24:08 +08:00
|
|
|
profile.print(std::cout);
|
|
|
|
|
|
|
|
|
2010-07-15 02:50:41 +08:00
|
|
|
osg::ref_ptr<osg::Group> group = new osg::Group;
|
|
|
|
osg::Vec3 position;
|
|
|
|
|
|
|
|
for(unsigned int i=0; i<word.size(); ++i)
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osgText::Font3D::Glyph3D> glyph = font->getGlyph(word[i]);
|
|
|
|
if (!glyph) return 1;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::PositionAttitudeTransform> transform = new osg::PositionAttitudeTransform;
|
|
|
|
transform->setPosition(position);
|
|
|
|
transform->setAttitude(osg::Quat(osg::inDegrees(90.0),osg::Vec3d(1.0,0.0,0.0)));
|
|
|
|
|
|
|
|
position.x() += glyph->getHorizontalWidth();
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
|
|
|
|
|
2010-08-24 22:22:58 +08:00
|
|
|
osg::ref_ptr<osg::Geometry> glyphGeometry = osgText::computeGlyphGeometry(glyph.get(), thickness, width);
|
|
|
|
osg::ref_ptr<osg::Geometry> textGeometry = osgText::computeTextGeometry(glyphGeometry.get(), profile, width);
|
2010-08-25 19:07:30 +08:00
|
|
|
osg::ref_ptr<osg::Geometry> shellGeometry = outline ? osgText::computeShellGeometry(glyphGeometry.get(), profile, width) : 0;
|
2010-08-25 00:08:50 +08:00
|
|
|
if (textGeometry.valid()) geode->addDrawable(textGeometry.get());
|
|
|
|
if (shellGeometry.valid()) geode->addDrawable(shellGeometry.get());
|
|
|
|
|
|
|
|
// create the normals
|
2010-08-25 19:07:30 +08:00
|
|
|
if (smooth && textGeometry.valid())
|
2010-08-24 22:22:58 +08:00
|
|
|
{
|
2010-08-25 19:07:30 +08:00
|
|
|
osgUtil::SmoothingVisitor::smooth(*textGeometry, osg::DegreesToRadians(creaseAngle));
|
2010-08-24 22:22:58 +08:00
|
|
|
}
|
2010-07-15 02:50:41 +08:00
|
|
|
|
|
|
|
transform->addChild(geode.get());
|
|
|
|
|
|
|
|
group->addChild(transform.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string filename;
|
|
|
|
if (arguments.read("-o", filename)) osgDB::writeNodeFile(*group, filename);
|
2007-12-10 23:15:56 +08:00
|
|
|
|
2010-07-15 02:50:41 +08:00
|
|
|
osgViewer::Viewer viewer(arguments);
|
|
|
|
viewer.setSceneData(group.get());
|
|
|
|
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
|
2010-08-25 19:07:30 +08:00
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler);
|
2010-07-15 02:50:41 +08:00
|
|
|
return viewer.run();
|
2010-07-26 19:12:45 +08:00
|
|
|
}
|