osgText组件的使用

笔记来自OSG Quick Start Guide

-------------------------------------------------------------------------

字体:

使用了osgText组件

使用的通常步骤为:

1create Font对象:osgText::readFontFile( "fonts/arial.ttf" );

2create Text对象,关联到Font对象

3addDrawable(text.get())添加到Geode节点


字体属性设置:

位置坐标text->setPosition( osg::Vec3( 10.f, 0.f, 1.f ) );

文字方向text->setAxisAlignment( osgText::Text::SCREEN ); 

Text::AxisAlignment 的枚举类型总共有七种:Text::XY_PLANE(缺省),

Text::XZ_PLANEText::YZ_PLANE 将文字面向某个轴,放置在指定的平面上;

Text::REVERSED_XY_PLANEText::REVERSED_XZ_PLANE 

Text::REVERSED_YZ_PLANE 与此类似,但是文字朝向指定轴的负向;

Text::SCREEN使文字总是朝向屏幕

对齐方式text->setAlignment( osgText::Text::CENTER_TOP ); 

缺省选项是 Text::LEFT_BASE_LINE

RIGHT_BOTTOMCENTER_BOTTOMLEFT_BOTTOM, 

RIGHT_BOTTOM_BASE_LINECENTER_BOTTOM_BASE_LINE, 

LEFT_BOTTOM_BASE_LINERIGHT_BASE_LINECENTER_BASE_LINE

LEFT_BASE_LINE, RIGHT_CENTERCENTER_CENTER, LEFT_CENTER

RIGHT_TOPCENTER_TOP,及LEFT_TOP。 

文字大小text->setCharacterSize( 1.0f ); 

分辨率text->setFontResolution( 128, 128 ); 

颜色text->setColor( osg::Vec4( 0.f, 0.f, 1.f, 1.f ) ); 


实例:

下图为三个Text对象,

上方两个为:text->setAxisAlignment( osgText::Text::SCREEN );

下方为:text->setAxisAlignment( osgText::Text::XZ_PLANE );

text->setAlignment( osgText::Text::CENTER_TOP );

osgText组件的使用_第1张图片

代码:

#include <osg/Geode>
#include <osg/Geometry>
#include <osgText/Font>
#include <osgText/Text>

#include <osg/Node>
#include <osgDB/Registry>
#include <osgDB/WriteFile>
#include <osg/Notify>
#include <iostream>

using namespace std;

osg::ref_ptr<osg::Drawable> createBase()
{
	osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;

	osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
	geom->setVertexArray( v.get() );
	v->push_back( osg::Vec3( -1.f, 0.f, -1.f ) );
	v->push_back( osg::Vec3( 1.f, 0.f, -1.f ) );
	v->push_back( osg::Vec3( 1.f, 0.f, 1.f ) );
	v->push_back( osg::Vec3( -1.f, 0.f, 1.f ) );

	osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
	geom->setColorArray( c.get() );
	geom->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
	c->push_back( osg::Vec4( 1.f, 0.f, 0.f, 1.f ) );
	c->push_back( osg::Vec4( 0.f, 1.f, 0.f, 1.f ) );
	c->push_back( osg::Vec4( 0.f, 0.f, 1.f, 1.f ) );
	c->push_back( osg::Vec4( 1.f, 1.f, 1.f, 1.f ) );

	osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
	geom->setNormalArray( n.get() );
	geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
	n->push_back( osg::Vec3( 0.f, -1.f, 0.f ) );

	geom->addPrimitiveSet(
		new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );

	return geom.get();
}

osg::ref_ptr<osg::Node> createSceneGraph()
{
	// Create the root (and only) node.
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;

	geode->addDrawable( createBase().get() );//draw a rect

	osg::ref_ptr<osgText::Font> font = osgText::readFontFile( "fonts/arial.ttf" );

	osg::Vec4 white( 1.f, 1.f, 1.f, 1.f );

	{ //top right
		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" );
		geode->addDrawable( text.get() );
	}
	{ //top left
		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-left" );
		geode->addDrawable( text.get() );
	}
	{ //bottom
		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" );
		geode->addDrawable( text.get() );
	}

	return geode.get();
}

int	main( int argc, char** argv )
{
	osg::ref_ptr<osg::Node> 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;
}





你可能感兴趣的:(c,String)