osg中使用MatrixTransform來實現模型的平移/旋轉/縮放
阿新 • • 發佈:2019-01-26
MatrixTransform是從Transform - Group繼承而來,因此可以在它的下面掛接Node物件。
通過設定其矩陣,來實現其下子節點的模型變換。
-- 用區域性座標系來理解(區域性座標系又稱慣性座標系,其與模型的相對位置在變換的過程中始終不變)
如下程式碼:
01 |
// 建立圓柱體 |
02 |
double
r = 0.5; |
03 |
double
h = 3.0; |
04 |
osg::Vec3 orginPt(0.0, 0.0, 0.0);
|
05 |
osg::ref_ptr<osg::Geode> cylinderGeode =
new osg::Geode;
|
06 |
osg::ref_ptr<osg::Cylinder> geoCylinder =
new osg::Cylinder(orginPt, r, h);
|
07 |
osg::ref_ptr<osg::ShapeDrawable> cylinderDrawable =
new osg::ShapeDrawable(geoCylinder.get());
|
08 |
cylinderDrawable->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
|
09 |
cylinderGeode->addDrawable(cylinderDrawable.get());
|
10 |
11 |
// -- 以下操作都是針對區域性座標系而言 -- |
12 |
// 先將圓柱體平移(20.0, -12.0, -35.0)
|
13 |
// 再將z軸方向旋轉至向量n方向 此時區域性座標系的z軸和n向量一致
|
14 |
// 接著,將旋轉後的模型的沿z方向平移0.5*h長度 (全域性座標系,相當於沿n方向平移0.5*h長度)
|
15 |
// 最後將模型放大2倍 |
16 |
osg::Vec3 n(1.0, 1.0, -1.0); |
17 |
osg::Vec3 z(0.0, 0.0, 1.0); |
18 |
n.normalize(); |
19 |
osg::ref_ptr<osg::MatrixTransform> mt =
new osg::MatrixTransform;
|
20 |
mt->setMatrix(osg::Matrix::scale(osg::Vec3(2.0, 2.0, 2.0))*
|
21 |
osg::Matrix::translate(osg::Vec3(0, 0, 0.5*h))*
|
22 |
osg::Matrix::rotate(z, n)*
|
23 |
osg::Matrix::translate(osg::Vec3(20.0, -12.0, -35.0)));
|
24 |
mt->addChild(cylinderGeode); |
根據上面的特點,可以計算變化後的模型的三維座標。
對於下圖的包含關係(MatrixTransform A中包含一個MatrixTransform B,MatrixTransform B中包含一個模型)
那麼,模型的新座標 (X, Y, Z) = (x,y,z)* B.matrix * A.matrix;