OSG菜单实现视图的缩放旋转等
我想实现对一个视图的缩放,旋转,在菜单状态下,就是例如点击菜单一个按钮,视图就放大,或者缩小。主要是通过改变视图的坐标系。但是代码调试不出来了,可能是我编辑错误?请大神看一看
#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,"osgWidgetd.lib")
#pragma comment(lib,"osgManipulatord.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,"osgWidget.lib")
#pragma comment(lib,"osgManipulator.lib")
#endif
#include <iostream>
#include <osgDB/ReadFile>
#include <osgWidget/Util>
#include <osgWidget/WindowManager>
#include <osgWidget/Box>
#include <osgWidget/Label>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osgManipulator/TrackballDragger>
#include <osgManipulator/TranslateAxisDragger>
#include <osgManipulator/ScaleAxisDragger>
#include <osg/MatrixTransform>
#include <osgGA/GUIEventHandler>
#include <osgDB/ReaderWriter>
#include <osgGA/StateSetManipulator>
osg::Node* cow=osgDB::readNodeFile("cow.osg");
osgViewer::Viewer *viewer=new osgViewer::Viewer();
class Rotate: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 if(ea.getKey()==osgGA::GUIEventAdapter::KEY_Up)
{
mt->setMatrix(osg::Matrix::rotate(i,osg::Vec3(1.0,0.0,0.0)));
i+=0.1;
}
else if(ea.getKey()==osgGA::GUIEventAdapter::KEY_Down)
{
mt->setMatrix(osg::Matrix::rotate(i,osg::Vec3(1.0,0.0,0.0)));
i-=0.1;
}
else
{}
}
break;
default:
break;
}
}
}
return false;
}
};
struct ColorLabel:public osgWidget::Label{
ColorLabel(const char * label):
osgWidget::Label("",""){
setFont("font/Vera.ttf");
setFontSize(14);
setFontColor(1.0f,1.0f,1.0f,1.0f);
setColor(0.3f,0.3f,0.3f,1.0f);
addHeight(18.0f);
setCanFill(true);
setLabel(label);
setEventMask(osgWidget::EVENT_MOUSE_PUSH|osgWidget::EVENT_MASK_MOUSE_MOVE);
strMenuName.clear();
strMenuName+=label;
}
bool mousePush(double,double,const osgWidget::WindowManager* wm){
this->getParent()->hide();
Rotate A;
A.handle(& ea,& aa);
osgViewer::Viewer* vw=dynamic_cast<osgViewer::Viewer*>(&aa);
if(strMenuName=="Exit")
{
exit(0);
}
else if(strMenuName=="cow.osg")
{
//cow->setNodeMask(!cow->getNodeMask());
//osgViewer::Viewer* vw=dynamic_cast<osgViewer::Viewer*>(&aa);
osg::MatrixTransform* mt=dynamic_cast<osg::MatrixTransform*> (viewer->getSceneData());
mt->setMatrix(osg::Matrix::rotate(0.5,osg::Vec3(0.0,0.0,1.0)));
}
return 0;
}
bool mouseEnter(double,double,const osgWidget::WindowManager*) {
setColor(0.6f,0.6f,0.6f,1.0f);
return true;
}
bool mouseLeave(double,double,const osgWidget::WindowManager*) {
setColor(0.3f,0.3f,0.3f,1.0f);
return true;
}
private:
std::string strMenuName;
};
class ColorLabelMenu:public ColorLabel {
osg::ref_ptr<osgWidget::Window> _window;
public:
ColorLabelMenu(const char* label):
ColorLabel(label){
_window=new osgWidget::Box(std::string("Menu_")+label,osgWidget::Box::VERTICAL,true);
strMenuName.clear();
strMenuName+=label;
if(strMenuName=="File")
{
_window->addWidget(new ColorLabel("Exit"));
}
else if(strMenuName=="Module")
{
_window->addWidget(new ColorLabel("cow.osg"));
}
_window->resize();
setColor(0.8f,0.8f,0.8f,0.8f);
}
void managed(osgWidget::WindowManager* wm){
osgWidget::Label::managed(wm);
wm->addChild(_window.get());
_window->hide();
}
void positioned(){
osgWidget::Label::positioned();
_window->setOrigin(getX(),getHeight());
_window->resize(getWidth());
}
bool mousePush(double,double,const osgWidget::WindowManager * wm)
{
if(!_window->isVisible()) _window->show();
else
_window->hide();
return true;
}
bool mouseLeave(double,double,const osgWidget::WindowManager *) {
if(!_window->isVisible()) setColor(0.8f,0.8f,0.8f,0.8f);
return true;
}
private:
std::string strMenuName;
};
int main(int argc,char **argv)
{
osgWidget::WindowManager* wm=new osgWidget::WindowManager(viewer,800.0f,600.0f,1,osgWidget::WindowManager::WM_USE_RENDERBINS);
osgWidget::Window* menu=new osgWidget::Box("menu",osgWidget::Box::HORIZONTAL);
menu->addWidget(new ColorLabelMenu("File"));
menu->addWidget(new ColorLabelMenu("Module"));
wm->addChild(menu);
menu->getBackground()->setColor(1.0f,1.0f,1.0f,0.0f);
menu->resizePercent(50.0f);
osg::ref_ptr<osg::Group> gp=new osg::Group();
gp->addChild(cow);
osgWidget::createExample(*viewer,wm,gp);
return 0;
}