Osg-Osg实例圆柱体对象局部旋转效果(Qt5.14.2+osgEarht3.6.5+win10)-No11-CylinderRotate


相关资料:

https://www.cnblogs.com/kekec/archive/2011/08/15/2139893.html     osg中使用MatrixTransform来实现模型的平移/旋转/缩放

代码实例:

.pro

 1 QT       += core gui widgets
 2 TARGET = TestOsgQt
 3 TEMPLATE = app
 4 DEFINES += QT_DEPRECATED_WARNINGS
 5 CONFIG += c++11
 6 
 7 SOURCES += \
 8         main.cpp
 9 
10 HEADERS +=
11 
12 OsgDir = D:\\RuanJian\\osg365R
13 CONFIG(release, debug|release) {
14         LIBS += -L$${OsgDir}/lib/ -losgDB -losgViewer -lOpenThreads -losgAnimation -losg \
15                                   -losgEarth -losgEarthAnnotation -losgEarthFeatures -losgEarthSymbology -losgEarthUtil \
16                                   -losgQOpenGL -losgUtil -losgText -losgTerrain -losgSim \
17                                   -losgShadow -losgParticle -losgManipulator -losgGA -losgFX \
18                                   -losgWidget
19 } else {
20         LIBS += -L$${OsgDir}/debug/lib/ -losgDBd -losgViewerd -lOpenThreadsd -losgAnimationd -losgd \
21                                   -losgEarthd -losgEarthAnnotationd -losgEarthFeaturesd -losgEarthSymbologyd -losgEarthUtild \
22                                   -losgQOpenGLd -losgUtild -losgTextd -losgTerraind -losgSimd \
23                                   -losgShadowd -losgParticled -losgManipulatord -losgGAd -losgFXd \
24 }
25 
26 
27 INCLUDEPATH += $${OsgDir}/include
28 DEPENDPATH += $${OsgDir}/include

main.cpp

 1 #include 
 2 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 #include 
 8 #include 
 9 #include 
10 #include 
11 #include 
12 #include 
13 // 雨雪效果
14 #include 
15 // 粒子效果
16 #include 
17 #include 
18 #include 
19 #include 
20 #include 
21 #include 
22 #include 
23 #include 
24 #include 
25 #include 
26 #include 
27 #include 
28 #include 
29 //
30 #include 
31 #include 
32 #include 
33 #include 
34 #include 
35 #include 
36 #include 
37 // 旋转
38 #include 
39 #include 
40 #include 
41 #include 
42 #include 
43 #include 
44 // 圆形
45 #include 
46 
47 int main(int argc, char *argv[])
48 { 
49     // 创建圆柱体
50     double r = 0.5;
51     double h = 3.0;
52     osg::Vec3 orginPt(0.0, 0.0, 0.0);
53     osg::ref_ptr cylinderGeode = new osg::Geode;
54     osg::ref_ptr geoCylinder = new osg::Cylinder(orginPt, r, h);
55     osg::ref_ptr cylinderDrawable = new osg::ShapeDrawable(geoCylinder.get());
56     cylinderDrawable->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
57     cylinderGeode->addDrawable(cylinderDrawable.get());
58 
59     // -- 以下操作都是针对局部坐标系而言 --
60     // 先将圆柱体平移(20.0, -12.0, -35.0)
61     // 再将z轴方向旋转至向量n方向  此时局部坐标系的z轴和n向量一致
62     // 接着,将旋转后的模型的沿z方向平移0.5*h长度 (全局坐标系,相当于沿n方向平移0.5*h长度)
63     // 最后将模型放大2倍
64     osg::Vec3 n(1.0, 1.0, -1.0);
65     osg::Vec3 z(0.0, 0.0, 1.0);
66     n.normalize();
67     osg::ref_ptr root = new osg::MatrixTransform;
68     root->setMatrix(osg::Matrix::scale(osg::Vec3(2.0, 2.0, 2.0))*
69                  osg::Matrix::translate(osg::Vec3(0, 0, 0.5*h))*
70             osg::Matrix::rotate(z, n)*
71             osg::Matrix::translate(osg::Vec3(20.0, -12.0, -35.0)));
72     root->addChild(cylinderGeode);
73 
74     osgViewer::Viewer viewer;
75     //创建节点到场景中
76     viewer.setUpViewInWindow(50,50,500,400);
77     viewer.setSceneData(root);
78     viewer.realize();
79     return viewer.run();
80 }

相关