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

Last change on this file since 31 was 31, checked in by Torben Dannhauer, 14 years ago

Adding first version of osgVisual!!

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