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

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