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

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