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

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

Object definition via XML file: Angels are now interpreted as degree.

File size: 21.6 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
310void visual_object::setNewPositionAttitude( double lat_, double lon_, double alt_, double azimuthAngle_psi_, double pitchAngle_theta_, double bankAngle_phi_ )
311{
312        lat = lat_;
313        lon = lon_;
314        alt = alt_;
315
316        azimuthAngle_psi = azimuthAngle_psi_;
317        pitchAngle_theta = pitchAngle_theta_;
318        bankAngle_phi = bankAngle_phi_;
319}
320
321void visual_object::setNewPosition( double lat_, double lon_, double alt_ )
322{
323        lat = lat_;
324        lon = lon_;
325        alt = alt_;
326}
327
328void visual_object::setNewAttitude( double azimuthAngle_psi_, double pitchAngle_theta_, double bankAngle_phi_ )
329{
330        azimuthAngle_psi = azimuthAngle_psi_;
331        pitchAngle_theta = pitchAngle_theta_;
332        bankAngle_phi = bankAngle_phi_;
333}
334
335void visual_object::setGeometryOffset( double rotX_, double rotY_, double rotZ_ )
336{
337        geometry_offset_rotation.makeRotate( rotX_, osg::Vec3f(1.0, 0.0, 0.0), 
338                                                rotY_, osg::Vec3f(0.0, 1.0, 0.0),
339                                                rotZ_, osg::Vec3f(0.0, 0.0, 1.0) );
340}
341
342void visual_object::setScale( double scale_ )
343{
344        scaleX = scale_;
345        scaleY = scale_;
346        scaleZ = scale_;
347}
348
349void visual_object::setScale( double scaleX_, double scaleY_, double scaleZ_ )
350{
351        scaleX = scaleX_;
352        scaleY = scaleY_;
353        scaleZ = scaleZ_;
354}
355
356bool visual_object::loadGeometry(std::string filename_)
357{
358        // Check if file exists
359        if( !osgDB::fileExists(filename_) )
360        {
361                OSG_NOTIFY(osg::FATAL) << "Error: Model not loaded. File '" << filename_ << "' does not exist." << std::endl;
362        }
363
364        osg::ref_ptr<osg::Node> tmpModel = osgDB::readNodeFile( filename_ );
365       
366        if( tmpModel.valid() )
367        {
368                // remove old geometry
369                geometry->removeChildren(0, geometry->getNumChildren());
370
371                // add new geometry
372                geometry->addChild( tmpModel.get() );
373                return true;
374        }
375        else
376        {
377                std::cout <<": No model loaded: " << filename_ << std::endl;
378        return false;
379    }
380}
381
382bool visual_object::setGeometry(osg::Node* geometry_)
383{
384        // remove old geometry
385        geometry->removeChildren(0, geometry->getNumChildren());
386
387        // add new geometry
388        geometry->addChild( geometry_ );
389
390        return true;
391}
392
393void visual_object::unsetGeometry()
394{
395        // remove old geometry
396        geometry->removeChildren(0, geometry->getNumChildren());
397
398        // Set std OSG Node to allow tracking of an osgVisual without
399        geometry->addChild( new osg::Node() ); 
400}
401
402void visual_object::addUpdater( object_updater* updater_ )
403{
404        if ( updater.valid() )
405                updater->addUpdater( updater_ );
406        else
407                updater = updater_;
408}
409
410void visual_object::clearAllUpdater()
411{
412        // release only first updater. Because smartpointer: Will be deleted if not referenced.
413        if ( updater.valid() )
414                updater = NULL;
415}
416
417std::vector<object_updater*> visual_object::getUpdaterList()
418{
419        // iterate through updater and add all pointer.
420        std::vector<object_updater*> updaterList;
421        osg::ref_ptr<object_updater> tmpUpdater = updater;
422
423        while (tmpUpdater.valid())
424        {
425                updaterList.push_back( tmpUpdater );
426                tmpUpdater = tmpUpdater->getPointer();
427        }
428
429        // return list
430        return updaterList;
431}
432
433void visual_object::visual_objectPositionCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
434{
435        visual_object* object = dynamic_cast<visual_object*>(node);
436        if ( !object )
437        {
438                OSG_NOTIFY(osg::FATAL) << "ERROR : No object found. Unable to apply this callback!" << std::endl;
439                return;
440        }
441
442        // execute preUpdater to get new data of this object.
443        if ( object->updater.valid() )
444                object->updater->preUpdate(object);
445   
446        // Nodepath from this node to absolute parent (if no endnode specified)
447        osg::NodePath nodePath = nv->getNodePath();
448
449        // If Nodepath != empty, then mt = last element of node path
450        osg::MatrixTransform* mt = nodePath.empty() ? 0 : dynamic_cast<osg::MatrixTransform*>(nodePath.back());
451        if (mt)
452        {
453                osg::CoordinateSystemNode* csn = 0;
454
455                // find coordinate system node from our parental chain: traverse chain and try to convert every node to a csn.
456                unsigned int i;
457                for(i=0; i<nodePath.size() && csn==0; ++i)      // "&& csn" means: exit loop if csn found
458                {
459                        csn = dynamic_cast<osg::CoordinateSystemNode*>(nodePath[i]);    // dynamic_cast returns 0 if dynamic_cast fails.
460                }
461       
462                // Wenn csn gefunden:
463                if (csn)
464                {
465                        // Ellipsoidmodel erfragen
466                        osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel();
467                        if (ellipsoid)
468                        {
469                                osg::Matrix inheritedMatrix;
470
471                                // durch den _restlichen_ Nodepath durchgehen und alle anfallenden Transformationen durchführen.
472                                for(i+=1; i<nodePath.size()-1; ++i)
473                                {
474                                        osg::Transform* transform = nodePath[i]->asTransform(); // Versuchen, den Node zu einer Transformation zu konvertieren
475                   
476                                        // wenn Node wirklich Trafo, dann die Tranformationsmatrix von Nodekoordinaten nach Global auf inheritedMatrix draufschlagen.
477                                        if (transform) transform->computeLocalToWorldMatrix(inheritedMatrix, nv);
478                                }
479               
480                                osg::Matrixd matrix(inheritedMatrix);
481
482                                // Set position
483                                ellipsoid->computeLocalToWorldTransformFromLatLongHeight(object->lat, object->lon, object->alt, matrix);
484
485                                // Set Upvector for position
486                                double X,Y,Z;
487                                util::calculateXYZAtWGS84Coordinate(object->lat, object->lon, object->alt, csn, X, Y, Z );
488                                object->upVector = ellipsoid->computeLocalUpVector(X,Y,Z);
489
490                                // Set scale
491                                osg::Matrixd scaleMatrix;
492                                scaleMatrix.makeScale( object->scaleX, object->scaleY, object->scaleZ );
493                                matrix.preMult( scaleMatrix );
494
495                                // Set rotation
496                                // rotation von links ranmultiplizieren, entspricht: matrix = rotation * matrix. Da rotation ein Quat ist, wäre die direkte Multiplikation nur über Umwege machbar.
497                                // Rotate Object to Attitude.
498                                osg::Matrixd rotationMatrix;
499                                // Move Model by Azimuth
500                                rotationMatrix.makeRotate( -object->azimuthAngle_psi, osg::Vec3d(0.0, 0.0, 1.0) );
501                                matrix.preMult(rotationMatrix); 
502                                // Move Model by Pitch
503                                rotationMatrix.makeRotate( object->pitchAngle_theta, osg::Vec3d(1.0, 0.0, 0.0) );
504                                matrix.preMult(rotationMatrix);
505                                // Move Model by Bank
506                                rotationMatrix.makeRotate( object->bankAngle_phi, osg::Vec3d(0.0, 1.0, 0.0) );
507                                matrix.preMult(rotationMatrix);
508
509                                // Also update camera matrix (without geometry offset, because camera is interested in the objects matrix, not in the model's matrix.)
510                                object->cameraMatrix = matrix;
511                                /** \todo : Clean up camera matrix management: try to solve it with a single matrix. (each frame two matrix mults less) */
512                                // dont know, why this rotation is necessary - maybe manipulator and node MatrixTransform interpret a matrix in different way?
513                                object->cameraMatrix.preMult( object->cameraTranslationOffset );
514                                object->cameraMatrix.preMult( object->cameraRotationOffset );
515                                                       
516
517                                // Set geometry correction
518                                matrix.preMultRotate( object->geometry_offset_rotation );
519
520                                // Set cumulated object matrix as the matrix of this matrix transform
521                                mt->setMatrix(matrix);
522                        }
523                }       
524        }
525     
526        // Call any nested callbacks.
527        traverse(node,nv);
528
529        // If SLAVE: execute postUpdater to pass new data of this object to dataIO.
530        if( visual_dataIO::getInstance()->isSlave() )
531        {
532                if ( object->updater.valid() )
533                        object->updater->postUpdate(object);
534        }
535
536}   // Callbackfunction [ Operater() ] END
537
538void visual_object::setCameraOffsetTranslation( double x_, double y_, double z_)
539{
540        cameraTranslationOffset.makeTranslate( osg::Vec3d(x_, y_, z_) );        // Trans: (rechts davon, longitudinal, vertikal)
541}
542
543void visual_object::setCameraOffset(double x_, double y_, double z_, double rotX_, double rotY_, double rotZ_)
544{
545        setCameraOffsetTranslation( x_, y_, z_);
546        setCameraOffsetRotation( rotX_, rotY_, rotZ_);
547}
548
549void visual_object::setCameraOffsetRotation(double rotX_, double rotY_, double rotZ_)
550{
551        osg::Matrix tmp;
552        cameraRotationOffset.makeRotate( osg::DegreesToRadians( 90.0 ), osg::Vec3(1, 0, 0) );
553        tmp.makeRotate( -rotZ_, osg::Vec3d(0.0, 1.0, 0.0) );
554        cameraRotationOffset.preMult(tmp);
555        tmp.makeRotate( rotY_, osg::Vec3d(1.0, 0.0, 0.0) );     
556        cameraRotationOffset.preMult(tmp);
557        tmp.makeRotate( -rotX_, osg::Vec3d(0.0, 0.0, 1.0) );   
558        cameraRotationOffset.preMult(tmp);
559}
560
561void visual_object::clearLabels()
562{
563        labels->removeDrawables(0, labels->getNumDrawables());
564}
565
566void visual_object::addLabel(std::string idString_, std::string label_, osg::Vec4 color_, osg::Vec3 offset_)
567{
568        osg::ref_ptr<osgText::Text> text = new osgText::Text();
569
570        text->setName(idString_);
571        text->setText(label_);
572        text->setColor(color_);
573        text->setFont("fonts/arial.ttf");
574        text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
575        text->setAutoRotateToScreen(true);
576        text->setPosition(offset_);
577
578        text->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
579        labels->addDrawable( text );
580}
581
582bool visual_object::removeLabel(std::string idString_)
583{
584        osg::Node* labelToRemove = util::findNamedNode(idString_, this);
585
586        if(labelToRemove)
587        {
588                removeChild( labelToRemove );
589                return true;
590        }
591        else
592                return false;
593}
594
595bool visual_object::updateLabelText(std::string idString_, std::string label_)
596{
597        osg::Node* labelToUpdate = util::findNamedNode(idString_, this);
598
599        if(labelToUpdate)
600        {
601                osgText::Text* text = dynamic_cast<osgText::Text*>(labelToUpdate);
602                if(text)
603                {
604                        text->setText(label_);
605                        return true;
606                }
607                return false;
608        }
609        return false;
610}
611
612osgText::Text* visual_object::getLabel(std::string idString_)
613{
614        osg::Node* labelToFind = util::findNamedNode(idString_, this);
615
616        if(labelToFind)
617        {
618                osgText::Text* text = dynamic_cast<osgText::Text*>(labelToFind);
619                if(text)
620                        return text;
621        }
622        return NULL;
623
624}
625
626bool visual_object::setDrawLabelAsOverlay(std::string idString_, bool drawAsOverlay)
627{
628        osg::Node* labelToFind = util::findNamedNode(idString_, this);
629
630        if(labelToFind)
631        {
632                if (drawAsOverlay)
633                        labelToFind->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
634                else
635                        labelToFind->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);
636                return true;
637        }
638        else 
639                return false;
640}
641
642bool visual_object::getDrawLabelAsOverlay(std::string idString_)
643{
644        osg::Node* labelToFind = util::findNamedNode(idString_, this);
645
646        if(labelToFind)
647        {
648                if(labelToFind->getOrCreateStateSet()->getMode(GL_DEPTH_TEST) == osg::StateAttribute::OFF)
649                        return false;
650                else 
651                        return true;
652        }
653        return false;
654}
Note: See TracBrowser for help on using the repository browser.