Chinaunix首页 | 论坛 | 博客
  • 博客访问: 571895
  • 博文数量: 208
  • 博客积分: 3286
  • 博客等级: 中校
  • 技术积分: 1780
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-24 20:38
文章分类

全部博文(208)

文章存档

2012年(7)

2011年(28)

2010年(21)

2009年(76)

2008年(65)

2007年(11)

我的朋友

分类:

2009-09-02 15:27:40

线性变换

    在VRML中,Transform节点是一个基本结构,它是一个常见的群组节点(Grouping node),可以作许多物体的容器。然而,Transform提供了更强大的功能,在计算机图形中,“运动”总是和变换(transformation)紧密相连的—无论是缩放比例、旋转或是平移。VRML2.0中,Transform节点将其定义的变换施加于物体。它的各字段的缺省值定义如下:

Transform {

center 0 0 0

translation 0 0 0

rotation 0 0 1 0

scale 1 1 1

scaleOrientation 0 0 1 0

children [ ]

}

    VRML场景图包含许多群组节点以便定义一个方便的等级来操纵场景。当然,也可以把Transform和其它节点组成群组节点(例如,在一个物体等级的内部某一层放置一个传感器,用一个事件触发它,但同时又保留着操纵整个组活动的能力)。

    Transform字段的值,例如旋转,可以由任何Shape节点或它的子节点所继承。子节点的关键字children声明其下面方括号中的节点是父组节点的子节点。在程序例1.4中,我们将介绍场景中名为CUBE的Box和它的外观,然后用Transform父组节点缩放和旋转Box。

程序例5.2.4 cube.wrl

#VRML V2.0 utf8

#move the Box

DEF transformCUBE Transform {

translation –3.0 2.0 1.0

children [

DEF CUBE Shape {

appearance Appearance {

material Material {

diffuseColor 1 0 0

specularColor 0.9 0.9 0.9

}

}

geometry Box { }

}

]

}

图5-2-4 cube.wrl

 

    在此例中,Box节点的位置被转移,沿Z轴的负轴向左平移了3米,沿Y轴正轴向上移了2米,沿Z轴正轴方向移了1米。

    注意:无论何时你使用群组节点来管理VRML场景图形元素,都必须在子节点字段中指定群组节点的内容。父节点定义了本地的坐标空间,子节点从父节点中继承了Transform,依次地这些父节点又继承了它们的父节点中的Transform,使得本地的translation、scaling或rotation 变换经常与较高群组的最后的Transform执行有关。也就是说,群组节点的Transform字段在结构中积累。

    为了检验这个等级结构继承关系,我们在场景中同时对TransformCUBE、TransformCONE群组节点内外添加几何图形对象。

程序例5.2.5 vrmllogo.wrl

#VRML V2.0 utf8

#move the Box

DEF transformCUBE Transform {

translation -3.0 2.0 1.0

children [

DEF CUBE Shape {

appearance Appearance {

material Material{

diffuseColor 1 0 0

specularColor 0.9 0.9 0.9

}

}

geometry Box{}

}

DEF transformBALL Transform {

translation 3.0 –2.0 0

children [

DEF BALL Shape {

appearance Appearance {

material Material{

diffuseColor 1 0 0

specularColor 0.9 0.9 0.9

}

}

geometry Sphere{}

}

]

}

]

}

 

DEF transformCONE Transform {

translation 3 0 0

children [

DEF CONE Shape {

appearance Appearance {

material Material {

diffuseColor 0 0 1

specularColor 0.9 0.9 0.9

}

}

geometry Cone{}

}

]

}

DirectionalLight {

direction -1 -1 -5

intensity 0.8

}

图5-2-5 vrmllogo.wrl

    CONE是在transformCUBE层的外部定义的,它提供了一个指向CUBE和BALL的translation的参考值。CONE不受在CUBE和BALL中完成的Transform的影响。然而,由于BALL的Transform父被定义为CUBE的Transform父的子,所以BALL继承了相同的CUBE Transform。

    正如例中看到的那样,一个群组节点可以被另一个群组节点所包含(相同或不相同类型)并继承它父节点Transform包含的变换。如果CONE对象也包含在这种等级中,那么整个场景将通过高层的Transform的单一变换在3D空间中移动。这种能力在场景图变得复杂时非常有用。

 

    对象的旋转可以通过设置Transform父组节点的Rotation字段值来实现。Rotation字段接受三个在-1.01.0区间的值,这三个值表示旋转轴的方向,其后跟随一个用弧度表示的旋转量。例如,取Rotation值为(1 0 0则定义X轴为旋转轴。旋转的角度是以弧度形式表示的;旋转角的正负,可用右手规则判别。

程序例5.2.6 Rotation.wrl

#VRML V2.0 utf8

DEF transformCUBE Transform {

translation -3.0 2.0 1.0

rotation -1 1 0.8 0.785

children [

DEF CUBE Shape {

appearance Appearance {

material Material{

diffuseColor 1 0 0

specularColor 0.9 0.9 0.9

}

}

geometry Box{}

}

]

}

 

DEF transformBALL Transform {

translation 0.0 2.0 1.0

children[

DEF BALL Shape{

appearance Appearance {

material Material{

diffuseColor 1 0 0

specularColor 0.9 0.9 0.9

}

}

geometry Sphere{}

}

]

}

 

DEF transformCONE Transform {

translation 3 2 1

rotation 0.5 0.1 0.75 0.785

children [

DEF CONE Shape {

appearance Appearance {

material Material {

diffuseColor 0 0 1

specularColor 0.9 0.9 0.9

}

}

geometry Cone{}

}

]

}

DirectionalLight {

direction -2 1 -5

intensity 0.8

}

图5-2-6 Rotation.wrl

 

 

    改变对象的比例可以通过设置Transform父组节点的Scale字段值来实现。该字段的三个值分别表示沿X、Y、Z轴的比例缩放因子。

程序例5.2.7 cyl.wrl

#VRML V2.0 utf8

Transform{

scale 0.5 1 1 #比例缩放

children [

Transform{

rotation 0 0 1 -1.57 #旋转

children [

Shape{geometry Cylinder{}}

]

}

]

}

 

图5-2-7 cyl.wrl

 
 

    使用Center字段定义任意一点,从这点可以计算Transorm层的子节点的translation、rotationscale,这样就可以抵消对场景或物体所作的Transform的操作。Scale操作可以进一步使你为TransformScaleOrienta tion字段赋一个矢量值,完成改变大小前对本地坐标系的旋转。

阅读(721) | 评论(0) | 转发(0) |
0

上一篇:VRML的简单介绍

下一篇:场景交互

给主人留下些什么吧!~~