OpenSceneGraph控制模型
一、简介
对模型的控制就是修改模型的位置和方向属性,使模型的位置和方向发生改变,通常通过移动、旋转、缩放来实现。在三维CAD软件中通常要对模型的位置进行修改,如装配模型时把其中一个零件模型移动一个位置。由计算机图形学知识得三维图形的几何变换可用一个四阶齐次矩阵来表示,即模型的几何变换都是对矩阵进行操作。 二、OSG模型控制
在OSG中,加入模型的默认位置是屏幕中心,对模型的位置、方向控制是通过类osg::MatrixTransform来实现。由类图知,类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 Transforms parent coordinates.
*/
类osg::MatrixTransform是类osg::Transform的子类,它有一个类osg::Matrix的成员变量_matrix来表示其子节点到其父节点的四阶齐次坐标变换。
声明类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 OpenGLs
* 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节点类家族来实现几何数据的变换。osg::Transform类继承自osg::Group类,它可以有多个子节点。但是osg::Transform类是一个无法由程序实例化的虚基类。用户应当使用osg::MatrixTransform或osg::PositionAttitudeTransform来替代它,这两个类均继承自osg::Transform类。根据用户程序的需要,可以使用其中任意一个或者同时使用他们。关于类osg::Transform和osg::MatrixTransform类的更多内容,请参考《OpenSceneGraph快速入门》书中的组节点一章。 三、OSG中模型控制实现方法
在OSG中,因为矩阵变换类osg::MatrixTransform继承自osg::Group,所以矩阵变换类可以当作一个特殊节点加入到场景中,矩阵变换类中也可以加入节点,加入的节点就会被这个矩阵变换类处理,可以对加入的节点模型进行移动、旋转、缩放操作。
编程实现模型控制程序,为了简便起见,模型节点仍然从文件中得到。得到模型节点后,分别对其进行移动、旋转和缩放操作。程序代码如下:
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: }
运行效果如下图所示:
Figure 3.2 Translate, Scale, Rotate Model
PDF Version:
优势特征蒸馏(Privileged Features Distillation)在手淘信息流推荐中的应用 立即下载
相关文章
- 基于system generator的LED控制模型设计
- 54EasyUI 树形菜单- 树形菜单拖放控制
- DB2 权限控制
- AM402和SV660N、IS620N运动控制
- MySQL流程控制语句详解
- 【ROS进阶篇】第十一讲 基于Gazebo和Rviz的机器人联合仿真(运动控制与传感器)
- QT5完成一个数据实时显示控制的Demo
- RxJava 和 RxAndroid 三(生命周期控制和内存优化)
- 2.idea进行debug热部署,如何控制鼠标离开控制台是否立即热部署
- 强化学习学习笔记(四):具有时差(TD)和Q学习的无模型预测与控制
- 算法=逻辑+控制(左耳朵耗子:编程的本质是什么?)
- C++ 多线程阻塞 (多线程同步)(MsgWaitForMultipleObjects)(连着消息一起控制,牛)
- 【转载】 当模型预测控制遇见机器学习
- js一次控制 多个style样式
- ERP系统实施对企业内部控制的利弊分析
- leaflet 根据缩放级别控制图层的显示