| 1 | #include <osg/MatrixTransform> |
|---|
| 2 | #include <osg/ClipNode> |
|---|
| 3 | #include <osg/Billboard> |
|---|
| 4 | #include <osg/Geode> |
|---|
| 5 | #include <osg/Group> |
|---|
| 6 | #include <osg/Notify> |
|---|
| 7 | #include <osg/Material> |
|---|
| 8 | #include <osg/PolygonOffset> |
|---|
| 9 | #include <osg/PolygonMode> |
|---|
| 10 | #include <osg/LineStipple> |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/Registry> |
|---|
| 13 | #include <osgDB/ReadFile> |
|---|
| 14 | |
|---|
| 15 | #include <osgGA/TrackballManipulator> |
|---|
| 16 | #include <osgGA/FlightManipulator> |
|---|
| 17 | #include <osgGA/DriveManipulator> |
|---|
| 18 | #include <osgUtil/TransformCallback> |
|---|
| 19 | |
|---|
| 20 | #include <osgProducer/Viewer> |
|---|
| 21 | |
|---|
| 22 | #include <osgUtil/Optimizer> |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | osg::Node* decorate_with_clip_node(osg::Node* subgraph) |
|---|
| 26 | { |
|---|
| 27 | osg::Group* rootnode = new osg::Group; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 33 | |
|---|
| 34 | osg::PolygonMode* polymode = new osg::PolygonMode; |
|---|
| 35 | polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); |
|---|
| 36 | stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 37 | |
|---|
| 38 | osg::Group* wireframe_subgraph = new osg::Group; |
|---|
| 39 | wireframe_subgraph->setStateSet(stateset); |
|---|
| 40 | wireframe_subgraph->addChild(subgraph); |
|---|
| 41 | rootnode->addChild(wireframe_subgraph); |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | osg::MatrixTransform* transform= new osg::MatrixTransform; |
|---|
| 67 | |
|---|
| 68 | osg::NodeCallback* nc = new osgUtil::TransformCallback(subgraph->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f)); |
|---|
| 69 | transform->setUpdateCallback(nc); |
|---|
| 70 | |
|---|
| 71 | osg::ClipNode* clipnode = new osg::ClipNode; |
|---|
| 72 | osg::BoundingSphere bs = subgraph->getBound(); |
|---|
| 73 | bs.radius()*= 0.4f; |
|---|
| 74 | |
|---|
| 75 | osg::BoundingBox bb; |
|---|
| 76 | bb.expandBy(bs); |
|---|
| 77 | |
|---|
| 78 | clipnode->createClipBox(bb); |
|---|
| 79 | clipnode->setCullingActive(false); |
|---|
| 80 | |
|---|
| 81 | transform->addChild(clipnode); |
|---|
| 82 | rootnode->addChild(transform); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | osg::Group* clipped_subgraph = new osg::Group; |
|---|
| 87 | |
|---|
| 88 | clipped_subgraph->setStateSet(clipnode->getStateSet()); |
|---|
| 89 | clipped_subgraph->addChild(subgraph); |
|---|
| 90 | rootnode->addChild(clipped_subgraph); |
|---|
| 91 | |
|---|
| 92 | return rootnode; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | int main( int argc, char **argv ) |
|---|
| 97 | { |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use multi-pass and osg::ClipNode to clip parts of the scene away.."); |
|---|
| 104 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 105 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | osgProducer::Viewer viewer(arguments); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 118 | { |
|---|
| 119 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 120 | return 1; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | if (arguments.errors()) |
|---|
| 128 | { |
|---|
| 129 | arguments.writeErrorMessages(std::cout); |
|---|
| 130 | return 1; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (arguments.argc()<=1) |
|---|
| 134 | { |
|---|
| 135 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 136 | return 1; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 141 | if (!loadedModel) |
|---|
| 142 | { |
|---|
| 143 | return 1; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | osg::Node* rootnode = decorate_with_clip_node(loadedModel); |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | osgUtil::Optimizer optimzer; |
|---|
| 153 | optimzer.optimize(rootnode); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | viewer.setSceneData(rootnode); |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | viewer.realize(); |
|---|
| 160 | |
|---|
| 161 | while( !viewer.done() ) |
|---|
| 162 | { |
|---|
| 163 | |
|---|
| 164 | viewer.sync(); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | viewer.update(); |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | viewer.frame(); |
|---|
| 172 | |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | viewer.sync(); |
|---|
| 177 | |
|---|
| 178 | return 0; |
|---|
| 179 | } |
|---|