osg解析系列-[osgManipulator::*Dragger]- 节点拖拽 应用示例

// 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节点进行缩放,如下图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值