head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.39; 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.02; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.58; 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

# oursun.cxx

    00001 // oursun.hxx -- model earth's sun
    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: oursun.cxx,v 1.14 2002/09/02 22:18:31 curt Exp $
    00026
    00027
    00028 #ifdef HAVE_CONFIG_H
    00029 #  include <simgear_config.h>
    00030 #endif
    00031
    00032 #include <simgear/compiler.h>
    00033
    00034 #include <stdio.h>
    00035 #include STL_IOSTREAM
    00036
    00037 #include <plib/sg.h>
    00038 #include <plib/ssg.h>
    00039
    00040 // define the following to enable a cheesy lens flare effect for the sun
    00041 // #define FG_TEST_CHEESY_LENS_FLARE
    00042
    00043 #ifdef FG_TEST_CHEESY_LENS_FLARE
    00044 #  include <plib/ssgaLensFlare.h>
    00045 #endif
    00046
    00047 #include "sphere.hxx"
    00048 #include "oursun.hxx"
    00049
    00050
    00051 // Set up sun rendering call backs
    00052 static int sgSunOrbPreDraw( ssgEntity *e ) {
    00053     /* cout << endl << "Sun orb pre draw" << endl << "----------------"
    00054          << endl << endl; */
    00055
    00056     ssgLeaf *f = (ssgLeaf *)e;
    00057     if ( f -> hasState () ) f->getState()->apply() ;
    00058
    00059     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
    00060     // cout << "push error = " << glGetError() << endl;
    00061
    00062     glDisable( GL_DEPTH_TEST );
    00063     glDisable( GL_FOG );
    00064
    00065     return true;
    00066 }
    00067
    00068 static int sgSunOrbPostDraw( ssgEntity *e ) {
    00069     /* cout << endl << "Sun orb post draw" << endl << "----------------"
    00070          << endl << endl; */
    00071
    00072     glPopAttrib();
    00073     // cout << "pop error = " << glGetError() << endl;
    00074
    00075     // glEnable( GL_DEPTH_TEST );
    00076     // glEnable( GL_FOG );
    00077
    00078     return true;
    00079 }
    00080
    00081 static int sgSunHaloPreDraw( ssgEntity *e ) {
    00082     /* cout << endl << "Sun halo pre draw" << endl << "----------------"
    00083          << endl << endl; */
    00084
    00085     ssgLeaf *f = (ssgLeaf *)e;
    00086     if ( f -> hasState () ) f->getState()->apply() ;
    00087
    00088     glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
    00089     // cout << "push error = " << glGetError() << endl;
    00090
    00091     glDisable( GL_DEPTH_TEST );
    00092     glDisable( GL_FOG );
    00093     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
    00094
    00095     return true;
    00096 }
    00097
    00098 static int sgSunHaloPostDraw( ssgEntity *e ) {
    00099     /* cout << endl << "Sun halo post draw" << endl << "----------------"
    00100          << endl << endl; */
    00101
    00102     glPopAttrib();
    00103     // cout << "pop error = " << glGetError() << endl;
    00104
    00105     // glEnable( GL_DEPTH_TEST );
    00106     // glEnable( GL_FOG );
    00107
    00108     return true;
    00109 }
    00110
    00111
    00112 // Constructor
    00113 SGSun::SGSun( void ) {
    00114 }
    00115
    00116
    00117 // Destructor
    00118 SGSun::~SGSun( void ) {
    00119 }
    00120
    00121
    00122 #if 0
    00123 // this might be nice to keep, just as an example of how to generate a
    00124 // texture on the fly ...
    00125 static GLuint makeHalo( GLubyte *sun_texbuf, int width ) {
    00126     int texSize;
    00127     GLuint texid;
    00128     GLubyte *p;
    00129     int i,j;
    00130     double radius;
    00131
    00132     // create a texture id
    00133 #ifdef GL_VERSION_1_1
    00134     glGenTextures(1, &texid);
    00135     glBindTexture(GL_TEXTURE_2D, texid);
    00136 #elif GL_EXT_texture_object
    00137     glGenTexturesEXT(1, &texid);
    00138     glBindTextureEXT(GL_TEXTURE_2D, texid);
    00139 #else
    00140 #   error port me
    00141 #endif
    00142
    00143     glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
    00144     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    00145     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    00146     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
    00147
    00148     // create the actual texture contents
    00149     texSize = width * width;
    00150
    00151     if ( !sun_texbuf ) {
    00152         cout << "ouch ..." << endl;
    00153         exit(-1);  // Ugly!
    00154     }
    00155
    00156     p = sun_texbuf;
    00157
    00158     radius = (double)(width / 2);
    00159
    00160     GLubyte value;
    00161     double x, y, d;
    00162     for ( i = 0; i < width; i++ ) {
    00163         for ( j = 0; j < width; j++ ) {
    00164             x = fabs((double)(i - (width / 2)));
    00165             y = fabs((double)(j - (width / 2)));
    00166             d = sqrt((x * x) + (y * y));
    00167             if (d < radius) {
    00168                 // t is 1.0 at center, 0.0 at edge
    00169                 double t = 1.0 - (d / radius);
    00170
    00171                 // inverse square looks nice
    00172                 value = (int)((double) 0xff * (t*t));
    00173             } else {
    00174                 value = 0x00;
    00175             }
    00176             *p = value;
    00177             *(p+1) = value;
    00178             *(p+2) = value;
    00179             // *(p+3) = value;
    00180
    00181             p += 3;
    00182         }
    00183     }
    00184
    00185     /* glTexImage2D( GL_TEXTURE_2D,
    00186                   0,
    00187                   GL_RGBA,
    00188                   width, width,
    00189                   0,
    00190                   GL_RGBA, GL_UNSIGNED_BYTE,
    00191                   sun_texbuf ); */
    00192
    00193     return texid;
    00194 }
    00195
    00196
    00197 #define RGB  3                  // 3 bytes of color info per pixel
    00198 #define RGBA 4                  // 4 bytes of color+alpha info
    00199 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
    00200 {
    00201     int i, j, k, q;
    00202     unsigned char *ibuffer;
    00203     FILE *fp;
    00204     int pixelSize = mode==GL_RGBA?4:3;
    00205
    00206     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
    00207
    00208     fp = fopen(filename, "wb");
    00209     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
    00210             win_width, win_height, UCHAR_MAX);
    00211     q = 0;
    00212     for (i = 0; i < win_height; i++) {
    00213         for (j = 0; j < win_width; j++) {
    00214             for (k = 0; k < RGB; k++) {
    00215                 ibuffer[q++] = (unsigned char)
    00216                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
    00217             }
    00218         }
    00219     }
    00220
    00221     // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
    00222
    00223     fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
    00224     fclose(fp);
    00225     free(ibuffer);
    00226
    00227     printf("wrote file (%d x %d pixels, %d bytes)\n",
    00228            win_width, win_height, RGB*win_width*win_height);
    00229 }
    00230 #endif
    00231
    00232
    00233 // initialize the sun object and connect it into our scene graph root
    00234 ssgBranch * SGSun::build( SGPath path, double sun_size ) {
    00235
    00236     // set up the orb state
    00237     orb_state = new ssgSimpleState();
    00238     orb_state->setShadeModel( GL_SMOOTH );
    00239     orb_state->disable( GL_LIGHTING );
    00240     // orb_state->enable( GL_LIGHTING );
    00241     orb_state->disable( GL_CULL_FACE );
    00242     orb_state->disable( GL_TEXTURE_2D );
    00243     orb_state->enable( GL_COLOR_MATERIAL );
    00244     orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
    00245     orb_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
    00246     orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
    00247     orb_state->disable( GL_BLEND );
    00248     orb_state->disable( GL_ALPHA_TEST );
    00249
    00250     cl = new ssgColourArray( 1 );
    00251     sgVec4 color;
    00252     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
    00253     cl->add( color );
    00254
    00255     ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10,
    00256                                     sgSunOrbPreDraw, sgSunOrbPostDraw );
    00257
    00258     // force a repaint of the sun colors with arbitrary defaults
    00259     repaint( 0.0 );
    00260
    00261     // build the halo
    00262     // sun_texbuf = new GLubyte[64*64*3];
    00263     // sun_texid = makeHalo( sun_texbuf, 64 );
    00264     // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
    00265
    00266     // set up the halo state
    00267     path.append( "halo.rgba" );
    00268     halo_state = new ssgSimpleState();
    00269     halo_state->setTexture( (char *)path.c_str() );
    00270     halo_state->enable( GL_TEXTURE_2D );
    00271     halo_state->disable( GL_LIGHTING );
    00272     // halo_state->enable( GL_LIGHTING );
    00273     halo_state->setShadeModel( GL_SMOOTH );
    00274     halo_state->disable( GL_CULL_FACE );
    00275     halo_state->enable( GL_COLOR_MATERIAL );
    00276     halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
    00277     halo_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
    00278     halo_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
    00279     halo_state->enable( GL_ALPHA_TEST );
    00280     halo_state->setAlphaClamp(0.01);
    00281     halo_state->enable ( GL_BLEND ) ;
    00282
    00283     // Build ssg structure
    00284     double size = sun_size * 10.0;
    00285     sgVec3 v3;
    00286     halo_vl = new ssgVertexArray;
    00287     sgSetVec3( v3, -size, 0.0, -size );
    00288     halo_vl->add( v3 );
    00289     sgSetVec3( v3, size, 0.0, -size );
    00290     halo_vl->add( v3 );
    00291     sgSetVec3( v3, -size, 0.0,  size );
    00292     halo_vl->add( v3 );
    00293     sgSetVec3( v3, size, 0.0,  size );
    00294     halo_vl->add( v3 );
    00295
    00296     sgVec2 v2;
    00297     halo_tl = new ssgTexCoordArray;
    00298     sgSetVec2( v2, 0.0f, 0.0f );
    00299     halo_tl->add( v2 );
    00300     sgSetVec2( v2, 1.0, 0.0 );
    00301     halo_tl->add( v2 );
    00302     sgSetVec2( v2, 0.0, 1.0 );
    00303     halo_tl->add( v2 );
    00304     sgSetVec2( v2, 1.0, 1.0 );
    00305     halo_tl->add( v2 );
    00306
    00307     ssgLeaf *halo =
    00308         new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
    00309     halo->setState( halo_state );
    00310
    00311     // build the ssg scene graph sub tree for the sky and connected
    00312     // into the provide scene graph branch
    00313     sun_transform = new ssgTransform;
    00314
    00315     halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
    00316     halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
    00317     sun_transform->addKid( halo );
    00318     sun_transform->addKid( orb );
    00319
    00320 #ifdef FG_TEST_CHEESY_LENS_FLARE
    00321     // cheesy lens flair
    00322     sun_transform->addKid( new ssgaLensFlare );
    00323 #endif
    00324
    00325     return sun_transform;
    00326 }
    00327
    00328
    00329 // repaint the sun colors based on current value of sun_angle in
    00330 // degrees relative to verticle
    00331 // 0 degrees = high noon
    00332 // 90 degrees = sun rise/set
    00333 // 180 degrees = darkest midnight
    00334 bool SGSun::repaint( double sun_angle ) {
    00335     if ( sun_angle * SGD_RADIANS_TO_DEGREES < 100 ) {
    00336         // else sun is well below horizon (so no point in repainting it)
    00337
    00338         // x_10 = sun_angle^10
    00339         double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
    00340             * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
    00341
    00342         float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
    00343         if (ambient < 0.3) { ambient = 0.3; }
    00344         if (ambient > 1.0) { ambient = 1.0; }
    00345
    00346         sgVec4 color;
    00347         sgSetVec4( color,
    00348                    (ambient * 6.0)  - 1.0, // minimum value = 0.8
    00349                    (ambient * 11.0) - 3.0, // minimum value = 0.3
    00350                    (ambient * 12.0) - 3.6, // minimum value = 0.0
    00351                    1.0 );
    00352
    00353         // temp test, forces the color to always be white
    00354         // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
    00355
    00356         if (color[0] > 1.0) color[0] = 1.0;
    00357         if (color[1] > 1.0) color[1] = 1.0;
    00358         if (color[2] > 1.0) color[2] = 1.0;
    00359
    00360         // cout << "color = " << color[0] << " " << color[1] << " "
    00361         //      << color[2] << endl;
    00362
    00363         float *ptr;
    00364         ptr = cl->get( 0 );
    00365         sgCopyVec4( ptr, color );
    00366     }
    00367
    00368     return true;
    00369 }
    00370
    00371
    00372 // reposition the sun at the specified right ascension and
    00373 // declination, offset by our current position (p) so that it appears
    00374 // fixed at a great distance from the viewer.  Also add in an optional
    00375 // rotation (i.e. for the current time of day.)
    00376 bool SGSun::reposition( sgVec3 p, double angle,
    00377                         double rightAscension, double declination,
    00378                         double sun_dist )
    00379 {
    00380     sgMat4 T1, T2, GST, RA, DEC;
    00381     sgVec3 axis;
    00382     sgVec3 v;
    00383
    00384     sgMakeTransMat4( T1, p );
    00385
    00386     sgSetVec3( axis, 0.0, 0.0, -1.0 );
    00387     sgMakeRotMat4( GST, angle, axis );
    00388
    00389     // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
    00390     //             0.0, 0.0, 1.0);
    00391     sgSetVec3( axis, 0.0, 0.0, 1.0 );
    00392     sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
    00393
    00394     // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
    00395     sgSetVec3( axis, 1.0, 0.0, 0.0 );
    00396     sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis );
    00397
    00398     // xglTranslatef(0,sun_dist);
    00399     sgSetVec3( v, 0.0, sun_dist, 0.0 );
    00400     sgMakeTransMat4( T2, v );
    00401
    00402     sgMat4 TRANSFORM;
    00403     sgCopyMat4( TRANSFORM, T1 );
    00404     sgPreMultMat4( TRANSFORM, GST );
    00405     sgPreMultMat4( TRANSFORM, RA );
    00406     sgPreMultMat4( TRANSFORM, DEC );
    00407     sgPreMultMat4( TRANSFORM, T2 );
    00408
    00409     sgCoord skypos;
    00410     sgSetCoord( &skypos, TRANSFORM );
    00411
    00412     sun_transform->setTransform( &skypos );
    00413
    00414     return true;
    00415 }

@ 1.3 log @Oops, try again. @ text @d33 1 a33 1 00025 // $Id: oursun.cxx,v 1.13 2002/08/27 21:45:48 curt Exp $ d48 15 a62 15 00040 #ifdef FG_PLIB_SUPPORTS_LENS_FLARE 00041 # include <plib/ssgaLensFlare.h> 00042 #endif 00043 00044 #include "sphere.hxx" 00045 #include "oursun.hxx" 00046 00047 00048 // Set up sun rendering call backs 00049 static int sgSunOrbPreDraw( ssgEntity *e ) { 00050 /* cout << endl << "Sun orb pre draw" << endl << "----------------" 00051 << endl << endl; */ 00052 00053 ssgLeaf *f = (ssgLeaf *)e; 00054 if ( f -> hasState () ) f->getState()->apply() ; d64 2 a65 2 00056 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT ); 00057 // cout << "push error = " << glGetError() << endl; d67 2 a68 2 00059 glDisable( GL_DEPTH_TEST ); 00060 glDisable( GL_FOG ); d70 2 a71 2 00062 return true; 00063 } d73 6 a78 6 00065 static int sgSunOrbPostDraw( ssgEntity *e ) { 00066 /* cout << endl << "Sun orb post draw" << endl << "----------------" 00067 << endl << endl; */ 00068 00069 glPopAttrib(); 00070 // cout << "pop error = " << glGetError() << endl; d80 2 a81 2 00072 // glEnable( GL_DEPTH_TEST ); 00073 // glEnable( GL_FOG ); d83 2 a84 2 00075 return true; 00076 } d86 6 a91 6 00078 static int sgSunHaloPreDraw( ssgEntity *e ) { 00079 /* cout << endl << "Sun halo pre draw" << endl << "----------------" 00080 << endl << endl; */ 00081 00082 ssgLeaf *f = (ssgLeaf *)e; 00083 if ( f -> hasState () ) f->getState()->apply() ; d93 2 a94 2 00085 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_FOG_BIT ); 00086 // cout << "push error = " << glGetError() << endl; d96 6 a101 6 00088 glDisable( GL_DEPTH_TEST ); 00089 glDisable( GL_FOG ); 00090 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ; 00091 00092 return true; 00093 } d103 6 a108 6 00095 static int sgSunHaloPostDraw( ssgEntity *e ) { 00096 /* cout << endl << "Sun halo post draw" << endl << "----------------" 00097 << endl << endl; */ 00098 00099 glPopAttrib(); 00100 // cout << "pop error = " << glGetError() << endl; d110 2 a111 2 00102 // glEnable( GL_DEPTH_TEST ); 00103 // glEnable( GL_FOG ); d113 2 a114 2 00105 return true; 00106 } d116 78 a193 78 00108 00109 // Constructor 00110 SGSun::SGSun( void ) { 00111 } 00112 00113 00114 // Destructor 00115 SGSun::~SGSun( void ) { 00116 } 00117 00118 00119 #if 0 00120 // this might be nice to keep, just as an example of how to generate a 00121 // texture on the fly ... 00122 static GLuint makeHalo( GLubyte *sun_texbuf, int width ) { 00123 int texSize; 00124 GLuint texid; 00125 GLubyte *p; 00126 int i,j; 00127 double radius; 00128 00129 // create a texture id 00130 #ifdef GL_VERSION_1_1 00131 glGenTextures(1, &texid); 00132 glBindTexture(GL_TEXTURE_2D, texid); 00133 #elif GL_EXT_texture_object 00134 glGenTexturesEXT(1, &texid); 00135 glBindTextureEXT(GL_TEXTURE_2D, texid); 00136 #else 00137 # error port me 00138 #endif 00139 00140 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 ); 00141 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 00142 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00143 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ; 00144 00145 // create the actual texture contents 00146 texSize = width * width; 00147 00148 if ( !sun_texbuf ) { 00149 cout << "ouch ..." << endl; 00150 exit(-1); // Ugly! 00151 } 00152 00153 p = sun_texbuf; 00154 00155 radius = (double)(width / 2); 00156 00157 GLubyte value; 00158 double x, y, d; 00159 for ( i = 0; i < width; i++ ) { 00160 for ( j = 0; j < width; j++ ) { 00161 x = fabs((double)(i - (width / 2))); 00162 y = fabs((double)(j - (width / 2))); 00163 d = sqrt((x * x) + (y * y)); 00164 if (d < radius) { 00165 // t is 1.0 at center, 0.0 at edge 00166 double t = 1.0 - (d / radius); 00167 00168 // inverse square looks nice 00169 value = (int)((double) 0xff * (t*t)); 00170 } else { 00171 value = 0x00; 00172 } 00173 *p = value; 00174 *(p+1) = value; 00175 *(p+2) = value; 00176 // *(p+3) = value; 00177 00178 p += 3; 00179 } 00180 } 00181 00182 /* glTexImage2D( GL_TEXTURE_2D, 00183 0, 00184 GL_RGBA, 00185 width, width, d195 5 a199 5 00187 GL_RGBA, GL_UNSIGNED_BYTE, 00188 sun_texbuf ); */ 00189 00190 return texid; 00191 } d201 39 a239 39 00193 00194 #define RGB 3 // 3 bytes of color info per pixel 00195 #define RGBA 4 // 4 bytes of color+alpha info 00196 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode) 00197 { 00198 int i, j, k, q; 00199 unsigned char *ibuffer; 00200 FILE *fp; 00201 int pixelSize = mode==GL_RGBA?4:3; 00202 00203 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB); 00204 00205 fp = fopen(filename, "wb"); 00206 fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n", 00207 win_width, win_height, UCHAR_MAX); 00208 q = 0; 00209 for (i = 0; i < win_height; i++) { 00210 for (j = 0; j < win_width; j++) { 00211 for (k = 0; k < RGB; k++) { 00212 ibuffer[q++] = (unsigned char) 00213 *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k)); 00214 } 00215 } 00216 } 00217 00218 // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k)); 00219 00220 fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp); 00221 fclose(fp); 00222 free(ibuffer); 00223 00224 printf("wrote file (%d x %d pixels, %d bytes)\n", 00225 win_width, win_height, RGB*win_width*win_height); 00226 } 00227 #endif 00228 00229 00230 // initialize the sun object and connect it into our scene graph root 00231 ssgBranch * SGSun::build( SGPath path, double sun_size ) { d241 21 a261 21 00233 // set up the orb state 00234 orb_state = new ssgSimpleState(); 00235 orb_state->setShadeModel( GL_SMOOTH ); 00236 orb_state->disable( GL_LIGHTING ); 00237 // orb_state->enable( GL_LIGHTING ); 00238 orb_state->disable( GL_CULL_FACE ); 00239 orb_state->disable( GL_TEXTURE_2D ); 00240 orb_state->enable( GL_COLOR_MATERIAL ); 00241 orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE ); 00242 orb_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 ); 00243 orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 ); 00244 orb_state->disable( GL_BLEND ); 00245 orb_state->disable( GL_ALPHA_TEST ); 00246 00247 cl = new ssgColourArray( 1 ); 00248 sgVec4 color; 00249 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 ); 00250 cl->add( color ); 00251 00252 ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10, 00253 sgSunOrbPreDraw, sgSunOrbPostDraw ); d263 2 a264 2 00255 // force a repaint of the sun colors with arbitrary defaults 00256 repaint( 0.0 ); d266 61 a326 61 00258 // build the halo 00259 // sun_texbuf = new GLubyte[64*64*3]; 00260 // sun_texid = makeHalo( sun_texbuf, 64 ); 00261 // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB); 00262 00263 // set up the halo state 00264 path.append( "halo.rgba" ); 00265 halo_state = new ssgSimpleState(); 00266 halo_state->setTexture( (char *)path.c_str() ); 00267 halo_state->enable( GL_TEXTURE_2D ); 00268 halo_state->disable( GL_LIGHTING ); 00269 // halo_state->enable( GL_LIGHTING ); 00270 halo_state->setShadeModel( GL_SMOOTH ); 00271 halo_state->disable( GL_CULL_FACE ); 00272 halo_state->enable( GL_COLOR_MATERIAL ); 00273 halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE ); 00274 halo_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 ); 00275 halo_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 ); 00276 halo_state->enable( GL_ALPHA_TEST ); 00277 halo_state->setAlphaClamp(0.01); 00278 halo_state->enable ( GL_BLEND ) ; 00279 00280 // Build ssg structure 00281 double size = sun_size * 10.0; 00282 sgVec3 v3; 00283 halo_vl = new ssgVertexArray; 00284 sgSetVec3( v3, -size, 0.0, -size ); 00285 halo_vl->add( v3 ); 00286 sgSetVec3( v3, size, 0.0, -size ); 00287 halo_vl->add( v3 ); 00288 sgSetVec3( v3, -size, 0.0, size ); 00289 halo_vl->add( v3 ); 00290 sgSetVec3( v3, size, 0.0, size ); 00291 halo_vl->add( v3 ); 00292 00293 sgVec2 v2; 00294 halo_tl = new ssgTexCoordArray; 00295 sgSetVec2( v2, 0.0f, 0.0f ); 00296 halo_tl->add( v2 ); 00297 sgSetVec2( v2, 1.0, 0.0 ); 00298 halo_tl->add( v2 ); 00299 sgSetVec2( v2, 0.0, 1.0 ); 00300 halo_tl->add( v2 ); 00301 sgSetVec2( v2, 1.0, 1.0 ); 00302 halo_tl->add( v2 ); 00303 00304 ssgLeaf *halo = 00305 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl ); 00306 halo->setState( halo_state ); 00307 00308 // build the ssg scene graph sub tree for the sky and connected 00309 // into the provide scene graph branch 00310 sun_transform = new ssgTransform; 00311 00312 halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw ); 00313 halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw ); 00314 sun_transform->addKid( halo ); 00315 sun_transform->addKid( orb ); 00316 00317 #if 0 00318 // enable this code to add a super cheesy lens flare effect to the sun. d328 1 a328 1 00320 # ifdef FG_PLIB_SUPPORTS_LENS_FLARE d331 1 a331 1 00323 # endif d333 60 a392 60 00325 #endif 00326 00327 return sun_transform; 00328 } 00329 00330 00331 // repaint the sun colors based on current value of sun_angle in 00332 // degrees relative to verticle 00333 // 0 degrees = high noon 00334 // 90 degrees = sun rise/set 00335 // 180 degrees = darkest midnight 00336 bool SGSun::repaint( double sun_angle ) { 00337 if ( sun_angle * SGD_RADIANS_TO_DEGREES < 100 ) { 00338 // else sun is well below horizon (so no point in repainting it) 00339 00340 // x_10 = sun_angle^10 00341 double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle 00342 * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle; 00343 00344 float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0)); 00345 if (ambient < 0.3) { ambient = 0.3; } 00346 if (ambient > 1.0) { ambient = 1.0; } 00347 00348 sgVec4 color; 00349 sgSetVec4( color, 00350 (ambient * 6.0) - 1.0, // minimum value = 0.8 00351 (ambient * 11.0) - 3.0, // minimum value = 0.3 00352 (ambient * 12.0) - 3.6, // minimum value = 0.0 00353 1.0 ); 00354 00355 // temp test, forces the color to always be white 00356 // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 ); 00357 00358 if (color[0] > 1.0) color[0] = 1.0; 00359 if (color[1] > 1.0) color[1] = 1.0; 00360 if (color[2] > 1.0) color[2] = 1.0; 00361 00362 // cout << "color = " << color[0] << " " << color[1] << " " 00363 // << color[2] << endl; 00364 00365 float *ptr; 00366 ptr = cl->get( 0 ); 00367 sgCopyVec4( ptr, color ); 00368 } 00369 00370 return true; 00371 } 00372 00373 00374 // reposition the sun at the specified right ascension and 00375 // declination, offset by our current position (p) so that it appears 00376 // fixed at a great distance from the viewer. Also add in an optional 00377 // rotation (i.e. for the current time of day.) 00378 bool SGSun::reposition( sgVec3 p, double angle, 00379 double rightAscension, double declination, 00380 double sun_dist ) 00381 { 00382 sgMat4 T1, T2, GST, RA, DEC; 00383 sgVec3 axis; 00384 sgVec3 v; d394 27 a420 27 00386 sgMakeTransMat4( T1, p ); 00387 00388 sgSetVec3( axis, 0.0, 0.0, -1.0 ); 00389 sgMakeRotMat4( GST, angle, axis ); 00390 00391 // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0), 00392 // 0.0, 0.0, 1.0); 00393 sgSetVec3( axis, 0.0, 0.0, 1.0 ); 00394 sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis ); 00395 00396 // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0); 00397 sgSetVec3( axis, 1.0, 0.0, 0.0 ); 00398 sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis ); 00399 00400 // xglTranslatef(0,sun_dist); 00401 sgSetVec3( v, 0.0, sun_dist, 0.0 ); 00402 sgMakeTransMat4( T2, v ); 00403 00404 sgMat4 TRANSFORM; 00405 sgCopyMat4( TRANSFORM, T1 ); 00406 sgPreMultMat4( TRANSFORM, GST ); 00407 sgPreMultMat4( TRANSFORM, RA ); 00408 sgPreMultMat4( TRANSFORM, DEC ); 00409 sgPreMultMat4( TRANSFORM, T2 ); 00410 00411 sgCoord skypos; 00412 sgSetCoord( &skypos, TRANSFORM ); d422 3 a424 5 00414 sun_transform->setTransform( &skypos ); 00415 00416 return true; 00417 }