Open Inventor_节点与群组节点_以创建二氧化碳分子为例

节点:
节点(Node)是构成场景的基本单元,一个场景可由一个或者多个节点构成,每个节点都可以代表场景中的一个几何对象、属性对象或群组对象,以及这些对象的组合。

群组节点:
在实际的开发应用中,通常并不是使用单个的节点来完成场景的构建,因为那将为后续的操作带来诸多麻烦。对于这个问题,通常使用群组节点来完成各个节点的统一管理。

群组节点都派生自SoGroup类。因此,要创建一个群组,可以首先创建SoGroup的一个实例,然后使用addChild()函数在实例中添加相应的子节点。

以下代码是创建一个CO2分子模型。

#include <Inventor\Win\SoWin.h>
#include <Inventor\Win\viewers\SoWinExaminerViewer.h>
#include <Inventor\nodes\SoGroup.h>
#include <Inventor\nodes\SoMaterial.h>
#include <Inventor\nodes\SoSeparator.h>
#include <Inventor\nodes\SoSphere.h>
#include <Inventor\nodes\SoTransform.h>

SoGroup *makeCO2Molecule()
{
    //创建所有部件
    SoGroup *co2Molecule = new SoGroup;

    SoGroup *carbonGroup = new SoGroup;
    SoMaterial *blackPlastic = new SoMaterial;
    SoSphere *spherel = new SoSphere;

    SoGroup *oxygen1 = new SoGroup;
    SoGroup *oxygen2 = new SoGroup;
    SoTransform *oxygenXform1 = new SoTransform;
    SoTransform *oxygenXform2 = new SoTransform;
    SoMaterial *redPlastic = new SoMaterial;
    SoSphere *sphere2 = new SoSphere;
    SoSphere *sphere3 = new SoSphere;

    blackPlastic->ambientColor.setValue(0, 0, 0);
    blackPlastic->diffuseColor.setValue(0, 0, 0);
    blackPlastic->specularColor.setValue(0.5, 0.5, 0.5);
    blackPlastic->shininess = 0.5;

    oxygenXform1->scaleFactor.setValue(0.75, 0.75, 0.75);
    oxygenXform1->translation.setValue(-1.2, 0, 0);
    oxygenXform2->translation.setValue(3.4, 0, 0);
    redPlastic->ambientColor.setValue(1.0, 0, 0);
    redPlastic->specularColor.setValue(0.5, 0.5, 0.5);
    redPlastic->diffuseColor.setValue(1.0, 0, 0);
    redPlastic->shininess = 0.5;

    co2Molecule->addChild(carbonGroup);
    co2Molecule->addChild(oxygen1);
    co2Molecule->addChild(oxygen2);

    carbonGroup->addChild(blackPlastic);
    carbonGroup->addChild(spherel);
    oxygen1->addChild(oxygenXform1);
    oxygen1->addChild(redPlastic);
    oxygen1->addChild(sphere2);
    oxygen2->addChild(oxygenXform2);
    oxygen2->addChild(sphere3);

    return co2Molecule;
}

int main(int, char **argv)
{
    //初始化open inventor和Windows
    HWND myWindow = SoWin::init(argv[0]);
    if (myWindow == NULL)
        exit(1);

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

    //添加所创建的场景
    root->addChild(makeCO2Molecule());

    SoWinExaminerViewer *myViewer = new SoWinExaminerViewer(myWindow);
    myViewer->setSceneGraph(root);
    myViewer->setBackgroundColor(SbColor(1.0, 1.0, 1.0));
    //将背景色设置为白色
    myViewer->setTitle("CO2");
    myViewer->show();
    myViewer->viewAll();

    SoWin::show(myWindow);//显示主窗口
    SoWin::mainLoop();//进入主事件循环

    return 0;
}

运行后得到的图形如下:
二氧化碳分子

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值