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

52 篇文章 37 订阅
  • 简介

NeHe这节课主要讨论如何使用OpenGL显示2D文字。OpenGL显示2D文字是通过wglUseFontBitmaps函数创建了一系列的显示列表来实现的。通过阅读NeHe教程,读者应该能感觉到在OpenGL中显示文字还是比较麻烦的。在OSG中显示2D文字相对来说比较简单,OSG提供了专门用来处理文字的库osgText(包括二维和三维字体)

  • 实现

首先创建文字节点,由于osgText::Text继承于osg::Drawable可绘制物体,因此创建文本之后直接将其添加到叶节点中

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

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

	osg::Geode *fontGeode = new osg::Geode;

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

	moveMT->addChild(fontGeode);

	return root;
对于OSG中的Text类,它提供了丰富的成员函数允许用户自由地调整排版格式、采用不同的位置和姿态、使用不同的颜色等操作。

由于场景中需要不断修改字体的颜色,我们使用Drawable的UpdateCallback来完成

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

	virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
	{
		if (dynamic_cast<osgText::Text*>(drawable))
		{
			osgText::Text *text = dynamic_cast<osgText::Text*>(drawable);
			text->setColor(osg::Vec4(1.0f*float(cos(cnt1)),1.0f*float(sin(cnt2)),1.0f-0.5f*float(cos(cnt1+cnt2)), 1));

			std::stringstream os;
			std::string str;
			os.precision(2);
			os << std::fixed << "Active OpenGL Text With NeHe - " << cnt1;
			str = os.str();

			text->setText(str);
			text->setPosition(osg::Vec3(-4.5f+0.5f*float(cos(cnt1)), 1.92f*float(sin(cnt2)),0));
			cnt1+=0.051f;
			cnt2+=0.005f;	

		}
	}
};
在每帧中修改字体的颜色和位置,与NeHe教程中的模式一样

最后编译运行程序:


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

#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 <iostream>
#include <sstream>

float cnt1, cnt2;

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

	virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
	{
		if (dynamic_cast<osgText::Text*>(drawable))
		{
			osgText::Text *text = dynamic_cast<osgText::Text*>(drawable);
			text->setColor(osg::Vec4(1.0f*float(cos(cnt1)),1.0f*float(sin(cnt2)),1.0f-0.5f*float(cos(cnt1+cnt2)), 1));

			std::stringstream os;
			std::string str;
			os.precision(2);
			os << std::fixed << "Active OpenGL Text With NeHe - " << cnt1;
			str = os.str();

			text->setText(str);
			text->setPosition(osg::Vec3(-4.5f+0.5f*float(cos(cnt1)), 1.92f*float(sin(cnt2)),0));
			cnt1+=0.051f;
			cnt2+=0.005f;	

		}
	}
};



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(0, 0, -10));
	root->addChild(moveMT);

	osg::Geode *fontGeode = new osg::Geode;

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

	moveMT->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();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译运行 OpenSceneGraph-OpenSceneGraph-3.6.4 需要按照以下步骤进行操作: 1. 首先,确保你的计算机上已经安装了 CMake,C++ 编译器和 OpenGL 兼容的图形驱动程序。这些是编译和运行 OpenSceneGraph 所必需的工具和库。 2. 下载 OpenSceneGraph-OpenSceneGraph-3.6.4 的源代码,可以在官方网站或开源项目托管网站上找到。确保下载的版本正确,避免出错。 3. 解压源代码文件并进入解压后的目录。 4. 创建一个用于构建的构建目录,并进入该目录。例如:mkdir build && cd build。 5. 运行 CMake 命令来生成构建系统所需的文件和配置。命令可能类似于:cmake path/to/OpenSceneGraph-OpenSceneGraph-3.6.4。你可以使用其他参数和选项来自定义构建过程。 6. 确保 CMake 执行成功并生成了构建系统所需的文件。 7. 使用你的 C++ 编译器来构建 OpenSceneGraph。可以使用 make 命令,或者其他编译工具,根据你的操作系统和编译环境来选择。例如:make。 8. 等待编译完成,这可能需要一段时间,具体取决于你的计算机性能。 9. 构建完成后,检查是否有错误或警告信息。如果有,需要解决它们,并重新运行编译步骤。 10. 运行编译好的 OpenSceneGraph 可执行文件,这将启动 OpenSceneGraph 程序并运行示例或其他自定义的应用程序。 总之,编译和运行 OpenSceneGraph-OpenSceneGraph-3.6.4 需要先安装必需的工具和库,然后使用 CMake 生成构建系统所需的文件,再使用 C++ 编译器进行编译,最后运行生成的可执行文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值