head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.37; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.52; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.00; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.55; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# cloud.cxx

    00001 // cloud.cxx -- model a single cloud layer
    00002 //
    00003 // Written by Curtis Olson, started June 2000.
    00004 //
    00005 // Copyright (C) 2000  Curtis L. Olson  - curt@@flightgear.org
    00006 //
    00007 // This program is distributed in the hope that it will be useful, but
    00008 // WITHOUT ANY WARRANTY; without even the implied warranty of
    00009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00010 // General Public License for more details.
    00011 //
    00012 // You should have received a copy of the GNU General Public License
    00013 // along with this program; if not, write to the Free Software
    00014 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00015 //
    00016 // $Id: cloud.cxx,v 1.19 2002/07/18 20:23:30 david Exp $
    00017
    00018
    00019 #include <simgear/compiler.h>
    00020
    00021 #include <stdio.h>
    00022 #include STL_IOSTREAM
    00023
    00024 #include <plib/sg.h>
    00025 #include <plib/ssg.h>
    00026
    00027 #include <simgear/math/point3d.hxx>
    00028 #include <simgear/math/polar3d.hxx>
    00029 #include <simgear/math/sg_random.h>
    00030 #include <simgear/debug/logstream.hxx>
    00031 #include <simgear/misc/sg_path.hxx>
    00032
    00033 #include "cloud.hxx"
    00034
    00035 ssgSimpleState *
    00036 SGCloudLayer::layer_states[SGCloudLayer::SG_MAX_CLOUD_TYPES];
    00037
    00038
    00039 // Constructor
    00040 SGCloudLayer::SGCloudLayer( const string &tex_path )
    00041   : layer_root(new ssgRoot),
    00042     layer_transform(new ssgTransform),
    00043     layer(0),
    00044     texture_path(tex_path),
    00045     layer_span(0),
    00046     layer_asl(0),
    00047     layer_thickness(0),
    00048     layer_transition(0),
    00049     layer_type(SG_CLOUD_CLEAR)
    00050 {
    00051     layer_root->addKid(layer_transform);
    00052     rebuild();
    00053 }
    00054
    00055 // Destructor
    00056 SGCloudLayer::~SGCloudLayer()
    00057 {
    00058     delete layer_root;          // deletes layer_transform and layer as well
    00059 }
    00060
    00061 float
    00062 SGCloudLayer::getSpan_m () const
    00063 {
    00064     return layer_span;
    00065 }
    00066
    00067 void
    00068 SGCloudLayer::setSpan_m (float span_m)
    00069 {
    00070     if (span_m != layer_span) {
    00071         layer_span = span_m;
    00072         rebuild();
    00073     }
    00074 }
    00075
    00076 float
    00077 SGCloudLayer::getElevation_m () const
    00078 {
    00079     return layer_asl;
    00080 }
    00081
    00082 void
    00083 SGCloudLayer::setElevation_m (float elevation_m)
    00084 {
    00085     layer_asl = elevation_m;
    00086 }
    00087
    00088 float
    00089 SGCloudLayer::getThickness_m () const
    00090 {
    00091     return layer_thickness;
    00092 }
    00093
    00094 void
    00095 SGCloudLayer::setThickness_m (float thickness_m)
    00096 {
    00097     layer_thickness = thickness_m;
    00098 }
    00099
    00100 float
    00101 SGCloudLayer::getTransition_m () const
    00102 {
    00103     return layer_transition;
    00104 }
    00105
    00106 void
    00107 SGCloudLayer::setTransition_m (float transition_m)
    00108 {
    00109     layer_transition = transition_m;
    00110 }
    00111
    00112 SGCloudLayer::Type
    00113 SGCloudLayer::getType () const
    00114 {
    00115     return layer_type;
    00116 }
    00117
    00118 void
    00119 SGCloudLayer::setType (Type type)
    00120 {
    00121     if (type != layer_type) {
    00122         layer_type = type;
    00123         rebuild();
    00124     }
    00125 }
    00126
    00127
    00128 // build the cloud object
    00129 void
    00130 SGCloudLayer::rebuild()
    00131 {
    00132                                 // Initialize states and sizes if necessary.
    00133     if (layer_states[0] == 0) {
    00134       SGPath cloud_path;
    00135
    00136       cloud_path.set(texture_path.str());
    00137       cloud_path.append("overcast.rgb");
    00138       layer_states[SG_CLOUD_OVERCAST] = SGCloudMakeState(cloud_path.str());
    00139
    00140       cloud_path.set(texture_path.str());
    00141       cloud_path.append("mostlycloudy.rgba");
    00142       layer_states[SG_CLOUD_MOSTLY_CLOUDY] =
    00143         SGCloudMakeState(cloud_path.str());
    00144
    00145       cloud_path.set(texture_path.str());
    00146       cloud_path.append("mostlysunny.rgba");
    00147       layer_states[SG_CLOUD_MOSTLY_SUNNY] = SGCloudMakeState(cloud_path.str());
    00148
    00149       cloud_path.set(texture_path.str());
    00150       cloud_path.append("cirrus.rgba");
    00151       layer_states[SG_CLOUD_CIRRUS] = SGCloudMakeState(cloud_path.str());
    00152
    00153       layer_states[SG_CLOUD_CLEAR] = 0;
    00154     }
    00155
    00156     scale = 4000.0;
    00157
    00158     last_lon = last_lat = -999.0f;
    00159
    00160     cl = new ssgColourArray( 4 );
    00161     vl = new ssgVertexArray( 4 );
    00162     tl = new ssgTexCoordArray( 4 );
    00163
    00164     // build the cloud layer
    00165     sgVec4 color;
    00166     sgVec3 vertex;
    00167     sgVec2 tc;
    00168     sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
    00169
    00170     sgSetVec3( vertex, -layer_span, -layer_span, 0.0f );
    00171     sgVec2 base;
    00172     sgSetVec2( base, sg_random(), sg_random() );
    00173     sgSetVec2( tc, base[0], base[1] );
    00174     cl->add( color );
    00175     vl->add( vertex );
    00176     tl->add( tc );
    00177
    00178     sgSetVec3( vertex, layer_span, -layer_span, 0.0f );
    00179     sgSetVec2( tc, base[0] + layer_span / scale, base[1] );
    00180     cl->add( color );
    00181     vl->add( vertex );
    00182     tl->add( tc );
    00183
    00184     sgSetVec3( vertex, -layer_span, layer_span, 0.0f );
    00185     sgSetVec2( tc, base[0], base[1] + layer_span / scale );
    00186     cl->add( color );
    00187     vl->add( vertex );
    00188     tl->add( tc );
    00189
    00190     sgSetVec3( vertex, layer_span, layer_span, 0.0f );
    00191     sgSetVec2( tc, base[0] + layer_span / scale, base[1] + layer_span / scale );
    00192     cl->add( color );
    00193     vl->add( vertex );
    00194     tl->add( tc );
    00195
    00196     if (layer != 0)
    00197       layer_transform->removeKid(layer); // automatic delete
    00198     layer = new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
    00199     if (layer_states[layer_type] != 0)
    00200       layer->setState( layer_states[layer_type] );
    00201
    00202     // force a repaint of the moon colors with arbitrary defaults
    00203     repaint( color );
    00204
    00205     // moon_transform->addKid( halo );
    00206     layer_transform->addKid( layer );
    00207 }
    00208
    00209
    00210 // repaint the cloud layer colors
    00211 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
    00212     float *color;
    00213
    00214     for ( int i = 0; i < 4; ++i ) {
    00215         color = cl->get( i );
    00216         sgCopyVec4( color, fog_color );
    00217     }
    00218
    00219     return true;
    00220 }
    00221
    00222
    00223 // reposition the cloud layer at the specified origin and orientation
    00224 // lon specifies a rotation about the Z axis
    00225 // lat specifies a rotation about the new Y axis
    00226 // spin specifies a rotation about the new Z axis (and orients the
    00227 // sunrise/set effects
    00228 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
    00229                                double alt )
    00230 {
    00231     sgMat4 T1, LON, LAT;
    00232     sgVec3 axis;
    00233
    00234     // combine p and asl (meters) to get translation offset
    00235     sgVec3 asl_offset;
    00236     sgCopyVec3( asl_offset, up );
    00237     sgNormalizeVec3( asl_offset );
    00238     if ( alt <= layer_asl ) {
    00239         sgScaleVec3( asl_offset, layer_asl );
    00240     } else {
    00241         sgScaleVec3( asl_offset, layer_asl + layer_thickness );
    00242     }
    00243     // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
    00244     //      << "," << asl_offset[2] << endl;
    00245     sgAddVec3( asl_offset, p );
    00246     // cout << "  asl_offset = " << asl_offset[0] << "," << asl_offset[1]
    00247     //      << "," << asl_offset[2] << endl;
    00248
    00249     // Translate to zero elevation
    00250     // Point3D zero_elev = current_view.get_cur_zero_elev();
    00251     // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
    00252     sgMakeTransMat4( T1, asl_offset );
    00253
    00254     // printf("  Translated to %.2f %.2f %.2f\n",
    00255     //        zero_elev.x, zero_elev.y, zero_elev.z );
    00256
    00257     // Rotate to proper orientation
    00258     // printf("  lon = %.2f  lat = %.2f\n",
    00259     //        lon * SGD_RADIANS_TO_DEGREES,
    00260     //        lat * SGD_RADIANS_TO_DEGREES);
    00261     // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
    00262     sgSetVec3( axis, 0.0, 0.0, 1.0 );
    00263     sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
    00264
    00265     // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
    00266     //             0.0, 1.0, 0.0 );
    00267     sgSetVec3( axis, 0.0, 1.0, 0.0 );
    00268     sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
    00269
    00270     sgMat4 TRANSFORM;
    00271
    00272     sgCopyMat4( TRANSFORM, T1 );
    00273     sgPreMultMat4( TRANSFORM, LON );
    00274     sgPreMultMat4( TRANSFORM, LAT );
    00275
    00276     sgCoord layerpos;
    00277     sgSetCoord( &layerpos, TRANSFORM );
    00278
    00279     layer_transform->setTransform( &layerpos );
    00280
    00281     // now calculate update texture coordinates
    00282     if ( last_lon < -900 ) {
    00283         last_lon = lon;
    00284         last_lat = lat;
    00285     }
    00286
    00287     if ( lon != last_lon || lat != last_lat ) {
    00288         Point3D start( last_lon, last_lat, 0.0 );
    00289         Point3D dest( lon, lat, 0.0 );
    00290         double course, dist;
    00291         calc_gc_course_dist( dest, start, &course, &dist );
    00292         // cout << "course = " << course << ", dist = " << dist << endl;
    00293
    00294         double xoff = cos( course ) * dist / (2 * scale);
    00295         double yoff = sin( course ) * dist / (2 * scale);
    00296
    00297         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
    00298
    00299         float *base, *tc;
    00300         base = tl->get( 0 );
    00301
    00302         base[0] += xoff;
    00303
    00304         // the while loops can lead to *long* pauses if base[0] comes
    00305         // with a bogus value.
    00306         // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
    00307         // while ( base[0] < 0.0 ) { base[0] += 1.0; }
    00308         if ( base[0] > -10.0 && base[0] < 10.0 ) {
    00309             base[0] -= (int)base[0];
    00310         } else {
    00311             base[0] = 0.0;
    00312             SG_LOG(SG_ASTRO, SG_DEBUG,
    00313                    "Error: base = " << base[0] << "," << base[1]);
    00314         }
    00315
    00316         base[1] += yoff;
    00317         // the while loops can lead to *long* pauses if base[0] comes
    00318         // with a bogus value.
    00319         // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
    00320         // while ( base[1] < 0.0 ) { base[1] += 1.0; }
    00321         if ( base[1] > -10.0 && base[1] < 10.0 ) {
    00322             base[1] -= (int)base[1];
    00323         } else {
    00324             base[1] = 0.0;
    00325             SG_LOG(SG_ASTRO, SG_ALERT,
    00326                    "Error: base = " << base[0] << "," << base[1]);
    00327         }
    00328
    00329         // cout << "base = " << base[0] << "," << base[1] << endl;
    00330
    00331         tc = tl->get( 1 );
    00332         sgSetVec2( tc, base[0] + layer_span / scale, base[1] );
    00333
    00334         tc = tl->get( 2 );
    00335         sgSetVec2( tc, base[0], base[1] + layer_span / scale );
    00336
    00337         tc = tl->get( 3 );
    00338         sgSetVec2( tc, base[0] + layer_span / scale, base[1] + layer_span / scale );
    00339
    00340         last_lon = lon;
    00341         last_lat = lat;
    00342     }
    00343
    00344     return true;
    00345 }
    00346
    00347
    00348 void SGCloudLayer::draw() {
    00349     if (layer_type != SG_CLOUD_CLEAR)
    00350       ssgCullAndDraw( layer_root );
    00351 }
    00352
    00353
    00354 // make an ssgSimpleState for a cloud layer given the named texture
    00355 ssgSimpleState *SGCloudMakeState( const string &path ) {
    00356     ssgSimpleState *state = new ssgSimpleState();
    00357
    00358     state->setTexture( (char *)path.c_str() );
    00359     state->setShadeModel( GL_SMOOTH );
    00360     state->disable( GL_LIGHTING );
    00361     state->disable( GL_CULL_FACE );
    00362     state->enable( GL_TEXTURE_2D );
    00363     state->enable( GL_COLOR_MATERIAL );
    00364     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
    00365     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
    00366     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
    00367     state->enable( GL_BLEND );
    00368     state->enable( GL_ALPHA_TEST );
    00369     state->setAlphaClamp( 0.01 );
    00370
    00371     return state;
    00372 }

@ 1.3 log @Oops, try again. @ text @d381 1 a381 1