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