Nehe OpenGL tutorial with OpenSceneGraph-48th lesson

Source: Internet
Author: User
Tags addchild cos traits
Brief Introduction

The Nehe tutorial introduces the trackball technique to us in this lesson. Trackball technology allows users to rotate objects directly using the mouse, which is the most basic function that all interactive 3D software must provide. Powerful bots have been provided in the OSG, including Trackballmanipulator, Firstpersonmanipulator, Drivemanipulator, and so on, which can be used to see the source code section of the Osgga library.

Implement

First create the two geometry in the scene:

osg::geode*	Createspheregeode ()
osg::geode*	createtorusgeode (float Minorradius, float Majorradius)

Add both to the scene root node

	Osg::matrixtransform *torusmt = new Osg::matrixtransform;
	Torusmt->setmatrix (Osg::matrix::translate ( -1.5, 0.0, -6.0));	
	Torusmt->addchild (Createtorusgeode (0.3,1.0));

	Osg::matrixtransform *spheremt = new Osg::matrixtransform;
	Spheremt->setmatrix (osg::matrix::translate (1.5, 0.0, -6.0));
	Spheremt->addchild (Createspheregeode ());

	Root->addchild (TORUSMT);
	Root->addchild (SPHEREMT);

The last thing you need to do is add a line of code to add the rover to the scene:

		This->setcameramanipulator (new Osgga::trackballmanipulator);

To compile and run the program:


Attached: This lesson source code (source code may exist errors and deficiencies, for reference only)

#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/ShapeDrawable> #include <osgGA/TrackballManipulator> osg::

	geode* Createspheregeode () {Osg::geode *spheregeode = new Osg::geode;
	Osg::shapedrawable *shapedrawable = new Osg::shapedrawable;
	Shapedrawable->setshape (New Osg::sphere (OSG::VEC3 (), 1.3));
	Shapedrawable->setcolor (OSG::VEC4 (1.0f,0.75f,0.75f, 1.0f));

	Spheregeode->adddrawable (shapedrawable);
return spheregeode;
	} osg::geode* Createtorusgeode (float Minorradius, float majorradius) {Osg::geode *geode = new Osg::geode;
	Osg::geometry *geometry = new Osg::geometry;
	Osg::vec3array *vertexarray = new Osg::vec3array;
	Osg::vec3array *normalarray = new Osg::vec3array;

	Osg::vec3array *colorarray = new Osg::vec3array; COlorarray->push_back (OSG::VEC3 (0.75f,0.75f,1.0f)); for (int i=0; i<20; i++)//Stacks {for (int j=-1; j<20; j + +)//Slices {float Wrapfrac
			= (j%20)/(float) 20;
			Float phi = 2* osg::P I*wrapfrac;
			float Sinphi = float (sin (phi));

			float Cosphi = float (cos (PHI));

			float R = Majorradius + Minorradius*cosphi; Normalarray->push_back (OSG::VEC3 (2* osg::P i* (I%20+wrapfrac)/(float)) *cosphi, Sinphi, float (cos (2*
			OSG::P i* (I%20+wrapfrac)/(float)) *cosphi)); Vertexarray->push_back (OSG::VEC3 (2* osg::P i* (I%20+wrapfrac)/(float)) *r,minorradius*sinphi,float
			(Cos (2* OSG::P i* (I%20+wrapfrac)/(float)) *r)); Normalarray->push_back (OSG::VEC3 (2* osg::P i* (I+1%20+wrapfrac)/(float)) *cosphi, Sinphi, float (cos (
			2* OSG::P i* (I+1%20+wrapfrac)/(float)) *cosphi)); Vertexarray->push_back (OSG::VEC3 (2* osg::P i* (I+1%20+wrapfrac)/(float)) *r,minorradius*sinphi, float (cos (2* OSG::P i* (I+1%20+wrapfrac)/(Float))) (*r));
	} geometry->setvertexarray (Vertexarray);
	Geometry->setnormalarray (Normalarray, Osg::array::bind_per_vertex);
	Geometry->addprimitiveset (new OSG::D rawarrays (osg::P rimitiveset::triangle_strip, 0, Vertexarray->size ()));

	Geode->adddrawable (geometry);
return geode; Class Viewerwidget:public Qwidget, public osgviewer::viewer {public:viewerwidget (osg::node *scene = NULL) {Q

		widget* 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->getcamer
		A ();

		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);

		Add Locus ball this->setcameramanipulator (new osgga::trackballmanipulator);
	return Gw->getglwidget (); osgqt::graphicswindowqt* Creategraphicswindow (int x, int y, int w, int h, const std::string& name= "", bool Window
		Decoration=false) {osg::D isplaysettings* ds = osg::D isplaysettings::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 *torusmt = new Osg::matrixtransform;	
	Torusmt->setmatrix (Osg::matrix::translate (-1.5, 0.0,-6.0));

	Torusmt->addchild (Createtorusgeode (0.3,1.0));
	Osg::matrixtransform *spheremt = new Osg::matrixtransform;
	Spheremt->setmatrix (osg::matrix::translate (1.5, 0.0,-6.0));

	Spheremt->addchild (Createspheregeode ());
	Root->addchild (TORUSMT);

	Root->addchild (SPHEREMT);
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 ();
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.