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

  • 简介

这节课将在第四课的基础上进行扩展,创建3D物体。我们将三角形变为3D的三棱锥形,把四边形变为立方体。同时三棱锥沿Y轴旋转,四边形沿着(1,1,1)方向旋转。

  • 实现

本课与第二课十分类似,只需要多绘制几个面即可,三棱锥的绘制代码如下

	osg::Geometry *triangleGeometry = new osg::Geometry;
	osg::Vec3Array *triangleVertexArray = new osg::Vec3Array;
	for (unsigned i = 0; i < sizeof(TriangleVertices); ++i)
	{
		triangleVertexArray->push_back(osg::Vec3(TriangleVertices[i][0], TriangleVertices[i][1], TriangleVertices[i][2]));
	}
	triangleGeometry->setVertexArray(triangleVertexArray);
	osg::Vec4Array *triangleColorArray = new osg::Vec4Array;
	for (unsigned i = 0; i < sizeof(TriangleColors); ++i)
	{
		triangleColorArray->push_back(osg::Vec4(TriangleColors[i][0], TriangleColors[i][1], TriangleColors[i][2], 1.0f));
	}
	triangleGeometry->setColorArray(triangleColorArray);
	triangleGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
	triangleGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, 12));
在这里需要注意在DrawArrays类的构造函数需要将第三个参数修改为所有Vertex的总数,即12个

以下是立方体的绘制代码

	osg::Geometry *quadGeometry = new osg::Geometry;
	osg::Vec3Array *quadVertexArray = new osg::Vec3Array;
	for (unsigned i = 0; i < sizeof(QuadVertices); ++i)
	{
		quadVertexArray->push_back(osg::Vec3(QuadVertices[i][0], QuadVertices[i][1], QuadVertices[i][2]));
	}
	quadGeometry->setVertexArray(quadVertexArray);

	int first = 0;
	for (unsigned i = 0; i < 6; ++i)
	{
		osg::DrawArrays *vertexIndices = new osg::DrawArrays(osg::PrimitiveSet::QUADS, first, 4);
		first += 4;
		quadGeometry->addPrimitiveSet(vertexIndices);
	}
	osg::Vec4Array *quadColorArray = new osg::Vec4Array;
	for (unsigned i = 0; i < sizeof(QuadColors); ++i)
	{
		quadColorArray->push_back(osg::Vec4(QuadColors[i][0], QuadColors[i][1], QuadColors[i][2], 1.0f));
	}
	quadGeometry->setColorArray(quadColorArray);
	quadGeometry->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);

注意到颜色绑定的方式是每个PrimitiveSet绑定一次,总共需要6个颜色值。

此处如果使用DrawArrayLengths那么绑定的方式不能设置为BIND_PER_PRIMITIVE_SET,因为它只是一个PrimitiveSet,只会绑定第一个颜色值。

编译运行程序

用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 <osg/NodeVisitor>
#include <osg/AnimationPath>


 float TriangleVertices[][3] = {  {0.0f, 1.0f, 0.0f}, {-1.0f,-1.0f, 1.0f}, { 1.0f,-1.0f, 1.0f},
{0.0f, 1.0f, 0.0f}, {1.0f,-1.0f, 1.0f}, { 1.0f,-1.0f, -1.0f}, 
{0.0f, 1.0f, 0.0f},{1.0f,-1.0f, -1.0f},{ -1.0f,-1.0f, -1.0f},
{0.0f, 1.0f, 0.0f}, {-1.0f,-1.0f, -1.0f}, { -1.0f,-1.0f, 1.0f} };

 float TriangleColors[][3] = {  { 1.0f, 0.0f, 0.0f}, { 0.0f,1.0f,0.0f}, { 0.0f,0.0f,1.0f}, 
{ 1.0f, 0.0f, 0.0f}, { 0.0f,0.0f,1.0f}, { 0.0f,1.0f,0.0f},
{ 1.0f, 0.0f, 0.0f}, { 0.0f,1.0f,0.0f}, { 0.0f,0.0f,1.0f},  
{ 1.0f,0.0f,0.0f}, { 0.0f,0.0f,1.0f},{ 0.0f,1.0f,0.0f} };


 float QuadVertices[][3] = { 
	 {1.0f, 1.0f,-1.0f},					// Top Right Of The Quad (Top)
	 {-1.0f, 1.0f,-1.0f},					// Top Left Of The Quad (Top)
	 {-1.0f, 1.0f, 1.0f},					// Bottom Left Of The Quad (Top)
	 { 1.0f, 1.0f, 1.0f},					// Bottom Right Of The Quad (Top)};
	 { 1.0f,-1.0f, 1.0f},				// Top Right Of The Quad {Bottom}
	 {-1.0f,-1.0f, 1.0f},					// Top Left Of The Quad {Bottom}
	 {-1.0f,-1.0f,-1.0f},					// Bottom Left Of The Quad {Bottom}
	 { 1.0f,-1.0f,-1.0f},					// Bottom Right Of The Quad {Bottom}

	 { 1.0f, 1.0f, 1.0f},					// Top Right Of The Quad {Front}
	 {-1.0f, 1.0f, 1.0f},					// Top Left Of The Quad {Front}
	 {-1.0f,-1.0f, 1.0f},					// Bottom Left Of The Quad {Front}
	 { 1.0f,-1.0f, 1.0f},					// Bottom Right Of The Quad {Front}

	 { 1.0f,-1.0f,-1.0f},					// Top Right Of The Quad {Back}
	 {-1.0f,-1.0f,-1.0f},					// Top Left Of The Quad {Back}
	 {-1.0f, 1.0f,-1.0f},					// Bottom Left Of The Quad {Back}
	 { 1.0f, 1.0f,-1.0f},					// Bottom Right Of The Quad {Back}

	 {-1.0f, 1.0f, 1.0f},					// Top Right Of The Quad {Left}
	 {-1.0f, 1.0f,-1.0f},					// Top Left Of The Quad {Left}
	 {-1.0f,-1.0f,-1.0f},					// Bottom Left Of The Quad {Left}
	 {-1.0f,-1.0f, 1.0f},					// Bottom Right Of The Quad {Left}

	 { 1.0f, 1.0f,-1.0f},					// Top Right Of The Quad {Right}
	 { 1.0f, 1.0f, 1.0f},					// Top Left Of The Quad {Right}
	 { 1.0f,-1.0f, 1.0f},					// Bottom Left Of The Quad {Right}
	 { 1.0f,-1.0f,-1.0f}					// Bottom Right Of The Quad {Right}

 };

 const float QuadColors[][3] ={{ 0.0f,1.0f,0.0f },{ 1.0f,0.5f,0.0f },{ 1.0f, 0.0f, 0.0f },
 { 1.0f, 1.0f, 0.0f },{ 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 1.0f } };


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 *triangleMT = new osg::MatrixTransform;
	triangleMT->setMatrix(osg::Matrix::translate(-1.5, 0.0, -6.0));

	osg::AnimationPathCallback *triangleAnimationCallback = 
		new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, 2);
	osg::MatrixTransform *triangleRotMT = new osg::MatrixTransform;
	triangleRotMT->setUpdateCallback(triangleAnimationCallback);

	osg::Geometry *triangleGeometry = new osg::Geometry;
	osg::Vec3Array *triangleVertexArray = new osg::Vec3Array;
	for (unsigned i = 0; i < sizeof(TriangleVertices); ++i)
	{
		triangleVertexArray->push_back(osg::Vec3(TriangleVertices[i][0], TriangleVertices[i][1], TriangleVertices[i][2]));
	}
	triangleGeometry->setVertexArray(triangleVertexArray);
	osg::Vec4Array *triangleColorArray = new osg::Vec4Array;
	for (unsigned i = 0; i < sizeof(TriangleColors); ++i)
	{
		triangleColorArray->push_back(osg::Vec4(TriangleColors[i][0], TriangleColors[i][1], TriangleColors[i][2], 1.0f));
	}
	triangleGeometry->setColorArray(triangleColorArray);
	triangleGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
	triangleGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, 12));
	osg::Geode *triangleGeode = new osg::Geode;

	triangleGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
	triangleGeode->addDrawable(triangleGeometry);
	triangleRotMT->addChild(triangleGeode);
	triangleMT->addChild(triangleRotMT);

	//四边形部分
	osg::MatrixTransform *quadMT = new osg::MatrixTransform;
	quadMT->setMatrix(osg::Matrix::translate(1.5, 0.0, -6.0));

	osg::AnimationPathCallback *quadAnimationCallback = 
		new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Vec3(1, 1, 1), 2.5);
	osg::MatrixTransform *quadRotMT = new osg::MatrixTransform;
	quadRotMT->setUpdateCallback(quadAnimationCallback);

	osg::Geometry *quadGeometry = new osg::Geometry;
	osg::Vec3Array *quadVertexArray = new osg::Vec3Array;
	for (unsigned i = 0; i < sizeof(QuadVertices); ++i)
	{
		quadVertexArray->push_back(osg::Vec3(QuadVertices[i][0], QuadVertices[i][1], QuadVertices[i][2]));
	}
	quadGeometry->setVertexArray(quadVertexArray);

	int first = 0;
	for (unsigned i = 0; i < 6; ++i)
	{
		osg::DrawArrays *vertexIndices = new osg::DrawArrays(osg::PrimitiveSet::QUADS, first, 4);
		first += 4;
		quadGeometry->addPrimitiveSet(vertexIndices);
	}
	//////////////////////////////////////////////////////////////////////////
	//osg::DrawArrayLengths是一个PrimitiveSet,绑定颜色方式为BIND_PER_PRIMITIVE_SET
	//会出失败,只能显示为一种颜色
	//osg::DrawArrayLengths *v = new osg::DrawArrayLengths(osg::PrimitiveSet::QUADS, 0);
	//for (unsigned i = 0; i < 6; ++i)
	//{
	//	v->push_back(4);
	//}
	//quadGeometry->addPrimitiveSet(v);
	
	osg::Vec4Array *quadColorArray = new osg::Vec4Array;
	for (unsigned i = 0; i < sizeof(QuadColors); ++i)
	{
		quadColorArray->push_back(osg::Vec4(QuadColors[i][0], QuadColors[i][1], QuadColors[i][2], 1.0f));
	}
	quadGeometry->setColorArray(quadColorArray);
	quadGeometry->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);

	osg::Geode *quadGeode = new osg::Geode;
	quadGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
	quadGeode->addDrawable(quadGeometry);
	quadRotMT->addChild(quadGeode);
	quadMT->addChild(quadRotMT);

	root->addChild(triangleMT);
	root->addChild(quadMT);

	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)