Coin3D三维可视化教程2

这个是在Coin3D三维可视化教程1基础上的继续。

上次介绍了SoRotationXYZ 与时间SoElapsedTime绑定实现动态旋转,这次实现手动旋转的方式。方法是在圆锥的外面添加一个操作器(轨迹球)SoTrackballManip。轨迹球本身看上去像3个围绕在圆锥四周的圆环。当鼠标左键点击轨迹球后,轨迹球以不同的颜色高亮显示自己,以此表明它现在是处于激活状态。当轨迹球处于激活状态时,可以使用鼠标同时旋转轨迹球和位于它内部的物体(在这里是圆锥)。

每次用户旋转轨迹球时,轨迹球的内部数据都会被修改,同时圆锥也会跟它一起旋转。因为渲染区内有一个传感器附着在场景中,所以当场景数据被修改后,场景将会自动被渲染,这样圆锥看上去就运动起来了。

#include "Coin3Dtest1.h"
#include <QtWidgets/QApplication>
#include <Inventor/Qt/SoQt.h>
#include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
#include <Inventor/Qt/SoQtRenderArea.h>
#include <Inventor/engines/SoElapsedTime.h>
#include <Inventor/manips/SoTrackballManip.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoPerspectiveCamera.h>
#include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoCone.h>

int main(int argc, char* argv[])
{
	//Initialize Inventor. This returns a main window to use
	QWidget* myWindow = SoQt::init(argc, argv, argv[0]);
	if (myWindow == NULL)exit(1);

	//Make a scene containing a red cone
	SoSeparator* root = new SoSeparator;
	SoPerspectiveCamera* myCamera = new SoPerspectiveCamera;
	SoMaterial* myMaterial = new SoMaterial;
	root->ref();
	root->addChild(myCamera);
	root->addChild(new SoDirectionalLight);
	root->addChild(new SoTrackballManip);
	myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0);
	root->addChild(myMaterial);
	root->addChild(new SoCone);

	//Create a renderArea in which to see our scene graph
	//The render area will appear within the main window
	SoQtRenderArea* myRenderArea = new SoQtRenderArea(myWindow);

	//Make myCamera see everything
	myCamera->viewAll(root, myRenderArea->getViewportRegion());

	//Put out scene in myRenderArea, change the title
	myRenderArea->setSceneGraph(root);
	myRenderArea->setTitle("Trackball");
	myRenderArea->show();

	SoQt::show(myWindow);  //Display main window
	SoQt::mainLoop();  //Main Inventor event loop

}

运行效果如下:

trackball cone.gif

 

下面介绍一个更强大的交互方式Examiner观察器,使用更简单,它对相机、光照等已经内置了,最早安装测试用的就是这个,在QT平台下是SoQtExaminerViewer, 在MAC平台下是SoXtEXaminerViewer。

Examiner观察器通过修改照相机的参数,允许我们从不同的方位来观察圆锥。它提供了一个用户接口,允许使用鼠标修改照相机在场景中的位置。但是,这里是移动照相机,而不是移动圆锥本身( 这和在现实生活中观察物体是一样的,我们可以旋转物体本身观察它,也可以通过我们自己绕这个物体转动来观察它,这两种观察方式的结果都是一样的。(译者注这和在现实生活中观察物体是一样的,我们可以旋转物体本身观察它,也可以通过我们自己绕这个物体转动来观察它,这两种观察方式的结果都
是一样的。译者注 ))。这个程序不需要创建一个照相机,也不需要调用照相机的viewAll()函数,因为Examiner观察器内部已经自动替我们做好了这些工作。

#include "Coin3Dtest1.h"
#include <QtWidgets/QApplication>
#include <Inventor/Qt/SoQt.h>
#include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
#include <Inventor/Qt/SoQtRenderArea.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoPerspectiveCamera.h>
#include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoCone.h>

int main(int argc, char* argv[])
{
	//Initialize Inventor. This returns a main window to use
	QWidget* myWindow = SoQt::init(argc, argv, argv[0]);
	if (myWindow == NULL)exit(1);

	//Make a scene containing a red cone
	SoSeparator* root = new SoSeparator;
	SoMaterial* myMaterial = new SoMaterial;
	root->ref();
	myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0);
	root->addChild(myMaterial);
	root->addChild(new SoCone);

	//set up viewer
	SoQtExaminerViewer* myViewer = new SoQtExaminerViewer(myWindow);
	myViewer->setSceneGraph(root);
	myViewer->setTitle("Examiner Viewer");
	myViewer->show();

	SoQt::show(myWindow);  //Display main window
	SoQt::mainLoop();  //Main Inventor event loop

}

运行效果如下

Examiner Viewer

posted @ 2022-08-21 10:13  Oliver2022  阅读(237)  评论(0编辑  收藏  举报