OpenSceneGraph控制模型
一、簡介
對模型的控制就是修改模型的位置和方向?qū)傩裕鼓P偷奈恢煤头较虬l(fā)生改變,通常通過移動、旋轉(zhuǎn)、縮放來實(shí)現(xiàn)。在三維CAD軟件中通常要對模型的位置進(jìn)行修改,如裝配模型時(shí)把其中一個(gè)零件模型移動一個(gè)位置。由計(jì)算機(jī)圖形學(xué)知識得三維圖形的幾何變換可用一個(gè)四階齊次矩陣來表示,即模型的幾何變換都是對矩陣進(jìn)行操作。
二、OSG模型控制
在OSG中,加入模型的默認(rèn)位置是屏幕中心,對模型的位置、方向控制是通過類osg::MatrixTransform來實(shí)現(xiàn)。由類圖知,類osg::MatrixTransform繼承自類osg::Transform,而類osg::Transform是由類osg::Group繼承而來。
Figure 3.1 Inheritance Diagram for osg::MatrixTransform
聲明類osg::MatrixTransform中的注釋為:
/** MatrixTransform - is a subclass of Transform which has an osg::Matrix
* which represents a 4x4 transformation of its children from local coordinates
* into the Transform's parent coordinates.
*/
類osg::MatrixTransform是類osg::Transform的子類,它有一個(gè)類osg::Matrix的成員變量_matrix來表示其子節(jié)點(diǎn)到其父節(jié)點(diǎn)的四階齊次坐標(biāo)變換。
聲明類osg::Transform中的注釋為:
/** A Transform is a group node for which all children are transformed by
* a 4x4 matrix. It is often used for positioning objects within a scene,
* producing trackball functionality or for animation.
*
* Transform itself does not provide set/get functions, only the interface
* for defining what the 4x4 transformation is. Subclasses, such as
* MatrixTransform and PositionAttitudeTransform support the use of an
* osg::Matrix or a osg::Vec3/osg::Quat respectively.
*
* Note: If the transformation matrix scales the subgraph then the normals
* of the underlying geometry will need to be renormalized to be unit
* vectors once more. This can be done transparently through OpenGL's
* use of either GL_NORMALIZE and GL_RESCALE_NORMAL modes. For further
* background reading see the glNormalize documentation in the OpenGL
* Reference Guide (the blue book). To enable it in the OSG, you simply
* need to attach a local osg::StateSet to the osg::Transform, and set
* the appropriate mode to ON via
* stateset->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
*/
OSG通過osg::Transform節(jié)點(diǎn)類家族來實(shí)現(xiàn)幾何數(shù)據(jù)的變換。osg::Transform類繼承自osg::Group類,它可以有多個(gè)子節(jié)點(diǎn)。但是osg::Transform類是一個(gè)無法由程序?qū)嵗奶摶悺S脩魬?yīng)當(dāng)使用osg::MatrixTransform或osg::PositionAttitudeTransform來替代它,這兩個(gè)類均繼承自osg::Transform類。根據(jù)用戶程序的需要,可以使用其中任意一個(gè)或者同時(shí)使用他們。關(guān)于類osg::Transform和osg::MatrixTransform類的更多內(nèi)容,請參考《OpenSceneGraph快速入門》書中的組節(jié)點(diǎn)一章。
三、OSG中模型控制實(shí)現(xiàn)方法
在OSG中,因?yàn)榫仃囎儞Q類osg::MatrixTransform繼承自osg::Group,所以矩陣變換類可以當(dāng)作一個(gè)特殊節(jié)點(diǎn)加入到場景中,矩陣變換類中也可以加入節(jié)點(diǎn),加入的節(jié)點(diǎn)就會被這個(gè)矩陣變換類處理,可以對加入的節(jié)點(diǎn)模型進(jìn)行移動、旋轉(zhuǎn)、縮放操作。
編程實(shí)現(xiàn)模型控制程序,為了簡便起見,模型節(jié)點(diǎn)仍然從文件中得到。得到模型節(jié)點(diǎn)后,分別對其進(jìn)行移動、旋轉(zhuǎn)和縮放操作。程序代碼如下:
1: //--------------------------------------------------------------------------
2: // Copyright (c) 2012 eryar All Rights Reserved.
3: //
4: // File : Main.cpp
5: // Author : eryar@163.com
6: // Date : 2012-1-5 21:42
7: // Version : 1.0v
8: //
9: // Description : Model transformations: Translate, Rotate, Scale.
10: //
11: //==========================================================================
12:
13: #include <osgDB/ReadFile>
14: #include <osgViewer/Viewer>
15: #include <osg/MatrixTransform>
16: #include <osgViewer/ViewerEventHandlers>
17:
18: int main(int argc, char* argv[])
19: {
20: osgViewer::Viewer viewer;
21: viewer.addEventHandler(new osgViewer::WindowSizeHandler);
22: viewer.addEventHandler(new osgViewer::StatsHandler);
23:
24: osg::ref_ptr<osg::Group> root = new osg::Group;
25: osg::ref_ptr<osg::Node> axes = osgDB::readNodeFile("axes.osgt");
26:
27: // Translate: Offset along X axis 2 unit;
28: osg::ref_ptr<osg::MatrixTransform> mtMove = new osg::MatrixTransform;
29: mtMove->setMatrix(osg::Matrix::translate(-2, 0, 0));
30: mtMove->addChild(axes.get());
31:
32: // Rotate: Rotate along Z axis about 45 degree then translate along x axis 2 unit.
33: osg::ref_ptr<osg::MatrixTransform> mtRotate = new osg::MatrixTransform;
34: mtRotate->setMatrix(osg::Matrix::rotate(
35: osg::DegreesToRadians(45.0),osg::Z_AXIS) * osg::Matrix::translate(2,0,0));
36: mtRotate->addChild(axes.get());
37:
38: // Scale
39: osg::ref_ptr<osg::MatrixTransform> mtScale = new osg::MatrixTransform;
40: mtScale->setMatrix(osg::Matrix::scale(0.5,0.5,0.5));
41: mtScale->addChild(axes.get());
42:
43: root->addChild(mtMove);
44: root->addChild(mtRotate);
45: root->addChild(mtScale);
46:
47: viewer.setSceneData(root.get());
48: viewer.realize();
49: return viewer.run();
50: }
運(yùn)行效果如下圖所示:
Figure 3.2 Translate, Scale, Rotate Model
PDF Version: