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

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