用OpenSceneGraph实现的NeHe OpenGL教程 - 第三十五课_附录1

  • 简介

在OSG中支持动态纹理,因此不需要我们对视频文件进行大量的解析工作,这些内容都已经在osgPlugins中完成了,在OSG中通过纹理osg::Image的派生类支持这一特性

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

有了以上内容,在OSG中实现就十分简单了,使用过程类似于我们常见的添加osgTexture2D的过程:

	osg::Texture2D *texture2D = new osg::Texture2D;
	texture2D = new osg::Texture2D;
	texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
	texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
	osg::ImageStream *imageStream = dynamic_cast<osg::ImageStream*>(osgDB::readImageFile("Data/Child.gif"));
	if(imageStream)
		imageStream->play();
	texture2D->setImage(imageStream);
	root->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D);

其他部分与之前的课程介绍中一样。

由于我并没有编译支持AVI的插件,因此此处以已经编译好的GIF插件做参考,读者可以自行编译AVI的插件来支持AVI格式的文件。

编译运行程序:

用OpenSceneGraph实现的NeHe OpenGL教程 - 第三十五课_附录1_第2张图片

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

#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 <osg/Texture2D>
#include <osg/ImageStream>
#include <osg/ShapeDrawable>

#include <osg/Switch>
#include <osg/AnimationPath>

#include <osg/TexGen>
#include <osg/TexGenNode>


osg::Switch *g_SwitchNode = NULL;



//////////////////////////////////////////////////////////////////////////
///////////////////////绘制几何体/////////////////////////////////
//////////////////////////////////////////////////////////////////////////
osg::Geode*	createBackGroundGeode()
{
	osg::Geode *geode = new osg::Geode;
	osg::Geometry *geometry = new osg::Geometry;
	osg::Vec3Array *vertexArray = new osg::Vec3Array;
	osg::Vec2Array *textureArray = new osg::Vec2Array;

	vertexArray->push_back(osg::Vec3(11.0f,  8.3f, -20.0f));
	vertexArray->push_back(osg::Vec3(-11.0f,  8.3f, -20.0f));
	vertexArray->push_back(osg::Vec3(-11.0f, -8.3f, -20.0f));
	vertexArray->push_back(osg::Vec3(11.0f, -8.3f, -20.0f));

	textureArray->push_back(osg::Vec2(1.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 0.0f));

	geometry->setVertexArray(vertexArray);
	geometry->setTexCoordArray(0, textureArray, osg::Array::BIND_PER_VERTEX);
	geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, vertexArray->size()));
	geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
	geode->addDrawable(geometry);

	return geode;
}

osg::Node*	createBox()
{
	osg::MatrixTransform *mtX = new osg::MatrixTransform;
	mtX->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::X_AXIS, 1.5));
	osg::MatrixTransform *mtY = new osg::MatrixTransform;
	mtY->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::Y_AXIS, 2.5));
	osg::MatrixTransform *mtZ = new osg::MatrixTransform;
	mtZ->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::Z_AXIS, 3.5));

	osg::Geode *geode = new osg::Geode;
	osg::Geometry *geometry = new osg::Geometry;
	osg::Vec3Array *vertexArray = new osg::Vec3Array;
	osg::Vec2Array *textureArray = new osg::Vec2Array;
	osg::Vec3Array *normalArray = new osg::Vec3Array;

	for (int i = 0; i < 4; ++i)
		normalArray->push_back(osg::Vec3(0.0f, 0.0f, 0.5f));
	textureArray->push_back(osg::Vec2(0.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f, -1.0f,  1.0f));
	vertexArray->push_back(osg::Vec3(1.0f, -1.0f,  1.0f));
	vertexArray->push_back(osg::Vec3(1.0f,  1.0f,  1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f,  1.0f,  1.0f));

	for (int i = 0; i < 4; ++i)
		normalArray->push_back(osg::Vec3(0.0f, 0.0f,-0.5f));
	textureArray->push_back(osg::Vec2(1.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 0.0f));
	vertexArray->push_back(osg::Vec3(-1.0f, -1.0f,  -1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f, 1.0f,  -1.0f));
	vertexArray->push_back(osg::Vec3(1.0f,  1.0f, - 1.0f));
	vertexArray->push_back(osg::Vec3(1.0f,  -1.0f,  -1.0f));

	for (int i = 0; i < 4; ++i)
		normalArray->push_back(osg::Vec3(0.0f, 0.5f,0.0f));
	textureArray->push_back(osg::Vec2(0.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f, 1.0f,  -1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f, 1.0f,  1.0f));
	vertexArray->push_back(osg::Vec3(1.0f,  1.0f,  1.0f));
	vertexArray->push_back(osg::Vec3(1.0f,  1.0f,  -1.0f));

	for (int i = 0; i < 4; ++i)
		normalArray->push_back(osg::Vec3(0.0f, -0.5f,0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 0.0f));
	vertexArray->push_back(osg::Vec3(-1.0f, -1.0f,  -1.0f));
	vertexArray->push_back(osg::Vec3(1.0f, -1.0f,  -1.0f));
	vertexArray->push_back(osg::Vec3(1.0f,  -1.0f,  1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f,  -1.0f,  1.0f));

	for (int i = 0; i < 4; ++i)
		normalArray->push_back(osg::Vec3(0.5f, 0.0f,0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 0.0f));
	vertexArray->push_back(osg::Vec3(1.0f, -1.0f,  -1.0f));
	vertexArray->push_back(osg::Vec3(1.0f, 1.0f,  -1.0f));
	vertexArray->push_back(osg::Vec3(1.0f,  1.0f,  1.0f));
	vertexArray->push_back(osg::Vec3(1.0f,  -1.0f,  1.0f));

	for (int i = 0; i < 4; ++i)
		normalArray->push_back(osg::Vec3(-0.5f, 0.0f,0.0f));
	textureArray->push_back(osg::Vec2(0.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 0.0f));
	textureArray->push_back(osg::Vec2(1.0f, 1.0f));
	textureArray->push_back(osg::Vec2(0.0f, 1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f, -1.0f,  -1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f, -1.0f,  1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f,  1.0f,  1.0f));
	vertexArray->push_back(osg::Vec3(-1.0f,  1.0f,  -1.0f));

	geometry->setVertexArray(vertexArray);
	geometry->setNormalArray(normalArray, osg::Array::BIND_PER_VERTEX);
	geometry->setTexCoordArray(0, textureArray, osg::Array::BIND_PER_VERTEX);
	geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, vertexArray->size()));
	geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
	geode->addDrawable(geometry);

	mtX->addChild(mtY);
	mtY->addChild(mtZ);
	mtZ->addChild(geode);

	return mtX;
}

osg::Node*	createSphere()
{
	osg::MatrixTransform *mtX = new osg::MatrixTransform;
	mtX->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::X_AXIS, 1.5));
	osg::MatrixTransform *mtY = new osg::MatrixTransform;
	mtY->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::Y_AXIS, 2.5));
	osg::MatrixTransform *mtZ = new osg::MatrixTransform;
	mtZ->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::Z_AXIS, 3.5));

	osg::Geode *sphere = new osg::Geode;
	osg::ShapeDrawable *sphereDrawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0,0,0), 1.3));
	sphere->addDrawable(sphereDrawable);

	mtX->addChild(mtY);
	mtY->addChild(mtZ);
	mtZ->addChild(sphere);

	return mtX;
}

osg::Node*	createCylinder()
{
	osg::MatrixTransform *mtX = new osg::MatrixTransform;
	mtX->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::X_AXIS, 1.5));
	osg::MatrixTransform *mtY = new osg::MatrixTransform;
	mtY->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::Y_AXIS, 2.5));
	osg::MatrixTransform *mtZ = new osg::MatrixTransform;
	mtZ->addUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(), osg::Z_AXIS, 3.5));

	osg::Geode *cylinder = new osg::Geode;
	osg::ShapeDrawable *cylinderDrawable = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0,0,0), 0.5, 2.0));
	cylinder->addDrawable(cylinderDrawable);

	mtX->addChild(mtY);
	mtY->addChild(mtZ);
	mtZ->addChild(cylinder);


	return mtX;
}

//////////////////////////////////////////////////////////////////////////
/////////////////////////////场景交互//////////////////////////////
//////////////////////////////////////////////////////////////////////////
class ManipulatorSceneHandler : public osgGA::GUIEventHandler
{
public:
	ManipulatorSceneHandler()
	{
	}

	virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
	{
		osgViewer::Viewer *viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
		if (!viewer)
			return false;
		if (!viewer->getSceneData())
			return false;
		if (ea.getHandled()) 
			return false;

		switch(ea.getEventType())
		{
		case(osgGA::GUIEventAdapter::KEYDOWN):
			{
				if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Space)
				{
					if (!g_SwitchNode)
						return false;

					static int i = 0;
					++i;
					if (i > 2)
						i = 0;
					g_SwitchNode->setSingleChildOn(i);
				}
				if (ea.getKey() == osgGA::GUIEventAdapter::KEY_E)
				{
					static bool flag = true;
					if(flag){
						g_SwitchNode->getOrCreateStateSet()->setTextureMode(0, GL_TEXTURE_GEN_S, osg::StateAttribute::OFF);
						g_SwitchNode->getOrCreateStateSet()->setTextureMode(0, GL_TEXTURE_GEN_T, osg::StateAttribute::OFF);
					}else{
						g_SwitchNode->getOrCreateStateSet()->setTextureMode(0, GL_TEXTURE_GEN_S, osg::StateAttribute::ON);
						g_SwitchNode->getOrCreateStateSet()->setTextureMode(0, GL_TEXTURE_GEN_T, osg::StateAttribute::ON);
					}
					flag = !flag;
				}
			}
		default: break;
		}
		return false;
	}
};
//////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////
//////////////////////////osgNeHe框架////////////////////////////
//////////////////////////////////////////////////////////////////////////
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 );
		this->addEventHandler(new ManipulatorSceneHandler);

		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::Texture2D *texture2D = new osg::Texture2D;
	texture2D = new osg::Texture2D;
	texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
	texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
	osg::ImageStream *imageStream = dynamic_cast<osg::ImageStream*>(osgDB::readImageFile("Data/Child.gif"));
	if(imageStream)
		imageStream->play();
	texture2D->setImage(imageStream);
	root->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D);

	osg::Switch *switchNode = new osg::Switch;
	g_SwitchNode = switchNode;
	switchNode->addChild(createBox());
	switchNode->addChild(createCylinder());
	switchNode->addChild(createSphere());
	switchNode->setSingleChildOn(0);
	osg::TexGen *texGen = new osg::TexGen;
	texGen->setMode(osg::TexGen::SPHERE_MAP);
	switchNode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texGen);

	osg::MatrixTransform *zoomMT = new osg::MatrixTransform;
	zoomMT->setMatrix(osg::Matrix::translate(0.0f, 0.0f, -10.0f));
	zoomMT->addChild(switchNode);

	root->addChild(createBackGroundGeode());
	root->addChild(zoomMT);
	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)