OpenSceneGraph控制模型
OpenSceneGraph控制模型
转自:http://www.cppblog.com/eryar/archive/2012/05/28/176538.html
一、简介
对模型的控制就是修改模型的位置和方向属性,使模型的位置和方向发生改变,通常通过移动、旋转、缩放来实现。在三维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 Transform's 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 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节点类家族来实现几何数据的变换。osg::Transform类继承自osg::Group类,它可以有多个子节点。但是osg::Transform类是一个无法由程序实例化的虚基类。用户应当使用osg::MatrixTransform或osg::PositionAttitudeTransform来替代它,这两个类均继承自osg::Transform类。根据用户程序的需要,可以使用其中任意一个或者同时使用他们。关于类osg::Transform和osg::MatrixTransform类的更多内容,请参考《OpenSceneGraph快速入门》书中的组节点一章。
三、OSG中模型控制实现方法
在OSG中,因为矩阵变换类osg::MatrixTransform继承自osg::Group,所以矩阵变换类可以当作一个特殊节点加入到场景中,矩阵变换类中也可以加入节点,加入的节点就会被这个矩阵变换类处理,可以对加入的节点模型进行移动、旋转、缩放操作。
编程实现模型控制程序,为了简便起见,模型节点仍然从文件中得到。得到模型节点后,分别对其进行移动、旋转和缩放操作。程序代码如下:
//--------------------------------------------------------------------------
: // Copyright (c) 2012 eryar All Rights Reserved.
: //
: // File : Main.cpp
: // Author : eryar@163.com
: // Date : 2012-1-5 21:42
: // Version : 1.0v
: //
: // Description : Model transformations: Translate, Rotate, Scale.
: //
: //==========================================================================
:
: #include <osgDB/ReadFile>
: #include <osgViewer/Viewer>
: #include <osg/MatrixTransform>
: #include <osgViewer/ViewerEventHandlers>
:
: int main(int argc, char* argv[])
: {
: osgViewer::Viewer viewer;
: viewer.addEventHandler(new osgViewer::WindowSizeHandler);
: viewer.addEventHandler(new osgViewer::StatsHandler);
:
: osg::ref_ptr<osg::Group> root = new osg::Group;
: osg::ref_ptr<osg::Node> axes = osgDB::readNodeFile("axes.osgt");
:
: // Translate: Offset along X axis 2 unit;
: osg::ref_ptr<osg::MatrixTransform> mtMove = new osg::MatrixTransform;
: mtMove->setMatrix(osg::Matrix::translate(-, , ));
: mtMove->addChild(axes.get());
:
: // Rotate: Rotate along Z axis about 45 degree then translate along x axis 2 unit.
: osg::ref_ptr<osg::MatrixTransform> mtRotate = new osg::MatrixTransform;
: mtRotate->setMatrix(osg::Matrix::rotate(
: osg::DegreesToRadians(45.0),osg::Z_AXIS) * osg::Matrix::translate(,,));
: mtRotate->addChild(axes.get());
:
: // Scale
: osg::ref_ptr<osg::MatrixTransform> mtScale = new osg::MatrixTransform;
: mtScale->setMatrix(osg::Matrix::scale(0.5,0.5,0.5));
: mtScale->addChild(axes.get());
:
: root->addChild(mtMove);
: root->addChild(mtRotate);
: root->addChild(mtScale);
:
: viewer.setSceneData(root.get());
: viewer.realize();
: return viewer.run();
: }
运行效果如下图所示:
![]()
Figure 3.2 Translate, Scale, Rotate Model
PDF Version:
OSG Transform
OpenSceneGraph控制模型的更多相关文章
- 在DirectX9中使用DXUT定制按钮来控制模型旋转的问题
使用DXUT中的按钮控件类实现 控制模型旋转的过程如下: 1.创建一个CDXUTDialog对话框,并绑定至CDXUTDialogResourceManager对话框资源管理器. 2.绑定回调函数GU ...
- 1.使用脚本控制模型的移动 --《Unity 3D 游戏开发》笔记
由于最新版的unity已经不支持javascript语言啦,本人又是个C#小白,所以记录一下自己写的脚本. first 创建一个模型,放在平面上,调整下角度,就像这样: 然后写一个脚本来控制模型移动: ...
- 图文详解基于角色的权限控制模型RBAC
我们开发一个系统,必然面临权限控制的问题,即不同的用户具有不同的访问.操作.数据权限.形成理论的权限控制模型有:自主访问控制(DAC: Discretionary Access Control).强制 ...
- python RBAC权限控制模型扩展 基于JWT实现
jwt,全称 json web token,是使用一定的加密规则生成的token串来保证登录状态.验证用户身份.做权限认证等工作 以往保存用户登录状态多用session实现,但是当服务涉及多台服务器分 ...
- threejs 鼠标移动控制模型旋转
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Unity在UI界面上显示3D模型/物体,控制模型旋转
Unity3D物体在UI界面的显示 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- osg fbx 绘制坐标轴、控制模型影藏与显示
int main() { osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer; osg::ref_ptr< ...
- 两种RBAC权限控制模型详解
序言 由于最近一直卡在权限控制这个坎上,原来设计的比较简单的权限控制思路已经无法满足比较复杂一些的场景,因此一直在探索一种在大部分场景下比较通用的权限模型. 首先,这里说明一下两种RBAC权限模型分别 ...
- unity3d控制模型的运动
这里就不多做解释了,直接上代码,只为了备忘. public class HeroMove : MonoBehaviour { private float speed;//人物行动速度 private ...
随机推荐
- 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ ProcessBar)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得前两篇文章吗.主要讲述了以“jQuery的方式如何开发插件”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方式各有利 ...
- Android之仿微信Tab滑动
这个项目实现了以下的功能:有三个标签聊天.发现和通讯录,左右滑动下面的ViewPager可以切换不同的标签,且标签下面的蓝色条可以随着手指的滑动来实时滑动.另外,如果第二次滑动到“聊天”界面,可以在“ ...
- sql 比模糊查询速度快的查询方法
INSTR方法: 1:查询TM不包括大学的所有结果集 SELECT TM, ID FROM X_1459307704859 WHERE instr(TM, '大学') = 0; 2:查询TM所有包含大 ...
- [Data Structure] Bit-map空间压缩和快速排序去重
Bit-map是一种很巧妙的数据存储结构.所谓的Bit-map就是用一个bit位来标记某个元素对应的Value,而Key即是该元素.由于采用了Bit为单位来存储数据,可以大大节省存储空间.Bit-ma ...
- 《征服 C 指针》摘录3:数组 与 指针
一.数组 和 指针 的微妙关系 数组 是指将固定个数.相同类型的变量排列起来的对象. 正如之前说明的那样,给指针加 N,指针前进“当前指针指向的变量类型的长度 X N”. 因此,给指向数组的某个元素的 ...
- Linux启动新进程的几种方法汇总
有时候,我们需要在自己的程序(进程)中启动另一个程序(进程)来帮助我们完成一些工作,那么我们需要怎么才能在自己的进程中启动其他的进程呢?在Linux中提供了不少的方法来实现这一点,下面就来介绍一个这些 ...
- OS X EI Capitan安装refind时出现Could not set boot device property: 0xe00002bc
参考:terminal - OSX 10.11 El Capitan - setting boot device property not working ... 解决办法: 1.重启MacMini, ...
- [BZOJ4408][Fjoi 2016]神秘数
[BZOJ4408][Fjoi 2016]神秘数 试题描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1 ...
- Proj.4库的编译及使用
Proj.4库的编译及使用 Proj.4是开源GIS最著名的地图投影库,GRASS GIS, MapServer, PostGIS, Thuban, OGDI, Mapnik, TopoCad, GD ...
- gcc编译时头文件和库文件搜索路径
特殊情况:用户自定义的头文件使用#include"mylib"时,gcc编译器会从当前目录查找头文件 一.头文件 gcc 在编译时寻找所需要的头文件 : ※搜寻会从-I开始( ...