|
楼主 |
发表于 2009-4-1 01:40:02
|
显示全部楼层
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial applications,
* as long as this copyright notice is maintained.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>
#include <osg/MatrixTransform>
#include <osg/Switch>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <iostream>
class IpodlikeEventHandler : public osgGA::GUIEventHandler
{
public:
IpodlikeEventHandler(osg::Group* root)
{
_root = root;
}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter:RAG):
{
rotate(ea.getXnormalized(), ea.getYnormalized());
break;
}
case(osgGA::GUIEventAdapter::MOVE):
{
_prevX = ea.getXnormalized();
_prevY = ea.getYnormalized();
break;
}
default:
break;
}
return false;
}
void registerModelGroupTransform(osg::MatrixTransform* modelGroupTransform)
{
_modelGroupTransform = modelGroupTransform;
_rotCenter = modelGroupTransform->getBound().center();
}
private:
void rotate(float x, float y)
{
osg::Matrix baseMatrix = _modelGroupTransform->getMatrix();
baseMatrix.preMultTranslate(_rotCenter);
baseMatrix.preMultRotate(osg:uat((x - _prevX) * 3, osg::Vec3d(0.0, 0.0, 1.0)));
baseMatrix.preMultRotate(osg::Quat(-(y - _prevY) * 3, (baseMatrix * osg::Vec3d(1.0, 0.0, 0.0))));
baseMatrix.preMultTranslate(-_rotCenter);
_modelGroupTransform->setMatrix(baseMatrix);
_prevX = x;
_prevY = y;
};
osg::Group* _root;
float _prevX;
float _prevY;
osg::Vec3 _rotCenter;
osg::MatrixTransform* _modelGroupTransform;
};
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Node> loadedModel;
// load the scene.
if (argc>1) loadedModel = osgDB::readNodeFile(argv[1]);
// if not loaded assume no arguments passed in, try use default mode instead.
if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osg");
if (!loadedModel)
{
std::cout << argv[0] <<": No data loaded." << std::endl;
return 1;
}
// create the window to draw to.
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = 200;
traits->y = 200;
traits->width = 800;
traits->height = 600;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->sharedContext = 0;
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
if (!gw)
{
osg::notify(osg::NOTICE)<<"Error: unable to create graphics window."<<std::endl;
return 1;
}
// create the view of the scene.
osgViewer::Viewer viewer;
viewer.getCamera()->setGraphicsContext(gc.get());
viewer.getCamera()->setViewport(0,0,800,600);
viewer.setSceneData(loadedModel.get());
// create a tracball manipulator to move the camera around in response to keyboard/mouse events
//viewer.setCameraManipulator( new osgGA::TrackballManipulator );
osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator(viewer.getCamera()->getStateSet());
viewer.addEventHandler(statesetManipulator.get());
osg::MatrixTransform* modelGroupTransform = new osg::MatrixTransform;
osg::Group* modelGroup = new osg::Group;
modelGroupTransform->addChild(modelGroup);
modelGroup->addChild(loadedModel.get());
IpodlikeEventHandler* mouseEventHandler = new IpodlikeEventHandler(modelGroup);
mouseEventHandler->registerModelGroupTransform(modelGroupTransform);
viewer.addEventHandler(mouseEventHandler);
viewer.realize();
osg::Matrix homePosition;
homePosition.makeLookAt(osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(0.0f,1.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f));
while(!viewer.done())
{
viewer.getCamera()->setViewMatrix(homePosition);
viewer.frame();
}
return 0;
}
为什么这样就什么也显示不了了呢? |
|