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 ...
随机推荐
- XMPP开发环境配置
首先配置XMPP开发环境配置需要的软件 先安装xampp-osx-1.8.3-5-installer.dmg 安装成功后launchpad里会多出一个XAMPP(其他),点开里面的manager-os ...
- 一步一步学习Bootstrap系列--表单布局
前言:Bootstrap 属于前端 ui 库,通过现成的ui组件能够迅速搭建前端页面,简直是我们后端开发的福音,通过几个项目的锻炼有必要总结些常用的知识,本篇把常用的Bootstrap表单布局进行归纳 ...
- SQL Server基础知识
1.SQL Server表名为什么要加方括号? 这个不是必须要加,但表名或字段名如果引用了sqlserver中的关键字,数据库会不识别这到底是关键字还是表名(或字段名)时就必须要加. 比如,一个表名叫 ...
- 【Go入门教程2】内置基础类型(Boolean、数值、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
这小节我们将要介绍如何定义变量.常量.Go内置类型以及Go程序设计中的一些技巧. 定义变量 Go语言里面定义变量有多种方式. 使用var关键字是Go最基本的定义变量方式,与C语言不同的是Go把变量类型 ...
- Unix目录结构的来历
作者: 阮一峰 Unix(包含Linux)的初学者,常常会很困惑,不明白目录结构的含义何在. 举例来说,根目录下面有一个子目录/bin,用于存放二进制程序.但是,/usr子目录下面还有/usr/bin ...
- sql语句,多个表之间,通过相同内容字段的联合查询
1 , select j.id, jt.Name, j.ApproveType , j.ProductCode, j.CustomerCo ...
- cain使用教程
Cain & Abel 是由Oxid.it开发的一个针对Microsoft操作系统的免费口令恢复工具.号称穷人使用的L0phtcrack.它的功能十分强大,可以网络嗅探,网络欺骗,破解加密口令 ...
- delphi XE4 隐藏程序在任务管理器和隐藏任务栏
最新处理方法,偶然发现delphi下有个ShowMainForm属性,只要在Application.Initialize;后面加上Application.ShowMainForm := false;就 ...
- testng 教程
Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations 注释,如 @test ...
- 7.openstack之mitaka搭建dashboard
部署控制面板dashboard 控制节点 1.安装软件包 yum install openstack-dashboard -y 2.配置 vim /etc/openstack-dashboard/lo ...