Open Inventor练习-SoWin中SoWinRenderArea显示三维场景

前面我们演示了SoWinExaminerViewer在三维显示中的应用,这里我们演示它的基类SoWinRenderArea,他没有SoWinExaminerViewer修饰控制边条等功能,然而SoWinExaminerViewer的绝大部分三维显示功能他都是具有的。这里是用SoWinRenderArea显示四个旋转的立方体,下面是例子代码。

// TestCoin.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#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/SoWin.h>
#include <Inventor/Win/SoWinRenderArea.h>

#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoRotor.h>
#include <Inventor/nodes/SoArray.h>
#include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/nodes/SoPerspectiveCamera.h>
#include <Inventor/nodes/SoSeparator.h>

// Set up a simple scenegraph, just for demonstration purposes.
static SoSeparator* get_scene_graph(void)
{
	SoSeparator * root = new SoSeparator;

	SoGroup * group = new SoGroup;

	SoRotor * rotor = new SoRotor;
	rotor->rotation = SbRotation(SbVec3f(0.2, 0.5, 0.9), M_PI / 4.0);
	group->addChild(rotor);

	SoCube * cube = new SoCube;
	group->addChild(cube);

	SoArray * array = new SoArray;
	array->origin = SoArray::CENTER;
	array->addChild(group);
	array->numElements1 = 2;
	array->numElements2 = 2;
	array->separation1 = SbVec3f(4, 0, 0);
	array->separation2 = SbVec3f(0, 4, 0);

	root->addChild(array);
	return root;
}

int main(int argc, char ** argv)
{
	HWND window = SoWin::init(argv[0]);

	SoSeparator * root = new SoSeparator;
	root->ref();

	SoPerspectiveCamera * camera;
	root->addChild(camera = new SoPerspectiveCamera);

	root->addChild(new SoDirectionalLight);

	SoSeparator * userroot = get_scene_graph();
	root->addChild(userroot);

	SoWinRenderArea * renderarea = new SoWinRenderArea(window);
	camera->viewAll(userroot, renderarea->getViewportRegion());
	renderarea->setSceneGraph(root);
	renderarea->setBackgroundColor(SbColor(0.0f, 0.2f, 0.3f));
	if (argc > 1) 
	{
		renderarea->setTitle(argv[1]);
		renderarea->setIconTitle(argv[1]);
	}
	renderarea->show();

	SoWin::show(window);
	SoWin::mainLoop();

	delete renderarea;
	root->unref();

	return 0;
}

SoCube是场景物体,他和SoCone,SoCylinder,SoSphere是三维显示的基本元素,这里利用了SoArray对SoCube阵列扩展显示,origin是原点控制的阵列扩展位置,子节点就是需要阵列扩展的物体节点,numElements1和numElements2分别控制阵列扩展的航和列数目,与之对应的separation1和separation2这是行和列的间隔大小。

SoRotor是三维变换中旋转变换的控制节点,rotation是表示初始显示时旋转的大小,speed用来设置旋转的速度,speed如果不设置则用默认值,这里是初始化沿着0.2, 0.5, 0.9轴旋转45度,速度默认。

SoPerspectiveCamera是perspective摄像头,用来观察SoWinRenderArea的视境,camera->viewAll(userroot, renderarea->getViewportRegion());就是用SoPerspectiveCamera观察场景中的所有内容;再者SoPerspectiveCamera必须放在需要观察的物体节点前面,否则物体放在了摄像头的后面,是无法观察到的,root->addChild(camera = new SoPerspectiveCamera);就是实现这个功能,我们同样可以利用这个性质来隐藏不想显示的节点内容。

SoWinRenderArea的用法与SoWinExaminerViewer类似,设定场景节点,然后显示即可。

如下是程序运行的效果。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值