source: osgVisual/src/sky_Silverlining/visual_skySilverLining.cpp @ 136

Last change on this file since 136 was 136, checked in by Torben Dannhauer, 14 years ago
File size: 25.4 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_skySilverLining.h>
18
19using namespace osgVisual;
20
21visual_skySilverLining::visual_skySilverLining(osgViewer::Viewer* viewer_)
22{
23        OSG_NOTIFY( osg::ALWAYS ) << "Initialize visual_skySilverlining..." << std::endl;
24
25        atmosphereInitialized = false;
26        postInitialized = false;
27        atmosphere = NULL;
28        viewer = viewer_;
29        lat = 0;
30        lon = 0;
31        upVectorLat = 5;        // Just to ensure deltaLatToUpdate is big enough to enforce the sky to update at the first frame
32        upVectorLon = 5;        // Just to ensure deltaLonToUpdate is big enough to enforce the sky to update at the first frame
33        for( int i=0; i<MAX_CLOUDLAYER_SLOTS; i++ )
34        {
35                cloudLayerSlots.push_back( cloudLayerSlot() );
36                cloudLayerSlots.back().slot_id = i;
37        }
38}
39
40visual_skySilverLining::~visual_skySilverLining(void)
41{
42        this->removeUpdateCallback( updateCallback );
43        if ( atmosphere != NULL )
44                delete atmosphere;
45}
46
47void visual_skySilverLining::skyUpdateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
48{
49        //std::cout << "Sky silverlining update callback" << std::endl;
50        // Check if atmosphere is initialized.
51        if (!sky->isInitialized())
52                return;
53
54        // on first time: perform Post-Init.
55        sky->postInit();
56
57        // Update sky
58        double lat, lon, height;
59        util::getWGS84ofCamera( sceneCamera, csn, lat, lon, height );
60        //std::cout << "lat: " << osg::RadiansToDegrees(lat) << ", lon: " << osg::RadiansToDegrees(lon) << ", height: " << height << std::endl;
61        sky->setLocation( lat, lon, height );
62}
63
64void visual_skySilverLining::setDateTime( int year_, int month_, int day_, int hour_, int minute_, int second_, bool daylightSaving_, double timezoneOffset_ )
65{
66        // Check if atmosphere is initialized.
67        if (!isInitialized())
68                return;
69
70        SilverLining::LocalTime t = atmosphere->GetConditions()->GetTime();
71        t.SetYear( year_ );
72        t.SetMonth( month_ );
73        t.SetDay( day_ );
74        t.SetHour( hour_ );
75        t.SetMinutes( minute_ );
76        t.SetSeconds( second_ );
77        t.SetObservingDaylightSavingsTime( daylightSaving_ );
78        t.SetTimeZone( timezoneOffset_ );
79}
80
81void visual_skySilverLining::setTime( int hour_, int minute_, int second_ )
82{
83        // Check if atmosphere is initialized.
84        if (!isInitialized())
85                return;
86       
87        SilverLining::LocalTime t = atmosphere->GetConditions()->GetTime();
88        t.SetHour( hour_ );
89        t.SetMinutes( minute_ );
90        t.SetSeconds( second_ );
91        atmosphere->GetConditions()->SetTime( t );
92}
93
94void visual_skySilverLining::setDate( int year_, int month_, int day_ )
95{
96        // Check if atmosphere is initialized.
97        if (!isInitialized())
98                return;
99
100        SilverLining::LocalTime t = atmosphere->GetConditions()->GetTime();
101        t.SetYear( year_ );
102        t.SetMonth( month_ );
103        t.SetDay( day_ );
104}
105
106void visual_skySilverLining::setDateByEpoch( int secondsSince1970_ )
107{
108        // Check if atmosphere is initialized.
109        if (!isInitialized())
110                return;
111       
112        SilverLining::LocalTime t = atmosphere->GetConditions()->GetTime();
113        t.SetFromEpochSeconds( secondsSince1970_ );
114}
115
116void visual_skySilverLining::setLocation(double lat_, double lon_, double alt_)
117{
118        // Check if atmosphere is initialized.
119        if (!isInitialized())
120                return;
121
122        lat = lat_;
123        lon = lon_;
124        height = alt_;
125       
126        SilverLining::Location loc = atmosphere->GetConditions()->GetLocation();
127        loc.SetAltitude(alt_);
128        loc.SetLatitude(lat_);
129        loc.SetLongitude(lon_);         
130        atmosphere->GetConditions()->SetLocation( loc );
131
132        updateUpVector();
133}
134
135
136bool visual_skySilverLining::isInitialized()
137{
138        // Check if atmosphere is initialized. If not: make a deep lookup. If initialized, perform. Otherwise return
139        if (!atmosphereInitialized)
140        {
141                skySilverLining_atmosphereReference *ar = dynamic_cast<skySilverLining_atmosphereReference *>(viewer->getCamera()->getUserData());
142                if (ar != NULL )
143                {
144                        if (ar->atmosphereInitialized)
145                                atmosphereInitialized = true;
146                }
147        }
148        return(atmosphereInitialized);
149}
150
151void visual_skySilverLining::init(osg::Group *distortedRoot, osg::CoordinateSystemNode *sceneGraphRoot)
152{
153        sceneRoot = sceneGraphRoot;
154
155        // Use projection matrix callback oder fixed Cullsettings?
156        bool useProjMatrixCallback = true;
157
158        // add Sky to SceneGraphRoot
159        sceneGraphRoot->addChild( this );
160
161        // Deactivate culling for the sky node (required by the silverlining sky framework)
162        this->setCullingActive(false);
163
164        // Instantiate an Atmosphere and associate it with this camera. If you have multiple cameras
165        // in multiple contexts, be sure to instantiate seperate Atmosphere objects for each.
166    // ***IMPORTANT!**** Check that the path to the resources folder for SilverLining in SkyDrawable.cpp
167    // SkyDrawable::initializeSilverLining matches with where you installed SilverLining.
168        atmosphere = new SilverLining::Atmosphere(SILVERLINING_LICENSEE, SILVERLINING_LICENSE);
169
170    // Add the sky (calls Atmosphere::BeginFrame and handles initialization once you're in
171    // the rendering thread)
172        skyDrawable = new skySilverLining_skyDrawable(viewer, sceneRoot);
173
174        if(distortedRoot)       // if distortion used:
175        {
176                int rootKids = distortedRoot->getNumChildren();
177                for (int i = 0; i < rootKids; i++)
178                {
179                        osg::Node *n = distortedRoot->getChild(i);
180                        osg::Camera *cam = dynamic_cast<osg::Camera*>(n);
181                        if (cam && cam->getRenderOrder() == osg::Camera::PRE_RENDER)
182                                sceneCamera = cam;
183                }
184        }
185        else    // if no distortion used:
186                sceneCamera = viewer->getCamera();
187
188        osg::Camera *mainCamera = viewer->getCamera();
189        if (!useProjMatrixCallback)
190        {
191                mainCamera->setClearMask(0);
192                mainCamera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
193                double fovy, aspect, zNear, zFar;
194                mainCamera->getProjectionMatrixAsPerspective(fovy, aspect, zNear, zFar);
195                mainCamera->setProjectionMatrixAsPerspective(fovy, aspect, 2, 125000);
196        }
197        else
198        {
199                cb = new skySilverLining_projectionMatrixCallback( atmosphere, viewer->getCamera(), sceneRoot);
200                sceneCamera->setClampProjectionMatrixCallback(cb);
201                cb->setSkyDrawable(skyDrawable);
202        }
203
204        // append atmosphere pointer to the cameras.
205        sceneCamera->setClearMask(0);
206        osg::ref_ptr<skySilverLining_atmosphereReference> ar = new skySilverLining_atmosphereReference;
207        ar->atmosphere = atmosphere;
208        sceneCamera->setUserData(ar);
209        mainCamera->setUserData(ar);
210
211        // Create and install updateCallback (for position etc.)
212        updateCallback = new skyUpdateCallback( sceneGraphRoot, sceneCamera, this );
213        this->setUpdateCallback( updateCallback );
214
215    // Use a RenderBin to enforce that the sky gets drawn first, then the scene, then the clouds
216        skyDrawable->getOrCreateStateSet()->setRenderBinDetails(-1, "RenderBin");
217
218    // Add the models
219    sceneGraphRoot->getOrCreateStateSet()->setRenderBinDetails(1, "RenderBin");
220
221    // Add the clouds (note, you need this even if you don't want clouds - it calls
222    // Atmosphere::EndFrame() )
223        cloudsDrawable = new skySilverLining_cloudsDrawable(viewer);
224        cloudsDrawable->getOrCreateStateSet()->setRenderBinDetails(99, "RenderBin");
225
226        // Add drawable to this geode to get rendered
227        this->addDrawable(skyDrawable);
228        this->addDrawable(cloudsDrawable);
229
230        //SilverLining::Atmosphere::EnableHDR( true );
231}
232
233void visual_skySilverLining::postInit()
234{
235        // Only allow one execution
236        //if(postInitialized)
237        //      return;
238        //else postInitialized = true;
239
240        // Execute Updatecallback once before adding Clouds.
241        //updateCallback->operator ()(this, NULL);
242
243        //atmosphere->GetConditions()->SetFog( 0.8, 1, 1, 1);   // use this for simulation real fog.
244
245        //Todo: secure memory-manager of timer*. oder remove paragraph
246        //MyMillisecondTimer *timer = new MyMillisecondTimer();
247 //   atmosphere->GetConditions()->SetMillisecondTimer(timer);
248        //atmosphere->GetConditions()->EnableTimePassage(true, -1);
249}
250
251void visual_skySilverLining::updateUpVector()
252{
253        //// Allowed deltaLat and deltaLon between Updating the upvector
254        //double deltaLatToUpdate = osg::DegreesToRadians( 0.1 );       // 0.5 deg
255        //double deltaLonToUpdate = osg::DegreesToRadians( 0.1 ); // 0.5 deg
256
257        //if ( fabs(lat-upVectorLat) > deltaLatToUpdate || fabs(lon-upVectorLon) > deltaLonToUpdate )
258        //{
259        //      // Get ellipsoid model
260        //      osg::EllipsoidModel* ellipsoid = sceneRoot->getEllipsoidModel();
261        //      if ( !ellipsoid )
262        //              return;
263
264        //      //OSG_NOTIFY( osg::ALWAYS ) << "update Upvector.." << std::endl;
265
266        //      // Calculate up vector
267        //      double x,y,z;
268        //      util::getXYZofCamera(sceneCamera, x, y, z);
269        //      osg::Vec3d upVector = ellipsoid->computeLocalUpVector( x, y, z );
270        //      upVector.normalize();
271        //     
272        //      // Calculate side vector
273        //      osg::Matrixd localToWorld;
274        //      ellipsoid->computeLocalToWorldTransformFromLatLongHeight(lat, lon, height, localToWorld);
275
276        //      osg::Vec3d sideVector(localToWorld(0, 0), localToWorld(0, 1), localToWorld(0, 2));
277        //      sideVector.normalize();
278
279        //      // Update silverlining vectors
280        //      //std::cout << "upVector: X: " << upVector.x() << ", Y: "<<  upVector.y() << ", Z: "<< upVector.z() << std::endl;
281        //      //std::cout << "sideVector: X: " << sideVector.x() << ", Y: "<<  sideVector.y() << ", Z: "<< sideVector.z() << std::endl;
282        //      atmosphere->SetUpVector( upVector.x(), upVector.y(), upVector.z() );
283        //      atmosphere->SetRightVector( sideVector.x(), sideVector.y(), sideVector.z() );
284
285        //      // Note new upvector lat/lon
286        //      upVectorLon = lon;
287        //      upVectorLat = lat;
288        //}     // If update requiered END
289
290        double x,y,z;
291    util::getXYZofCamera(sceneCamera, x, y, z);
292
293    osg::Vec3d up(x, y, z);
294
295    up.normalize();
296    osg::Vec3d north(0, 0, 1);  // Z is north
297    osg::Vec3d east = north ^ up;       // Cross product
298    east.normalize();
299
300    atmosphere->SetUpVector(up.x(), up.y(), up.z());
301    atmosphere->SetRightVector(east.x(), east.y(), east.z());
302
303    osg::Matrixd view = sceneCamera->getViewMatrix();
304    osg::Matrixd proj = sceneCamera->getProjectionMatrix();
305
306    double dView[16], dProj[16];
307
308    int i = 0;
309    for (int row = 0; row < 4; row++)
310    {
311        for (int col = 0; col < 4; col++)
312        {
313            dView[i] = view(row, col);
314            dProj[i] = proj(row, col);
315            i++;
316        }
317    }
318    atmosphere->SetCameraMatrix(dView);
319    atmosphere->SetProjectionMatrix(dProj);
320}
321
322void visual_skySilverLining::shutdown()
323{
324        if (isInitialized())
325        {
326                // Remove this Node from scenegraph
327                sceneRoot->removeChild( this );
328               
329                // Remove updatecallback
330                this->removeUpdateCallback( updateCallback );
331                updateCallback = NULL;
332
333                // delete drawables
334                skyDrawable->shutdown();
335                this->removeDrawable(skyDrawable);
336                this->removeDrawable(cloudsDrawable);
337        }
338}
339
340void visual_skySilverLining::setVisibility(double visibility_)
341{
342        if (isInitialized())
343        {
344                atmosphere->GetConditions()->SetVisibility( visibility_ );
345        }
346
347}
348
349double visual_skySilverLining::getVisibility()
350{
351        if (isInitialized())
352        {
353                return atmosphere->GetConditions()->GetVisibility();
354        }
355        else
356                return -1;
357}
358
359void visual_skySilverLining::setTurbidity(double turbidity_)
360{
361        if (isInitialized())
362        {
363                atmosphere->GetConditions()->SetTurbidity( turbidity_ );
364        }
365}
366
367double visual_skySilverLining::getTurbidity()
368{
369        if (isInitialized())
370        {
371                return atmosphere->GetConditions()->GetTurbidity();
372        }
373        else
374                return -1;
375}
376
377void visual_skySilverLining::clearAllWindVolumes()
378{
379        if (isInitialized())
380        {
381                atmosphere->GetConditions()->ClearWindVolumes();
382        }
383}
384
385bool visual_skySilverLining::insideWind(double height_, double& bottom_, double& top_, double& speed_, double& direction_)
386{
387        if (isInitialized())
388        {
389                // Calculation earth radius on the wind positionl approximated through the now used position.
390                double radius;
391                if ( util::calculateEarthRadiusAtWGS84Coordinate(lat, lon, sceneRoot, radius) )
392                {
393                        // go through all wind volumes an check them for inside()
394                        //std::map<int, SilverLining::WindVolume> windvolumes = atmosphere->GetConditions()->GetWindVolumes();
395                        SL_MAP(int, SilverLining::WindVolume) windvolumes = atmosphere->GetConditions()->GetWindVolumes();
396                        for(unsigned int i=0; i<windvolumes.size(); i++)
397                        {
398                                if( windvolumes[i].Inside(radius + height_) )
399                                {
400                                        // save wind data
401                                        bottom_ = windvolumes[i].GetMinAltitude();
402                                        top_ = windvolumes[i].GetMaxAltitude() - radius;
403                                        speed_ = windvolumes[i].GetWindSpeed() - radius;
404                                        direction_ = windvolumes[i].GetDirection();
405                                        // return that wind was found
406                                        return true;
407                                }
408                        }       // For END
409                }       // If valid radius END
410        }       // If initialized() END
411        bottom_ = -1;
412        top_ = -1;
413        speed_ = -1;
414        direction_ = -1;
415        return false;
416}
417
418void visual_skySilverLining::addWindVolume(double bottom_, double top_, double speed_, int direction_)
419{
420        if (isInitialized())           
421        {
422                // Calculation earth radius on current lat lon position
423                double radius;
424                if ( util::calculateEarthRadiusAtWGS84Coordinate(lat, lon, sceneRoot, radius) )
425                {
426                        // correct wind value:
427                        if ( direction_ < 180 )
428                                direction_ += 180;
429                        else direction_ -= 180;
430
431                        // Setting up Wind
432                        SilverLining::WindVolume wv;
433                        wv.SetDirection( direction_ );
434                        wv.SetMinAltitude( radius + bottom_ );
435                        wv.SetMaxAltitude( radius + top_ );
436                        wv.SetWindSpeed( speed_ );
437                        atmosphere->GetConditions()->SetWind(wv);
438                }
439        }
440}
441
442void visual_skySilverLining::setLightPollution(double lightPollution_)
443{
444        if (isInitialized())
445        {
446                return atmosphere->GetConditions()->SetLightPollution( lightPollution_ );
447        }
448}
449
450double visual_skySilverLining::getLightPollution()
451{
452        if (isInitialized())
453        {
454                return atmosphere->GetConditions()->GetLightPollution();
455        }
456        else
457                return -1;
458
459}
460
461void visual_skySilverLining::addCloudLayer(int slot_, double baseLength_, double baseWidth_, double thickness_, double baseHeight_, double density_, CloudTypes cloudtype_ )
462{
463        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
464        {
465                // create cloudlayer order and pass to skyDrawable to instantiate.
466                cloudlayerOrder newCL;
467                newCL.slot = slot_;
468                newCL.lat = lat;
469                newCL.lon = lon;
470                newCL.baseLength = baseLength_;
471                newCL.baseWidth = baseWidth_;
472                newCL.thickness = thickness_;
473                newCL.baseHeight = baseHeight_;
474                newCL.density = density_;
475                newCL.cloudtype = cloudtype_;
476                newCL.assocCloudLayerSlot = &cloudLayerSlots[slot_];
477               
478                skyDrawable->addCloudLayerOrder( newCL );
479
480        }        // If isInitialized() END
481}
482
483void visual_skySilverLining::removeCloudLayer( int slot_ )
484{
485        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
486        {
487                atmosphere->GetConditions()->RemoveCloudLayer( cloudLayerSlots[slot_].cloudLayerHandle );
488                cloudLayerSlots[slot_].used = false;
489                cloudLayerSlots[slot_].cloudLayerHandle = -1;
490                cloudLayerSlots[slot_].cloudLayerPointer = NULL;
491                cloudLayerSlots[slot_].enabled = false;
492        }
493}
494
495void visual_skySilverLining::clearAllSlots()
496{
497        if (isInitialized())
498        {
499                atmosphere->GetConditions()->RemoveAllCloudLayers();
500                for( int i=0; i<MAX_CLOUDLAYER_SLOTS; i++ )
501                {
502                        cloudLayerSlots[i].used = false;
503                        cloudLayerSlots[i].cloudLayerHandle = -1;
504                        cloudLayerSlots[i].cloudLayerPointer = NULL;
505                        cloudLayerSlots[i].enabled = false;
506                }
507        }
508}
509
510SilverLining::CloudLayer* visual_skySilverLining::getCloudLayer( int slot_ )
511{
512        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
513        {
514                if ( cloudLayerSlots[slot_].used )
515                        return cloudLayerSlots[slot_].cloudLayerPointer; 
516        }
517
518        return NULL;
519}
520
521void visual_skySilverLining::setEnabled(int slot_, bool enabled_ )
522{
523        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
524        {
525                cloudLayerSlots[slot_].enabled = enabled_;
526        }
527}
528
529bool visual_skySilverLining::isEnabled( int slot_ )
530{
531        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
532        {
533                return cloudLayerSlots[slot_].enabled;
534        }
535        return false;
536}
537
538void visual_skySilverLining::fadeVisibility( int slot_, int fadetimeMS_ )
539{
540        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
541        {
542                if (cloudLayerSlots[slot_].enabled)
543                {
544                        cloudLayerSlots[slot_].enabled = false;
545                        cloudLayerSlots[slot_].cloudLayerPointer->SetEnabled( false, fadetimeMS_ );
546                }
547                else
548                {
549                        cloudLayerSlots[slot_].enabled = true;
550                        cloudLayerSlots[slot_].cloudLayerPointer->SetEnabled( true, fadetimeMS_ );
551                }
552        }
553}
554
555std::string visual_skySilverLining::getCloudLayerTypeName( int slot_ )
556{
557        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
558        {
559                return cloudLayerSlots[slot_].typeName;
560        }
561        return "";
562}
563
564void visual_skySilverLining::clearGlobalPrecipitation()
565{
566        if (isInitialized())
567        {
568                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::NONE, 0.0 );
569        }
570}
571
572void visual_skySilverLining::setGlobalPrecipitation( double rate_mmPerHour_rain_, double rate_mmPerHour_drySnow_, double rate_mmPerHour_wetSnow_, double rate_mmPerHour_sleet_ )
573{
574        if ( isInitialized() )
575        {
576                // Delete old Precipitation
577                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::NONE, 0.0 );
578               
579                // Set new Precipitation
580                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::RAIN, rate_mmPerHour_rain_ );
581                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::WET_SNOW, rate_mmPerHour_drySnow_ );
582                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::DRY_SNOW, rate_mmPerHour_wetSnow_ );
583                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::SLEET, rate_mmPerHour_sleet_ );
584        }
585}
586       
587bool visual_skySilverLining::getOverallPrecipitationAtLocation( double& rate_mmPerHour_rain, double& rate_mmPerHour_drySnow, double& rate_mmPerHour_wetSnow, double& rate_mmPerHour_sleet, double lat_, double lon_, double height_ )
588{
589        if (isInitialized())
590        {
591                // Init
592                bool hasPrecipitation = false;
593                double x = 0;
594                double y = 0;
595                double z = 0;
596                //// If -1 : Use sky internal values
597                if ( lat_ == -1 )
598                        lat_ = lat;
599                if ( lon_ == -1 )
600                        lon_ = lon;
601                if ( height_ == -1 )
602                        height_ = height;
603
604                // Set precipitation to zero;
605                rate_mmPerHour_rain = 0;
606                rate_mmPerHour_drySnow = 0;
607                rate_mmPerHour_wetSnow = 0;
608                rate_mmPerHour_sleet = 0;
609
610                // Get global position
611                util::calculateXYZAtWGS84Coordinate(lat_, lon_, height_, sceneRoot, x, y, z);
612
613                // Look up every cloud layer for it's precipitation.
614                for( int i=0; i<MAX_CLOUDLAYER_SLOTS; i++ )
615                {
616                        if ( cloudLayerSlots[i].used )  // IF used, Pointer should be valid
617                        {
618                                if( cloudLayerSlots[i].cloudLayerPointer->HasPrecipitationAtPosition(x, y, z) )
619                                {
620                                        hasPrecipitation = true;
621                                        SL_MAP (int, double) precipitationMap = cloudLayerSlots[i].cloudLayerPointer->GetPrecipitation();
622                                        for( SL_MAP (int, double)::iterator it = precipitationMap.begin(); it != precipitationMap.end(); it++ )
623                                        {
624                                                switch(it->first)
625                                                {
626                                                        case SilverLining::CloudLayer::RAIN : rate_mmPerHour_rain += it->second;
627                                                                break;
628                                                        case SilverLining::CloudLayer::DRY_SNOW : rate_mmPerHour_drySnow += it->second;
629                                                                break;
630                                                        case SilverLining::CloudLayer::WET_SNOW : rate_mmPerHour_wetSnow += it->second;
631                                                                break;
632                                                        case SilverLining::CloudLayer::SLEET : rate_mmPerHour_sleet += it->second;
633                                                                break;
634                                                        default: OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_skySilverLining::getOverallPrecipitationAtLocation() : Wrong precipitation type in map!" << std::endl;
635                                                                break;
636                                                };
637                                        }
638                                }       // If slot has Precipitation END
639                        }       // If used END
640                }       // For all slots END
641
642                OSG_NOTIFY( osg::ALWAYS ) << "Rain: " << rate_mmPerHour_rain << ", dry snow: " << rate_mmPerHour_drySnow << ", wet snow: " << rate_mmPerHour_wetSnow << ", sleet: " << rate_mmPerHour_sleet << std::endl;
643                return hasPrecipitation;
644
645        }       // If initialized END
646        return false;
647}
648
649
650bool visual_skySilverLining::getSlotPrecipitationAtLocation( int slot_, double& rate_mmPerHour_rain, double& rate_mmPerHour_drySnow, double& rate_mmPerHour_wetSnow, double& rate_mmPerHour_sleet, double lat_, double lon_, double height_ )
651{
652        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
653        {
654                if( cloudLayerSlots[slot_].used )       // IF used, Pointer should be valid
655                {
656                        double x = 0;
657                        double y = 0;
658                        double z = 0;
659                        rate_mmPerHour_rain = 0;
660                        rate_mmPerHour_drySnow = 0;
661                        rate_mmPerHour_wetSnow = 0;
662                        rate_mmPerHour_sleet = 0;
663                        //// If -1 : Use sky internal values
664                        if ( lat_ == -1 )
665                                lat_ = lat;
666                        if ( lon_ == -1 )
667                                lon_ = lon;
668                        if ( height_ == -1 )
669                                height_ = height;
670
671                        // Get global position
672                        util::calculateXYZAtWGS84Coordinate(lat_, lon_, height_, sceneRoot, x, y, z);
673
674                        // Check for precipitation
675                        if( cloudLayerSlots[slot_].cloudLayerPointer->HasPrecipitationAtPosition(x, y, z) )
676                        {
677                                SL_MAP (int, double) precipitationMap = cloudLayerSlots[slot_].cloudLayerPointer->GetPrecipitation();
678                                for( SL_MAP (int, double)::iterator it = precipitationMap.begin(); it != precipitationMap.end(); it++ )
679                                {
680                                        switch(it->first)
681                                        {
682                                                case SilverLining::CloudLayer::RAIN : rate_mmPerHour_rain = it->second;
683                                                        break;
684                                                case SilverLining::CloudLayer::DRY_SNOW : rate_mmPerHour_drySnow = it->second;
685                                                        break;
686                                                case SilverLining::CloudLayer::WET_SNOW : rate_mmPerHour_wetSnow = it->second;
687                                                        break;
688                                                case SilverLining::CloudLayer::SLEET : rate_mmPerHour_sleet = it->second;
689                                                        break;
690                                                default: OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_skySilverLining::getSlotPrecipitationAtLocation() : Wrong precipitation type in map!" << std::endl;
691                                                        break;
692                                        };
693                                }       // FOR END
694                                OSG_NOTIFY( osg::ALWAYS ) << "Rain: " << rate_mmPerHour_rain << ", dry snow: " << rate_mmPerHour_drySnow << ", wet snow: " << rate_mmPerHour_wetSnow << ", sleet: " << rate_mmPerHour_sleet << std::endl;
695                                return true;
696                        }       // If slot has Precipitation END
697                        else 
698                                return false;
699                }       // If used END
700                else
701                        return false;
702        }       // If initialized END
703        return false;
704}
705
706bool visual_skySilverLining::getSlotPrecipitation( int slot_, double& rate_mmPerHour_rain, double& rate_mmPerHour_drySnow, double& rate_mmPerHour_wetSnow, double& rate_mmPerHour_sleet )
707{
708        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
709        {
710                if( cloudLayerSlots[slot_].used )       // IF used, Pointer should be valid
711                {
712                        rate_mmPerHour_rain = 0;
713                        rate_mmPerHour_drySnow = 0;
714                        rate_mmPerHour_wetSnow = 0;
715                        rate_mmPerHour_sleet = 0;
716
717                        // Check for precipitation
718
719                                SL_MAP (int, double) precipitationMap = cloudLayerSlots[slot_].cloudLayerPointer->GetPrecipitation();
720                                for( SL_MAP (int, double)::iterator it = precipitationMap.begin(); it != precipitationMap.end(); it++ )
721                                {
722                                        switch(it->first)
723                                        {
724                                                case SilverLining::CloudLayer::RAIN : rate_mmPerHour_rain = it->second;
725                                                        break;
726                                                case SilverLining::CloudLayer::DRY_SNOW : rate_mmPerHour_drySnow = it->second;
727                                                        break;
728                                                case SilverLining::CloudLayer::WET_SNOW : rate_mmPerHour_wetSnow = it->second;
729                                                        break;
730                                                case SilverLining::CloudLayer::SLEET : rate_mmPerHour_sleet = it->second;
731                                                        break;
732                                                default: OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_skySilverLining::getSlotPrecipitation() : Wrong precipitation type in map!" << std::endl;
733                                                        break;
734                                        };
735                                }       // FOR END
736                               
737                                if ( rate_mmPerHour_rain>0 || rate_mmPerHour_drySnow>0 || rate_mmPerHour_wetSnow>0 || rate_mmPerHour_sleet>0)
738                                {
739                                        OSG_NOTIFY( osg::ALWAYS ) << "Rain: " << rate_mmPerHour_rain << ", dry snow: " << rate_mmPerHour_drySnow << ", wet snow: " << rate_mmPerHour_wetSnow << ", sleet: " << rate_mmPerHour_sleet << std::endl;
740                                        return true;
741                                }
742                                else
743                                        return false;
744                }       // If used END
745                else
746                        return false;
747        }       // If initialized END
748        return false;
749}
750
751void visual_skySilverLining::clearAllPrecipitation( int slot_ )
752{
753        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
754        {
755                if( cloudLayerSlots[slot_].used )       // IF used, Pointer should be valid
756                {
757                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::NONE, 0.0 );
758                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::NONE, 0.0 ); // Second Call to clear precipitation rate.
759                }
760        }
761}
762
763void visual_skySilverLining::setSlotPrecipitation( int slot_, double rate_mmPerHour_rain_, double rate_mmPerHour_drySnow_, double rate_mmPerHour_wetSnow_, double rate_mmPerHour_sleet_ )
764{
765        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
766        {
767                if( cloudLayerSlots[slot_].used )       // IF used, Pointer should be valid
768                {
769                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::RAIN, rate_mmPerHour_rain_ );
770                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::DRY_SNOW, rate_mmPerHour_drySnow_ );
771                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::WET_SNOW, rate_mmPerHour_wetSnow_ );
772                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::SLEET, rate_mmPerHour_sleet_ );
773                }
774        }
775}
Note: See TracBrowser for help on using the repository browser.