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