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

  • 简介

本节课实现在场景中绘制3D的轮廓字体,实现方式和第十三课中类似,只不过我们需要使用的是osgText库中的三维字体类osgText::Text3D

  • 实现

首先同样需要绘制osgText,将它加到场景的叶节点之中


	osg::Group *root = new osg::Group;

	osg::MatrixTransform *moveMT = new osg::MatrixTransform;
	moveMT->setMatrix(osg::Matrix::translate(-2, 0, -10));
	root->addChild(moveMT);

	osg::MatrixTransform *rotX = new osg::MatrixTransform;
	rotX->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::X_AXIS, 0.5));
	osg::MatrixTransform *rotY = new osg::MatrixTransform;
	rotY->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, 0.4));
	osg::MatrixTransform *rotZ = new osg::MatrixTransform;
	rotZ->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Z_AXIS, 0.3));

	moveMT->addChild(rotX);
	rotX->addChild(rotY);
	rotY->addChild(rotZ);
	
	osg::Geode *fontGeode = new osg::Geode;

	osgText::Text3D *text = new osgText::Text3D;
	text->setCharacterSize(0.8f);
	text->setCharacterDepth(0.2f);
	text->setUpdateCallback(new FontColorCallback);
	text->setText("OpenGL NeHe");
	text->setFont("fonts/arial.ttf");
	fontGeode->addDrawable(text);

	rotZ->addChild(fontGeode);

	return root;

唯一不同的是我们需要设置字体的"厚度",通过setCharacterDepth函数实现,为了简便起见,我们使用AnimationPathCallback来实现旋转

为了实现动态的颜色改变效果,我们使用和上一课一样的方式

class FontColorCallback : public osg::Drawable::UpdateCallback
{
public:

	virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
	{
		if (dynamic_cast<osgText::Text3D*>(drawable))
		{
			osgText::Text3D *text = dynamic_cast<osgText::Text3D*>(drawable);
			text->setColor(osg::Vec4(1.0f*float(cos(rot/20.0f)),1.0f*float(sin(rot/25.0f)),1.0f-0.5f*float(cos(rot/17.0f)), 1));

			std::stringstream os;
			std::string str;
			os.precision(2);
			os << std::fixed << "NeHe - " << rot;
			str = os.str();

			text->setText(str);
			rot+=0.5f;
		}
	}
};

最后编译运行程序

用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 <osgDB/ReadFile>
#include <osgText/Text>
#include <osgText/Text3D>

#include <osg/AnimationPath>

#include <iostream>
#include <sstream>

float rot = 0.0;

class FontColorCallback : public osg::Drawable::UpdateCallback
{
public:

	virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
	{
		if (dynamic_cast<osgText::Text3D*>(drawable))
		{
			osgText::Text3D *text = dynamic_cast<osgText::Text3D*>(drawable);
			text->setColor(osg::Vec4(1.0f*float(cos(rot/20.0f)),1.0f*float(sin(rot/25.0f)),1.0f-0.5f*float(cos(rot/17.0f)), 1));

			std::stringstream os;
			std::string str;
			os.precision(2);
			os << std::fixed << "NeHe - " << rot;
			str = os.str();

			text->setText(str);
			rot+=0.5f;
		}
	}
};



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, 1), osg::Vec3d(0, 0, 0), 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;

	osg::MatrixTransform *moveMT = new osg::MatrixTransform;
	moveMT->setMatrix(osg::Matrix::translate(-2, 0, -10));
	root->addChild(moveMT);

	osg::MatrixTransform *rotX = new osg::MatrixTransform;
	rotX->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::X_AXIS, 0.5));
	osg::MatrixTransform *rotY = new osg::MatrixTransform;
	rotY->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, 0.4));
	osg::MatrixTransform *rotZ = new osg::MatrixTransform;
	rotZ->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Z_AXIS, 0.3));

	moveMT->addChild(rotX);
	rotX->addChild(rotY);
	rotY->addChild(rotZ);
	
	osg::Geode *fontGeode = new osg::Geode;

	osgText::Text3D *text = new osgText::Text3D;
	text->setCharacterSize(0.8f);
	text->setCharacterDepth(0.2f);
	text->setUpdateCallback(new FontColorCallback);
	text->setText("OpenGL NeHe");
	text->setFont("fonts/arial.ttf");
	fontGeode->addDrawable(text);

	rotZ->addChild(fontGeode);

	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)