| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| [4333] | 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osgUtil/Optimizer> |
|---|
| 20 | #include <osgDB/ReadFile> |
|---|
| 21 | |
|---|
| 22 | #include <osg/Material> |
|---|
| 23 | #include <osg/Geode> |
|---|
| 24 | #include <osg/BlendFunc> |
|---|
| 25 | #include <osg/Depth> |
|---|
| 26 | #include <osg/Projection> |
|---|
| 27 | #include <osg/PolygonOffset> |
|---|
| 28 | #include <osg/MatrixTransform> |
|---|
| [5757] | 29 | #include <osg/Camera> |
|---|
| [4395] | 30 | #include <osg/FrontFace> |
|---|
| [4333] | 31 | |
|---|
| 32 | #include <osgText/Text> |
|---|
| 33 | |
|---|
| [5991] | 34 | #include <osgGA/TrackballManipulator> |
|---|
| 35 | #include <osgGA/FlightManipulator> |
|---|
| [5992] | 36 | #include <osgGA/StateSetManipulator> |
|---|
| [7505] | 37 | #include <osgViewer/ViewerEventHandlers> |
|---|
| [4333] | 38 | |
|---|
| [5991] | 39 | #include <osgViewer/CompositeViewer> |
|---|
| [4333] | 40 | |
|---|
| [5998] | 41 | #include <osgFX/Scribe> |
|---|
| [5992] | 42 | |
|---|
| [5998] | 43 | #include <osg/io_utils> |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | class PickHandler : public osgGA::GUIEventHandler { |
|---|
| [9564] | 47 | public: |
|---|
| [5998] | 48 | |
|---|
| 49 | PickHandler(): |
|---|
| 50 | _mx(0.0f), |
|---|
| 51 | _my(0.0f) {} |
|---|
| [9564] | 52 | |
|---|
| [5998] | 53 | ~PickHandler() {} |
|---|
| [9564] | 54 | |
|---|
| [5998] | 55 | bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa) |
|---|
| 56 | { |
|---|
| 57 | osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa); |
|---|
| 58 | if (!view) return false; |
|---|
| [9564] | 59 | |
|---|
| [5998] | 60 | switch(ea.getEventType()) |
|---|
| 61 | { |
|---|
| 62 | case(osgGA::GUIEventAdapter::PUSH): |
|---|
| 63 | { |
|---|
| 64 | _mx = ea.getX(); |
|---|
| 65 | _my = ea.getY(); |
|---|
| 66 | break; |
|---|
| 67 | } |
|---|
| 68 | case(osgGA::GUIEventAdapter::RELEASE): |
|---|
| 69 | { |
|---|
| 70 | if (_mx==ea.getX() && _my==ea.getY()) |
|---|
| 71 | { |
|---|
| 72 | pick(view, ea.getX(), ea.getY()); |
|---|
| 73 | } |
|---|
| 74 | break; |
|---|
| 75 | } |
|---|
| 76 | default: |
|---|
| 77 | break; |
|---|
| 78 | } |
|---|
| 79 | return false; |
|---|
| 80 | } |
|---|
| [9564] | 81 | |
|---|
| [5998] | 82 | void pick(osgViewer::View* view, float x, float y) |
|---|
| 83 | { |
|---|
| 84 | osg::Node* node = 0; |
|---|
| 85 | osg::Group* parent = 0; |
|---|
| 86 | |
|---|
| 87 | osgUtil::LineSegmentIntersector::Intersections intersections; |
|---|
| 88 | if (view->computeIntersections(x, y, intersections)) |
|---|
| 89 | { |
|---|
| 90 | osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin(); |
|---|
| 91 | osg::NodePath& nodePath = intersection.nodePath; |
|---|
| 92 | node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
|---|
| 93 | parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
|---|
| [9564] | 94 | } |
|---|
| [5998] | 95 | |
|---|
| 96 | |
|---|
| 97 | if (parent && node) |
|---|
| 98 | { |
|---|
| 99 | |
|---|
| 100 | osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent); |
|---|
| 101 | if (!parentAsScribe) |
|---|
| 102 | { |
|---|
| 103 | |
|---|
| 104 | osgFX::Scribe* scribe = new osgFX::Scribe(); |
|---|
| 105 | scribe->addChild(node); |
|---|
| 106 | parent->replaceChild(node,scribe); |
|---|
| 107 | } |
|---|
| 108 | else |
|---|
| 109 | { |
|---|
| 110 | |
|---|
| 111 | osg::Node::ParentList parentList = parentAsScribe->getParents(); |
|---|
| 112 | for(osg::Node::ParentList::iterator itr=parentList.begin(); |
|---|
| 113 | itr!=parentList.end(); |
|---|
| 114 | ++itr) |
|---|
| 115 | { |
|---|
| 116 | (*itr)->replaceChild(parentAsScribe,node); |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | } |
|---|
| [9564] | 122 | |
|---|
| [5998] | 123 | float _mx, _my; |
|---|
| 124 | |
|---|
| 125 | }; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| [4333] | 128 | int main( int argc, char **argv ) |
|---|
| 129 | { |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| [9564] | 133 | |
|---|
| [4333] | 134 | |
|---|
| 135 | osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments); |
|---|
| 136 | |
|---|
| [5991] | 137 | if (!scene) return 1; |
|---|
| 138 | |
|---|
| 139 | |
|---|
| [7536] | 140 | osgViewer::CompositeViewer viewer(arguments); |
|---|
| [7505] | 141 | |
|---|
| [9564] | 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| [5991] | 145 | if (arguments.read("-1")) |
|---|
| [5998] | 146 | { |
|---|
| 147 | { |
|---|
| 148 | osgViewer::View* view = new osgViewer::View; |
|---|
| [9564] | 149 | view->setName("Single view"); |
|---|
| [5998] | 150 | view->setSceneData(osgDB::readNodeFile("fountain.osg")); |
|---|
| [9564] | 151 | |
|---|
| [7516] | 152 | view->addEventHandler( new osgViewer::StatsHandler ); |
|---|
| [9564] | 153 | |
|---|
| [5998] | 154 | view->setUpViewAcrossAllScreens(); |
|---|
| 155 | view->setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| 156 | viewer.addView(view); |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | if (arguments.read("-2")) |
|---|
| 161 | { |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | { |
|---|
| 165 | osgViewer::View* view = new osgViewer::View; |
|---|
| [9565] | 166 | view->setName("View one"); |
|---|
| [5998] | 167 | viewer.addView(view); |
|---|
| 168 | |
|---|
| 169 | view->setUpViewOnSingleScreen(0); |
|---|
| 170 | view->setSceneData(scene.get()); |
|---|
| 171 | view->setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator; |
|---|
| 175 | statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet()); |
|---|
| 176 | |
|---|
| 177 | view->addEventHandler( statesetManipulator.get() ); |
|---|
| 178 | } |
|---|
| [9564] | 179 | |
|---|
| [5998] | 180 | |
|---|
| 181 | { |
|---|
| 182 | osgViewer::View* view = new osgViewer::View; |
|---|
| [9565] | 183 | view->setName("View two"); |
|---|
| [5998] | 184 | viewer.addView(view); |
|---|
| 185 | |
|---|
| 186 | view->setUpViewOnSingleScreen(1); |
|---|
| 187 | view->setSceneData(scene.get()); |
|---|
| 188 | view->setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| [9564] | 189 | |
|---|
| [7516] | 190 | view->addEventHandler( new osgViewer::StatsHandler ); |
|---|
| [7505] | 191 | |
|---|
| [9564] | 192 | |
|---|
| [5998] | 193 | |
|---|
| 194 | view->addEventHandler(new PickHandler()); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| [9564] | 198 | |
|---|
| [5998] | 199 | if (arguments.read("-3") || viewer.getNumViews()==0) |
|---|
| [9564] | 200 | { |
|---|
| [5992] | 201 | |
|---|
| [5991] | 202 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| [9564] | 203 | if (!wsi) |
|---|
| [5991] | 204 | { |
|---|
| 205 | osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 206 | return 1; |
|---|
| 207 | } |
|---|
| [4333] | 208 | |
|---|
| [5991] | 209 | unsigned int width, height; |
|---|
| 210 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); |
|---|
| [4660] | 211 | |
|---|
| [5991] | 212 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| [5998] | 213 | traits->x = 100; |
|---|
| 214 | traits->y = 100; |
|---|
| 215 | traits->width = 1000; |
|---|
| 216 | traits->height = 800; |
|---|
| [5991] | 217 | traits->windowDecoration = true; |
|---|
| 218 | traits->doubleBuffer = true; |
|---|
| 219 | traits->sharedContext = 0; |
|---|
| [4660] | 220 | |
|---|
| [5991] | 221 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 222 | if (gc.valid()) |
|---|
| 223 | { |
|---|
| 224 | osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<<std::endl; |
|---|
| [4333] | 225 | |
|---|
| [5991] | 226 | |
|---|
| 227 | |
|---|
| 228 | gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); |
|---|
| 229 | gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| 230 | } |
|---|
| 231 | else |
|---|
| 232 | { |
|---|
| 233 | osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| [5992] | 236 | |
|---|
| 237 | { |
|---|
| 238 | osgViewer::View* view = new osgViewer::View; |
|---|
| [9565] | 239 | view->setName("View one"); |
|---|
| [5998] | 240 | viewer.addView(view); |
|---|
| 241 | |
|---|
| [5992] | 242 | view->setSceneData(scene.get()); |
|---|
| [9565] | 243 | view->getCamera()->setName("Cam one"); |
|---|
| [5998] | 244 | view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height/2)); |
|---|
| [5992] | 245 | view->getCamera()->setGraphicsContext(gc.get()); |
|---|
| 246 | view->setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| [5991] | 247 | |
|---|
| [5992] | 248 | |
|---|
| 249 | osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator; |
|---|
| 250 | statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet()); |
|---|
| 251 | |
|---|
| 252 | view->addEventHandler( statesetManipulator.get() ); |
|---|
| [9564] | 253 | |
|---|
| [7515] | 254 | view->addEventHandler( new osgViewer::StatsHandler ); |
|---|
| 255 | view->addEventHandler( new osgViewer::HelpHandler ); |
|---|
| [7516] | 256 | view->addEventHandler( new osgViewer::WindowSizeHandler ); |
|---|
| 257 | view->addEventHandler( new osgViewer::ThreadingHandler ); |
|---|
| 258 | view->addEventHandler( new osgViewer::RecordCameraPathHandler ); |
|---|
| [5992] | 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | { |
|---|
| 263 | osgViewer::View* view = new osgViewer::View; |
|---|
| [9565] | 264 | view->setName("View two"); |
|---|
| [5998] | 265 | viewer.addView(view); |
|---|
| 266 | |
|---|
| [5992] | 267 | view->setSceneData(scene.get()); |
|---|
| [9565] | 268 | view->getCamera()->setName("Cam two"); |
|---|
| [5998] | 269 | view->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2)); |
|---|
| [5992] | 270 | view->getCamera()->setGraphicsContext(gc.get()); |
|---|
| 271 | view->setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| [9564] | 272 | |
|---|
| [5998] | 273 | |
|---|
| 274 | view->addEventHandler(new PickHandler()); |
|---|
| [9564] | 275 | |
|---|
| [5992] | 276 | } |
|---|
| 277 | |
|---|
| [5999] | 278 | |
|---|
| [5992] | 279 | { |
|---|
| 280 | osgViewer::View* view = new osgViewer::View; |
|---|
| [9565] | 281 | view->setName("View three"); |
|---|
| [5992] | 282 | viewer.addView(view); |
|---|
| 283 | |
|---|
| [5998] | 284 | view->setSceneData(osgDB::readNodeFile("cessnafire.osg")); |
|---|
| 285 | |
|---|
| [9565] | 286 | view->getCamera()->setName("Cam three"); |
|---|
| [5998] | 287 | view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(traits->width) / double(traits->height/2), 1.0, 1000.0); |
|---|
| 288 | view->getCamera()->setViewport(new osg::Viewport(0, traits->height/2, traits->width, traits->height/2)); |
|---|
| [5992] | 289 | view->getCamera()->setGraphicsContext(gc.get()); |
|---|
| 290 | view->setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | } |
|---|
| [7515] | 294 | |
|---|
| [9564] | 295 | |
|---|
| [5991] | 296 | while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); } |
|---|
| [7536] | 297 | while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullDrawThreadPerContext); } |
|---|
| 298 | while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullThreadPerCameraDrawThreadPerContext); } |
|---|
| [9564] | 299 | |
|---|
| [5999] | 300 | |
|---|
| 301 | return viewer.run(); |
|---|
| [4333] | 302 | } |
|---|