前面我们演示了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类似,设定场景节点,然后显示即可。
如下是程序运行的效果。