| 1 | |
|---|
| 2 | |
|---|
| 3 | #include <iostream> |
|---|
| 4 | #include <list> |
|---|
| 5 | #include <string> |
|---|
| 6 | #include <fstream> |
|---|
| 7 | #include <sstream> |
|---|
| 8 | |
|---|
| 9 | #include <osg/Geode> |
|---|
| 10 | #include <osg/ShapeDrawable> |
|---|
| 11 | #include <osg/Material> |
|---|
| 12 | #include <osg/Texture2D> |
|---|
| 13 | #include <osg/Geometry> |
|---|
| 14 | #include <osg/MatrixTransform> |
|---|
| 15 | #include <osg/PositionAttitudeTransform> |
|---|
| 16 | #include <osg/BlendFunc> |
|---|
| 17 | #include <osg/ClearNode> |
|---|
| 18 | #include <osg/Depth> |
|---|
| 19 | #include <osg/Projection> |
|---|
| 20 | #include <osg/io_utils> |
|---|
| 21 | |
|---|
| 22 | #include <osgUtil/Tesselator> |
|---|
| 23 | #include <osgUtil/TransformCallback> |
|---|
| 24 | #include <osgUtil/CullVisitor> |
|---|
| 25 | #include <osgUtil/Optimizer> |
|---|
| 26 | |
|---|
| 27 | #include <osgText/Text> |
|---|
| 28 | |
|---|
| 29 | #include <osgGA/TrackballManipulator> |
|---|
| 30 | |
|---|
| 31 | #include <osgProducer/Viewer> |
|---|
| 32 | |
|---|
| 33 | #include <osgDB/ReadFile> |
|---|
| 34 | #include <osgDB/FileUtils> |
|---|
| 35 | |
|---|
| 36 | int runApp(std::string xapp); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | class PickHandler : public osgGA::GUIEventHandler { |
|---|
| 40 | public: |
|---|
| 41 | |
|---|
| 42 | PickHandler(osgProducer::Viewer* viewer,osgText::Text* updateText): |
|---|
| 43 | _viewer(viewer), |
|---|
| 44 | _updateText(updateText) {} |
|---|
| 45 | |
|---|
| 46 | ~PickHandler() {} |
|---|
| 47 | |
|---|
| 48 | bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us); |
|---|
| 49 | |
|---|
| 50 | std::string pick(float x, float y); |
|---|
| 51 | |
|---|
| 52 | void highlight(const std::string& name) |
|---|
| 53 | { |
|---|
| 54 | if (_updateText.get()) _updateText->setText(name); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | protected: |
|---|
| 58 | |
|---|
| 59 | osgProducer::Viewer* _viewer; |
|---|
| 60 | osg::ref_ptr<osgText::Text> _updateText; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) |
|---|
| 64 | { |
|---|
| 65 | switch(ea.getEventType()) |
|---|
| 66 | { |
|---|
| 67 | case(osgGA::GUIEventAdapter::FRAME): |
|---|
| 68 | case(osgGA::GUIEventAdapter::MOVE): |
|---|
| 69 | { |
|---|
| 70 | |
|---|
| 71 | std::string picked_name = pick(ea.getX(),ea.getY()); |
|---|
| 72 | highlight(picked_name); |
|---|
| 73 | return false; |
|---|
| 74 | } |
|---|
| 75 | case(osgGA::GUIEventAdapter::PUSH): |
|---|
| 76 | { |
|---|
| 77 | |
|---|
| 78 | std::string picked_name = pick(ea.getX(),ea.getY()); |
|---|
| 79 | if (!picked_name.empty()) |
|---|
| 80 | { |
|---|
| 81 | runApp(picked_name); |
|---|
| 82 | return true; |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | return false; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | default: |
|---|
| 90 | return false; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | std::string PickHandler::pick(float x, float y) |
|---|
| 96 | { |
|---|
| 97 | osgUtil::IntersectVisitor::HitList hlist; |
|---|
| 98 | if (_viewer->computeIntersections(x, y, hlist)) |
|---|
| 99 | { |
|---|
| 100 | for(osgUtil::IntersectVisitor::HitList::iterator hitr=hlist.begin(); |
|---|
| 101 | hitr!=hlist.end(); |
|---|
| 102 | ++hitr) |
|---|
| 103 | { |
|---|
| 104 | if (hitr->_geode.valid() && !hitr->_geode->getName().empty()) return hitr->_geode->getName(); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | return ""; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | osg::Node* createHUD(osgText::Text* updateText) |
|---|
| 111 | { |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | osg::MatrixTransform* modelview_abs = new osg::MatrixTransform; |
|---|
| 116 | modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 117 | modelview_abs->setMatrix(osg::Matrix::identity()); |
|---|
| 118 | |
|---|
| 119 | osg::Projection* projection = new osg::Projection; |
|---|
| 120 | projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
|---|
| 121 | projection->addChild(modelview_abs); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | std::string timesFont("fonts/times.ttf"); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | osg::Vec3 position(50.0f,510.0f,0.0f); |
|---|
| 128 | osg::Vec3 delta(0.0f,-60.0f,0.0f); |
|---|
| 129 | |
|---|
| 130 | { |
|---|
| 131 | osg::Geode* geode = new osg::Geode(); |
|---|
| 132 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 133 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 134 | stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
|---|
| 135 | geode->setName("The text label"); |
|---|
| 136 | geode->addDrawable( updateText ); |
|---|
| 137 | modelview_abs->addChild(geode); |
|---|
| 138 | |
|---|
| 139 | updateText->setCharacterSize(20.0f); |
|---|
| 140 | updateText->setFont(timesFont); |
|---|
| 141 | updateText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 142 | updateText->setText(""); |
|---|
| 143 | updateText->setPosition(position); |
|---|
| 144 | |
|---|
| 145 | position += delta; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | return projection; |
|---|
| 149 | |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | static osg::Vec3 defaultPos( 0.0f, 0.0f, 0.0f ); |
|---|
| 156 | static osg::Vec3 centerScope(0.0f, 0.0f, 0.0f); |
|---|
| 157 | |
|---|
| 158 | class Xample |
|---|
| 159 | { |
|---|
| 160 | std::string texture; |
|---|
| 161 | std::string app; |
|---|
| 162 | public: |
|---|
| 163 | Xample(std::string image, std::string prog) |
|---|
| 164 | { |
|---|
| 165 | texture = image; |
|---|
| 166 | app = prog; |
|---|
| 167 | osg::notify(osg::INFO) << "New Xample!" << std::endl; |
|---|
| 168 | }; |
|---|
| 169 | ~Xample() { }; |
|---|
| 170 | |
|---|
| 171 | std::string getTexture() |
|---|
| 172 | { |
|---|
| 173 | return texture; |
|---|
| 174 | } |
|---|
| 175 | std::string getApp() |
|---|
| 176 | { |
|---|
| 177 | return app; |
|---|
| 178 | } |
|---|
| 179 | }; |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | typedef std::list<Xample>::iterator OP; |
|---|
| 183 | static std::list<Xample> Xamplelist; |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | void printList() |
|---|
| 187 | { |
|---|
| 188 | osg::notify(osg::INFO) << "start printList()" << std::endl; |
|---|
| 189 | for (OP i = Xamplelist.begin() ; i != Xamplelist.end() ; ++i) |
|---|
| 190 | { |
|---|
| 191 | Xample& x = *i; |
|---|
| 192 | osg::notify(osg::INFO) << "current x.texture = " << x.getTexture() << std::endl; |
|---|
| 193 | osg::notify(osg::INFO) << "current x.app = " << x.getApp() << std::endl; |
|---|
| 194 | } |
|---|
| 195 | osg::notify(osg::INFO) << "end printList()" << std::endl; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | int runApp(std::string xapp) |
|---|
| 200 | { |
|---|
| 201 | osg::notify(osg::INFO) << "start runApp()" << std::endl; |
|---|
| 202 | for (OP i = Xamplelist.begin() ; i != Xamplelist.end() ; ++i) |
|---|
| 203 | { |
|---|
| 204 | Xample& x = *i; |
|---|
| 205 | if(!xapp.compare(x.getApp())) |
|---|
| 206 | { |
|---|
| 207 | osg::notify(osg::INFO) << "app found!" << std::endl; |
|---|
| 208 | |
|---|
| 209 | const char* cxapp = xapp.c_str(); |
|---|
| 210 | |
|---|
| 211 | osg::notify(osg::INFO) << "char* = " << cxapp <<std::endl; |
|---|
| 212 | |
|---|
| 213 | system(cxapp); |
|---|
| 214 | return 1; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | osg::notify(osg::INFO) << "app not found!" << std::endl; |
|---|
| 218 | return 0; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | void readConfFile(char* confFile) |
|---|
| 223 | { |
|---|
| 224 | osg::notify(osg::INFO) << "Start reading confFile" << std::endl; |
|---|
| 225 | |
|---|
| 226 | std::string fileName = osgDB::findDataFile(confFile); |
|---|
| 227 | if (fileName.empty()) |
|---|
| 228 | { |
|---|
| 229 | osg::notify(osg::INFO) << "Config file not found"<<confFile << std::endl; |
|---|
| 230 | return; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | std::ifstream in(fileName.c_str()); |
|---|
| 235 | if (!in) |
|---|
| 236 | { |
|---|
| 237 | osg::notify(osg::INFO) << "File " << fileName << " can not be opened!" << std::endl; |
|---|
| 238 | exit(1); |
|---|
| 239 | } |
|---|
| 240 | std::string imageBuffer; |
|---|
| 241 | std::string appBuffer; |
|---|
| 242 | |
|---|
| 243 | while (!in.eof()) |
|---|
| 244 | { |
|---|
| 245 | std::getline(in, imageBuffer); |
|---|
| 246 | std::getline(in, appBuffer); |
|---|
| 247 | if(imageBuffer == "" || appBuffer == ""); |
|---|
| 248 | else |
|---|
| 249 | { |
|---|
| 250 | osg::notify(osg::INFO) << "imageBuffer: " << imageBuffer << std::endl; |
|---|
| 251 | osg::notify(osg::INFO) << "appBuffer: " << appBuffer << std::endl; |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | Xample tmp(imageBuffer, appBuffer); |
|---|
| 255 | |
|---|
| 256 | Xamplelist.push_back(tmp); |
|---|
| 257 | |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | in.close(); |
|---|
| 262 | |
|---|
| 263 | osg::notify(osg::INFO) << "End reading confFile" << std::endl; |
|---|
| 264 | |
|---|
| 265 | printList(); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | void SetObjectTextureState(osg::Geode *geodeCurrent, std::string texture) |
|---|
| 270 | { |
|---|
| 271 | |
|---|
| 272 | osg::StateSet* stateTexture = geodeCurrent->getOrCreateStateSet(); |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | osg::Image* imgTexture = osgDB::readImageFile( texture ); |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | if (imgTexture) |
|---|
| 279 | { |
|---|
| 280 | |
|---|
| 281 | osg::Texture2D* texCube = new osg::Texture2D; |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | texCube->setImage(imgTexture); |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | stateTexture->setTextureAttributeAndModes(0,texCube,osg::StateAttribute::ON); |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | geodeCurrent->setStateSet(stateTexture); |
|---|
| 291 | } |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | osg::Geode* createTexturedCube(float fRadius,osg::Vec3 vPosition, std::string texture, std::string geodeName) |
|---|
| 296 | { |
|---|
| 297 | |
|---|
| 298 | osg::Box *bCube = new osg::Box(vPosition,fRadius); |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | osg::ShapeDrawable *sdCube = new osg::ShapeDrawable(bCube); |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | osg::Geode* geodeCube = new osg::Geode(); |
|---|
| 306 | geodeCube->setName( geodeName ); |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | SetObjectTextureState(geodeCube, texture); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | geodeCube->addDrawable(sdCube); |
|---|
| 313 | |
|---|
| 314 | return(geodeCube); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | osg::PositionAttitudeTransform* getPATransformation(osg::Node* object, osg::Vec3 position, osg::Vec3 scale, osg::Vec3 pivot) |
|---|
| 319 | { |
|---|
| 320 | osg::PositionAttitudeTransform* tmpTrans = new osg::PositionAttitudeTransform(); |
|---|
| 321 | tmpTrans->addChild( object ); |
|---|
| 322 | |
|---|
| 323 | tmpTrans->setPosition( position ); |
|---|
| 324 | tmpTrans->setScale( scale ); |
|---|
| 325 | tmpTrans->setPivotPoint( pivot ); |
|---|
| 326 | |
|---|
| 327 | return tmpTrans; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | void printBoundings(osg::Node* current, std::string name) |
|---|
| 331 | { |
|---|
| 332 | const osg::BoundingSphere& currentBound = current->getBound(); |
|---|
| 333 | osg::notify(osg::INFO) << name << std::endl; |
|---|
| 334 | osg::notify(osg::INFO) << "center = " << currentBound.center() << std::endl; |
|---|
| 335 | osg::notify(osg::INFO) << "radius = " << currentBound.radius() << std::endl; |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | osg::Group* setupGraph() |
|---|
| 342 | { |
|---|
| 343 | osg::Group* xGroup = new osg::Group(); |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | float defaultRadius = 0.8f; |
|---|
| 348 | |
|---|
| 349 | int itemsInLine = 4; |
|---|
| 350 | float offset = 0.05f; |
|---|
| 351 | float bs = (defaultRadius / 4) + offset; |
|---|
| 352 | float xstart = (3*bs) * (-1); |
|---|
| 353 | float zstart = xstart * (-1); |
|---|
| 354 | float xnext = xstart; |
|---|
| 355 | float znext = zstart; |
|---|
| 356 | float xjump = (2*bs); |
|---|
| 357 | float zjump = xjump; |
|---|
| 358 | osg::Vec3 vScale( 0.5f, 0.5f, 0.5f ); |
|---|
| 359 | osg::Vec3 vPivot( 0.0f, 0.0f, 0.0f ); |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | int z = 1; |
|---|
| 363 | for (OP i = Xamplelist.begin() ; i != Xamplelist.end() ; ++i, ++z) |
|---|
| 364 | { |
|---|
| 365 | Xample& x = *i; |
|---|
| 366 | |
|---|
| 367 | osg::Node* tmpCube = createTexturedCube(defaultRadius, defaultPos, x.getTexture(), x.getApp()); |
|---|
| 368 | printBoundings(tmpCube, x.getApp()); |
|---|
| 369 | osg::Vec3 vPosition( xnext, 0.0f, znext ); |
|---|
| 370 | osg::PositionAttitudeTransform* transX = getPATransformation(tmpCube, vPosition, vScale, vPivot); |
|---|
| 371 | xGroup->addChild( transX ); |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | if(z < itemsInLine) |
|---|
| 375 | xnext += xjump; |
|---|
| 376 | else |
|---|
| 377 | { |
|---|
| 378 | xnext = xstart; |
|---|
| 379 | znext -= zjump; |
|---|
| 380 | z = 0; |
|---|
| 381 | } |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | return xGroup; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | int main( int argc, char **argv ) |
|---|
| 389 | { |
|---|
| 390 | if (argc<=1) |
|---|
| 391 | { |
|---|
| 392 | readConfFile("osg.conf"); |
|---|
| 393 | } |
|---|
| 394 | else |
|---|
| 395 | { |
|---|
| 396 | readConfFile(argv[1]); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | osgProducer::Viewer viewer; |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 404 | |
|---|
| 405 | osg::ref_ptr<osgText::Text> updateText = new osgText::Text; |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | viewer.getEventHandlerList().push_front(new PickHandler(&viewer,updateText.get())); |
|---|
| 409 | |
|---|
| 410 | osg::Group* root = new osg::Group(); |
|---|
| 411 | |
|---|
| 412 | root->addChild( setupGraph() ); |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | root->addChild(createHUD(updateText.get())); |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | viewer.setSceneData( root ); |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | viewer.realize(); |
|---|
| 422 | |
|---|
| 423 | osg::Matrix lookAt; |
|---|
| 424 | lookAt.makeLookAt(osg::Vec3(0.0f, -4.0f, 0.0f), centerScope, osg::Vec3(0.0f, 0.0f, 1.0f)); |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | while( !viewer.done() ) |
|---|
| 429 | { |
|---|
| 430 | |
|---|
| 431 | viewer.sync(); |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | viewer.update(); |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | viewer.setView(lookAt); |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | viewer.frame(); |
|---|
| 443 | |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | viewer.sync(); |
|---|
| 448 | |
|---|
| 449 | return 0; |
|---|
| 450 | } |
|---|