Open Inventor练习-三维物体选择操作

选择(SoSelection)是三维操作的基本功能,三维物体变换也是三维场景中的变换形式,这里将二者相结合,用户选择模式下,选中物体,可以实现添加和删除变换外围立方体的切换,操作变换立方体SoTransformBoxManip,可以实现物体的变换。如下式详细代码。

#define COIN_DLL  
#define SOWIN_DLL  
// 加载COIN库文件  
#ifdef _DEBUG  
#pragma comment(lib, "SoWin1d.lib")  
#pragma comment(lib, "Coin3d.lib")  
#else  
#pragma comment(lib, "SoWin1.lib")  
#pragma comment(lib, "Coin3.lib")  
#endif  
// 添加COIN头文件-Window操作显示库和节点库  
#include <Inventor/Win/viewers/SoWinExaminerViewer.h>
#include <Inventor/Win/SoWin.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoSphere.h>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoTranslation.h>
#include <Inventor/nodes/SoSelection.h>
#include <Inventor/actions/SoSearchAction.h>
#include <Inventor/manips/SoTransformBoxManip.h>

static SoSearchAction * searcher;
static SoSelection * selection;

SoPath * findRelatedTransform(SoPath * pathtocube) 
{
	searcher->reset();
	searcher->setInterest(SoSearchAction::LAST);
	searcher->setType(SoTransform::getClassTypeId());
	searcher->apply(pathtocube);
	return searcher->getPath(); 
}

void selectionCB(void * userdata, SoPath * selection) 
{
	SoPath * transform = findRelatedTransform(selection);
	assert(transform != NULL);
	SoTransformBoxManip * manip = new SoTransformBoxManip;
	manip->replaceNode(transform);
}

void deselectionCB(void * userdata, SoPath * deselection) 
{
	SoPath * transform = findRelatedTransform(deselection);
	assert(transform != NULL);
	SoTransformBoxManip * manip = (SoTransformBoxManip *) transform->getTail();
	manip->replaceManip(transform, new SoTransform);
}

SoNode * makeTransformableCube(float x, float y, float z)
{
	SoTransform * transform = new SoTransform;
	transform->translation.setValue(SbVec3f(x, y, z));
	SoSeparator * group = new SoSeparator;
	group->addChild(transform);
	group->addChild(new SoCube);
	return group;
}

SoNode * makeSceneGraph(void)
{
	selection = new SoSelection;
	selection->addChild(makeTransformableCube(0,0,0));
	selection->addChild(makeTransformableCube(3,0,0));
	selection->addChild(makeTransformableCube(3,3,0));
	selection->addChild(makeTransformableCube(0,3,0));
	selection->addSelectionCallback(selectionCB, NULL);
	selection->addDeselectionCallback(deselectionCB, NULL);
	return selection;
}

int main(int argc, char ** argv) 
{
	HWND w = SoWin::init(argv[0]);
	SoWinExaminerViewer * viewer = new SoWinExaminerViewer(w);
	viewer->show();
	SoWin::show(w);
	viewer->setSceneGraph(makeSceneGraph());
	viewer->redrawOnSelectionChange(selection);
	searcher = new SoSearchAction;
	SoWin::mainLoop();
	return 0;
}

演示效果如下


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值