// M23.12.W6.osgManipulator.Dragger.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
//osg
#include <osg/Texture2D>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/PolygonMode>
#include <osg/PolygonOffset>
#include <osg/OccluderNode>
#include <osg/LineWidth>
#include <osg/io_utils>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgGA/AnimationPathManipulator>
//osgUtil
#include <osgUtil/Optimizer>
//osgManipulator
#include <osgManipulator/TabBoxDragger>
#include <osgManipulator/TabBoxTrackballDragger>
#include <osgManipulator/TabPlaneDragger>
#include <osgManipulator/TabPlaneTrackballDragger>
#include <osgManipulator/Scale1DDragger>
#include <osgManipulator/Scale2DDragger>
#include <osgManipulator/TrackballDragger>
#include <osgManipulator/Translate1DDragger>
#include <osgManipulator/Translate2DDragger>
#include <osgManipulator/TranslateAxisDragger>
#include <osgManipulator/TranslatePlaneDragger>
#include <osgManipulator/RotateCylinderDragger>
#include <osgManipulator/AntiSquish>
//osglib
#ifdef DEBUG
#pragma comment(lib,"OpenThreads_d.lib")
#pragma comment(lib,"osg_d.lib")
#pragma comment(lib,"osgDB_d.lib")
#pragma comment(lib,"osgFX_d.lib")
#pragma comment(lib,"osgGA_d.lib")
#pragma comment(lib,"osgUtil_d.lib")
#pragma comment(lib,"osgViewer_d.lib")
#pragma comment(lib,"osgShadow_d.lib")
#pragma comment(lib,"osgManipulator_d.lib")
#else
#pragma comment(lib,"OpenThreads.lib")
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgFX.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgUtil.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgShadow.lib")
#pragma comment(lib,"osgManipulator.lib")
#endif // DEBUG
osg::Node* addDraggerToScene(osg::Node* node)
{
node->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
osg::MatrixTransform* transform = new osg::MatrixTransform;
transform->addChild(node);
osgManipulator::Scale2DDragger* dragger = new osgManipulator::Scale2DDragger();
dragger->setupDefaultGeometry();
float scale = node->getBound().radius() * 1.6;
dragger->setMatrix(osg::Matrix::scale(scale, scale, scale) *
osg::Matrix::translate(node->getBound().center()));
dragger->addTransformUpdating(transform);
// we want the dragger to handle it's own events automatically
dragger->setHandleEvents(true);
// you'll have to press either than mod key or the specified key to
dragger->setActivationModKeyMask(osgGA::GUIEventAdapter::MODKEY_CTRL);
osg::Group* root = new osg::Group;
root->addChild(dragger);
root->addChild(transform);
return root;
}
int main(int argc, char **argv)
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc, argv);
// construct the viewer.
osgViewer::Viewer viewer(arguments);
// read the scene
osg::ref_ptr<osg::Node> geode_1 = osgDB::readRefNodeFile("Data/cow.osg");
osg::ref_ptr<osg::Node> geode_2 = osgDB::readRefNodeFile("Data/robot.osg");
osg::ref_ptr<osg::MatrixTransform> transform_2 = new osg::MatrixTransform;
transform_2->setMatrix(osg::Matrix::translate(osg::Vec3(10.0f, 10.0f, 0.0f)));
transform_2->addChild(geode_2);
osg::Group* root = new osg::Group;
root->addChild(addDraggerToScene(geode_1));
root->addChild(transform_2);
// optimize the scene graph, remove redundant nodes and state etc.
osgUtil::Optimizer optimizer;
optimizer.optimize(root);
viewer.setSceneData(root);
viewer.run();
return 0;
}
运行结果:按住Ctrl,对cow节点进行缩放,如下图