|
楼主 |
发表于 2011-7-8 15:25:27
|
显示全部楼层
- #include <osg/ref_ptr>
- #include <osg/Notify>
- #include <osg/Geode>
- #include <osg/Geometry>
- #include <osg/Group>
- #include <osgText/Font>
- #include <osgText/Text>
- #include <osgDB/Registry>
- #include <osgDB/WriteFile>
- #include <iostream>
- using std::endl;
- osg::ref_ptr<osg::Group> createSceneGraph()
- {
- // Create the root (and only) node.
- osg::ref_ptr<osg::Group> root = new osg::Group();
- osg::ref_ptr<osgText::Font> font = osgText::readFontFile( "fonts/arial.ttf" );
- osg::Vec4 white( 1.f, 1.f, 1.f, 1.f );
- osg::ref_ptr<osg::Geode> geode1 = new osg::Geode();
- {
- osg::ref_ptr<osgText::Text> text = new osgText::Text;
- text->setFont( font.get() );
- text->setColor( white );
- text->setCharacterSize( .15f );
- text->setPosition( osg::Vec3( 1.f, 0.f, 1.f ) );
- text->setAxisAlignment( osgText::Text::SCREEN );
- text->setText( "Top-right" );
- geode1->addDrawable( text.get() );
- }
- {
- osg::ref_ptr<osgText::Text> text = new osgText::Text;
- text->setFont( font.get() );
- text->setColor( white );
- text->setCharacterSize( .4f );
- text->setPosition( osg::Vec3( 1.f, 0.f, 1.f ) );
- text->setAxisAlignment( osgText::Text::SCREEN );
- //text->setAxisAlignment( osgText::Text::XZ_PLANE );
- text->setText( "Top-right" );
- geode1->addDrawable( text.get() );
- }
- root->addChild(geode1.get());
- {
- osg::ref_ptr<osgText::Text> text = new osgText::Text;
- text->setFont( font.get() );
- text->setFontResolution( 128, 128 );
- text->setColor( white );
- text->setCharacterSize( .4f );
- text->setPosition( osg::Vec3( 0.f, 0.f, -1.04f ) );
- text->setAxisAlignment( osgText::Text::XZ_PLANE );
- text->setAlignment( osgText::Text::CENTER_TOP );
- text->setText( "Hello OSG World" );
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- geode->addDrawable( text.get() );
- root->addChild(geode.get());
- }
- return root.get();
- }
- int
- main( int argc, char** argv )
- {
- osg::ref_ptr<osg::Group> root = createSceneGraph();
- if (!root.valid())
- {
- osg::notify(osg::FATAL) << "Failed in createSceneGraph()." << endl;
- return 1;
- }
- std::string out( "Text.osg" );
- if ( !(osgDB::writeNodeFile( *(root.get()), out )) )
- {
- osg::notify(osg::FATAL) << "Failed in osgDB::writeNodeFile()." << endl;
- return 1;
- }
- osg::notify(osg::ALWAYS) << "Successfully wrote "" << out << "". Execute "osgviewer " << out << "" to view." << endl;
- }
复制代码 |
|