|
目标:加载两个模型,名称分别为logo与glider。按下键盘的左右键,logo分别左右旋转,而glider保持原位不动。
但效果为:只是显示了两个模型,并不能实现键盘控制旋转操作
思路: 1、建立一个旋转类,名为RotateGlider
2、建立组节点gp
3、建立节点logo和glider
4、建立矩阵变换节点 mt
5、将logo、glider、mt作为gp的子节点。
代码:#ifdef _DEBUG
#pragma comment (lib,"osgViewerd.lib")
#pragma comment (lib,"osgDBd.lib")
#pragma comment (lib,"OpenThreadsd.lib")
#pragma comment (lib,"osgd.lib")
#pragma comment (lib,"osgGAd.lib")
#pragma comment (lib,"osgFXd.lib")
#else
#pragma comment (lib,"osgViewer.lib")
#pragma comment (lib,"osgDB.lib")
#pragma comment (lib,"OpenThreads.lib")
#pragma comment (lib,"osg.lib")
#pragma comment (lib,"osgGA.lib")
#pragma comment (lib,"osgFX.lib")
#endif
#include <osgDB/ReadFile>
#include <osgDB/ReaderWriter>
#include <osgViewer/Viewer>
#include <osgViewer/api/Win32/GraphicsWindowWin32>
#include <iostream>
#include <osg/Node>
#include <osg/Group>
#include <osgFX/Scribe>
#include <osgUtil/LineSegmentIntersector>
#include <osg/Geode>
#include <osg/Geometry>
#include <osgGA/GUIEventHandler>
#include <osg/MatrixTransform>
class RotateGlider: public osgGA::GUIEventHandler
{
public: bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
osgViewer::Viewer *vw = dynamic_cast<osgViewer::Viewer*> (&aa);
if (vw)
{
osg::MatrixTransform* mt= dynamic_cast<osg::MatrixTransform *> (vw->getSceneData());
if(mt)
{
static double i=0.0;
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
{
if(ea.getKey()==osgGA::GUIEventAdapter::KEY_Left)
{
mt->setMatrix(osg::Matrix::rotate(i,osg::Vec3(0.0,0.0,1.0)));
i+=0.1;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right)
{
mt->setMatrix(osg::Matrix::rotate(i,osg::Vec3(0.0,0.0,1.0)));
i-=0.1;
}
else
{}
}
break;
default:
break;
}
}
}
return false;
}
};
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
osg::ref_ptr<osg::Group> gp = new osg::Group;
osg::ref_ptr<osg::Node> glider= osgDB::readNodeFile("glider.osg");
osg::ref_ptr<osg::Node> logo= osgDB::readNodeFile("osgcool.osgt");
mt->addChild(logo);
gp->addChild(mt);
gp->addChild(glider);
gp->addChild(logo);
viewer->setSceneData(gp.get());
viewer->addEventHandler(new RotateGlider );
return viewer->run();
}
|
|