| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include <osgDB/ReadFile> |
|---|
| 21 | #include <osgDB/WriteFile> |
|---|
| 22 | #include <osgViewer/Viewer> |
|---|
| 23 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 24 | #include <osgGA/TrackballManipulator> |
|---|
| 25 | #include <osgGA/FlightManipulator> |
|---|
| 26 | #include <osgGA/AnimationPathManipulator> |
|---|
| 27 | #include <osgGA/GUIEventHandler> |
|---|
| 28 | #include <osgGA/StateSetManipulator> |
|---|
| 29 | #include <osg/PolygonMode> |
|---|
| 30 | #include <osgUtil/Optimizer> |
|---|
| 31 | #include <osg/Depth> |
|---|
| 32 | #include <iostream> |
|---|
| 33 | #include <osg/Switch> |
|---|
| 34 | #include <osgSim/MultiSwitch> |
|---|
| 35 | #include <osgSim/DOFTransform> |
|---|
| 36 | |
|---|
| 37 | #include <osg/AlphaFunc> |
|---|
| 38 | #include <osg/BlendFunc> |
|---|
| 39 | |
|---|
| 40 | using namespace osg; |
|---|
| 41 | using namespace osgGA; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | class SwitchDOFVisitor : public osg::NodeVisitor, public osgGA::GUIEventHandler |
|---|
| 45 | { |
|---|
| 46 | public: |
|---|
| 47 | SwitchDOFVisitor(): |
|---|
| 48 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 49 | { |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | virtual void apply(Group& node) |
|---|
| 53 | { |
|---|
| 54 | osgSim::MultiSwitch* pMSwitch = dynamic_cast<osgSim::MultiSwitch*>(&node); |
|---|
| 55 | |
|---|
| 56 | if (pMSwitch) |
|---|
| 57 | { |
|---|
| 58 | mSwitches.push_back(pMSwitch); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | osg::NodeVisitor::apply(node); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | virtual void apply(Transform& node) |
|---|
| 65 | { |
|---|
| 66 | osgSim::DOFTransform* pDof = dynamic_cast<osgSim::DOFTransform*>(&node); |
|---|
| 67 | |
|---|
| 68 | if (pDof) |
|---|
| 69 | { |
|---|
| 70 | mDofs.push_back(pDof); |
|---|
| 71 | pDof->setAnimationOn(true); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | osg::NodeVisitor::apply(node); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void nextSwitch() |
|---|
| 78 | { |
|---|
| 79 | for (size_t i=0; i < mSwitches.size(); i++) |
|---|
| 80 | { |
|---|
| 81 | if (mSwitches[i]->getSwitchSetList().size() > 1) |
|---|
| 82 | { |
|---|
| 83 | |
|---|
| 84 | unsigned int nextSwitchSet = mSwitches[i]->getActiveSwitchSet(); |
|---|
| 85 | nextSwitchSet++; |
|---|
| 86 | if (nextSwitchSet >= mSwitches[i]->getSwitchSetList().size()) |
|---|
| 87 | nextSwitchSet = 0; |
|---|
| 88 | mSwitches[i]->setActiveSwitchSet(nextSwitchSet); |
|---|
| 89 | } |
|---|
| 90 | else if (mSwitches[i]->getSwitchSetList().size() == 1) |
|---|
| 91 | { |
|---|
| 92 | |
|---|
| 93 | osgSim::MultiSwitch::ValueList values = mSwitches[i]->getValueList(0); |
|---|
| 94 | for (size_t j=0; j < values.size(); j++) |
|---|
| 95 | { |
|---|
| 96 | if (values[j]) |
|---|
| 97 | { |
|---|
| 98 | unsigned int nextValue = j+1; |
|---|
| 99 | if (nextValue >= values.size()) |
|---|
| 100 | nextValue = 0; |
|---|
| 101 | mSwitches[i]->setSingleChildOn(0, nextValue); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | void multiplyAnimation(float scale) |
|---|
| 109 | { |
|---|
| 110 | for (size_t i=0; i < mDofs.size(); i++) |
|---|
| 111 | { |
|---|
| 112 | mDofs[i]->setIncrementHPR(mDofs[i]->getIncrementHPR() * scale); |
|---|
| 113 | mDofs[i]->setIncrementScale(mDofs[i]->getIncrementScale() * scale); |
|---|
| 114 | mDofs[i]->setIncrementTranslate(mDofs[i]->getIncrementTranslate() * scale); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 119 | { |
|---|
| 120 | osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa); |
|---|
| 121 | if (!viewer) return false; |
|---|
| 122 | |
|---|
| 123 | if (ea.getHandled()) return false; |
|---|
| 124 | |
|---|
| 125 | if(ea.getEventType()==GUIEventAdapter::KEYDOWN) |
|---|
| 126 | { |
|---|
| 127 | |
|---|
| 128 | int key = ea.getKey() ; |
|---|
| 129 | |
|---|
| 130 | switch( ea.getKey() ) |
|---|
| 131 | { |
|---|
| 132 | case osgGA::GUIEventAdapter::KEY_Right: |
|---|
| 133 | |
|---|
| 134 | nextSwitch(); |
|---|
| 135 | return true; |
|---|
| 136 | break; |
|---|
| 137 | case osgGA::GUIEventAdapter::KEY_Up: |
|---|
| 138 | |
|---|
| 139 | multiplyAnimation(2); |
|---|
| 140 | return true; |
|---|
| 141 | break; |
|---|
| 142 | case osgGA::GUIEventAdapter::KEY_Down: |
|---|
| 143 | |
|---|
| 144 | multiplyAnimation(0.5); |
|---|
| 145 | return true; |
|---|
| 146 | break; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | return false; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | private: |
|---|
| 153 | std::vector<osgSim::MultiSwitch*> mSwitches; |
|---|
| 154 | std::vector<osgSim::DOFTransform*> mDofs; |
|---|
| 155 | }; |
|---|
| 156 | |
|---|
| 157 | void singleWindowSideBySideCameras(osgViewer::Viewer& viewer) |
|---|
| 158 | { |
|---|
| 159 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 160 | if (!wsi) |
|---|
| 161 | { |
|---|
| 162 | osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 163 | return; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | unsigned int width, height; |
|---|
| 167 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | width /= 2; |
|---|
| 173 | height /= 2; |
|---|
| 174 | |
|---|
| 175 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 176 | traits->x = 100; |
|---|
| 177 | traits->y = 100; |
|---|
| 178 | traits->width = width; |
|---|
| 179 | traits->height = height; |
|---|
| 180 | traits->windowDecoration = true; |
|---|
| 181 | traits->doubleBuffer = true; |
|---|
| 182 | traits->sharedContext = 0; |
|---|
| 183 | |
|---|
| 184 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 185 | if (gc.valid()) |
|---|
| 186 | { |
|---|
| 187 | osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<<std::endl; |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); |
|---|
| 192 | gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| 193 | } |
|---|
| 194 | else |
|---|
| 195 | { |
|---|
| 196 | osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | osg::Camera* master = viewer.getCamera(); |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | double fovy, aspectRatio, zNear, zFar; |
|---|
| 204 | master->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar); |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | double windowAspectRatio = double(width)/double(height); |
|---|
| 208 | master->setProjectionMatrixAsPerspective(fovy, windowAspectRatio, 1.0, 10000.0); |
|---|
| 209 | |
|---|
| 210 | master->setName("MasterCam"); |
|---|
| 211 | |
|---|
| 212 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 213 | camera->setCullMask(1); |
|---|
| 214 | camera->setName("Left"); |
|---|
| 215 | camera->setGraphicsContext(gc.get()); |
|---|
| 216 | camera->setViewport(new osg::Viewport(0, 0, width/2, height)); |
|---|
| 217 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 218 | camera->setDrawBuffer(buffer); |
|---|
| 219 | camera->setReadBuffer(buffer); |
|---|
| 220 | viewer.addSlave(camera.get(), osg::Matrixd::scale(1.0,0.5,1.0), osg::Matrixd()); |
|---|
| 221 | |
|---|
| 222 | camera = new osg::Camera; |
|---|
| 223 | camera->setCullMask(2); |
|---|
| 224 | camera->setName("Right"); |
|---|
| 225 | camera->setGraphicsContext(gc.get()); |
|---|
| 226 | camera->setViewport(new osg::Viewport(width/2, 0, width/2, height)); |
|---|
| 227 | buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 228 | camera->setDrawBuffer(buffer); |
|---|
| 229 | camera->setReadBuffer(buffer); |
|---|
| 230 | viewer.addSlave(camera.get(), osg::Matrixd::scale(1.0,0.5,1.0), osg::Matrixd()); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | int main( int argc, char **argv ) |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 237 | |
|---|
| 238 | if (argc<2) |
|---|
| 239 | { |
|---|
| 240 | std::cout << argv[0] <<": requires filename argument." << std::endl; |
|---|
| 241 | return 1; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | osgViewer::Viewer viewer(arguments); |
|---|
| 245 | |
|---|
| 246 | std::string outputfile("output.osg"); |
|---|
| 247 | while (arguments.read("-o",outputfile)) {} |
|---|
| 248 | |
|---|
| 249 | while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); } |
|---|
| 250 | while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); } |
|---|
| 251 | while (arguments.read("-d")) { viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); } |
|---|
| 252 | while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); } |
|---|
| 253 | |
|---|
| 254 | singleWindowSideBySideCameras(viewer); |
|---|
| 255 | |
|---|
| 256 | viewer.setCameraManipulator( new osgGA::TrackballManipulator() ); |
|---|
| 257 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 258 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 259 | viewer.addEventHandler(new osgViewer::WindowSizeHandler()); |
|---|
| 260 | viewer.addEventHandler(new osgViewer::LODScaleHandler()); |
|---|
| 261 | viewer.addEventHandler(new osgGA::StateSetManipulator()); |
|---|
| 262 | |
|---|
| 263 | SwitchDOFVisitor* visit = new SwitchDOFVisitor; |
|---|
| 264 | viewer.addEventHandler(visit); |
|---|
| 265 | |
|---|
| 266 | osg::ref_ptr<osg::Node> loadedModel; |
|---|
| 267 | |
|---|
| 268 | loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 269 | |
|---|
| 270 | if (!loadedModel) |
|---|
| 271 | { |
|---|
| 272 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 273 | return 1; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | osg::Group* group = new osg::Group; |
|---|
| 277 | |
|---|
| 278 | osg::Group* group1 = new osg::Group; |
|---|
| 279 | group1->addChild(loadedModel.get()); |
|---|
| 280 | group1->setNodeMask(1); |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | osgDB::writeNodeFile(*loadedModel.get(), outputfile); |
|---|
| 286 | osg::ref_ptr<osg::Node> convertedModel = osgDB::readNodeFile(outputfile); |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | osg::Group* group2 = new osg::Group; |
|---|
| 291 | group2->addChild(convertedModel.get()); |
|---|
| 292 | group2->setNodeMask(2); |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | loadedModel->accept(*visit); |
|---|
| 296 | convertedModel->accept(*visit); |
|---|
| 297 | |
|---|
| 298 | group->addChild(group1); |
|---|
| 299 | group->addChild(group2); |
|---|
| 300 | |
|---|
| 301 | viewer.setSceneData(group); |
|---|
| 302 | |
|---|
| 303 | viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); |
|---|
| 304 | viewer.realize(); |
|---|
| 305 | |
|---|
| 306 | viewer.run(); |
|---|
| 307 | |
|---|
| 308 | return 0; |
|---|
| 309 | } |
|---|