| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #include <osgUtil/Optimizer> |
|---|
| 19 | #include <osgDB/ReadFile> |
|---|
| 20 | #include <osgProducer/Viewer> |
|---|
| 21 | |
|---|
| 22 | #include <osg/Material> |
|---|
| 23 | #include <osg/Geode> |
|---|
| 24 | #include <osg/BlendFunc> |
|---|
| 25 | #include <osg/Depth> |
|---|
| 26 | #include <osg/Projection> |
|---|
| 27 | #include <osg/MatrixTransform> |
|---|
| 28 | #include <osg/CameraNode> |
|---|
| 29 | #include <osg/io_utils> |
|---|
| 30 | |
|---|
| 31 | #include <osgText/Text> |
|---|
| 32 | |
|---|
| 33 | #include <sstream> |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | class PickHandler : public osgGA::GUIEventHandler { |
|---|
| 37 | public: |
|---|
| 38 | |
|---|
| 39 | PickHandler(osgProducer::Viewer* viewer,osgText::Text* updateText): |
|---|
| 40 | _viewer(viewer), |
|---|
| 41 | _updateText(updateText) {} |
|---|
| 42 | |
|---|
| 43 | ~PickHandler() {} |
|---|
| 44 | |
|---|
| 45 | bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us); |
|---|
| 46 | |
|---|
| 47 | virtual void pick(const osgGA::GUIEventAdapter& ea); |
|---|
| 48 | |
|---|
| 49 | void setLabel(const std::string& name) |
|---|
| 50 | { |
|---|
| 51 | if (_updateText.get()) _updateText->setText(name); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | protected: |
|---|
| 55 | |
|---|
| 56 | osgProducer::Viewer* _viewer; |
|---|
| 57 | osg::ref_ptr<osgText::Text> _updateText; |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) |
|---|
| 61 | { |
|---|
| 62 | switch(ea.getEventType()) |
|---|
| 63 | { |
|---|
| 64 | case(osgGA::GUIEventAdapter::FRAME): |
|---|
| 65 | { |
|---|
| 66 | pick(ea); |
|---|
| 67 | } |
|---|
| 68 | return false; |
|---|
| 69 | |
|---|
| 70 | default: |
|---|
| 71 | return false; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | void PickHandler::pick(const osgGA::GUIEventAdapter& ea) |
|---|
| 76 | { |
|---|
| 77 | osgUtil::IntersectVisitor::HitList hlist; |
|---|
| 78 | |
|---|
| 79 | std::string gdlist=""; |
|---|
| 80 | if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist)) |
|---|
| 81 | { |
|---|
| 82 | for(osgUtil::IntersectVisitor::HitList::iterator hitr=hlist.begin(); |
|---|
| 83 | hitr!=hlist.end(); |
|---|
| 84 | ++hitr) |
|---|
| 85 | { |
|---|
| 86 | std::ostringstream os; |
|---|
| 87 | if (hitr->_geode.valid() && !hitr->_geode->getName().empty()) |
|---|
| 88 | { |
|---|
| 89 | |
|---|
| 90 | os<<"Object \""<<hitr->_geode->getName()<<"\""<<std::endl; |
|---|
| 91 | } |
|---|
| 92 | else if (hitr->_drawable.valid()) |
|---|
| 93 | { |
|---|
| 94 | os<<"Object \""<<hitr->_drawable->className()<<"\""<<std::endl; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | os<<" local coords vertex("<< hitr->getLocalIntersectPoint()<<")"<<" normal("<<hitr->getLocalIntersectNormal()<<")"<<std::endl; |
|---|
| 98 | os<<" world coords vertex("<< hitr->getWorldIntersectPoint()<<")"<<" normal("<<hitr->getWorldIntersectNormal()<<")"<<std::endl; |
|---|
| 99 | osgUtil::Hit::VecIndexList& vil = hitr->_vecIndexList; |
|---|
| 100 | for(unsigned int i=0;i<vil.size();++i) |
|---|
| 101 | { |
|---|
| 102 | os<<" vertex indices ["<<i<<"] = "<<vil[i]<<std::endl; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | gdlist += os.str(); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | setLabel(gdlist); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | osg::Node* createHUD(osgText::Text* updateText) |
|---|
| 112 | { |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | osg::CameraNode* hudCamera = new osg::CameraNode; |
|---|
| 120 | hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 121 | hudCamera->setProjectionMatrixAsOrtho2D(0,1280,0,1024); |
|---|
| 122 | hudCamera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 123 | hudCamera->setRenderOrder(osg::CameraNode::POST_RENDER); |
|---|
| 124 | hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 125 | |
|---|
| 126 | std::string timesFont("fonts/times.ttf"); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | osg::Vec3 position(150.0f,800.0f,0.0f); |
|---|
| 130 | osg::Vec3 delta(0.0f,-60.0f,0.0f); |
|---|
| 131 | |
|---|
| 132 | { |
|---|
| 133 | osg::Geode* geode = new osg::Geode(); |
|---|
| 134 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 135 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 136 | stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
|---|
| 137 | geode->setName("simple"); |
|---|
| 138 | hudCamera->addChild(geode); |
|---|
| 139 | |
|---|
| 140 | osgText::Text* text = new osgText::Text; |
|---|
| 141 | geode->addDrawable( text ); |
|---|
| 142 | |
|---|
| 143 | text->setFont(timesFont); |
|---|
| 144 | text->setText("Picking in Head Up Displays is simple!"); |
|---|
| 145 | text->setPosition(position); |
|---|
| 146 | |
|---|
| 147 | position += delta; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | for (int i=0; i<5; i++) { |
|---|
| 152 | osg::Vec3 dy(0.0f,-30.0f,0.0f); |
|---|
| 153 | osg::Vec3 dx(120.0f,0.0f,0.0f); |
|---|
| 154 | osg::Geode* geode = new osg::Geode(); |
|---|
| 155 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 156 | const char *opts[]={"One", "Two", "Three", "January", "Feb", "2003"}; |
|---|
| 157 | osg::Geometry *quad=new osg::Geometry; |
|---|
| 158 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 159 | stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
|---|
| 160 | std::string name="subOption"; |
|---|
| 161 | name += " "; |
|---|
| 162 | name += std::string(opts[i]); |
|---|
| 163 | geode->setName(name); |
|---|
| 164 | osg::Vec3Array* vertices = new osg::Vec3Array(4); |
|---|
| 165 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 166 | colors = new osg::Vec4Array; |
|---|
| 167 | colors->push_back(osg::Vec4(0.8-0.1*i,0.1*i,0.2*i, 1.0)); |
|---|
| 168 | quad->setColorArray(colors); |
|---|
| 169 | quad->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); |
|---|
| 170 | (*vertices)[0]=position; |
|---|
| 171 | (*vertices)[1]=position+dx; |
|---|
| 172 | (*vertices)[2]=position+dx+dy; |
|---|
| 173 | (*vertices)[3]=position+dy; |
|---|
| 174 | quad->setVertexArray(vertices); |
|---|
| 175 | quad->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
|---|
| 176 | geode->addDrawable(quad); |
|---|
| 177 | hudCamera->addChild(geode); |
|---|
| 178 | |
|---|
| 179 | position += delta; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | { |
|---|
| 185 | osg::Geode* geode = new osg::Geode(); |
|---|
| 186 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 187 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 188 | stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
|---|
| 189 | geode->setName("The text label"); |
|---|
| 190 | geode->addDrawable( updateText ); |
|---|
| 191 | hudCamera->addChild(geode); |
|---|
| 192 | |
|---|
| 193 | updateText->setCharacterSize(20.0f); |
|---|
| 194 | updateText->setFont(timesFont); |
|---|
| 195 | updateText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 196 | updateText->setText(""); |
|---|
| 197 | updateText->setPosition(position); |
|---|
| 198 | |
|---|
| 199 | position += delta; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | return hudCamera; |
|---|
| 203 | |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | int main( int argc, char **argv ) |
|---|
| 207 | { |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays."); |
|---|
| 214 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename] ..."); |
|---|
| 215 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | osgProducer::Viewer viewer(arguments); |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 229 | { |
|---|
| 230 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 231 | return 1; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | if (arguments.errors()) |
|---|
| 239 | { |
|---|
| 240 | arguments.writeErrorMessages(std::cout); |
|---|
| 241 | return 1; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments); |
|---|
| 246 | |
|---|
| 247 | osg::ref_ptr<osg::Group> group = dynamic_cast<osg::Group*>(scene.get()); |
|---|
| 248 | if (!group) |
|---|
| 249 | { |
|---|
| 250 | group = new osg::Group; |
|---|
| 251 | group->addChild(scene.get()); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | osg::ref_ptr<osgText::Text> updateText = new osgText::Text; |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | group->addChild(createHUD(updateText.get())); |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | viewer.getEventHandlerList().push_front(new PickHandler(&viewer,updateText.get())); |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | viewer.setSceneData(group.get()); |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | viewer.realize(); |
|---|
| 267 | |
|---|
| 268 | while( !viewer.done() ) |
|---|
| 269 | { |
|---|
| 270 | |
|---|
| 271 | viewer.sync(); |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | viewer.update(); |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | viewer.frame(); |
|---|
| 279 | |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | viewer.sync(); |
|---|
| 284 | |
|---|
| 285 | return 0; |
|---|
| 286 | } |
|---|