使用coin3d畫個小模型
阿新 • • 發佈:2019-01-12
coin3d的安裝和配置可參考網上教程。coin3d建模步驟主要分為是實物建模和座標系建模,要學會用分隔符節點 soseparator node。裡邊的座標系變換具體參考如下程式碼:
// stdafx.h : 標準系統包含檔案的包含檔案,
// 或是經常使用但不常更改的
// 特定於專案的包含檔案
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <Inventor/Win/SoWin.h>
#include <Inventor/Win/viewers/SoWinExaminerViewer. h>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoSphere.h>
#include <Inventor/nodes/SoTransform.h>
#include <Inventor/nodes/SoGroup.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoCylinder.h>
#include <Inventor/nodes/SoSeparator.h>
// robot.cpp : 定義控制檯應用程式的入口點。
//
#include "stdafx.h"
SoSeparator *makeScene()
{
SoSeparator* robot = new SoSeparator;
SoSeparator* body = new SoSeparator;
SoSeparator* head = new SoSeparator;
SoSeparator* leftleg = new SoSeparator;
SoSeparator* rightleg = new SoSeparator;
SoSeparator* leg = new SoSeparator;
//軀幹 socylinder圓柱形
SoCylinder* bodycylinder = new SoCylinder;
bodycylinder->radius = 2.5;
bodycylinder->height = 6;
SoMaterial* bronze = new SoMaterial; //設定軀幹顏色材質
bronze->ambientColor.setValue(0.33, 0.22, 0.27);
bronze->diffuseColor.setValue(0.78, 0.57, 0.11);
bronze->specularColor.setValue(0.99, 0.94, 0.81);
bronze->shininess = 0.28;
//建立機器人的基本腿群組,它由大腿、小腿、和腳構成
SoCube* thigh = new SoCube; //大腿 socube方形
thigh->width = 1.2;
thigh->height = 2.2;
thigh->depth = 1.1;
SoCube* calf = new SoCube; //小腿
calf->width = 1.0;
calf->height = 2.2;
calf->depth = 1.0;
SoCube* foot = new SoCube; //腳
foot->width = 0.8;
foot->height = 0.8;
foot->depth = 2.0;
//建立機器人的頭部
SoSphere* headsphere = new SoSphere;
SoMaterial* sliver = new SoMaterial;
sliver->ambientColor.setValue(0.2, 0.2, 0.2);
sliver->diffuseColor.setValue(0.6, 0.6, 0.6);
sliver->specularColor.setValue(0.5, 0.5, 0.5);
sliver->shininess = 0.5;
//座標系建立,自頂向下,方便理解座標系,設定使所有零件接觸
SoTransform* bodytransform = new SoTransform; //預設body座標系與世界座標系重合
bodytransform->translation.setValue(0.0, 0.0, 0.0);
SoTransform* headtransform = new SoTransform;
headtransform->translation.setValue(0.0, 4.0, 0.0); //頭部預設半徑為1,軀幹高為6,所以座標系應該Y軸走4
SoTransform* lefttransform = new SoTransform;
lefttransform->translation.setValue(1.0, -4.1 , 0.0); //因為左腿座標系和大腿座標系重合的,所以Y軸3+1.1 = 4.1
SoTransform* righttransform = new SoTransform;
righttransform->translation.setValue(-1.0, -4.1, 0.0);
//同一行的座標系是相對的,soseparator中
SoTransform* thightransform = new SoTransform;
thightransform->translation.setValue(0.0, 0.0, 0.0); //大腿座標系與leg的座標系重合
SoTransform* calfTransform = new SoTransform; //小腿1.1+1.1
calfTransform->translation.setValue(0.0, -2.2, 0.0);
SoTransform* foottransform = new SoTransform; //腳1.1+0.4
foottransform->translation.setValue(0.0, -1.5, 0.5);
//組建層次結構,從底向下
leg->addChild(thightransform);
leg->addChild(thigh);
leg->addChild(calfTransform);
leg->addChild(calf);
leg->addChild(foottransform);
leg->addChild(foot);
leftleg->addChild(lefttransform);
leftleg->addChild(leg);
rightleg->addChild(righttransform);
rightleg->addChild(leg);
body->addChild(bodytransform);
body->addChild(bronze);
body->addChild(bodycylinder);
body->addChild(leftleg);
body->addChild(rightleg);
head->addChild(headtransform);
head->addChild(sliver);
head->addChild(headsphere);
//組裝頭部和身體,形成機器人
/*SoSeparator* robot = new SoSeparator;*/
robot->addChild(body);
robot->addChild(head);
return robot;
}
int _tmain(int argc, _TCHAR* argv[])
{
HWND mywindow = SoWin::init("");
if (mywindow = NULL)
{
exit(1);
}
SoSeparator* root = new SoSeparator;
root->ref();
root->addChild(makeScene());
SoWinExaminerViewer* myviewer = new SoWinExaminerViewer(mywindow);
myviewer->setSceneGraph(root);
myviewer->setBackgroundColor(SbColor(0.8, 0.8, 0.8));
myviewer->setTitle("open invenor robot");
myviewer->show();
myviewer->viewAll();
SoWin::show(mywindow);
SoWin::mainLoop();
return 0;
}