head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.41; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.58; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.04; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.01; 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

# stars.cxx

    00001 // stars.cxx -- model the stars
    00002 //
    00003 // Written by Durk Talsma. Originally started October 1997, for distribution
    00004 // with the FlightGear project. Version 2 was written in August and
    00005 // September 1998. This code is based upon algorithms and data kindly
    00006 // provided by Mr. Paul Schlyter. (pausch@@saaf.se).
    00007 //
    00008 // Separated out rendering pieces and converted to ssg by Curt Olson,
    00009 // March 2000
    00010 // This library is free software; you can redistribute it and/or
    00011 // modify it under the terms of the GNU Library General Public
    00012 // License as published by the Free Software Foundation; either
    00013 // version 2 of the License, or (at your option) any later version.
    00014 //
    00015 // This library is distributed in the hope that it will be useful,
    00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00018 // Library General Public License for more details.
    00019 //
    00020 // You should have received a copy of the GNU Library General Public
    00021 // License along with this library; if not, write to the
    00022 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00023 // Boston, MA  02111-1307, USA.
    00024 //
    00025 // $Id: stars.cxx,v 1.18 2001/06/02 04:21:04 curt Exp $
    00026
    00027
    00028 #include <simgear/compiler.h>
    00029
    00030 #include <stdio.h>
    00031 #include STL_IOSTREAM
    00032
    00033 #include <plib/sg.h>
    00034 #include <plib/ssg.h>
    00035
    00036 #include "stars.hxx"
    00037
    00038 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
    00039 SG_USING_STD(cout);
    00040 SG_USING_STD(endl);
    00041 #endif
    00042
    00043
    00044 // Set up star rendering call backs
    00045 static int sgStarPreDraw( ssgEntity *e ) {
    00046     /* cout << endl << "Star pre draw" << endl << "----------------"
    00047        << endl << endl; */
    00048
    00049     ssgLeaf *f = (ssgLeaf *)e;
    00050     if ( f -> hasState () ) f->getState()->apply() ;
    00051
    00052     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
    00053
    00054     glDisable( GL_DEPTH_TEST );
    00055     glDisable( GL_FOG );
    00056     // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
    00057
    00058     return true;
    00059 }
    00060
    00061 static int sgStarPostDraw( ssgEntity *e ) {
    00062     /* cout << endl << "Star post draw" << endl << "----------------"
    00063        << endl << endl; */
    00064
    00065     glPopAttrib();
    00066
    00067     // glEnable( GL_DEPTH_TEST );
    00068     // glEnable( GL_FOG );
    00069
    00070     return true;
    00071 }
    00072
    00073
    00074 // Constructor
    00075 SGStars::SGStars( void ) :
    00076 old_phase(-1)
    00077 {
    00078 }
    00079
    00080
    00081 // Destructor
    00082 SGStars::~SGStars( void ) {
    00083 }
    00084
    00085
    00086 // initialize the stars object and connect it into our scene graph root
    00087 ssgBranch * SGStars::build( int num, sgdVec3 *star_data, double star_dist ) {
    00088     sgVec4 color;
    00089
    00090     if ( star_data == NULL ) {
    00091         cout << "WARNING: null star data passed to SGStars::build()" << endl;
    00092     }
    00093
    00094     // set up the orb state
    00095     state = new ssgSimpleState();
    00096     state->disable( GL_LIGHTING );
    00097     state->disable( GL_CULL_FACE );
    00098     state->disable( GL_TEXTURE_2D );
    00099     state->enable( GL_COLOR_MATERIAL );
    00100     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
    00101     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
    00102     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
    00103     state->enable( GL_BLEND );
    00104     state->disable( GL_ALPHA_TEST );
    00105
    00106     vl = new ssgVertexArray( num );
    00107     cl = new ssgColourArray( num );
    00108     // cl = new ssgColourArray( 1 );
    00109     // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
    00110     // cl->add( color );
    00111
    00112     // Build ssg structure
    00113     sgVec3 p;
    00114     for ( int i = 0; i < num; ++i ) {
    00115         // position seeded to arbitrary values
    00116         sgSetVec3( p,
    00117                    star_dist * cos( star_data[i][0] )
    00118                    * cos( star_data[i][1] ),
    00119                    star_dist * sin( star_data[i][0] )
    00120                    * cos( star_data[i][1] ),
    00121                    star_dist * sin( star_data[i][1] )
    00122                    );
    00123         vl->add( p );
    00124
    00125         // color (magnitude)
    00126         sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
    00127         cl->add( color );
    00128     }
    00129
    00130     ssgLeaf *stars_obj =
    00131         new ssgVtxTable ( GL_POINTS, vl, NULL, NULL, cl );
    00132     stars_obj->setState( state );
    00133     stars_obj->setCallback( SSG_CALLBACK_PREDRAW, sgStarPreDraw );
    00134     stars_obj->setCallback( SSG_CALLBACK_POSTDRAW, sgStarPostDraw );
    00135
    00136     // build the ssg scene graph sub tree for the sky and connected
    00137     // into the provide scene graph branch
    00138     stars_transform = new ssgTransform;
    00139
    00140     stars_transform->addKid( stars_obj );
    00141
    00142     cout << "stars = " << stars_transform << endl;
    00143
    00144     return stars_transform;
    00145 }
    00146
    00147
    00148 // repaint the sun colors based on current value of sun_angle in
    00149 // degrees relative to verticle
    00150 // 0 degrees = high noon
    00151 // 90 degrees = sun rise/set
    00152 // 180 degrees = darkest midnight
    00153 bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
    00154     // cout << "repainting stars" << endl;
    00155     // double min = 100;
    00156     // double max = -100;
    00157     double mag, nmag, alpha, factor, cutoff;
    00158     float *color;
    00159
    00160     int phase;
    00161
    00162     // determine which star structure to draw
    00163     if ( sun_angle > (0.5 * SGD_PI + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
    00164         // deep night
    00165         factor = 1.0;
    00166         cutoff = 4.5;
    00167         phase = 0;
    00168     } else if ( sun_angle > (0.5 * SGD_PI + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
    00169         factor = 1.0;
    00170         cutoff = 3.8;
    00171         phase = 1;
    00172     } else if ( sun_angle > (0.5 * SGD_PI + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
    00173         factor = 0.95;
    00174         cutoff = 3.1;
    00175         phase = 2;
    00176     } else if ( sun_angle > (0.5 * SGD_PI + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
    00177         factor = 0.9;
    00178         cutoff = 2.4;
    00179         phase = 3;
    00180     } else if ( sun_angle > (0.5 * SGD_PI + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
    00181         factor = 0.85;
    00182         cutoff = 1.8;
    00183         phase = 4;
    00184     } else if ( sun_angle > (0.5 * SGD_PI + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
    00185         factor = 0.8;
    00186         cutoff = 1.2;
    00187         phase = 5;
    00188     } else if ( sun_angle > (0.5 * SGD_PI + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
    00189         factor = 0.75;
    00190         cutoff = 0.6;
    00191         phase = 6;
    00192     } else {
    00193         // early dusk or late dawn
    00194         factor = 0.7;
    00195         cutoff = 0.0;
    00196         phase = 7;
    00197     }
    00198
    00199     if( phase != old_phase ) {
    00200         // cout << "  phase change, repainting stars, num = " << num << endl;
    00201         old_phase = phase;
    00202         for ( int i = 0; i < num; ++i ) {
    00203             // if ( star_data[i][2] < min ) { min = star_data[i][2]; }
    00204             // if ( star_data[i][2] > max ) { max = star_data[i][2]; }
    00205
    00206             // magnitude ranges from -1 (bright) to 4 (dim).  The
    00207             // range of star and planet magnitudes can actually go
    00208             // outside of this, but for our purpose, if it is brighter
    00209             // that -1, we'll color it full white/alpha anyway and 4
    00210             // is a convenient cutoff point which keeps the number of
    00211             // stars drawn at about 500.
    00212
    00213             // color (magnitude)
    00214             mag = star_data[i][2];
    00215             if ( mag < cutoff ) {
    00216                 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
    00217                 // alpha = nmag * 0.7 + 0.3; // translate to a 0.3 ... 1.0 scale
    00218                 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
    00219                 alpha *= factor;          // dim when the sun is brighter
    00220             } else {
    00221                 alpha = 0.0;
    00222             }
    00223
    00224             if (alpha > 1.0) { alpha = 1.0; }
    00225             if (alpha < 0.0) { alpha = 0.0; }
    00226
    00227             color = cl->get( i );
    00228             sgSetVec4( color, 1.0, 1.0, 1.0, alpha );
    00229             // cout << "alpha[" << i << "] = " << alpha << endl;
    00230         }
    00231     } else {
    00232         // cout << "  no phase change, skipping" << endl;
    00233     }
    00234
    00235     // cout << "min = " << min << " max = " << max << " count = " << num
    00236     //      << endl;
    00237
    00238     return true;
    00239 }
    00240
    00241
    00242 // reposition the stars for the specified time (GST rotation),
    00243 // offset by our current position (p) so that it appears fixed at a
    00244 // great distance from the viewer.
    00245 bool SGStars::reposition( sgVec3 p, double angle )
    00246 {
    00247     sgMat4 T1, GST;
    00248     sgVec3 axis;
    00249
    00250     sgMakeTransMat4( T1, p );
    00251
    00252     sgSetVec3( axis, 0.0, 0.0, -1.0 );
    00253     sgMakeRotMat4( GST, angle, axis );
    00254
    00255     sgMat4 TRANSFORM;
    00256     sgCopyMat4( TRANSFORM, T1 );
    00257     sgPreMultMat4( TRANSFORM, GST );
    00258
    00259     sgCoord skypos;
    00260     sgSetCoord( &skypos, TRANSFORM );
    00261
    00262     stars_transform->setTransform( &skypos );
    00263
    00264     return true;
    00265 }

@ 1.3 log @Oops, try again. @ text @d274 1 a274 1