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

Last change on this file since 116 was 116, checked in by Torben Dannhauer, 14 years ago
File size: 26.7 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        setTime(12,00,23);
244        setVisibility(50000);
245        //atmosphere->GetConditions()->SetFog( 0.8, 1, 1, 1);   // use this for simulation real fog.
246
247
248
249        //Todo: secure memory-manager of timer*. oder remove paragraph
250        //MyMillisecondTimer *timer = new MyMillisecondTimer();
251 //   atmosphere->GetConditions()->SetMillisecondTimer(timer);
252        //atmosphere->GetConditions()->EnableTimePassage(true, -1);
253
254        //addWindVolume( 0.0, 15000.0, 300.0, 90.0 );
255       
256        //addCloudLayer( 0, 300000, 300000, 600.0, 2351.0, 0.5, STRATUS );
257        //addCloudLayer( 0, 5000000, 5000000, 600.0, 7351.0, 0.2, CIRRUS_FIBRATUS );
258        //addCloudLayer( 0, 50000, 50000, 600.0, 7351.0, 0.2, CIRROCUMULUS );
259        ///addCloudLayer( 1, 200000, 200000, 3000, 2000.0, 0.05, CUMULUS_CONGESTUS );
260        //cloudLayerSlots[1].cloudLayerPointer->SetPrecipitation(SilverLining::CloudLayer::SLEET, 25.0 );
261
262}
263
264void visual_skySilverLining::updateUpVector()
265{
266        // Allowed deltaLat and deltaLon between Updating the upvector
267        double deltaLatToUpdate = osg::DegreesToRadians( 0.1 ); // 0.5 deg
268        double deltaLonToUpdate = osg::DegreesToRadians( 0.1 ); // 0.5 deg
269
270        if ( fabs(lat-upVectorLat) > deltaLatToUpdate || fabs(lon-upVectorLon) > deltaLonToUpdate )
271        {
272                // Get ellipsoid model
273                osg::EllipsoidModel* ellipsoid = sceneRoot->getEllipsoidModel();
274                if ( !ellipsoid )
275                        return;
276
277                //OSG_NOTIFY( osg::ALWAYS ) << "update Upvector.." << std::endl;
278
279                // Calculate up vector
280                double x,y,z;
281                util::getXYZofCamera(sceneCamera, x, y, z);
282                osg::Vec3d upVector = ellipsoid->computeLocalUpVector( x, y, z );
283                upVector.normalize();
284               
285                // Calculate side vector
286                osg::Matrixd localToWorld;
287                ellipsoid->computeLocalToWorldTransformFromLatLongHeight(lat, lon, height, localToWorld);
288
289                osg::Vec3d sideVector(localToWorld(0, 0), localToWorld(0, 1), localToWorld(0, 2));
290                sideVector.normalize();
291
292                // Update silverlining vectors
293                //std::cout << "upVector: X: " << upVector.x() << ", Y: "<<  upVector.y() << ", Z: "<< upVector.z() << std::endl;
294                //std::cout << "sideVector: X: " << sideVector.x() << ", Y: "<<  sideVector.y() << ", Z: "<< sideVector.z() << std::endl;
295                atmosphere->SetUpVector( upVector.x(), upVector.y(), upVector.z() );
296                atmosphere->SetRightVector( sideVector.x(), sideVector.y(), sideVector.z() );
297
298                // Note new upvector lat/lon
299                upVectorLon = lon;
300                upVectorLat = lat;
301        }       // If update requiered END
302}
303
304void visual_skySilverLining::shutdown()
305{
306        if (isInitialized())
307        {
308                // Remove this Node from scenegraph
309                sceneRoot->removeChild( this );
310               
311                // Remove updatecallback
312                this->removeUpdateCallback( updateCallback );
313                updateCallback = NULL;
314
315                // delete drawables
316                skyDrawable->shutdown();
317                this->removeDrawable(skyDrawable);
318                this->removeDrawable(cloudsDrawable);
319        }
320}
321
322void visual_skySilverLining::setVisibility(double visibility_)
323{
324        if (isInitialized())
325        {
326                atmosphere->GetConditions()->SetVisibility( visibility_ );
327        }
328
329}
330
331double visual_skySilverLining::getVisibility()
332{
333        if (isInitialized())
334        {
335                return atmosphere->GetConditions()->GetVisibility();
336        }
337        else
338                return -1;
339}
340
341void visual_skySilverLining::setTurbidity(double turbidity_)
342{
343        if (isInitialized())
344        {
345                atmosphere->GetConditions()->SetTurbidity( turbidity_ );
346        }
347}
348
349double visual_skySilverLining::getTurbidity()
350{
351        if (isInitialized())
352        {
353                return atmosphere->GetConditions()->GetTurbidity();
354        }
355        else
356                return -1;
357}
358
359void visual_skySilverLining::clearAllWindVolumes()
360{
361        if (isInitialized())
362        {
363                atmosphere->GetConditions()->ClearWindVolumes();
364        }
365}
366
367bool visual_skySilverLining::insideWind(double height_, double& bottom_, double& top_, double& speed_, double& direction_)
368{
369        if (isInitialized())
370        {
371                // Calculation earth radius on the wind positionl approximated through the now used position.
372                double radius;
373                if ( util::calculateEarthRadiusAtWGS84Coordinate(lat, lon, sceneRoot, radius) )
374                {
375                        // go through all wind volumes an check them for inside()
376                        std::map<int, SilverLining::WindVolume> windvolumes = atmosphere->GetConditions()->GetWindVolumes();
377                        for(unsigned int i=0; i<windvolumes.size(); i++)
378                        {
379                                if( windvolumes[i].Inside(radius + height_) )
380                                {
381                                        // save wind data
382                                        bottom_ = windvolumes[i].GetMinAltitude();
383                                        top_ = windvolumes[i].GetMaxAltitude() - radius;
384                                        speed_ = windvolumes[i].GetWindSpeed() - radius;
385                                        direction_ = windvolumes[i].GetDirection();
386                                        // return that wind was found
387                                        return true;
388                                }
389                        }       // For END
390                }       // If valid radius END
391        }       // If initialized() END
392        bottom_ = -1;
393        top_ = -1;
394        speed_ = -1;
395        direction_ = -1;
396        return false;
397}
398
399void visual_skySilverLining::addWindVolume(double bottom_, double top_, double speed_, int direction_)
400{
401        if (isInitialized())           
402        {
403                // Calculation earth radius on current lat lon position
404                double radius;
405                if ( util::calculateEarthRadiusAtWGS84Coordinate(lat, lon, sceneRoot, radius) )
406                {
407                        // correct wind value:
408                        if ( direction_ < 180 )
409                                direction_ += 180;
410                        else direction_ -= 180;
411
412                        // Setting up Wind
413                        SilverLining::WindVolume wv;
414                        wv.SetDirection( direction_ );
415                        wv.SetMinAltitude( radius + bottom_ );
416                        wv.SetMaxAltitude( radius + top_ );
417                        wv.SetWindSpeed( speed_ );
418                        atmosphere->GetConditions()->SetWind(wv);
419                }
420        }
421}
422
423void visual_skySilverLining::setLightPollution(double lightPollution_)
424{
425        if (isInitialized())
426        {
427                return atmosphere->GetConditions()->SetLightPollution( lightPollution_ );
428        }
429}
430
431double visual_skySilverLining::getLightPollution()
432{
433        if (isInitialized())
434        {
435                return atmosphere->GetConditions()->GetLightPollution();
436        }
437        else
438                return -1;
439
440}
441
442void visual_skySilverLining::addCloudLayer(int slot_, double baseLength_, double baseWidth_, double thickness_, double baseHeight_, double density_, CloudTypes cloudtype_ )
443{
444        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
445        {
446                // Calculation earth radius on current lat lon position
447                double radius;
448                if ( util::calculateEarthRadiusAtWGS84Coordinate(lat, lon, sceneRoot, radius) )
449                {
450                        // generate Cloud Layer
451                        SilverLining::CloudLayer *cloudLayer_;
452                        cloudLayer_ = SilverLining::CloudLayerFactory::Create(cloudtype_);
453                        cloudLayer_->SetBaseAltitude( baseHeight_ + radius);
454                       
455                        cloudLayer_->SetThickness(thickness_);
456                        cloudLayer_->SetBaseLength(baseLength_);
457                        cloudLayer_->SetBaseWidth(baseWidth_);
458                        cloudLayer_->SetDensity(density_);
459                        cloudLayer_->SetLayerPosition( 0.0, 0.0 );
460                        cloudLayer_->GenerateShadowMaps(false);
461
462                        //Save cloudlayer information into SLOT
463                        cloudLayerSlots[slot_].used = true;
464                        cloudLayerSlots[slot_].enabled = true;
465                        cloudLayerSlots[slot_].cloudLayerPointer = cloudLayer_;
466                        switch(cloudtype_)
467                        {
468                        case 0: cloudLayerSlots[slot_].typeName = "CIRROCUMULUS";                               // High planar cloud puffs             
469                                break;
470                        case 1: cloudLayerSlots[slot_].typeName = "CIRRUS_FIBRATUS";                    // High, thicker and fibrous clouds that signal changing weather
471                        break;
472                        case 2: cloudLayerSlots[slot_].typeName = "NIMBOSTRATUS";               // Low rain clouds that cover the sky
473                        break;
474                        case 3: cloudLayerSlots[slot_].typeName = "CUMULUS_MEDIOCRIS";          // Low, puffy clouds on fair days
475                        break;
476                        case 4: cloudLayerSlots[slot_].typeName = "CUMULUS_CONGESTUS";          // Large cumulus clouds that could turn into a thunderhead
477                        break;
478                        case 5: cloudLayerSlots[slot_].typeName = "CUMULONIMBUS_CAPPILATUS";    // Big storm clouds.
479                        break;
480                        case 6: cloudLayerSlots[slot_].typeName = "CUMULUS_CONGESTUS_INFINITE"; // Cumulus congestus layer that wraps to surround the camera at all times.
481                        break;
482                        case 7: cloudLayerSlots[slot_].typeName = "CUMULUS_MEDIOCRIS_INFINITE"; // Cumulus mediocris layer that wraps to surround the camera at all times.
483                                break;
484                        default: OSG_NOTIFY( osg::FATAL ) << "visual_skySilverlining::addCloudLayer - Invalid cloud type." << std::cout;
485                                 break;
486                        };
487               
488                        // Pass cloudlayer for seeding and adding to sky framework to the render thread
489                        skyDrawable->addCloudLayer( &cloudLayerSlots[slot_] );
490                } // If valid radius END
491        }        // If isInitialized() END
492}
493
494void visual_skySilverLining::removeCloudLayer( int slot_ )
495{
496        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
497        {
498                atmosphere->GetConditions()->RemoveCloudLayer( cloudLayerSlots[slot_].cloudLayerHandle );
499                cloudLayerSlots[slot_].used = false;
500                cloudLayerSlots[slot_].cloudLayerHandle = -1;
501                cloudLayerSlots[slot_].cloudLayerPointer = NULL;
502                cloudLayerSlots[slot_].enabled = false;
503        }
504}
505
506void visual_skySilverLining::clearAllSlots()
507{
508        if (isInitialized())
509        {
510                atmosphere->GetConditions()->RemoveAllCloudLayers();
511                for( int i=0; i<MAX_CLOUDLAYER_SLOTS; i++ )
512                {
513                        cloudLayerSlots[i].used = false;
514                        cloudLayerSlots[i].cloudLayerHandle = -1;
515                        cloudLayerSlots[i].cloudLayerPointer = NULL;
516                        cloudLayerSlots[i].enabled = false;
517                }
518        }
519}
520
521SilverLining::CloudLayer* visual_skySilverLining::getCloudLayer( int slot_ )
522{
523        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
524        {
525                if ( cloudLayerSlots[slot_].used )
526                        return cloudLayerSlots[slot_].cloudLayerPointer; 
527        }
528
529        return NULL;
530}
531
532void visual_skySilverLining::setEnabled(int slot_, bool enabled_ )
533{
534        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
535        {
536                cloudLayerSlots[slot_].enabled = enabled_;
537        }
538}
539
540bool visual_skySilverLining::isEnabled( int slot_ )
541{
542        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
543        {
544                return cloudLayerSlots[slot_].enabled;
545        }
546        return false;
547}
548
549void visual_skySilverLining::fadeVisibility( int slot_, int fadetimeMS_ )
550{
551        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
552        {
553                if (cloudLayerSlots[slot_].enabled)
554                {
555                        cloudLayerSlots[slot_].enabled = false;
556                        cloudLayerSlots[slot_].cloudLayerPointer->SetEnabled( false, fadetimeMS_ );
557                }
558                else
559                {
560                        cloudLayerSlots[slot_].enabled = true;
561                        cloudLayerSlots[slot_].cloudLayerPointer->SetEnabled( true, fadetimeMS_ );
562                }
563        }
564}
565
566std::string visual_skySilverLining::getCloudLayerTypeName( int slot_ )
567{
568        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
569        {
570                return cloudLayerSlots[slot_].typeName;
571        }
572        return "";
573}
574
575void visual_skySilverLining::clearGlobalPrecipitation()
576{
577        if (isInitialized())
578        {
579                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::NONE, 0.0 );
580        }
581}
582
583void visual_skySilverLining::setGlobalPrecipitation( double rate_mmPerHour_rain_, double rate_mmPerHour_drySnow_, double rate_mmPerHour_wetSnow_, double rate_mmPerHour_sleet_ )
584{
585        if ( isInitialized() )
586        {
587                // Delete old Precipitation
588                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::NONE, 0.0 );
589               
590                // Set new Precipitation
591                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::RAIN, rate_mmPerHour_rain_ );
592                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::WET_SNOW, rate_mmPerHour_drySnow_ );
593                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::DRY_SNOW, rate_mmPerHour_wetSnow_ );
594                atmosphere->GetConditions()->SetPrecipitation( SilverLining::CloudLayer::SLEET, rate_mmPerHour_sleet_ );
595        }
596}
597       
598bool 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_ )
599{
600        if (isInitialized())
601        {
602                // Init
603                bool hasPrecipitation = false;
604                double x = 0;
605                double y = 0;
606                double z = 0;
607                //// If -1 : Use sky internal values
608                if ( lat_ == -1 )
609                        lat_ = lat;
610                if ( lon_ == -1 )
611                        lon_ = lon;
612                if ( height_ == -1 )
613                        height_ = height;
614
615                // Set precipitation to zero;
616                rate_mmPerHour_rain = 0;
617                rate_mmPerHour_drySnow = 0;
618                rate_mmPerHour_wetSnow = 0;
619                rate_mmPerHour_sleet = 0;
620
621                // Get global position
622                util::calculateXYZAtWGS84Coordinate(lat_, lon_, height_, sceneRoot, x, y, z);
623
624                // Look up every cloud layer for it's precipitation.
625                for( int i=0; i<MAX_CLOUDLAYER_SLOTS; i++ )
626                {
627                        if ( cloudLayerSlots[i].used )  // IF used, Pointer should be valid
628                        {
629                                if( cloudLayerSlots[i].cloudLayerPointer->HasPrecipitationAtPosition(x, y, z) )
630                                {
631                                        hasPrecipitation = true;
632                                        std::map<int, double> precipitationMap = cloudLayerSlots[i].cloudLayerPointer->GetPrecipitation();
633                                        for( std::map<int, double>::iterator it = precipitationMap.begin(); it != precipitationMap.end(); it++ )
634                                        {
635                                                switch(it->first)
636                                                {
637                                                        case SilverLining::CloudLayer::RAIN : rate_mmPerHour_rain += it->second;
638                                                                break;
639                                                        case SilverLining::CloudLayer::DRY_SNOW : rate_mmPerHour_drySnow += it->second;
640                                                                break;
641                                                        case SilverLining::CloudLayer::WET_SNOW : rate_mmPerHour_wetSnow += it->second;
642                                                                break;
643                                                        case SilverLining::CloudLayer::SLEET : rate_mmPerHour_sleet += it->second;
644                                                                break;
645                                                        default: OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_skySilverLining::getOverallPrecipitationAtLocation() : Wrong precipitation type in map!" << std::endl;
646                                                                break;
647                                                };
648                                        }
649                                }       // If slot has Precipitation END
650                        }       // If used END
651                }       // For all slots END
652
653                OSG_NOTIFY( osg::ALWAYS ) << "Rain: " << rate_mmPerHour_rain << ", dry snow: " << rate_mmPerHour_drySnow << ", wet snow: " << rate_mmPerHour_wetSnow << ", sleet: " << rate_mmPerHour_sleet << std::endl;
654                return hasPrecipitation;
655
656        }       // If initialized END
657        return false;
658}
659
660
661bool 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_ )
662{
663        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
664        {
665                if( cloudLayerSlots[slot_].used )       // IF used, Pointer should be valid
666                {
667                        double x = 0;
668                        double y = 0;
669                        double z = 0;
670                        rate_mmPerHour_rain = 0;
671                        rate_mmPerHour_drySnow = 0;
672                        rate_mmPerHour_wetSnow = 0;
673                        rate_mmPerHour_sleet = 0;
674                        //// If -1 : Use sky internal values
675                        if ( lat_ == -1 )
676                                lat_ = lat;
677                        if ( lon_ == -1 )
678                                lon_ = lon;
679                        if ( height_ == -1 )
680                                height_ = height;
681
682                        // Get global position
683                        util::calculateXYZAtWGS84Coordinate(lat_, lon_, height_, sceneRoot, x, y, z);
684
685                        // Check for precipitation
686                        if( cloudLayerSlots[slot_].cloudLayerPointer->HasPrecipitationAtPosition(x, y, z) )
687                        {
688                                std::map<int, double> precipitationMap = cloudLayerSlots[slot_].cloudLayerPointer->GetPrecipitation();
689                                for( std::map<int, double>::iterator it = precipitationMap.begin(); it != precipitationMap.end(); it++ )
690                                {
691                                        switch(it->first)
692                                        {
693                                                case SilverLining::CloudLayer::RAIN : rate_mmPerHour_rain = it->second;
694                                                        break;
695                                                case SilverLining::CloudLayer::DRY_SNOW : rate_mmPerHour_drySnow = it->second;
696                                                        break;
697                                                case SilverLining::CloudLayer::WET_SNOW : rate_mmPerHour_wetSnow = it->second;
698                                                        break;
699                                                case SilverLining::CloudLayer::SLEET : rate_mmPerHour_sleet = it->second;
700                                                        break;
701                                                default: OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_skySilverLining::getSlotPrecipitationAtLocation() : Wrong precipitation type in map!" << std::endl;
702                                                        break;
703                                        };
704                                }       // FOR END
705                                OSG_NOTIFY( osg::ALWAYS ) << "Rain: " << rate_mmPerHour_rain << ", dry snow: " << rate_mmPerHour_drySnow << ", wet snow: " << rate_mmPerHour_wetSnow << ", sleet: " << rate_mmPerHour_sleet << std::endl;
706                                return true;
707                        }       // If slot has Precipitation END
708                        else 
709                                return false;
710                }       // If used END
711                else
712                        return false;
713        }       // If initialized END
714        return false;
715}
716
717bool visual_skySilverLining::getSlotPrecipitation( int slot_, double& rate_mmPerHour_rain, double& rate_mmPerHour_drySnow, double& rate_mmPerHour_wetSnow, double& rate_mmPerHour_sleet )
718{
719        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
720        {
721                if( cloudLayerSlots[slot_].used )       // IF used, Pointer should be valid
722                {
723                        rate_mmPerHour_rain = 0;
724                        rate_mmPerHour_drySnow = 0;
725                        rate_mmPerHour_wetSnow = 0;
726                        rate_mmPerHour_sleet = 0;
727
728                        // Check for precipitation
729
730                                std::map<int, double> precipitationMap = cloudLayerSlots[slot_].cloudLayerPointer->GetPrecipitation();
731                                for( std::map<int, double>::iterator it = precipitationMap.begin(); it != precipitationMap.end(); it++ )
732                                {
733                                        switch(it->first)
734                                        {
735                                                case SilverLining::CloudLayer::RAIN : rate_mmPerHour_rain = it->second;
736                                                        break;
737                                                case SilverLining::CloudLayer::DRY_SNOW : rate_mmPerHour_drySnow = it->second;
738                                                        break;
739                                                case SilverLining::CloudLayer::WET_SNOW : rate_mmPerHour_wetSnow = it->second;
740                                                        break;
741                                                case SilverLining::CloudLayer::SLEET : rate_mmPerHour_sleet = it->second;
742                                                        break;
743                                                default: OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_skySilverLining::getSlotPrecipitation() : Wrong precipitation type in map!" << std::endl;
744                                                        break;
745                                        };
746                                }       // FOR END
747                               
748                                if ( rate_mmPerHour_rain>0 || rate_mmPerHour_drySnow>0 || rate_mmPerHour_wetSnow>0 || rate_mmPerHour_sleet>0)
749                                {
750                                        OSG_NOTIFY( osg::ALWAYS ) << "Rain: " << rate_mmPerHour_rain << ", dry snow: " << rate_mmPerHour_drySnow << ", wet snow: " << rate_mmPerHour_wetSnow << ", sleet: " << rate_mmPerHour_sleet << std::endl;
751                                        return true;
752                                }
753                                else
754                                        return false;
755                }       // If used END
756                else
757                        return false;
758        }       // If initialized END
759        return false;
760}
761
762void visual_skySilverLining::clearAllPrecipitation( int slot_ )
763{
764        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
765        {
766                if( cloudLayerSlots[slot_].used )       // IF used, Pointer should be valid
767                {
768                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::NONE, 0.0 );
769                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::NONE, 0.0 ); // Second Call to clear precipitation rate.
770                }
771        }
772}
773
774void visual_skySilverLining::setSlotPrecipitation( int slot_, double rate_mmPerHour_rain_, double rate_mmPerHour_drySnow_, double rate_mmPerHour_wetSnow_, double rate_mmPerHour_sleet_ )
775{
776        if (isInitialized() && slot_ >= 0 && slot_ < MAX_CLOUDLAYER_SLOTS)
777        {
778                if( cloudLayerSlots[slot_].used )       // IF used, Pointer should be valid
779                {
780                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::RAIN, rate_mmPerHour_rain_ );
781                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::DRY_SNOW, rate_mmPerHour_drySnow_ );
782                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::WET_SNOW, rate_mmPerHour_wetSnow_ );
783                        cloudLayerSlots[slot_].cloudLayerPointer->SetPrecipitation( SilverLining::CloudLayer::SLEET, rate_mmPerHour_sleet_ );
784                }
785        }
786}
Note: See TracBrowser for help on using the repository browser.