Nehe OpenGL tutorial with OpenSceneGraph-46th lesson

Source: Internet
Author: User
Tags addchild traits
Brief Introduction

The Nehe tutorial introduces the extended (Arb_multisample) Multiple sampling techniques for OpenGL in this lesson. With this technique, full-screen anti-aliasing can be achieved. This will make the graphic look more beautiful.

In OSG, multiple sampling is opened and closed as stateset of nodes, and the implementation process is very simple. Implement

First add quadrilateral to the scene, this process has been repeated many times before, the code is as follows:

	for (int i=-10;i<10;i++) {for (int j=-10;j<10;j++) {Osg::matrixtransform *transmt = new Osg::matrixtransfor
			M
			Transmt->addupdatecallback (New Transcallback (I, j));
			Osg::matrixtransform *rotmt = new Osg::matrixtransform;
			Rotmt->addupdatecallback (new Rotcallback);
			Osg::geode *geode = new Osg::geode;
			Osg::geometry *geometry = new Osg::geometry;
			Osg::vec3array *vertexarray = new Osg::vec3array;
			Osg::vec3array *colorarray = new Osg::vec3array;

			Colorarray->push_back (OSG::VEC3 (1, 0, 0));
			Vertexarray->push_back (OSG::VEC3 (i, J, 0.0));
			Colorarray->push_back (OSG::VEC3 (0, 1, 0));
			Vertexarray->push_back (OSG::VEC3 (i + 2.0f,j,0.0f));
			Colorarray->push_back (OSG::VEC3 (0,0,1));
			Vertexarray->push_back (OSG::VEC3 (i + 2.0f,j + 2.0f,0.0f));
			Colorarray->push_back (OSG::VEC3 (1,1,1));

			Vertexarray->push_back (OSG::VEC3 (i,j + 2.0f,0.0f));
			Geometry->setvertexarray (Vertexarray); Geometry->setcolorarray (Colorarray, OSG::array::bind_per_vertex);
			Geometry->getorcreatestateset ()->setmode (gl_lighting, Osg::stateattribute::off);
			Geometry->addprimitiveset (new OSG::D rawarrays (osg::P rimitiveset::quads, 0, Vertexarray->size ()));

			Geode->adddrawable (geometry);
			Transmt->addchild (ROTMT);

			Rotmt->addchild (Geode);
		Root->addchild (TRANSMT);
 }
	}

You need to set the window sample value to 4 when creating a window traits to enable multiple sampling:

		Traits->samplebuffers = Ds->getmultisamples ();
		Traits->samples = 4/*ds->getnummultisamples () * *;

Then, to allow these quadrilateral to rotate and move, add a few callbacks to the node:

Class Transcallback:public Osg::nodecallback
{public
:
	transcallback (float I, float j): _i (i), _j (j) {}
  virtual void operator () (osg::node* Node, osg::nodevisitor* nv)
	{
		Osg::matrixtransform *mt = dynamic_cast< osg::matrixtransform*> (node);
		if (!MT) return
			;
		Mt->setmatrix (Osg::matrix::translate (_i*2.0f,_j*2.0f,-5.0f));
		Traverse (node, NV);
	}
	float _i;
	float _j;
};

To enable multiple sampling for a node:

	Osg::multisample *ms = new Osg::multisample;
	Ms->sethint (osg::multisample::nicest);
	Root->getorcreatestateset ()->setattributeandmodes (MS, Osg::stateattribute::on | osg::StateAttribute::O Verride);

Toggle the open and closed mode of multiple sampling in interaction with the keyboard:

				if (ea.getkey () = = Osgga::guieventadapter::key_space)
				{
					if (!g_root) return
						false;
					static BOOL flag = TRUE;
					if (flag) {
						g_root->getorcreatestateset ()->setmode (Gl_multisample_arb, false);
					else{
						G_root->getorcreatestateset ()->setmode (Gl_multisample_arb, true);
					flag =!flag;
				}

Compile and run the program, you can see that when the space is pressed, the edge of the edges of the quadrilateral has a significant change:


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/Multisample> osg::group *g_root = NULL;  Class Manipulatorscenehandler:public Osgga::guieventhandler {Public:manipulatorscenehandler () {} public:virtual BOOL Handle (const osgga::guieventadapter& EA, osgga::guiactionadapter& aa) {osgviewer::viewer *viewer = Dynam
		Ic_cast<osgviewer::viewer*> (&AMP;AA);
		if (!viewer) return false;
		if (!viewer->getscenedata ()) return false;

		if (ea.gethandled ()) return false;

		Osg::group *root = Viewer->getscenedata ()->asgroup (); Switch (Ea.geteventtype ()) {case (Osgga::guieventadapter::keydown): {if (ea.getkey () = = Osgga::guieventadapter
					:: Key_space) {if (!g_root) return false; Static BOOL flag = TRUE;
					if (flag) {G_root->getorcreatestateset ()->setmode (Gl_multisample_arb, false);
					}else{G_root->getorcreatestateset ()->setmode (Gl_multisample_arb, true);
				} flag =!flag;

		}} break;
		Default:break;
	return false;

}
}; Class Viewerwidget:public Qwidget, public osgviewer::viewer {public:viewerwidget (osg::node *scene = NULL) {QWidge

		t* 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);
		This->addeventhandler (new Manipulatorscenehandler);
	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 = 4/*ds->getnummultisamples () * *;
	return new OSGQT::GRAPHICSWINDOWQT (Traits.get ()); 
	virtual void PaintEvent (qpaintevent* event) {frame ();
} Protected:qtimer _timer;


}; Class Transcallback:public Osg::nodecallback {public:transcallback (float I, float j): _i (i), _j (j) {} virtual void Operator () (osg::node* Node, osg::nodevisitor* nv) {Osg::matrixtransform *MT = DYNAMIC_CAST&LT;OSG::MATRIXTRANSFORM*&G
		t; (node);
		if (!MT) return;
		Mt->setmatrix (Osg::matrix::translate (_i*2.0f,_j*2.0f,-5.0f));
	Traverse (node, NV);
	float _i;
float _j;


};
	Class Rotcallback:public Osg::nodecallback {public:virtual void operator () (osg::node* Node, osg::nodevisitor* nv)

		{static float angle = 0.0; Osg::matrixtransform *MT = Dynamic_cast<osg::matrixtransform*> (node);

		if (!MT) return;
		Mt->setmatrix (osg::matrix::rotate (angle, osg::z_axis));

		Angle + + OSG::D Egreestoradians (0.001f);
	Traverse (node, NV);



}


};
	osg::node* Buildscene () {Osg::group *root = new Osg::group;
	G_root = Root;
	Osg::multisample *ms = new Osg::multisample;
	Ms->sethint (osg::multisample::nicest); Root->getorcreatestateset ()->setattributeandmodes (MS, Osg::stateattribute::on | osg::StateAttribute::O

	Verride); for (int i=-10;i<10;i++) {for (int j=-10;j<10;j++) {Osg::matrixtransform *transmt = new Osg::matrixtransform
			;
			Transmt->addupdatecallback (New Transcallback (I, j));
			Osg::matrixtransform *rotmt = new Osg::matrixtransform;
			Rotmt->addupdatecallback (new Rotcallback);
			Osg::geode *geode = new Osg::geode;
			Osg::geometry *geometry = new Osg::geometry;
			Osg::vec3array *vertexarray = new Osg::vec3array;
			Osg::vec3array *colorarray = new Osg::vec3array; Colorarray->push_back (OSG::VEC3 (1, 0, 0));

			Vertexarray->push_back (OSG::VEC3 (i, J, 0.0));
			Colorarray->push_back (OSG::VEC3 (0, 1, 0));
			Vertexarray->push_back (OSG::VEC3 (i + 2.0f,j,0.0f));
			Colorarray->push_back (OSG::VEC3 (0,0,1));
			Vertexarray->push_back (OSG::VEC3 (i + 2.0f,j + 2.0f,0.0f));
			Colorarray->push_back (OSG::VEC3 (1,1,1));

			Vertexarray->push_back (OSG::VEC3 (i,j + 2.0f,0.0f));
			Geometry->setvertexarray (Vertexarray);
			Geometry->setcolorarray (Colorarray, Osg::array::bind_per_vertex);
			Geometry->getorcreatestateset ()->setmode (gl_lighting, Osg::stateattribute::off);
			Geometry->addprimitiveset (new OSG::D rawarrays (osg::P rimitiveset::quads, 0, Vertexarray->size ()));

			Geode->adddrawable (geometry);
			Transmt->addchild (ROTMT);

			Rotmt->addchild (Geode);
		Root->addchild (TRANSMT);
} 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.