Learning OSG programing---osgAnimation(3)
接下来是用createModel函数创建模型:
1 osg::ref_ptr<osg::Group> createModel(bool overlay, osgSim::OverlayNode::OverlayTechnique technique) 2 { 3 osg::Vec3 center(0.0f,0.0f,0.0f); 4 float radius = 100.0f; 5 6 osg::ref_ptr<osg::Group> root = new osg::Group; 7 8 float baseHeight = center.z()-radius*0.5; 9 osg::ref_ptr<osg::Node> baseModel = createBase(osg::Vec3(center.x(), center.y(), baseHeight),radius); 10 osg::ref_ptr<osg::Node> movingModel = createMovingModel(center,radius*0.8f); 11 12 if (overlay) 13 { 14 osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode(technique); 15 overlayNode->setContinuousUpdate(true); 16 overlayNode->setOverlaySubgraph(movingModel); 17 overlayNode->setOverlayBaseHeight(baseHeight-0.01); 18 overlayNode->addChild(baseModel); 19 root->addChild(overlayNode); 20 } 21 else 22 { 23 24 root->addChild(baseModel); 25 } 26 27 root->addChild(movingModel); 28 29 return root; 30 }
这个函数首先根据前面定义的函数,创建方格地板和运动模型。根据函数的参数overlay决定是否建立重叠效果。
若指定创建重叠效果,则根据函数的参数technique创建重叠节点,并将运动模型movingModel设为其重叠的模型。将方格地板加入到重叠节点,并将重叠节点加入到根节点中。
如未指定创建重叠模式,则直接将方格地板和运动模型添加到根节点返回。
最后是主函数代码:
1 int main( int argc, char **argv ) 2 { 3 4 bool overlay = false; 5 osg::ArgumentParser arguments(&argc,argv); 6 while (arguments.read("--overlay")) overlay = true; 7 8 osgSim::OverlayNode::OverlayTechnique technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; 9 while (arguments.read("--object")) { technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; overlay=true; } 10 while (arguments.read("--ortho") || arguments.read("--orthographic")) { technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; overlay=true; } 11 while (arguments.read("--persp") || arguments.read("--perspective")) { technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY; overlay=true; } 12 13 14 // initialize the viewer. 15 osgViewer::Viewer viewer; 16 17 // load the nodes from the commandline arguments. 18 osg::ref_ptr<osg::Group> model = createModel(overlay, technique); 19 if (!model) 20 { 21 return 1; 22 } 23 24 // tilt the scene so the default eye position is looking down on the model. 25 osg::ref_ptr<osg::MatrixTransform> rootnode = new osg::MatrixTransform; 26 rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f)); 27 rootnode->addChild(model); 28 29 // run optimization over the scene graph 30 osgUtil::Optimizer optimzer; 31 optimzer.optimize(rootnode); 32 33 std::string filename; 34 if (arguments.read("-o",filename)) 35 { 36 osgDB::writeNodeFile(*rootnode, filename); 37 return 1; 38 } 39 40 // set the scene to render 41 viewer.setSceneData(rootnode); 42 43 viewer.setCameraManipulator(new osgGA::TrackballManipulator()); 44 45 // viewer.setUpViewOnSingleScreen(1); 46 47 #if 0 48 49 // use of custom simulation time. 50 51 viewer.realize(); 52 53 double simulationTime = 0.0; 54 55 while (!viewer.done()) 56 { 57 viewer.frame(simulationTime); 58 simulationTime += 0.001; 59 } 60 61 return 0; 62 #else 63 64 // normal viewer usage. 65 return viewer.run(); 66 67 #endif 68 }
主函数中,根据运行程序时指定的命令行参数,用createModel函数创建不同的节点对象。最后对生成的节点进行选转,优化等操作。在运行程序时,若在命令行中输入 -o选项,并在其后指定文件名,则程序可将生成的模型保存在指定的文件中。
运行程序,分别指定和不指定overlay选项时,产生不同的效果。
overlay
可以看到地板上会有模型的投影。
nonOverlay
Enjoy!