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