source: osgVisual/trunk/src/object/visual_object.cpp @ 226

Last change on this file since 226 was 226, checked in by Torben Dannhauer, 13 years ago

TRacking nodes by XML config should work now

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