[osg-users] Upon Clicking the mouse left button the model should appear there

Rambabu Repaka ramboram488 at gmail.com
Sun Dec 4 23:35:08 PST 2016


Hi,
float CenLat = 12.0, CenLon = 77.0;
float MinLat, MaxLat,MinLon,MaxLon;
float Width = 1280, Height = 1024;
float DifLat, DifLon,scale;
float x,y,PrevX,PrevY;
class ModelController : public osgGA::GUIEventHandler
{
public:
	ModelController(osg::MatrixTransform* node) : _model(node) {}
	virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
protected:
	osg::ref_ptr<osg::MatrixTransform> _model;
	
};

bool ModelController::handle( const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa )
{
	if ( !_model ) return false;
	float msePreLon = MinLon + ((ea.getX())/scale);
	float msePresLt = MinLat + ((ea.getY())/ scale);
	if (msePreLon > 180)	{
		msePreLon = 180;
	}
	else if (msePreLon < -180)	{
		msePreLon = -180.0;
	}
	if (msePresLt > 90)	{
		msePresLt = 90;
	}
	else if (msePresLt < -90)	{
		msePresLt = -90;
	}
	osg::Matrix matrix   =  _model->getMatrix();
	switch ( ea.getEventType() )
	{
	case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
	{
		CenLon = msePreLon;
		CenLat = msePresLt ;
		if ((CenLon - (DifLon / 2.0)) < -180)       { CenLon = -180 + (DifLon / 2.0); }
		else if ((CenLon + (DifLon / 2.0)) > 180)   { CenLon =  180 - (DifLon / 2.0); }
		if ((CenLat - (DifLat / 2.0)) < -90)        { CenLat =  -90 + (DifLat / 2.0); }
		else if ((CenLat + (DifLat / 2.0)) > 90)    { CenLat =   90 - (DifLat / 2.0); }
		MinLon = CenLon - (DifLon / 2.0);
		MinLat = CenLat - (DifLat / 2.0);
		MaxLon = CenLon + (DifLon / 2.0);
		MaxLat = CenLat + (DifLat / 2.0);
		//matrix *= osg::Matrix::scale(scale,scale,0.0);
		matrix *= osg::Matrix::translate(CenLon, 0.0, CenLat);
	}
	_model->setMatrix( matrix );
		
	}
	return false;
}
int main(int argc, char **argv)
{
	osg::DisplaySettings::instance()->setNumMultiSamples(4);
	scale = Width;
	osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("D:/RamFolder/OSG/OSG_Binary/OpenSceneGraph-3.0.1-VS9.0.30729-x86-debug-12741/data/cessna.osg");
	osg::ref_ptr<osg::MatrixTransform > mt = new osg::MatrixTransform;
	mt->addChild(model.get());
	osg::ref_ptr<osg::Projection> projection = new osg::Projection;
	projection->setMatrix(osg::Matrix::ortho2D(-Width/2, Width / 2, -Height/2, Height / 2));
	projection->addChild(mt.get());
	if (scale < Width / 2)	{
		scale = Width / 2;
	}
	if (scale > Width * 100)	{
		scale = Width * 100;
	}
	DifLat = Width / scale;
	DifLon = Height / scale;

	if ((CenLon - (DifLon / 2.0)) < -180)       { CenLon = -180 + (DifLon / 2.0); }
	else if ((CenLon + (DifLon / 2.0)) > 180)   { CenLon = 180 - (DifLon / 2.0); }
	if ((CenLat - (DifLat / 2.0)) < -90)        { CenLat = -90 + (DifLat / 2.0); }
	else if ((CenLat + (DifLat / 2.0)) > 90)    { CenLat = 90 - (DifLat / 2.0); }
	MinLon = CenLon - (DifLon / 2.0);
	MinLat = CenLat - (DifLat / 2.0);
	MaxLon = CenLon + (DifLon / 2.0);
	MaxLat = CenLat + (DifLat / 2.0);

	osg::ref_ptr<ModelController> ctrler = new ModelController(mt.get());

	osgViewer::Viewer viewer;
	viewer.setSceneData(projection.get());
	viewer.addEventHandler(ctrler.get());
	viewer.setUpViewInWindow(500, 500, 500, 500);
	return viewer.run();
}

Upon clicking the mouse left button the model should appear there.But it is not happening.can anyone help me in finding the solution?

... 

Thank you!

Cheers,
Rambabu

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=69593#69593








More information about the osg-users mailing list