用OpenSceneGraph实现的NeHe OpenGL教程 - 第四十三课

  • 简介

这节课NeHe教我们怎么使用FreeType字体库来创建字体,在OSG中通过FreeType的插件已经支持FreeType字体了,前面很多课程中的字体就是使用FreeType字体库创建的。

  • 实现

本课的实现过程十分的简单,创建字体,然后再回调中修改字体显示:

创建字体代码如下:

osg::Node*	createFont1()
{
	osg::MatrixTransform *zoomMT = new osg::MatrixTransform;
	zoomMT->setMatrix(osg::Matrix::translate(-3.0f,3.0f,-10.0f));

	osgText::Text *textWords = new osgText::Text;
	textWords->setColor(osg::Vec4(0,0,1,1));
	textWords->setText("Active WGL Bitmap Text With NeHe");
	textWords->setFont("Fonts/Arial.ttf");
	textWords->setCharacterSize(0.3f);
	textWords->setUpdateCallback(new Font1UpdateCallback);

	osg::Geode *fontGeode = new osg::Geode;
	fontGeode->addDrawable(textWords);
	
	zoomMT->addChild(fontGeode);

	return zoomMT;
}

在字体回调中不断修改字体的显示内容:

class Font1UpdateCallback : public osg::Drawable::UpdateCallback
{
public:

	virtual void update(osg::NodeVisitor*, osg::Drawable* draw)
	{
		static float cnt1 = 0.0;

		osgText::Text *text = dynamic_cast<osgText::Text*>(draw);
		if (!text)
			return;

		std::stringstream os;
		std::string str;
		os.precision(2);
		os << std::fixed << "Active WGL Bitmap Text With NeHe - " << cnt1;
		str = os.str();
		text->setText(str);

		cnt1 += 0.01;
	}

};

将两部分添加到场景根节点中:

	osg::Group *root = new osg::Group;
	root->addChild(createFont1());
	root->addChild(createFont2());
	return root;

编译运行程序:

用OpenSceneGraph实现的NeHe OpenGL教程 - 第四十三课_第1张图片

附:本课源码(源码中可能存在错误和不足之处,仅供参考)

#include "../osgNeHe.h"

#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QVBoxLayout>

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgQt/GraphicsWindowQt>

#include <osg/MatrixTransform>

#include <osgText/Text>
#include <osg/AnimationPath>



//////////////////////////////////////////////////////////////////////////

class Font1UpdateCallback : public osg::Drawable::UpdateCallback
{
public:

	virtual void update(osg::NodeVisitor*, osg::Drawable* draw)
	{
		static float cnt1 = 0.0;

		osgText::Text *text = dynamic_cast<osgText::Text*>(draw);
		if (!text)
			return;

		std::stringstream os;
		std::string str;
		os.precision(2);
		os << std::fixed << "Active WGL Bitmap Text With NeHe - " << cnt1;
		str = os.str();
		text->setText(str);

		cnt1 += 0.01;
	}

};


class Font2UpdateCallback : public osg::Drawable::UpdateCallback
{
public:

	virtual void update(osg::NodeVisitor*, osg::Drawable* draw)
	{
		static float cnt1 = 0.0;

		osgText::Text *text = dynamic_cast<osgText::Text*>(draw);
		if (!text)
			return;

		std::stringstream os;
		std::string str;
		os.precision(2);
		os << std::fixed << "Active FreeType Text - " << cnt1;
		str = os.str();
		text->setText(str);

		cnt1 += 0.02;
	}
};




//////////////////////////////////////////////////////////////////////////

osg::Node*	createFont1()
{
	osg::MatrixTransform *zoomMT = new osg::MatrixTransform;
	zoomMT->setMatrix(osg::Matrix::translate(-3.0f,3.0f,-10.0f));

	osgText::Text *textWords = new osgText::Text;
	textWords->setColor(osg::Vec4(0,0,1,1));
	textWords->setText("Active WGL Bitmap Text With NeHe");
	textWords->setFont("Fonts/Arial.ttf");
	textWords->setCharacterSize(0.3f);
	textWords->setUpdateCallback(new Font1UpdateCallback);

	osg::Geode *fontGeode = new osg::Geode;
	fontGeode->addDrawable(textWords);
	
	zoomMT->addChild(fontGeode);

	return zoomMT;
}


osg::Node*	createFont2()
{
	osg::MatrixTransform *zoomMT = new osg::MatrixTransform;
	zoomMT->setMatrix(osg::Matrix::translate(0.0f,-2.0f,-1.0f));

	osg::MatrixTransform *rotZ = new osg::MatrixTransform;
	rotZ->setMatrix(osg::Matrix::rotate(0, osg::Z_AXIS));
	rotZ->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::Z_AXIS, 0.5));

	osg::MatrixTransform *transMT = new osg::MatrixTransform;
	transMT->setMatrix(osg::Matrix::translate(-5, 0, -10));

	zoomMT->addChild(rotZ);
	rotZ->addChild(transMT);
	
	osgText::Text *textWords = new osgText::Text;
	textWords->setColor(osg::Vec4(1,0,0,1));
	textWords->setText("Active FreeType Text");
	textWords->setFont("Fonts/Arial.ttf");
	textWords->setCharacterSize(0.6f);
	textWords->setUpdateCallback(new Font2UpdateCallback);

	osg::Geode *fontGeode = new osg::Geode;
	fontGeode->addDrawable(textWords);
	
	transMT->addChild(fontGeode);
	return zoomMT;
}

//////////////////////////////////////////////////////////////////////////




class ViewerWidget : public QWidget, public osgViewer::Viewer
{
public:
	ViewerWidget(osg::Node *scene = NULL)
	{
		QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene);

		QVBoxLayout* layout = new QVBoxLayout;
		layout->addWidget(renderWidget);
		layout->setContentsMargins(0, 0, 0, 1);
		setLayout( layout );

		connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
		_timer.start( 10 );
	}

	QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )
	{
		osg::Camera* camera = this->getCamera();
		camera->setGraphicsContext( gw );

		const osg::GraphicsContext::Traits* traits = gw->getTraits();

		camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 1.0) );
		camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
		camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f );
		camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 0), osg::Vec3d(0, 0, -1), osg::Vec3d(0, 1, 0));

		this->setSceneData( scene );

		return gw->getGLWidget();
	}

	osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
	{
		osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
		osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
		traits->windowName = name;
		traits->windowDecoration = windowDecoration;
		traits->x = x;
		traits->y = y;
		traits->width = w;
		traits->height = h;
		traits->doubleBuffer = true;
		traits->alpha = ds->getMinimumNumAlphaBits();
		traits->stencil = ds->getMinimumNumStencilBits();
		traits->sampleBuffers = ds->getMultiSamples();
		traits->samples = ds->getNumMultiSamples();

		return new osgQt::GraphicsWindowQt(traits.get());
	}

	virtual void paintEvent( QPaintEvent* event )
	{ 
		frame(); 
	}

protected:

	QTimer _timer;
};



osg::Node*	buildScene()
{
	osg::Group *root = new osg::Group;
	root->addChild(createFont1());
	root->addChild(createFont2());
	return root;
}



int main( int argc, char** argv )
{
	QApplication app(argc, argv);
	ViewerWidget* viewWidget = new ViewerWidget(buildScene());
	viewWidget->setGeometry( 100, 100, 640, 480 );
	viewWidget->show();
	return app.exec();
}


你可能感兴趣的:(C++,qt,OpenGL,nehe,OSG)