head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.38; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.55; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.01; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.57; 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

# moon.cxx

    00001 // moon.hxx -- model earth's moon
    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: moon.cxx,v 1.13 2002/02/14 14:21:48 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 <simgear/constants.h>
    00037
    00038 #include "sphere.hxx"
    00039 #include "moon.hxx"
    00040
    00041
    00042 // Set up moon rendering call backs
    00043 static int sgMoonOrbPreDraw( ssgEntity *e ) {
    00044     /* cout << endl << "Moon orb pre draw" << endl << "----------------"
    00045          << endl << endl; */
    00046
    00047     ssgLeaf *f = (ssgLeaf *)e;
    00048     if ( f -> hasState () ) f->getState()->apply() ;
    00049
    00050     glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT );
    00051     // cout << "push error = " << glGetError() << endl;
    00052
    00053     glDisable( GL_DEPTH_TEST );
    00054     glDisable( GL_FOG );
    00055     glBlendFunc ( GL_SRC_ALPHA, GL_ONE ) ;
    00056
    00057     return true;
    00058 }
    00059
    00060
    00061 static int sgMoonOrbPostDraw( ssgEntity *e ) {
    00062     /* cout << endl << "Moon orb post draw" << endl << "----------------"
    00063          << endl << endl; */
    00064     // glEnable( GL_DEPTH_TEST );
    00065     // glEnable( GL_FOG );
    00066
    00067     // Some drivers don't properly reset glBendFunc with a
    00068     // glPopAttrib() so we reset it to the 'default' here.
    00069     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
    00070
    00071     glPopAttrib();
    00072     // cout << "pop error = " << glGetError() << endl;
    00073
    00074     /* test
    00075     glDisable( GL_LIGHTING );
    00076     glDisable( GL_CULL_FACE );
    00077     glEnable( GL_COLOR_MATERIAL );
    00078     glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
    00079     glEnable( GL_CULL_FACE );
    00080     glEnable( GL_LIGHTING ); */
    00081
    00082     return true;
    00083 }
    00084
    00085
    00086 #if 0
    00087 static int sgMoonHaloPreDraw( ssgEntity *e ) {
    00088     /* cout << endl << "Moon halo pre draw" << endl << "----------------"
    00089          << endl << endl; */
    00090
    00091     ssgLeaf *f = (ssgLeaf *)e;
    00092     if ( f -> hasState () ) f->getState()->apply() ;
    00093
    00094     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT | GL_COLOR_BUFFER_BIT);
    00095     // cout << "push error = " << glGetError() << endl;
    00096
    00097     glDisable( GL_DEPTH_TEST );
    00098     glDisable( GL_FOG );
    00099     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
    00100
    00101     return true;
    00102 }
    00103
    00104 static int sgMoonHaloPostDraw( ssgEntity *e ) {
    00105     /* cout << endl << "Moon halo post draw" << endl << "----------------"
    00106          << endl << endl; */
    00107     // glEnable( GL_DEPTH_TEST );
    00108     // glEnable( GL_FOG );
    00109     // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
    00110
    00111     glPopAttrib();
    00112     // cout << "pop error = " << glGetError() << endl;
    00113
    00114     return true;
    00115 }
    00116 #endif
    00117
    00118
    00119 // Constructor
    00120 SGMoon::SGMoon( void ) {
    00121 }
    00122
    00123
    00124 // Destructor
    00125 SGMoon::~SGMoon( void ) {
    00126 }
    00127
    00128
    00129 // build the moon object
    00130 ssgBranch * SGMoon::build( SGPath path, double moon_size ) {
    00131
    00132     // set up the orb state
    00133     path.append( "moon.rgba" );
    00134     orb_state = new ssgSimpleState();
    00135     orb_state->setTexture( (char *)path.c_str() );
    00136     orb_state->setShadeModel( GL_SMOOTH );
    00137     orb_state->enable( GL_LIGHTING );
    00138     orb_state->enable( GL_CULL_FACE );
    00139     orb_state->enable( GL_TEXTURE_2D );
    00140     orb_state->enable( GL_COLOR_MATERIAL );
    00141     orb_state->setColourMaterial( GL_DIFFUSE );
    00142     orb_state->setMaterial( GL_AMBIENT, 0, 0, 0, 1.0 );
    00143     orb_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
    00144     orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
    00145     orb_state->enable( GL_BLEND );
    00146     orb_state->enable( GL_ALPHA_TEST );
    00147     orb_state->setAlphaClamp( 0.01 );
    00148
    00149     cl = new ssgColourArray( 1 );
    00150     sgVec4 color;
    00151     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
    00152     cl->add( color );
    00153
    00154     ssgBranch *orb = ssgMakeSphere( orb_state, cl, moon_size, 15, 15,
    00155                                     sgMoonOrbPreDraw, sgMoonOrbPostDraw );
    00156
    00157     // force a repaint of the moon colors with arbitrary defaults
    00158     repaint( 0.0 );
    00159
    00160     // build the halo
    00161     // moon_texbuf = new GLubyte[64*64*3];
    00162     // moon_texid = makeHalo( moon_texbuf, 64 );
    00163     // my_glWritePPMFile("moonhalo.ppm", moon_texbuf, 64, 64, RGB);
    00164
    00165 #if 0
    00166     // set up the halo state
    00167     halo_state = new ssgSimpleState();
    00168     halo_state->setTexture( "halo.rgb" );
    00169     // halo_state->setTexture( moon_texid );
    00170     halo_state->enable( GL_TEXTURE_2D );
    00171     halo_state->disable( GL_LIGHTING );
    00172     halo_state->setShadeModel( GL_SMOOTH );
    00173     halo_state->disable( GL_CULL_FACE );
    00174
    00175     halo_state->disable( GL_COLOR_MATERIAL );
    00176     halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
    00177     halo_state->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
    00178     halo_state->setMaterial ( GL_EMISSION, 0, 0, 0, 1 ) ;
    00179     halo_state->setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ) ;
    00180     // halo_state -> setShininess ( 0 ) ;
    00181     halo_state->enable( GL_ALPHA_TEST );
    00182     halo_state->setAlphaClamp(0.01);
    00183     halo_state->enable ( GL_BLEND ) ;
    00184
    00185
    00186     // Build ssg structure
    00187     double size = moon_size * 10.0;
    00188     sgVec3 v3;
    00189     halo_vl = new ssgVertexArray;
    00190     sgSetVec3( v3, -size, 0.0, -size );
    00191     halo_vl->add( v3 );
    00192     sgSetVec3( v3, size, 0.0, -size );
    00193     halo_vl->add( v3 );
    00194     sgSetVec3( v3, -size, 0.0,  size );
    00195     halo_vl->add( v3 );
    00196     sgSetVec3( v3, size, 0.0,  size );
    00197     halo_vl->add( v3 );
    00198
    00199     sgVec2 v2;
    00200     halo_tl = new ssgTexCoordArray;
    00201     sgSetVec2( v2, 0.0f, 0.0f );
    00202     halo_tl->add( v2 );
    00203     sgSetVec2( v2, 1.0, 0.0 );
    00204     halo_tl->add( v2 );
    00205     sgSetVec2( v2, 0.0, 1.0 );
    00206     halo_tl->add( v2 );
    00207     sgSetVec2( v2, 1.0, 1.0 );
    00208     halo_tl->add( v2 );
    00209
    00210     ssgLeaf *halo =
    00211         new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
    00212     halo->setState( halo_state );
    00213 #endif
    00214
    00215     // build the ssg scene graph sub tree for the sky and connected
    00216     // into the provide scene graph branch
    00217     moon_transform = new ssgTransform;
    00218
    00219     // moon_transform->addKid( halo );
    00220     moon_transform->addKid( orb );
    00221
    00222     return moon_transform;
    00223 }
    00224
    00225
    00226 // repaint the moon colors based on current value of moon_angle in
    00227 // degrees relative to verticle
    00228 // 0 degrees = high noon
    00229 // 90 degrees = moon rise/set
    00230 // 180 degrees = darkest midnight
    00231 bool SGMoon::repaint( double moon_angle ) {
    00232     if ( moon_angle * SGD_RADIANS_TO_DEGREES < 100 ) {
    00233         // else moon is well below horizon (so no point in repainting it)
    00234
    00235         // x_10 = moon_angle^10
    00236         double x_10 = moon_angle * moon_angle * moon_angle * moon_angle
    00237             * moon_angle * moon_angle * moon_angle * moon_angle * moon_angle
    00238             * moon_angle;
    00239
    00240         float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
    00241         if (ambient < 0.3) { ambient = 0.3; }
    00242         if (ambient > 1.0) { ambient = 1.0; }
    00243
    00244         sgVec4 color;
    00245         sgSetVec4( color,
    00246                    (ambient * 6.0)  - 1.0, // minimum value = 0.8
    00247                    (ambient * 11.0) - 3.0, // minimum value = 0.3
    00248                    (ambient * 12.0) - 3.6, // minimum value = 0.0
    00249                    0.5 );
    00250
    00251         // temp test, forces the color to always be white
    00252         // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
    00253
    00254         if (color[0] > 1.0) color[0] = 1.0;
    00255         if (color[1] > 1.0) color[1] = 1.0;
    00256         if (color[2] > 1.0) color[2] = 1.0;
    00257
    00258         // cout << "color = " << color[0] << " " << color[1] << " "
    00259         //      << color[2] << endl;
    00260
    00261         float *ptr;
    00262         ptr = cl->get( 0 );
    00263         sgCopyVec4( ptr, color );
    00264     }
    00265
    00266     return true;
    00267 }
    00268
    00269
    00270 // reposition the moon at the specified right ascension and
    00271 // declination, offset by our current position (p) so that it appears
    00272 // fixed at a great distance from the viewer.  Also add in an optional
    00273 // rotation (i.e. for the current time of day.)
    00274 bool SGMoon::reposition( sgVec3 p, double angle,
    00275                          double rightAscension, double declination,
    00276                          double moon_dist )
    00277 {
    00278     sgMat4 T1, T2, GST, RA, DEC;
    00279     sgVec3 axis;
    00280     sgVec3 v;
    00281
    00282     sgMakeTransMat4( T1, p );
    00283
    00284     sgSetVec3( axis, 0.0, 0.0, -1.0 );
    00285     sgMakeRotMat4( GST, angle, axis );
    00286
    00287     // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
    00288     //             0.0, 0.0, 1.0);
    00289     sgSetVec3( axis, 0.0, 0.0, 1.0 );
    00290     sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
    00291
    00292     // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
    00293     sgSetVec3( axis, 1.0, 0.0, 0.0 );
    00294     sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis );
    00295
    00296     // xglTranslatef(0,moon_dist);
    00297     sgSetVec3( v, 0.0, moon_dist, 0.0 );
    00298     sgMakeTransMat4( T2, v );
    00299
    00300     sgMat4 TRANSFORM;
    00301     sgCopyMat4( TRANSFORM, T1 );
    00302     sgPreMultMat4( TRANSFORM, GST );
    00303     sgPreMultMat4( TRANSFORM, RA );
    00304     sgPreMultMat4( TRANSFORM, DEC );
    00305     sgPreMultMat4( TRANSFORM, T2 );
    00306
    00307     sgCoord skypos;
    00308     sgSetCoord( &skypos, TRANSFORM );
    00309
    00310     moon_transform->setTransform( &skypos );
    00311
    00312     return true;
    00313 }
    00314

@ 1.3 log @Oops, try again. @ text @d323 1 a323 1