| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #include <osgUtil/Optimizer> |
|---|
| 25 | #include <osgDB/ReadFile> |
|---|
| 26 | #include <osgViewer/Viewer> |
|---|
| 27 | #include <osgViewer/CompositeViewer> |
|---|
| 28 | |
|---|
| 29 | #include <osgGA/TerrainManipulator> |
|---|
| 30 | #include <osgGA/StateSetManipulator> |
|---|
| 31 | #include <osgGA/AnimationPathManipulator> |
|---|
| 32 | #include <osgGA/TrackballManipulator> |
|---|
| 33 | #include <osgGA/FlightManipulator> |
|---|
| 34 | #include <osgGA/DriveManipulator> |
|---|
| 35 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 36 | #include <osgGA/StateSetManipulator> |
|---|
| 37 | #include <osgGA/AnimationPathManipulator> |
|---|
| 38 | #include <osgGA/TerrainManipulator> |
|---|
| 39 | |
|---|
| 40 | #include <osg/Material> |
|---|
| 41 | #include <osg/Geode> |
|---|
| 42 | #include <osg/BlendFunc> |
|---|
| 43 | #include <osg/Depth> |
|---|
| 44 | #include <osg/Projection> |
|---|
| 45 | #include <osg/MatrixTransform> |
|---|
| 46 | #include <osg/Camera> |
|---|
| 47 | #include <osg/io_utils> |
|---|
| 48 | |
|---|
| 49 | #include <osgText/Text> |
|---|
| 50 | |
|---|
| 51 | #include <sstream> |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | class PickHandler : public osgGA::GUIEventHandler { |
|---|
| 55 | public: |
|---|
| 56 | |
|---|
| 57 | PickHandler(osgText::Text* updateText): |
|---|
| 58 | _updateText(updateText) {} |
|---|
| 59 | |
|---|
| 60 | ~PickHandler() {} |
|---|
| 61 | |
|---|
| 62 | bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa); |
|---|
| 63 | |
|---|
| 64 | virtual void pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea); |
|---|
| 65 | |
|---|
| 66 | void setLabel(const std::string& name) |
|---|
| 67 | { |
|---|
| 68 | if (_updateText.get()) _updateText->setText(name); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | protected: |
|---|
| 72 | |
|---|
| 73 | osg::ref_ptr<osgText::Text> _updateText; |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa) |
|---|
| 77 | { |
|---|
| 78 | switch(ea.getEventType()) |
|---|
| 79 | { |
|---|
| 80 | case(osgGA::GUIEventAdapter::PUSH): |
|---|
| 81 | { |
|---|
| 82 | osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa); |
|---|
| 83 | if (view) pick(view,ea); |
|---|
| 84 | return false; |
|---|
| 85 | } |
|---|
| 86 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 87 | { |
|---|
| 88 | if (ea.getKey()=='c') |
|---|
| 89 | { |
|---|
| 90 | osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa); |
|---|
| 91 | osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter(ea); |
|---|
| 92 | event->setX((ea.getXmin()+ea.getXmax())*0.5); |
|---|
| 93 | event->setY((ea.getYmin()+ea.getYmax())*0.5); |
|---|
| 94 | if (view) pick(view,*event); |
|---|
| 95 | } |
|---|
| 96 | return false; |
|---|
| 97 | } |
|---|
| 98 | default: |
|---|
| 99 | return false; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | void PickHandler::pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea) |
|---|
| 104 | { |
|---|
| 105 | osgUtil::LineSegmentIntersector::Intersections intersections; |
|---|
| 106 | |
|---|
| 107 | std::string gdlist=""; |
|---|
| 108 | float x = ea.getX(); |
|---|
| 109 | float y = ea.getY(); |
|---|
| 110 | if (view->computeIntersections(x,y,intersections)) |
|---|
| 111 | { |
|---|
| 112 | for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); |
|---|
| 113 | hitr != intersections.end(); |
|---|
| 114 | ++hitr) |
|---|
| 115 | { |
|---|
| 116 | std::ostringstream os; |
|---|
| 117 | if (!hitr->nodePath.empty() && !(hitr->nodePath.back()->getName().empty())) |
|---|
| 118 | { |
|---|
| 119 | |
|---|
| 120 | os<<"Object \""<<hitr->nodePath.back()->getName()<<"\""<<std::endl; |
|---|
| 121 | } |
|---|
| 122 | else if (hitr->drawable.valid()) |
|---|
| 123 | { |
|---|
| 124 | os<<"Object \""<<hitr->drawable->className()<<"\""<<std::endl; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | os<<" local coords vertex("<< hitr->getLocalIntersectPoint()<<")"<<" normal("<<hitr->getLocalIntersectNormal()<<")"<<std::endl; |
|---|
| 128 | os<<" world coords vertex("<< hitr->getWorldIntersectPoint()<<")"<<" normal("<<hitr->getWorldIntersectNormal()<<")"<<std::endl; |
|---|
| 129 | const osgUtil::LineSegmentIntersector::Intersection::IndexList& vil = hitr->indexList; |
|---|
| 130 | for(unsigned int i=0;i<vil.size();++i) |
|---|
| 131 | { |
|---|
| 132 | os<<" vertex indices ["<<i<<"] = "<<vil[i]<<std::endl; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | gdlist += os.str(); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | setLabel(gdlist); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | osg::Node* createHUD(osgText::Text* updateText) |
|---|
| 142 | { |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | osg::Camera* hudCamera = new osg::Camera; |
|---|
| 150 | hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 151 | hudCamera->setProjectionMatrixAsOrtho2D(0,1280,0,1024); |
|---|
| 152 | hudCamera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 153 | hudCamera->setRenderOrder(osg::Camera::POST_RENDER); |
|---|
| 154 | hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 155 | |
|---|
| 156 | std::string timesFont("fonts/times.ttf"); |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | osg::Vec3 position(150.0f,800.0f,0.0f); |
|---|
| 160 | osg::Vec3 delta(0.0f,-60.0f,0.0f); |
|---|
| 161 | |
|---|
| 162 | { |
|---|
| 163 | osg::Geode* geode = new osg::Geode(); |
|---|
| 164 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 165 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 166 | stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
|---|
| 167 | geode->setName("simple"); |
|---|
| 168 | hudCamera->addChild(geode); |
|---|
| 169 | |
|---|
| 170 | osgText::Text* text = new osgText::Text; |
|---|
| 171 | geode->addDrawable( text ); |
|---|
| 172 | |
|---|
| 173 | text->setFont(timesFont); |
|---|
| 174 | text->setText("Picking in Head Up Displays is simple!"); |
|---|
| 175 | text->setPosition(position); |
|---|
| 176 | |
|---|
| 177 | position += delta; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | for (int i=0; i<5; i++) { |
|---|
| 182 | osg::Vec3 dy(0.0f,-30.0f,0.0f); |
|---|
| 183 | osg::Vec3 dx(120.0f,0.0f,0.0f); |
|---|
| 184 | osg::Geode* geode = new osg::Geode(); |
|---|
| 185 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 186 | const char *opts[]={"One", "Two", "Three", "January", "Feb", "2003"}; |
|---|
| 187 | osg::Geometry *quad=new osg::Geometry; |
|---|
| 188 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 189 | stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
|---|
| 190 | std::string name="subOption"; |
|---|
| 191 | name += " "; |
|---|
| 192 | name += std::string(opts[i]); |
|---|
| 193 | geode->setName(name); |
|---|
| 194 | osg::Vec3Array* vertices = new osg::Vec3Array(4); |
|---|
| 195 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 196 | colors = new osg::Vec4Array; |
|---|
| 197 | colors->push_back(osg::Vec4(0.8-0.1*i,0.1*i,0.2*i, 1.0)); |
|---|
| 198 | quad->setColorArray(colors); |
|---|
| 199 | quad->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); |
|---|
| 200 | (*vertices)[0]=position; |
|---|
| 201 | (*vertices)[1]=position+dx; |
|---|
| 202 | (*vertices)[2]=position+dx+dy; |
|---|
| 203 | (*vertices)[3]=position+dy; |
|---|
| 204 | quad->setVertexArray(vertices); |
|---|
| 205 | quad->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
|---|
| 206 | geode->addDrawable(quad); |
|---|
| 207 | hudCamera->addChild(geode); |
|---|
| 208 | |
|---|
| 209 | position += delta; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | { |
|---|
| 215 | osg::Geode* geode = new osg::Geode(); |
|---|
| 216 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 217 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 218 | stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
|---|
| 219 | geode->setName("The text label"); |
|---|
| 220 | geode->addDrawable( updateText ); |
|---|
| 221 | hudCamera->addChild(geode); |
|---|
| 222 | |
|---|
| 223 | updateText->setCharacterSize(20.0f); |
|---|
| 224 | updateText->setFont(timesFont); |
|---|
| 225 | updateText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 226 | updateText->setText(""); |
|---|
| 227 | updateText->setPosition(position); |
|---|
| 228 | updateText->setDataVariance(osg::Object::DYNAMIC); |
|---|
| 229 | |
|---|
| 230 | position += delta; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | return hudCamera; |
|---|
| 234 | |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | int main( int argc, char **argv ) |
|---|
| 238 | { |
|---|
| 239 | |
|---|
| 240 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments); |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | if (!scene) scene = osgDB::readNodeFile("fountain.osg"); |
|---|
| 247 | |
|---|
| 248 | osg::ref_ptr<osg::Group> group = dynamic_cast<osg::Group*>(scene.get()); |
|---|
| 249 | if (!group) |
|---|
| 250 | { |
|---|
| 251 | group = new osg::Group; |
|---|
| 252 | group->addChild(scene.get()); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | osg::ref_ptr<osgText::Text> updateText = new osgText::Text; |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | group->addChild(createHUD(updateText.get())); |
|---|
| 259 | |
|---|
| 260 | if (arguments.read("--CompositeViewer")) |
|---|
| 261 | { |
|---|
| 262 | osg::ref_ptr<osgViewer::View> view = new osgViewer::View; |
|---|
| 263 | |
|---|
| 264 | view->addEventHandler(new PickHandler(updateText.get())); |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | view->setSceneData(group.get()); |
|---|
| 268 | |
|---|
| 269 | view->setUpViewAcrossAllScreens(); |
|---|
| 270 | |
|---|
| 271 | osgViewer::CompositeViewer viewer; |
|---|
| 272 | viewer.addView(view.get()); |
|---|
| 273 | |
|---|
| 274 | return viewer.run(); |
|---|
| 275 | |
|---|
| 276 | } |
|---|
| 277 | else |
|---|
| 278 | { |
|---|
| 279 | osgViewer::Viewer viewer; |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | { |
|---|
| 284 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 285 | |
|---|
| 286 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 287 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 288 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 289 | |
|---|
| 290 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 291 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 292 | |
|---|
| 293 | std::string pathfile; |
|---|
| 294 | char keyForAnimationPath = '5'; |
|---|
| 295 | while (arguments.read("-p",pathfile)) |
|---|
| 296 | { |
|---|
| 297 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 298 | if (apm || !apm->valid()) |
|---|
| 299 | { |
|---|
| 300 | num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 301 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 302 | ++keyForAnimationPath; |
|---|
| 303 | } |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 307 | |
|---|
| 308 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | viewer.addEventHandler(new PickHandler(updateText.get())); |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | viewer.setSceneData(group.get()); |
|---|
| 316 | |
|---|
| 317 | return viewer.run(); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | } |
|---|