Osg-OSG利用DrawCallback实现模型重绘(Qt5.14.2+osgE3.6.5+win10)-No25-DrawCallback

相关资料:

 

实例代表:

.pro

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

main.cpp

 1 #include <QApplication>
 2 
 3 #include <osg/Node>
 4 #include <osg/Group>
 5 #include <osg/Geode>
 6 #include <osg/Geometry>
 7 #include <osg/Texture2D>
 8 #include <osg/StateSet>
 9 #include <osg/PositionAttitudeTransform>
10 #include <osgViewer/Viewer>
11 #include <osgDB/ReadFile>
12 #include <osgParticle/PrecipitationEffect>
13 // 雨雪效果
14 #include <osg/MatrixTransform>
15 // 粒子效果
16 #include <osgParticle/PrecipitationEffect>
17 #include <osgParticle/Particle>
18 #include <osgParticle/LinearInterpolator>
19 #include <osgParticle/ParticleSystem>
20 #include <osgParticle/RandomRateCounter>
21 #include <osgParticle/PointPlacer>
22 #include <osgParticle/RadialShooter>
23 #include <osgParticle/ModularEmitter>
24 #include <osgParticle/ParticleSystemUpdater>
25 #include <osgParticle/ModularProgram>
26 #include <osgUtil/Optimizer>
27 #include <osgUtil/Simplifier>
28 #include <osgParticle/FireEffect>
29 //
30 #include <osg/Fog>
31 #include <osgDB/ReadFile>
32 #include <osgViewer/Viewer>
33 #include <osg/StateSet>
34 #include <osg/StateAttribute>
35 #include <osgViewer/ViewerEventHandlers>
36 #include <osgWidget/ViewerEventHandlers>
37 //
38 #include <QDebug>
39 
40 class MyDrawCallback : public osg::Drawable::DrawCallback
41 {
42 public:
43     void drawImplementation(osg::RenderInfo &renderInfo, const osg::Drawable *drawable) const
44     {
45         qDebug() <<"draw call back - pre drawImplementation"<<drawable ;
46         drawable->drawImplementation(renderInfo);
47         qDebug() <<"draw call back - post drawImplementation"<<drawable ;
48 
49         static double dColor = 0;//颜色
50         glColor3f(dColor, 0, 0);
51         glBegin(GL_TRIANGLES);//在OSG中画一个opengl三角形
52         glVertex3f(0.0, 0.0, -2.0);
53         glVertex3f(0.2, 0.0, -2.0);
54         glVertex3f(0.0, 0.4, -2.0);
55         glEnd();
56         dColor += 0.01;//颜色渐变
57         if (dColor > 1.0)
58         {
59             dColor = 0.0;
60         }
61     }
62 };
63 
64 int main(int argc, char *argv[])
65 {
66     osgViewer::Viewer viewer;
67     osg::ref_ptr<osg::Group> root=new osg::Group();
68     osg::ref_ptr<osg::Node> osgcool=osgDB::readNodeFile("D:\\Gitee\\data\\cow.osg");
69 
70     osg::Group* ttt=osgcool->asGroup();
71     osg::Geometry* geom = ttt->getChild(0)->asGeode()->getChild(0)->asGeometry();
72     geom->setUseDisplayList(false);
73     geom->setDrawCallback(new MyDrawCallback());
74 
75     osg::ref_ptr<osg::MatrixTransform> trans=new osg::MatrixTransform();
76     // osg::Matrix::scale(0.5,0.5,0.5)表示缩放的比例,也就是原来物体的一般大小
77     // *osg::Matrix::translate(0,-10,0)表示平移正前
78     trans->setMatrix(osg::Matrix::scale(0.5,0.5,0.5)*osg::Matrix::translate(0,-10,0));
79     trans->addChild(osgcool.get());
80 
81     root->addChild(osgcool.get());
82     root->addChild(trans.get());
83 
84     viewer.setUpViewInWindow(50,50,500,400);
85     viewer.setSceneData(root.get());
86     viewer.realize();
87     viewer.run();
88 }
View Code

 

 

posted on 2022-08-11 18:36  疯狂delphi  阅读(87)  评论(0编辑  收藏  举报

导航