[31] | 1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer |
---|
| 2 | * |
---|
| 3 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
---|
| 7 | * |
---|
| 8 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
| 9 | * You have to aquire licenses for all used proprietary modules. |
---|
| 10 | * |
---|
| 11 | * This library is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * OpenSceneGraph Public License for more details. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #include <visual_object.h> |
---|
| 18 | |
---|
| 19 | using namespace osgVisual; |
---|
| 20 | |
---|
| 21 | visual_object::visual_object( osg::CoordinateSystemNode* sceneRoot_, std::string nodeName_) |
---|
| 22 | { |
---|
| 23 | // Add this node to Scenegraph |
---|
| 24 | sceneRoot_->addChild( this ); |
---|
| 25 | |
---|
| 26 | // Set Nodename for further identification |
---|
| 27 | this->setName( nodeName_ ); |
---|
| 28 | |
---|
| 29 | // Set callback. |
---|
| 30 | /** \todo: welcher update ist der richtige? voraussichtlich event.) */ |
---|
| 31 | //this->setUpdateCallback( new visual_objectPositionCallback() ); |
---|
| 32 | this->setEventCallback( new visual_objectPositionCallback() ); |
---|
| 33 | |
---|
| 34 | // Init Position and Attitude |
---|
| 35 | lat = 0; |
---|
| 36 | lon = 0; |
---|
| 37 | alt = 0; |
---|
| 38 | |
---|
| 39 | azimuthAngle_psi = 0; |
---|
| 40 | pitchAngle_theta = 0; |
---|
| 41 | bankAngle_phi = 0; |
---|
| 42 | |
---|
| 43 | geometry_offset_rotation.makeRotate( 0.0, 1.0, 1.0, 1.0 ); |
---|
| 44 | |
---|
[205] | 45 | // Init Scale factor |
---|
[31] | 46 | scaleX = 1.0; |
---|
| 47 | scaleY = 1.0; |
---|
| 48 | scaleZ = 1.0; |
---|
| 49 | |
---|
| 50 | // Init cameraOffset |
---|
| 51 | cameraTranslationOffset.makeTranslate( osg::Vec3d(0.0, 0.0, 0.0) ); // Trans: (y, x, -z_down) |
---|
| 52 | cameraRotationOffset.makeRotate( osg::DegreesToRadians( 90.0 ), osg::Vec3(1, 0, 0) ); // Rot: (-y, +x , -z) |
---|
| 53 | |
---|
| 54 | // Geometrynode hinzufügen |
---|
| 55 | geometry = new osg::Group(); |
---|
| 56 | this->addChild( geometry ); |
---|
| 57 | |
---|
| 58 | // Labelnode hinzufügen |
---|
| 59 | labels = new osg::Geode(); |
---|
| 60 | this->addChild( labels ); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | visual_object::~visual_object() |
---|
| 64 | { |
---|
| 65 | |
---|
| 66 | } |
---|
| 67 | |
---|
[202] | 68 | visual_object* visual_object::createNodeFromXMLConfig(osg::CoordinateSystemNode* sceneRoot_, xmlNode* a_node) |
---|
| 69 | { |
---|
[203] | 70 | if(a_node == NULL) |
---|
| 71 | return NULL; |
---|
| 72 | |
---|
| 73 | OSG_NOTIFY( osg::ALWAYS ) << __FUNCTION__ << "Try to creating a new Model.." << std::endl; |
---|
[207] | 74 | |
---|
| 75 | // Prepare Variables |
---|
[205] | 76 | std::string objectname="", filename="", label=""; |
---|
| 77 | bool dynamic = false; |
---|
| 78 | double lat=0.0, lon=0.0, alt=0.0, rot_x=0.0, rot_y=0.0, rot_z=0.0; |
---|
| 79 | double cam_trans_x=0.0, cam_trans_y=0.0, cam_trans_z=0.0, cam_rot_x=0.0, cam_rot_y=0.0, cam_rot_z=0.0; |
---|
| 80 | double geometry_rot_x=0.0, geometry_rot_y=0.0, geometry_rot_z=0.0; |
---|
| 81 | double geometry_scale_x=1.0, geometry_scale_y=1.0, geometry_scale_z=1.0; |
---|
| 82 | osg::ref_ptr<osgVisual::object_updater> updater = NULL; |
---|
[202] | 83 | |
---|
[207] | 84 | // extract model properties |
---|
| 85 | xmlAttr *attr = a_node->properties; |
---|
| 86 | while ( attr ) |
---|
| 87 | { |
---|
| 88 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 89 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 90 | if( attr_name == "objectname" ) objectname = attr_value; |
---|
| 91 | if( attr_name == "label" ) label = attr_value; |
---|
| 92 | if( attr_name == "dynamic" ) |
---|
| 93 | { |
---|
| 94 | if(attr_value=="yes") |
---|
| 95 | dynamic=true; |
---|
| 96 | else |
---|
| 97 | dynamic=false; |
---|
| 98 | } |
---|
| 99 | attr = attr->next; |
---|
| 100 | } |
---|
| 101 | for (xmlNode *cur_node = a_node->children; cur_node; cur_node = cur_node->next) |
---|
| 102 | { |
---|
| 103 | std::string node_name=reinterpret_cast<const char*>(cur_node->name); |
---|
| 104 | |
---|
| 105 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "position") |
---|
| 106 | { |
---|
| 107 | xmlAttr *attr = cur_node->properties; |
---|
| 108 | while ( attr ) |
---|
| 109 | { |
---|
| 110 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 111 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 112 | if( attr_name == "lat" ) |
---|
| 113 | { |
---|
| 114 | std::stringstream sstr(attr_value); |
---|
| 115 | sstr >> lat; |
---|
| 116 | } |
---|
| 117 | if( attr_name == "lon" ) |
---|
| 118 | { |
---|
| 119 | std::stringstream sstr(attr_value); |
---|
| 120 | sstr >> lon; |
---|
| 121 | } |
---|
| 122 | if( attr_name == "alt" ) |
---|
| 123 | { |
---|
| 124 | std::stringstream sstr(attr_value); |
---|
| 125 | sstr >> alt; |
---|
| 126 | } |
---|
| 127 | attr = attr->next; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "attitude") |
---|
| 132 | { |
---|
| 133 | xmlAttr *attr = cur_node->properties; |
---|
| 134 | while ( attr ) |
---|
| 135 | { |
---|
| 136 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 137 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 138 | if( attr_name == "rot_x" ) |
---|
| 139 | { |
---|
| 140 | std::stringstream sstr(attr_value); |
---|
| 141 | sstr >> rot_x; |
---|
| 142 | } |
---|
| 143 | if( attr_name == "rot_y" ) |
---|
| 144 | { |
---|
| 145 | std::stringstream sstr(attr_value); |
---|
| 146 | sstr >> rot_y; |
---|
| 147 | } |
---|
| 148 | if( attr_name == "rot_z" ) |
---|
| 149 | { |
---|
| 150 | std::stringstream sstr(attr_value); |
---|
| 151 | sstr >> rot_z; |
---|
| 152 | } |
---|
| 153 | attr = attr->next; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | //if(cur_node->type == XML_ELEMENT_NODE && node_name == "updater") |
---|
| 158 | //{ |
---|
| 159 | // for (xmlNode *sub_cur_node = cur_node->children; sub_cur_node; sub_cur_node = sub_cur_node->next) |
---|
| 160 | // { |
---|
| 161 | // std::string sub_node_name=reinterpret_cast<const char*>(cur_node->children->name); |
---|
| 162 | // if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "position") |
---|
| 163 | // { |
---|
| 164 | // <position lat="" lon="" alt=""></position> |
---|
| 165 | // } |
---|
| 166 | // if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "attitude") |
---|
| 167 | // { |
---|
| 168 | // <attitude rot_x="0.0" rot_y="0.0" rot_z="0.0"></attitude> |
---|
| 169 | // } |
---|
| 170 | // if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "label") |
---|
| 171 | // { |
---|
| 172 | // <label text=""></label> |
---|
| 173 | // } |
---|
| 174 | // } |
---|
| 175 | //} |
---|
| 176 | |
---|
| 177 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "cameraoffset") |
---|
| 178 | { |
---|
| 179 | for (xmlNode *sub_cur_node = cur_node->children; sub_cur_node; sub_cur_node = sub_cur_node->next) |
---|
| 180 | { |
---|
| 181 | std::string sub_node_name=reinterpret_cast<const char*>(cur_node->children->name); |
---|
| 182 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "translation") |
---|
| 183 | { |
---|
| 184 | xmlAttr *attr = sub_cur_node->properties; |
---|
| 185 | while ( attr ) |
---|
| 186 | { |
---|
| 187 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 188 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 189 | if( attr_name == "trans_x" ) |
---|
| 190 | { |
---|
| 191 | std::stringstream sstr(attr_value); |
---|
| 192 | sstr >> cam_trans_x; |
---|
| 193 | } |
---|
| 194 | if( attr_name == "trans_y" ) |
---|
| 195 | { |
---|
| 196 | std::stringstream sstr(attr_value); |
---|
| 197 | sstr >> cam_trans_y; |
---|
| 198 | } |
---|
| 199 | if( attr_name == "trans_z" ) |
---|
| 200 | { |
---|
| 201 | std::stringstream sstr(attr_value); |
---|
| 202 | sstr >> cam_trans_y; |
---|
| 203 | } |
---|
| 204 | attr = attr->next; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "rotation") |
---|
| 208 | { |
---|
| 209 | xmlAttr *attr = sub_cur_node->properties; |
---|
| 210 | while ( attr ) |
---|
| 211 | { |
---|
| 212 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 213 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 214 | if( attr_name == "rot_x" ) |
---|
| 215 | { |
---|
| 216 | std::stringstream sstr(attr_value); |
---|
| 217 | sstr >> cam_rot_x; |
---|
| 218 | } |
---|
| 219 | if( attr_name == "rot_y" ) |
---|
| 220 | { |
---|
| 221 | std::stringstream sstr(attr_value); |
---|
| 222 | sstr >> cam_rot_y; |
---|
| 223 | } |
---|
| 224 | if( attr_name == "rot_z" ) |
---|
| 225 | { |
---|
| 226 | std::stringstream sstr(attr_value); |
---|
| 227 | sstr >> cam_rot_y; |
---|
| 228 | } |
---|
| 229 | attr = attr->next; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "geometry") |
---|
| 236 | { |
---|
| 237 | // extract filename |
---|
| 238 | xmlAttr *attr = cur_node->properties; |
---|
| 239 | while ( attr ) |
---|
| 240 | { |
---|
| 241 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 242 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 243 | if( attr_name == "filename" ) |
---|
| 244 | filename = attr_value; |
---|
| 245 | attr = attr->next; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | // Extract optional settings |
---|
| 249 | for (xmlNode *sub_cur_node = cur_node->children; sub_cur_node; sub_cur_node = sub_cur_node->next) |
---|
| 250 | { |
---|
| 251 | std::string sub_node_name=reinterpret_cast<const char*>(sub_cur_node->name); |
---|
| 252 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "offset") |
---|
| 253 | { |
---|
| 254 | xmlAttr *attr = sub_cur_node->properties; |
---|
| 255 | while ( attr ) |
---|
| 256 | { |
---|
| 257 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 258 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 259 | if( attr_name == "rot_x" ) |
---|
| 260 | { |
---|
| 261 | std::stringstream sstr(attr_value); |
---|
| 262 | sstr >> geometry_rot_x; |
---|
| 263 | } |
---|
| 264 | if( attr_name == "rot_y" ) |
---|
| 265 | { |
---|
| 266 | std::stringstream sstr(attr_value); |
---|
| 267 | sstr >> geometry_rot_y; |
---|
| 268 | } |
---|
| 269 | if( attr_name == "rot_z" ) |
---|
| 270 | { |
---|
| 271 | std::stringstream sstr(attr_value); |
---|
| 272 | sstr >> geometry_rot_z; |
---|
| 273 | } |
---|
| 274 | attr = attr->next; |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "scalefactor") |
---|
| 278 | { |
---|
| 279 | xmlAttr *attr = sub_cur_node->properties; |
---|
| 280 | while ( attr ) |
---|
| 281 | { |
---|
| 282 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 283 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 284 | if( attr_name == "scale_x" ) |
---|
| 285 | { |
---|
| 286 | std::stringstream sstr(attr_value); |
---|
| 287 | sstr >> geometry_scale_x; |
---|
| 288 | } |
---|
| 289 | if( attr_name == "scale_y" ) |
---|
| 290 | { |
---|
| 291 | std::stringstream sstr(attr_value); |
---|
| 292 | sstr >> geometry_scale_y; |
---|
| 293 | } |
---|
| 294 | if( attr_name == "scale_z" ) |
---|
| 295 | { |
---|
| 296 | std::stringstream sstr(attr_value); |
---|
| 297 | sstr >> geometry_scale_z; |
---|
| 298 | } |
---|
| 299 | attr = attr->next; |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | |
---|
| 307 | |
---|
| 308 | |
---|
[205] | 309 | osgVisual::visual_object* object = new osgVisual::visual_object( sceneRoot_, objectname ); |
---|
| 310 | object->lat = lat; |
---|
| 311 | object->lon = lon; |
---|
| 312 | object->alt = alt; |
---|
| 313 | object->azimuthAngle_psi = rot_x; |
---|
| 314 | object->pitchAngle_theta = rot_y; |
---|
| 315 | object->bankAngle_phi = rot_z; |
---|
| 316 | if(label!="") |
---|
| 317 | object->addLabel("XML_defined_label", label); |
---|
| 318 | if(dynamic) |
---|
| 319 | { |
---|
| 320 | updater = new osgVisual::object_updater(object); |
---|
| 321 | } |
---|
| 322 | object->setCameraOffset( cam_trans_x, cam_trans_y, cam_trans_z, cam_rot_x, cam_rot_y, cam_rot_z); |
---|
| 323 | if(filename!="") |
---|
| 324 | { |
---|
| 325 | object->loadGeometry( filename ); |
---|
| 326 | object->setGeometryOffset( geometry_rot_x, geometry_rot_y, geometry_rot_z ); |
---|
| 327 | object->setScale( geometry_scale_x, geometry_scale_y, geometry_scale_z ); |
---|
| 328 | } |
---|
[202] | 329 | |
---|
[205] | 330 | if(updater.valid()) |
---|
| 331 | object->addUpdater( updater ); |
---|
| 332 | |
---|
| 333 | /* |
---|
| 334 | - updater [optional]: Channels to use for position and attitude update |
---|
| 335 | |
---|
| 336 | <updater> |
---|
| 337 | <translation-slots>todo</translation-slots> |
---|
| 338 | </updater> |
---|
| 339 | */ |
---|
| 340 | |
---|
[203] | 341 | OSG_NOTIFY( osg::ALWAYS ) << "Done." << std::endl; |
---|
[205] | 342 | return object; |
---|
[202] | 343 | } |
---|
| 344 | |
---|
[31] | 345 | void visual_object::setNewPositionAttitude( double lat_, double lon_, double alt_, double azimuthAngle_psi_, double pitchAngle_theta_, double bankAngle_phi_ ) |
---|
| 346 | { |
---|
| 347 | lat = lat_; |
---|
| 348 | lon = lon_; |
---|
| 349 | alt = alt_; |
---|
| 350 | |
---|
| 351 | azimuthAngle_psi = azimuthAngle_psi_; |
---|
| 352 | pitchAngle_theta = pitchAngle_theta_; |
---|
| 353 | bankAngle_phi = bankAngle_phi_; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | void visual_object::setNewPosition( double lat_, double lon_, double alt_ ) |
---|
| 357 | { |
---|
| 358 | lat = lat_; |
---|
| 359 | lon = lon_; |
---|
| 360 | alt = alt_; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | void visual_object::setNewAttitude( double azimuthAngle_psi_, double pitchAngle_theta_, double bankAngle_phi_ ) |
---|
| 364 | { |
---|
| 365 | azimuthAngle_psi = azimuthAngle_psi_; |
---|
| 366 | pitchAngle_theta = pitchAngle_theta_; |
---|
| 367 | bankAngle_phi = bankAngle_phi_; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | void visual_object::setGeometryOffset( double rotX_, double rotY_, double rotZ_ ) |
---|
| 371 | { |
---|
| 372 | geometry_offset_rotation.makeRotate( rotX_, osg::Vec3f(1.0, 0.0, 0.0), |
---|
| 373 | rotY_, osg::Vec3f(0.0, 1.0, 0.0), |
---|
| 374 | rotZ_, osg::Vec3f(0.0, 0.0, 1.0) ); |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | void visual_object::setScale( double scale_ ) |
---|
| 378 | { |
---|
| 379 | scaleX = scale_; |
---|
| 380 | scaleY = scale_; |
---|
| 381 | scaleZ = scale_; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | void visual_object::setScale( double scaleX_, double scaleY_, double scaleZ_ ) |
---|
| 385 | { |
---|
| 386 | scaleX = scaleX_; |
---|
| 387 | scaleY = scaleY_; |
---|
| 388 | scaleZ = scaleZ_; |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | bool visual_object::loadGeometry(std::string filename_) |
---|
| 392 | { |
---|
| 393 | // Check if file exists |
---|
| 394 | if( !osgDB::fileExists(filename_) ) |
---|
| 395 | { |
---|
| 396 | OSG_NOTIFY(osg::FATAL) << "Error: Model not loaded. File '" << filename_ << "' does not exist." << std::endl; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | osg::ref_ptr<osg::Node> tmpModel = osgDB::readNodeFile( filename_ ); |
---|
| 400 | |
---|
| 401 | if( tmpModel.valid() ) |
---|
| 402 | { |
---|
| 403 | // remove old geometry |
---|
| 404 | geometry->removeChildren(0, geometry->getNumChildren()); |
---|
| 405 | |
---|
| 406 | // add new geometry |
---|
| 407 | geometry->addChild( tmpModel.get() ); |
---|
| 408 | return true; |
---|
| 409 | } |
---|
| 410 | else |
---|
| 411 | { |
---|
| 412 | std::cout <<": No model loaded: " << filename_ << std::endl; |
---|
| 413 | return false; |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | bool visual_object::setGeometry(osg::Node* geometry_) |
---|
| 418 | { |
---|
| 419 | // remove old geometry |
---|
| 420 | geometry->removeChildren(0, geometry->getNumChildren()); |
---|
| 421 | |
---|
| 422 | // add new geometry |
---|
| 423 | geometry->addChild( geometry_ ); |
---|
| 424 | |
---|
| 425 | return true; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | void visual_object::unsetGeometry() |
---|
| 429 | { |
---|
| 430 | // remove old geometry |
---|
| 431 | geometry->removeChildren(0, geometry->getNumChildren()); |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | void visual_object::addUpdater( object_updater* updater_ ) |
---|
| 435 | { |
---|
| 436 | if ( updater.valid() ) |
---|
| 437 | updater->addUpdater( updater_ ); |
---|
| 438 | else |
---|
| 439 | updater = updater_; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | void visual_object::clearAllUpdater() |
---|
| 443 | { |
---|
| 444 | // release only first updater. Because smartpointer: Will be deleted if not referenced. |
---|
| 445 | if ( updater.valid() ) |
---|
| 446 | updater = NULL; |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | std::vector<object_updater*> visual_object::getUpdaterList() |
---|
| 450 | { |
---|
| 451 | // iterate through updater and add all pointer. |
---|
| 452 | std::vector<object_updater*> updaterList; |
---|
| 453 | osg::ref_ptr<object_updater> tmpUpdater = updater; |
---|
| 454 | |
---|
| 455 | while (tmpUpdater.valid()) |
---|
| 456 | { |
---|
| 457 | updaterList.push_back( tmpUpdater ); |
---|
| 458 | tmpUpdater = tmpUpdater->getPointer(); |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | // return list |
---|
| 462 | return updaterList; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | void visual_object::visual_objectPositionCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) |
---|
| 466 | { |
---|
| 467 | visual_object* object = dynamic_cast<visual_object*>(node); |
---|
| 468 | if ( !object ) |
---|
| 469 | { |
---|
| 470 | OSG_NOTIFY(osg::FATAL) << "ERROR : No object found. Unable to apply this callback!" << std::endl; |
---|
| 471 | return; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | // execute preUpdater to get new data of this object. |
---|
| 475 | if ( object->updater.valid() ) |
---|
| 476 | object->updater->preUpdate(object); |
---|
| 477 | |
---|
| 478 | // Nodepath from this node to absolute parent (if no endnode specified) |
---|
| 479 | osg::NodePath nodePath = nv->getNodePath(); |
---|
| 480 | |
---|
| 481 | // If Nodepath != empty, then mt = last element of node path |
---|
| 482 | osg::MatrixTransform* mt = nodePath.empty() ? 0 : dynamic_cast<osg::MatrixTransform*>(nodePath.back()); |
---|
| 483 | if (mt) |
---|
| 484 | { |
---|
| 485 | osg::CoordinateSystemNode* csn = 0; |
---|
| 486 | |
---|
| 487 | // find coordinate system node from our parental chain: traverse chain and try to convert every node to a csn. |
---|
| 488 | unsigned int i; |
---|
| 489 | for(i=0; i<nodePath.size() && csn==0; ++i) // "&& csn" means: exit loop if csn found |
---|
| 490 | { |
---|
| 491 | csn = dynamic_cast<osg::CoordinateSystemNode*>(nodePath[i]); // dynamic_cast returns 0 if dynamic_cast fails. |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | // Wenn csn gefunden: |
---|
| 495 | if (csn) |
---|
| 496 | { |
---|
| 497 | // Ellipsoidmodel erfragen |
---|
| 498 | osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel(); |
---|
| 499 | if (ellipsoid) |
---|
| 500 | { |
---|
| 501 | osg::Matrix inheritedMatrix; |
---|
| 502 | |
---|
| 503 | // durch den _restlichen_ Nodepath durchgehen und alle anfallenden Transformationen durchführen. |
---|
| 504 | for(i+=1; i<nodePath.size()-1; ++i) |
---|
| 505 | { |
---|
| 506 | osg::Transform* transform = nodePath[i]->asTransform(); // Versuchen, den Node zu einer Transformation zu konvertieren |
---|
| 507 | |
---|
| 508 | // wenn Node wirklich Trafo, dann die Tranformationsmatrix von Nodekoordinaten nach Global auf inheritedMatrix draufschlagen. |
---|
| 509 | if (transform) transform->computeLocalToWorldMatrix(inheritedMatrix, nv); |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | osg::Matrixd matrix(inheritedMatrix); |
---|
| 513 | |
---|
| 514 | // Set position |
---|
| 515 | ellipsoid->computeLocalToWorldTransformFromLatLongHeight(object->lat, object->lon, object->alt, matrix); |
---|
| 516 | |
---|
| 517 | // Set Upvector for position |
---|
| 518 | double X,Y,Z; |
---|
| 519 | util::calculateXYZAtWGS84Coordinate(object->lat, object->lon, object->alt, csn, X, Y, Z ); |
---|
| 520 | object->upVector = ellipsoid->computeLocalUpVector(X,Y,Z); |
---|
| 521 | |
---|
| 522 | // Set scale |
---|
| 523 | osg::Matrixd scaleMatrix; |
---|
| 524 | scaleMatrix.makeScale( object->scaleX, object->scaleY, object->scaleZ ); |
---|
| 525 | matrix.preMult( scaleMatrix ); |
---|
| 526 | |
---|
| 527 | // Set rotation |
---|
| 528 | // rotation von links ranmultiplizieren, entspricht: matrix = rotation * matrix. Da rotation ein Quat ist, wäre die direkte Multiplikation nur über Umwege machbar. |
---|
| 529 | // Rotate Object to Attitude. |
---|
| 530 | osg::Matrixd rotationMatrix; |
---|
| 531 | // Move Model by Azimuth |
---|
| 532 | rotationMatrix.makeRotate( -object->azimuthAngle_psi, osg::Vec3d(0.0, 0.0, 1.0) ); |
---|
| 533 | matrix.preMult(rotationMatrix); |
---|
| 534 | // Move Model by Pitch |
---|
| 535 | rotationMatrix.makeRotate( object->pitchAngle_theta, osg::Vec3d(1.0, 0.0, 0.0) ); |
---|
| 536 | matrix.preMult(rotationMatrix); |
---|
| 537 | // Move Model by Bank |
---|
| 538 | rotationMatrix.makeRotate( object->bankAngle_phi, osg::Vec3d(0.0, 1.0, 0.0) ); |
---|
| 539 | matrix.preMult(rotationMatrix); |
---|
| 540 | |
---|
| 541 | // Also update camera matrix (without geometry offset, because camera is interested in the objects matrix, not in the model's matrix.) |
---|
| 542 | object->cameraMatrix = matrix; |
---|
| 543 | /** \todo : Clean up camera matrix management: try to solve it with a single matrix. (each frame two matrix mults less) */ |
---|
| 544 | // dont know, why this rotation is necessary - maybe manipulator and node MatrixTransform interpret a matrix in different way? |
---|
| 545 | object->cameraMatrix.preMult( object->cameraTranslationOffset ); |
---|
| 546 | object->cameraMatrix.preMult( object->cameraRotationOffset ); |
---|
| 547 | |
---|
| 548 | |
---|
| 549 | // Set geometry correction |
---|
| 550 | matrix.preMultRotate( object->geometry_offset_rotation ); |
---|
| 551 | |
---|
[36] | 552 | // Set cumulated object matrix as the matrix of this matrix transform |
---|
[31] | 553 | mt->setMatrix(matrix); |
---|
| 554 | } |
---|
| 555 | } |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | // Call any nested callbacks. |
---|
| 559 | traverse(node,nv); |
---|
| 560 | |
---|
| 561 | // If SLAVE: execute postUpdater to pass new data of this object to dataIO. |
---|
| 562 | if( visual_dataIO::getInstance()->isSlave() ) |
---|
| 563 | { |
---|
| 564 | if ( object->updater.valid() ) |
---|
| 565 | object->updater->postUpdate(object); |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | } // Callbackfunction [ Operater() ] END |
---|
| 569 | |
---|
| 570 | void visual_object::setCameraOffsetTranslation( double x_, double y_, double z_) |
---|
| 571 | { |
---|
| 572 | cameraTranslationOffset.makeTranslate( osg::Vec3d(x_, y_, z_) ); // Trans: (rechts davon, longitudinal, vertikal) |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | void visual_object::setCameraOffset(double x_, double y_, double z_, double rotX_, double rotY_, double rotZ_) |
---|
| 576 | { |
---|
| 577 | setCameraOffsetTranslation( x_, y_, z_); |
---|
| 578 | setCameraOffsetRotation( rotX_, rotY_, rotZ_); |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | void visual_object::setCameraOffsetRotation(double rotX_, double rotY_, double rotZ_) |
---|
| 582 | { |
---|
| 583 | osg::Matrix tmp; |
---|
| 584 | cameraRotationOffset.makeRotate( osg::DegreesToRadians( 90.0 ), osg::Vec3(1, 0, 0) ); |
---|
| 585 | tmp.makeRotate( -rotZ_, osg::Vec3d(0.0, 1.0, 0.0) ); |
---|
| 586 | cameraRotationOffset.preMult(tmp); |
---|
| 587 | tmp.makeRotate( rotY_, osg::Vec3d(1.0, 0.0, 0.0) ); |
---|
| 588 | cameraRotationOffset.preMult(tmp); |
---|
| 589 | tmp.makeRotate( -rotX_, osg::Vec3d(0.0, 0.0, 1.0) ); |
---|
| 590 | cameraRotationOffset.preMult(tmp); |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | void visual_object::clearLabels() |
---|
| 594 | { |
---|
| 595 | labels->removeDrawables(0, labels->getNumDrawables()); |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | void visual_object::addLabel(std::string idString_, std::string label_, osg::Vec4 color_, osg::Vec3 offset_) |
---|
| 599 | { |
---|
| 600 | osg::ref_ptr<osgText::Text> text = new osgText::Text(); |
---|
| 601 | |
---|
| 602 | text->setName(idString_); |
---|
| 603 | text->setText(label_); |
---|
| 604 | text->setColor(color_); |
---|
| 605 | text->setFont("fonts/arial.ttf"); |
---|
| 606 | text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT); |
---|
| 607 | text->setAutoRotateToScreen(true); |
---|
| 608 | text->setPosition(offset_); |
---|
| 609 | |
---|
| 610 | text->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
---|
| 611 | labels->addDrawable( text ); |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | bool visual_object::removeLabel(std::string idString_) |
---|
| 615 | { |
---|
| 616 | osg::Node* labelToRemove = util::findNamedNode(idString_, this); |
---|
| 617 | |
---|
| 618 | if(labelToRemove) |
---|
| 619 | { |
---|
| 620 | removeChild( labelToRemove ); |
---|
| 621 | return true; |
---|
| 622 | } |
---|
| 623 | else |
---|
| 624 | return false; |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | bool visual_object::updateLabelText(std::string idString_, std::string label_) |
---|
| 628 | { |
---|
| 629 | osg::Node* labelToUpdate = util::findNamedNode(idString_, this); |
---|
| 630 | |
---|
| 631 | if(labelToUpdate) |
---|
| 632 | { |
---|
| 633 | osgText::Text* text = dynamic_cast<osgText::Text*>(labelToUpdate); |
---|
| 634 | if(text) |
---|
| 635 | { |
---|
| 636 | text->setText(label_); |
---|
| 637 | return true; |
---|
| 638 | } |
---|
| 639 | return false; |
---|
| 640 | } |
---|
| 641 | return false; |
---|
| 642 | } |
---|
| 643 | |
---|
| 644 | osgText::Text* visual_object::getLabel(std::string idString_) |
---|
| 645 | { |
---|
| 646 | osg::Node* labelToFind = util::findNamedNode(idString_, this); |
---|
| 647 | |
---|
| 648 | if(labelToFind) |
---|
| 649 | { |
---|
| 650 | osgText::Text* text = dynamic_cast<osgText::Text*>(labelToFind); |
---|
| 651 | if(text) |
---|
| 652 | return text; |
---|
| 653 | } |
---|
| 654 | return NULL; |
---|
| 655 | |
---|
| 656 | } |
---|
| 657 | |
---|
| 658 | bool visual_object::setDrawLabelAsOverlay(std::string idString_, bool drawAsOverlay) |
---|
| 659 | { |
---|
| 660 | osg::Node* labelToFind = util::findNamedNode(idString_, this); |
---|
| 661 | |
---|
| 662 | if(labelToFind) |
---|
| 663 | { |
---|
| 664 | if (drawAsOverlay) |
---|
| 665 | labelToFind->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
---|
| 666 | else |
---|
| 667 | labelToFind->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON); |
---|
| 668 | return true; |
---|
| 669 | } |
---|
| 670 | else |
---|
| 671 | return false; |
---|
| 672 | } |
---|
| 673 | |
---|
| 674 | bool visual_object::getDrawLabelAsOverlay(std::string idString_) |
---|
| 675 | { |
---|
| 676 | osg::Node* labelToFind = util::findNamedNode(idString_, this); |
---|
| 677 | |
---|
| 678 | if(labelToFind) |
---|
| 679 | { |
---|
| 680 | if(labelToFind->getOrCreateStateSet()->getMode(GL_DEPTH_TEST) == osg::StateAttribute::OFF) |
---|
| 681 | return false; |
---|
| 682 | else |
---|
| 683 | return true; |
---|
| 684 | } |
---|
| 685 | return false; |
---|
| 686 | } |
---|