]> git.mxchange.org Git - simgear.git/blobdiff - simgear/environment/visual_enviro.cxx
Allow tasks to be removed from the EventManager by name.
[simgear.git] / simgear / environment / visual_enviro.cxx
index 1c265f7f1fde3fba4a0d9bc36fea5b730208dc50..b0cb219059f4b8ceecf844b8d7cb3581a0c85886 100644 (file)
@@ -23,7 +23,6 @@
 #  include <simgear_config.h>
 #endif
 
-#include <plib/sg.h>
 #include <simgear/constants.h>
 #include <simgear/structure/SGReferenced.hxx>
 #include <simgear/structure/SGSharedPtr.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/math/point3d.hxx>
 #include <simgear/math/polar3d.hxx>
-#include <simgear/sound/soundmgr_openal.hxx>
+#include <simgear/sound/sample_group.hxx>
 #include <simgear/scene/sky/cloudfield.hxx>
 #include <simgear/scene/sky/newcloud.hxx>
+#include <simgear/props/props.hxx>
 #include "visual_enviro.hxx"
 
 #include <vector>
 
-SG_USING_STD(vector);
+using std::vector;
 
 
 typedef struct {
@@ -51,6 +51,85 @@ typedef struct {
 static float rainpos[MAX_RAIN_SLICE];
 #define MAX_LT_TREE_SEG        400
 
+#define DFL_MIN_LIGHT 0.35
+sgVec3 SGEnviro::min_light = {DFL_MIN_LIGHT, DFL_MIN_LIGHT, DFL_MIN_LIGHT};
+#define DFL_STREAK_BRIGHT_NEARMOST_LAYER 0.9
+SGfloat SGEnviro::streak_bright_nearmost_layer = DFL_STREAK_BRIGHT_NEARMOST_LAYER;
+#define DFL_STREAK_BRIGHT_FARMOST_LAYER 0.5
+SGfloat SGEnviro::streak_bright_farmost_layer = DFL_STREAK_BRIGHT_FARMOST_LAYER;
+#define DFL_STREAK_PERIOD_MAX 2.5
+SGfloat SGEnviro::streak_period_max = DFL_STREAK_PERIOD_MAX;
+#define DFL_STREAK_PERIOD_CHANGE_PER_KT 0.005
+SGfloat SGEnviro::streak_period_change_per_kt = DFL_STREAK_PERIOD_CHANGE_PER_KT;
+#define DFL_STREAK_PERIOD_MIN 1.0
+SGfloat SGEnviro::streak_period_min = DFL_STREAK_PERIOD_MIN;
+#define DFL_STREAK_LENGTH_MIN 0.03
+SGfloat SGEnviro::streak_length_min = DFL_STREAK_LENGTH_MIN;
+#define DFL_STREAK_LENGTH_CHANGE_PER_KT 0.0005
+SGfloat SGEnviro::streak_length_change_per_kt = DFL_STREAK_LENGTH_CHANGE_PER_KT;
+#define DFL_STREAK_LENGTH_MAX 0.1
+SGfloat SGEnviro::streak_length_max = DFL_STREAK_LENGTH_MAX;
+#define DFL_STREAK_COUNT_MIN 40
+int SGEnviro::streak_count_min = DFL_STREAK_COUNT_MIN;
+#define DFL_STREAK_COUNT_MAX 190
+#if (DFL_STREAK_COUNT_MAX > MAX_RAIN_SLICE)
+#error "Bad default!"
+#endif
+int SGEnviro::streak_count_max = DFL_STREAK_COUNT_MAX;
+#define DFL_CONE_BASE_RADIUS 15.0
+SGfloat SGEnviro::cone_base_radius = DFL_CONE_BASE_RADIUS;
+#define DFL_CONE_HEIGHT 30.0
+SGfloat SGEnviro::cone_height = DFL_CONE_HEIGHT;
+
+
+void SGEnviro::config(const SGPropertyNode* n)
+{
+       if (!n)
+               return;
+
+       const float ml = n->getFloatValue("min-light", DFL_MIN_LIGHT);
+       sgSetVec3(min_light, ml, ml, ml);
+
+       streak_bright_nearmost_layer = n->getFloatValue(
+                       "streak-brightness-nearmost-layer",
+                       DFL_STREAK_BRIGHT_NEARMOST_LAYER);
+       streak_bright_farmost_layer = n->getFloatValue(
+                       "streak-brightness-farmost-layer",
+                       DFL_STREAK_BRIGHT_FARMOST_LAYER);
+
+       streak_period_max = n->getFloatValue(
+                       "streak-period-max",
+                       DFL_STREAK_PERIOD_MAX);
+       streak_period_min = n->getFloatValue(
+                       "streak-period-min",
+                       DFL_STREAK_PERIOD_MIN);
+       streak_period_change_per_kt = n->getFloatValue(
+                       "streak-period-change-per-kt",
+                       DFL_STREAK_PERIOD_CHANGE_PER_KT);
+
+       streak_length_max = n->getFloatValue(
+                       "streak-length-max",
+                       DFL_STREAK_LENGTH_MAX);
+       streak_length_min = n->getFloatValue(
+                       "streak-length-min",
+                       DFL_STREAK_LENGTH_MIN);
+       streak_length_change_per_kt = n->getFloatValue(
+                       "streak-length-change-per-kt",
+                       DFL_STREAK_LENGTH_CHANGE_PER_KT);
+
+       streak_count_min = n->getIntValue(
+                       "streak-count-min", DFL_STREAK_COUNT_MIN);
+       streak_count_max = n->getIntValue(
+                       "streak-count-max", DFL_STREAK_COUNT_MAX);
+       if (streak_count_max > MAX_RAIN_SLICE)
+               streak_count_max = MAX_RAIN_SLICE;
+
+       cone_base_radius = n->getFloatValue(
+                       "cone-base-radius", DFL_CONE_BASE_RADIUS);
+       cone_height = n->getFloatValue("cone_height", DFL_CONE_HEIGHT);
+}
+
+
 /**
  * A class to render lightnings.
  */
@@ -96,7 +175,7 @@ SGEnviro::SGEnviro() :
        lightning_enable_state(false),
        elapsed_time(0.0),
        dt(0.0),
-       soundMgr(NULL),
+       sampleGroup(NULL),
        snd_active(false),
        snd_dist(0.0),
        min_time_before_lt(0.0),
@@ -110,6 +189,10 @@ SGEnviro::SGEnviro() :
 }
 
 SGEnviro::~SGEnviro(void) {
+       if (sampleGroup) delete sampleGroup;
+
+  // OSGFIXME
+  return;
        list_of_lightning::iterator iLightning;
        for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
                delete (*iLightning);
@@ -118,10 +201,10 @@ SGEnviro::~SGEnviro(void) {
 }
 
 void SGEnviro::startOfFrame( sgVec3 p, sgVec3 up, double lon, double lat, double alt, double delta_time) {
+  // OSGFIXME
+  return;
        view_in_cloud = false;
        // ask the impostor cache to do some cleanup
-       if(SGNewCloud::cldCache)
-               SGNewCloud::cldCache->startNewFrame();
        last_cloud_turbulence = cloud_turbulence;
        cloud_turbulence = 0.0;
        elapsed_time += delta_time;
@@ -172,43 +255,10 @@ void SGEnviro::set_view_in_cloud(bool incloud) {
        view_in_cloud = incloud;
 }
 
-int SGEnviro::get_CacheResolution(void) const {
-       return SGCloudField::get_CacheResolution();
-}
-
-int SGEnviro::get_clouds_CacheSize(void) const {
-       return SGCloudField::get_CacheSize();
-}
-float SGEnviro::get_clouds_visibility(void) const {
-       return SGCloudField::get_CloudVis();
-}
-float SGEnviro::get_clouds_density(void) const {
-       return SGCloudField::get_density();
-}
-bool SGEnviro::get_clouds_enable_state(void) const {
-       return SGCloudField::get_enable3dClouds();
-}
-
 bool SGEnviro::get_turbulence_enable_state(void) const {
        return turbulence_enable_state;
 }
 
-void SGEnviro::set_CacheResolution(int resolutionPixels) {
-       SGCloudField::set_CacheResolution(resolutionPixels);
-}
-
-void SGEnviro::set_clouds_CacheSize(int sizeKb) {
-       SGCloudField::set_CacheSize(sizeKb);
-}
-void SGEnviro::set_clouds_visibility(float distance) {
-       SGCloudField::set_CloudVis(distance);
-}
-void SGEnviro::set_clouds_density(float density) {
-       SGCloudField::set_density(density);
-}
-void SGEnviro::set_clouds_enable_state(bool enable) {
-       SGCloudField::set_enable3dClouds(enable);
-}
 void SGEnviro::set_turbulence_enable_state(bool enable) {
        turbulence_enable_state = enable;
 }
@@ -240,12 +290,14 @@ void SGEnviro::set_lightning_enable_state(bool enable) {
 }
 
 void SGEnviro::setLight(sgVec4 adj_fog_color) {
+  // OSGFIXME
+  return;
        sgCopyVec4( fog_color, adj_fog_color );
        if( false ) {
        //    ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
        }
 }
-
+#if 0
 void SGEnviro::callback_cloud(float heading, float alt, float radius, int family, float dist, int cloudId) {
        // send data to wx radar
        // compute turbulence
@@ -318,6 +370,7 @@ void SGEnviro::callback_cloud(float heading, float alt, float radius, int family
                        LWC = 0.29*2.0;
                        break;
        }
+
        // add to the list for the wxRadar instrument
        if( LWC > 0.0 )
                radarEcho.push_back( SGWxRadarEcho ( heading, alt, radius, dist, LWC, false, cloudId ) );
@@ -355,31 +408,35 @@ void SGEnviro::callback_cloud(float heading, float alt, float radius, int family
                }
 }
 
+#endif
+
 list_of_SGWxRadarEcho *SGEnviro::get_radar_echo(void) {
        return &radarEcho;
 }
 
 // precipitation rendering code
 void SGEnviro::DrawCone2(float baseRadius, float height, int slices, bool down, double rain_norm, double speed) {
+  // OSGFIXME
+  return;
        sgVec3 light;
-       sgVec3 min_light = {0.35, 0.35, 0.35};
        sgAddVec3( light, fog_color, min_light );
        float da = SG_PI * 2.0f / (float) slices;
        // low number = faster
-       float speedf = 2.5f - speed / 200.0;
-       if( speedf < 1.0f )
-               speedf = 1.0f;
-       float lenf = 0.03f + speed / 2000.0;
-       if( lenf > 0.10f )
-               lenf = 0.10f;
+       float speedf = streak_period_max - speed * streak_period_change_per_kt;
+       if( speedf < streak_period_min )
+               speedf = streak_period_min;
+       float lenf = streak_length_min + speed * streak_length_change_per_kt;
+       if( lenf > streak_length_max )
+               lenf = streak_length_max;
     float t = fmod((float) elapsed_time, speedf) / speedf;
 //     t = 0.1f;
        if( !down )
                t = 1.0f - t;
        float angle = 0.0f;
-       glColor4f(1.0f, 0.7f, 0.7f, 0.9f);
+       //glColor4f(1.0f, 0.7f, 0.7f, 0.9f); // XXX unneeded? overriden below
        glBegin(GL_LINES);
-       int rainpos_indice = 0;
+       if (slices >  MAX_RAIN_SLICE)
+               slices = MAX_RAIN_SLICE; // should never happen
        for( int i = 0 ; i < slices ; i++ ) {
                float x = cos(angle) * baseRadius;
                float y = sin(angle) * baseRadius;
@@ -387,12 +444,14 @@ void SGEnviro::DrawCone2(float baseRadius, float height, int slices, bool down,
                sgVec3 dir = {x, -height, y};
 
                // rain drops at 2 different speed to simulate depth
-               float t1 = (i & 1 ? t : t + t) + rainpos[rainpos_indice];
+               float t1 = (i & 1 ? t : t + t) + rainpos[i];
                if(t1 > 1.0f)   t1 -= 1.0f;
                if(t1 > 1.0f)   t1 -= 1.0f;
 
                // distant raindrops are more transparent
-               float c = (i & 1 ? t1 * 0.5f : t1 * 0.9f);
+               float c = t1 * (i & 1 ?
+                               streak_bright_farmost_layer
+                               : streak_bright_nearmost_layer);
                glColor4f(c * light[0], c * light[1], c * light[2], c);
                sgVec3 p1, p2;
                sgScaleVec3(p1, dir, t1);
@@ -402,25 +461,25 @@ void SGEnviro::DrawCone2(float baseRadius, float height, int slices, bool down,
 
                glVertex3f(p1[0], p1[1] + height, p1[2]);
                glVertex3f(p2[0], p2[1] + height, p2[2]);
-               if( ++rainpos_indice >= MAX_RAIN_SLICE )
-                       rainpos_indice = 0;
        }
        glEnd();
 }
 
 void SGEnviro::drawRain(double pitch, double roll, double heading, double hspeed, double rain_norm) {
+  // OSGFIXME
+  return;
 
 #if 0
        static int debug_period = 0;
        if (debug_period++ == 50) {
                debug_period = 0;
-               cout << "drawRain(" 
-                       << pitch << ", " 
-                       << roll  << ", " 
-                       << heading << ", " 
-                       << hspeed << ", " 
+               cout << "drawRain("
+                       << pitch << ", "
+                       << roll  << ", "
+                       << heading << ", "
+                       << hspeed << ", "
                        << rain_norm << ");"
-                       //" angle = " << angle 
+                       //" angle = " << angle
                        //<< " raindrop(KTS) = " << raindrop_speed_kts
                        << endl;
        }
@@ -437,14 +496,15 @@ void SGEnviro::drawRain(double pitch, double roll, double heading, double hspeed
        glDisable(GL_LIGHTING);
 
        int slice_count = static_cast<int>(
-                               (40.0 + rain_norm*150.0)* precipitation_density / 100.0);
+                       (streak_count_min + rain_norm*(streak_count_max-streak_count_min))
+                               * precipitation_density / 100.0);
 
        // www.wonderquest.com/falling-raindrops.htm says that
        // Raindrop terminal velocity is 5 to 20mph
        // Rather than model it accurately (temp, pressure, diameter), and make it
        // smaller than terminal when closer to the precipitation cloud base,
        // we interpolate in the 5-20mph range according to rain_norm.
-       double raindrop_speed_kts 
+       double raindrop_speed_kts
                = (5.0 + rain_norm*15.0) * SG_MPH_TO_MPS * SG_MPS_TO_KT;
 
        float angle = atanf(hspeed / raindrop_speed_kts) * SG_RADIANS_TO_DEGREES;
@@ -456,10 +516,12 @@ void SGEnviro::drawRain(double pitch, double roll, double heading, double hspeed
                glRotatef(angle, 1.0, 0.0, 0.0);
 
                // up cone
-               DrawCone2(15.0, 30.0, slice_count, true, rain_norm, hspeed);
+               DrawCone2(cone_base_radius, cone_height, 
+                               slice_count, true, rain_norm, hspeed);
                // down cone (usually not visible)
                if(angle > 0.0 || heading != 0.0)
-                       DrawCone2(15.0, -30.0, slice_count, false, rain_norm, hspeed);
+                       DrawCone2(cone_base_radius, -cone_height, 
+                                       slice_count, false, rain_norm, hspeed);
 
        glPopMatrix();
 
@@ -470,11 +532,13 @@ void SGEnviro::drawRain(double pitch, double roll, double heading, double hspeed
 
 }
 
-void SGEnviro::set_soundMgr(SGSoundMgr *mgr) {
-       soundMgr = mgr;
+void SGEnviro::set_sampleGroup(SGSampleGroup *sgr) {
+       sampleGroup = sgr;
 }
 
 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double hspeed) {
+  // OSGFIXME
+  return;
        if( precipitation_enable_state && rain_norm > 0.0)
          if( precipitation_max_alt >= last_alt )
                drawRain(pitch, roll, heading, hspeed, rain_norm);
@@ -497,6 +561,8 @@ SGLightning::~SGLightning() {
 
 // lightning rendering code
 void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize) {
+  // OSGFIXME
+  return;
 
        sgVec3 dir, newdir;
        int nseg = 0;
@@ -544,13 +610,15 @@ void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy
 }
 
 void SGLightning::lt_build(void) {
+  // OSGFIXME
+  return;
     Point3D top;
     nb_tree = 0;
     top[PX] = 0 ;
     top[PY] = alt;
     top[PZ] = 0;
     lt_build_tree_branch(0, top, 1.0, 50, top[PY] / 8.0);
-       if( ! sgEnviro.soundMgr )
+       if( ! sgEnviro.sampleGroup )
                return;
        Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
        Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
@@ -569,6 +637,8 @@ void SGLightning::lt_build(void) {
 
 
 void SGLightning::lt_Render(void) {
+  // OSGFIXME
+  return;
        float flash = 0.5;
        if( fmod(sgEnviro.elapsed_time*100.0, 100.0) > 50.0 )
                flash = sg_random() * 0.75f + 0.25f;
@@ -593,10 +663,12 @@ void SGLightning::lt_Render(void) {
        glDisable( GL_FOG );
        glPushMatrix();
        sgMat4 modelview, tmp;
-    ssgGetModelviewMatrix( modelview );
+    // OSGFIXME
+//     ssgGetModelviewMatrix( modelview );
        sgCopyMat4( tmp, sgEnviro.transform );
     sgPostMultMat4( tmp, modelview );
-    ssgLoadModelviewMatrix( tmp );
+    // OSGFIXME
+//     ssgLoadModelviewMatrix( tmp );
 
     Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
     Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
@@ -654,6 +726,8 @@ void SGLightning::lt_Render(void) {
 }
 
 void SGEnviro::addLightning(double lon, double lat, double alt) {
+  // OSGFIXME
+  return;
        if( lightnings.size() > 10)
                return;
        SGLightning *lt= new SGLightning(lon, lat, alt);
@@ -661,9 +735,11 @@ void SGEnviro::addLightning(double lon, double lat, double alt) {
 }
 
 void SGEnviro::drawLightning(void) {
+  // OSGFIXME
+  return;
        list_of_lightning::iterator iLightning;
        // play 'thunder' for lightning
-       if( snd_active )
+       if( snd_active ) {
                if( !snd_playing ) {
                        // wait until sound has reached us
                        snd_timer += dt;
@@ -677,20 +753,21 @@ void SGEnviro::drawLightning(void) {
                                double ax = 0.0, ay = 0.0;
                                ax = cos(course) * dist;
                                ay = sin(course) * dist;
-                               SGSharedPtr<SGSoundSample> snd = soundMgr->find("thunder");
+                               SGSharedPtr<SGSoundSample> snd = sampleGroup->find("thunder");
                                if( snd ) {
-                                       ALfloat pos[3]={ax, ay, -sgEnviro.last_alt };
-                                       snd->set_source_pos(pos);
+                                       SGVec3d pos = SGVec3d(ax, ay, -sgEnviro.last_alt);
+                                       snd->set_position(pos);
                                        snd->play_once();
                                }
                        }
                } else {
-                       if( !soundMgr->is_playing("thunder") ) {
+                       if( !sampleGroup->is_playing("thunder") ) {
                                snd_active = false;
                                snd_playing = false;
                        }
                }
 
+       }
        if( ! lightning_enable_state )
                return;