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

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