| 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 | void write_usage(std::ostream& out,const std::string& name) |
|---|
| 25 | { |
|---|
| 26 | out << std::endl; |
|---|
| 27 | out <<"usage:"<< std::endl; |
|---|
| 28 | out <<" "<<name<<" [options] infile1 [infile2 ...]"<< std::endl; |
|---|
| 29 | out << std::endl; |
|---|
| 30 | out <<"options:"<< std::endl; |
|---|
| 31 | out <<" -l libraryName - load plugin of name libraryName"<< std::endl; |
|---|
| 32 | out <<" i.e. -l osgdb_pfb"<< std::endl; |
|---|
| 33 | out <<" Useful for loading reader/writers which can load"<< std::endl; |
|---|
| 34 | out <<" other file formats in addition to its extension."<< std::endl; |
|---|
| 35 | out <<" -e extensionName - load reader/wrter plugin for file extension"<< std::endl; |
|---|
| 36 | out <<" i.e. -e pfb"<< std::endl; |
|---|
| 37 | out <<" Useful short hand for specifying full library name as"<< std::endl; |
|---|
| 38 | out <<" done with -l above, as it automatically expands to"<< std::endl; |
|---|
| 39 | out <<" the full library name appropriate for each platform."<< std::endl; |
|---|
| 40 | out <<std::endl; |
|---|
| 41 | out <<" -stereo - switch on stereo rendering, using the default of,"<< std::endl; |
|---|
| 42 | out <<" ANAGLYPHIC or the value set in the OSG_STEREO_MODE "<< std::endl; |
|---|
| 43 | out <<" environmental variable. See doc/stereo.html for "<< std::endl; |
|---|
| 44 | out <<" further details on setting up accurate stereo "<< std::endl; |
|---|
| 45 | out <<" for your system. "<< std::endl; |
|---|
| 46 | out <<" -stereo ANAGLYPHIC - switch on anaglyphic(red/cyan) stereo rendering."<< std::endl; |
|---|
| 47 | out <<" -stereo QUAD_BUFFER - switch on quad buffered stereo rendering."<< std::endl; |
|---|
| 48 | out <<std::endl; |
|---|
| 49 | out <<" -stencil - use a visual with stencil buffer enabled, this "<< std::endl; |
|---|
| 50 | out <<" also allows the depth complexity statistics mode"<< std::endl; |
|---|
| 51 | out <<" to be used (press 'p' three times to cycle to it)."<< std::endl; |
|---|
| 52 | out << std::endl; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | osg::Node* decorate_with_clip_node(osg::Node* subgraph) |
|---|
| 57 | { |
|---|
| 58 | osg::Group* rootnode = new osg::Group; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 64 | |
|---|
| 65 | osg::PolygonMode* polymode = new osg::PolygonMode; |
|---|
| 66 | polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); |
|---|
| 67 | stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 68 | |
|---|
| 69 | osg::Group* wireframe_subgraph = new osg::Group; |
|---|
| 70 | wireframe_subgraph->setStateSet(stateset); |
|---|
| 71 | wireframe_subgraph->addChild(subgraph); |
|---|
| 72 | rootnode->addChild(wireframe_subgraph); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | osg::MatrixTransform* transform= new osg::MatrixTransform; |
|---|
| 98 | |
|---|
| 99 | osg::NodeCallback* nc = new osgUtil::TransformCallback(subgraph->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f)); |
|---|
| 100 | transform->setUpdateCallback(nc); |
|---|
| 101 | |
|---|
| 102 | osg::ClipNode* clipnode = new osg::ClipNode; |
|---|
| 103 | osg::BoundingSphere bs = subgraph->getBound(); |
|---|
| 104 | bs.radius()*= 0.4f; |
|---|
| 105 | |
|---|
| 106 | osg::BoundingBox bb; |
|---|
| 107 | bb.expandBy(bs); |
|---|
| 108 | |
|---|
| 109 | clipnode->createClipBox(bb); |
|---|
| 110 | clipnode->setCullingActive(false); |
|---|
| 111 | |
|---|
| 112 | transform->addChild(clipnode); |
|---|
| 113 | rootnode->addChild(transform); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | osg::Group* clipped_subgraph = new osg::Group; |
|---|
| 118 | |
|---|
| 119 | clipped_subgraph->setStateSet(clipnode->getStateSet()); |
|---|
| 120 | clipped_subgraph->addChild(subgraph); |
|---|
| 121 | rootnode->addChild(clipped_subgraph); |
|---|
| 122 | |
|---|
| 123 | return rootnode; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | int main( int argc, char **argv ) |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use multi-pass and osg::ClipNode to clip parts of the scene away.."); |
|---|
| 135 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 136 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | osgProducer::Viewer viewer(arguments); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 149 | { |
|---|
| 150 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 151 | return 1; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | if (arguments.errors()) |
|---|
| 159 | { |
|---|
| 160 | arguments.writeErrorMessages(std::cout); |
|---|
| 161 | return 1; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | if (arguments.argc()<=1) |
|---|
| 165 | { |
|---|
| 166 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 167 | return 1; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 172 | if (!loadedModel) |
|---|
| 173 | { |
|---|
| 174 | write_usage(osg::notify(osg::NOTICE),argv[0]); |
|---|
| 175 | return 1; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | osg::Node* rootnode = decorate_with_clip_node(loadedModel); |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | osgUtil::Optimizer optimzer; |
|---|
| 185 | optimzer.optimize(rootnode); |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | viewer.setSceneData(rootnode); |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | viewer.realize(); |
|---|
| 192 | |
|---|
| 193 | while( !viewer.done() ) |
|---|
| 194 | { |
|---|
| 195 | |
|---|
| 196 | viewer.sync(); |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | viewer.update(); |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | viewer.frame(); |
|---|
| 204 | |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | viewer.sync(); |
|---|
| 209 | |
|---|
| 210 | return 0; |
|---|
| 211 | } |
|---|