Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

StarImages.cpp

Go to the documentation of this file.
00001 /*
00002  * StarImages.cpp
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  *   along with Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00036 #include "../Include/Globals.h"
00037 #include "../Include/Sky.h"
00038 #include "../Include/Utility.h"
00039 
00040 using namespace std;
00041 
00042 //
00043 // Pre-draw callback for star images
00044 //
00045 static int star_predraw (ssgEntity *e)
00046 {
00047   ssgLeaf *f = (ssgLeaf*)e;
00048   if (f->hasState()) f->getState()->apply();
00049 
00050   glPushAttrib (GL_DEPTH_BUFFER_BIT | GL_FOG_BIT | GL_POINT_BIT);
00051   glDisable (GL_DEPTH_TEST);
00052   glDisable (GL_FOG);
00053   glEnable (GL_POINT_SMOOTH);
00054   glPointSize (2.0);
00055 
00056   return true;
00057 }
00058 
00059 //
00060 // Post-draw callback for star images
00061 //
00062 static int star_postdraw (ssgEntity *e)
00063 {
00064   glPopAttrib ();
00065 
00066   return true;
00067 }
00068 
00069 //
00070 // This local function finds the next token in a tab-delimited record.  
00071 static char* getToken (const char* s, char* token, int size)
00072 {
00073   // Initialize token to null string
00074   strcpy (token, "");
00075 
00076   // Get pointer to next \t delimiter
00077   char *p = strchr (s, '\t');
00078   if (p != NULL) {
00079     int nChars = p - s;
00080     if (nChars > (size-1)) nChars = (size-1);
00081     strncpy (token, s, nChars);
00082     token[nChars] = '\0';
00083 
00084     // Increment p to point to start of next token
00085     p++;
00086   } else {
00087     // Delimiter not found, copy entire string to token
00088     strncpy (token, s, size);
00089   }
00090   
00091   return p;
00092 }
00093 
00094 //
00095 // This function initializes the star database from a text database in the format
00096 //   of the default Fly! II star database (DATA\\VISFK5.TXT in Startup.POD).
00097 //
00098 void CStarImages::LoadFlyDatabase (const char* dbFilename)
00099 {
00100   // Clear existing star database
00101   stars.erase (stars.begin(), stars.end());
00102 
00103   // Open tab-delimited text database file
00104   PODFILE* p = popen (&globals->pfs, dbFilename);
00105   if (p) {
00106     char s[256];
00107 
00108     // Skip header line
00109     pgets (s, 256, p);
00110 
00111     while (!peof(p)) {
00112       // Read tab-delimited data
00113       pgets (s, 256, p);
00114 
00115       // Initialize to first token (index number)
00116       char dummy[64];
00117       char *t = getToken (s, dummy, 64);
00118       int hr = atoi (dummy);
00119 
00120       // Read common name
00121       char name[64];
00122       t = getToken (t, dummy, 64);
00123       strcpy (name, dummy);
00124 
00125       // Skip FK5 index number
00126       t = getToken (t, dummy, 64);
00127 
00128       // Read right ascension (in hours) and convert to radians
00129       t = getToken (t, dummy, 64);
00130       double ra = atof (dummy);
00131       ra = (ra / 24.0) * SGD_PI * 2.0;
00132 
00133       // Read declinatin (in degrees) and convert to radians
00134       t = getToken (t, dummy, 64);
00135       double dec = atof (dummy);
00136       dec = DegToRad (dec);
00137 
00138       // Read magnitude
00139       t = getToken (t, dummy, 64);
00140       float mv = atof (dummy);
00141 
00142       // Store attributes in new SStar struct and add to list
00143       SStarData* star = new SStarData;
00144       strcpy (star->name, name);
00145       star->ra = WrapTwoPi (ra - (SGD_PI / 2));
00146       star->dec = dec;
00147       star->mv = mv;
00148       star->hr = hr;
00149       stars.push_back(star);
00150     }
00151   } else {
00152     gtfo ("CStarImages : Could not open Fly! II star database %s", dbFilename);
00153   }
00154 }
00155 
00156 //
00157 // This function initializes the star database from the 5th edition
00158 //   of the Yale Bright Star Catalog.  This database contains approx.
00159 //   9000 stars down to magnitude +6.5.  For efficiency, only those stars with
00160 //   visual magnitudes above the specified limit will be loaded.
00161 //
00162 /*
00163 
00164   Format of the BSC catalog data is as follows:
00165 
00166    1-  4  I4     ---     HR       [1/9110]+ Harvard Revised Number
00167                                     = Bright Star Number
00168    5- 14  A10    ---     Name     Name, generally Bayer and/or Flamsteed name
00169   15- 25  A11    ---     DM       Durchmusterung Identification (zone in
00170                                     bytes 17-19)
00171   26- 31  I6     ---     HD       [1/225300]? Henry Draper Catalog Number
00172   32- 37  I6     ---     SAO      [1/258997]? SAO Catalog Number
00173   38- 41  I4     ---     FK5      ? FK5 star Number
00174       42  A1     ---     IRflag   [I] I if infrared source
00175       43  A1     ---   r_IRflag  *[ ':] Coded reference for infrared source
00176       44  A1     ---    Multiple *[AWDIRS] Double or multiple-star code
00177   45- 49  A5     ---     ADS      Aitken's Double Star Catalog (ADS) designation
00178   50- 51  A2     ---     ADScomp  ADS number components
00179   52- 60  A9     ---     VarID    Variable star identification
00180   61- 62  I2     h       RAh1900  ?Hours RA, equinox B1900, epoch 1900.0 (1)
00181   63- 64  I2     min     RAm1900  ?Minutes RA, equinox B1900, epoch 1900.0 (1)
00182   65- 68  F4.1   s       RAs1900  ?Seconds RA, equinox B1900, epoch 1900.0 (1)
00183       69  A1     ---     DE-1900  ?Sign Dec, equinox B1900, epoch 1900.0 (1)
00184   70- 71  I2     deg     DEd1900  ?Degrees Dec, equinox B1900, epoch 1900.0 (1)
00185   72- 73  I2     arcmin  DEm1900  ?Minutes Dec, equinox B1900, epoch 1900.0 (1)
00186   74- 75  I2     arcsec  DEs1900  ?Seconds Dec, equinox B1900, epoch 1900.0 (1)
00187   76- 77  I2     h       RAh      ?Hours RA, equinox J2000, epoch 2000.0 (1)
00188   78- 79  I2     min     RAm      ?Minutes RA, equinox J2000, epoch 2000.0 (1)
00189   80- 83  F4.1   s       RAs      ?Seconds RA, equinox J2000, epoch 2000.0 (1)
00190       84  A1     ---     DE-      ?Sign Dec, equinox J2000, epoch 2000.0 (1)
00191   85- 86  I2     deg     DEd      ?Degrees Dec, equinox J2000, epoch 2000.0 (1)
00192   87- 88  I2     arcmin  DEm      ?Minutes Dec, equinox J2000, epoch 2000.0 (1)
00193   89- 90  I2     arcsec  DEs      ?Seconds Dec, equinox J2000, epoch 2000.0 (1)
00194   91- 96  F6.2   deg     GLON     ?Galactic longitude (1)
00195   97-102  F6.2   deg     GLAT     ?Galactic latitude (1)
00196  103-107  F5.2   mag     Vmag     ?Visual magnitude (1)
00197      108  A1     ---   n_Vmag    *[ HR] Visual magnitude code
00198      109  A1     ---   u_Vmag     [ :?] Uncertainty flag on V
00199  110-114  F5.2   mag     B-V      ? B-V color in the UBV system
00200      115  A1     ---   u_B-V      [ :?] Uncertainty flag on B-V
00201  116-120  F5.2   mag     U-B      ? U-B color in the UBV system
00202      121  A1     ---   u_U-B      [ :?] Uncertainty flag on U-B
00203  122-126  F5.2   mag     R-I      ? R-I   in system specified by n_R-I
00204      127  A1     ---   n_R-I      [CE:?D] Code for R-I system (Cousin, Eggen)
00205  128-147  A20    ---     SpType   Spectral type
00206      148  A1     ---   n_SpType   [evt] Spectral type code
00207  149-154  F6.3 arcsec/yr pmRA     ?Annual proper motion in RA J2000, FK5 system
00208  155-160  F6.3 arcsec/yr pmDE     ?Annual proper motion in Dec J2000, FK5 system
00209      161  A1     ---   n_Parallax [D] D indicates a dynamical parallax,
00210                                     otherwise a trigonometric parallax
00211  162-166  F5.3   arcsec  Parallax ? Trigonometric parallax (unless n_Parallax)
00212  167-170  I4     km/s    RadVel   ? Heliocentric Radial Velocity
00213  171-174  A4     ---   n_RadVel  *[V?SB123O ] Radial velocity comments
00214  175-176  A2     ---   l_RotVel   [<=> ] Rotational velocity limit characters
00215  177-179  I3     km/s    RotVel   ? Rotational velocity, v sin i
00216      180  A1     ---   u_RotVel   [ :v] uncertainty and variability flag on
00217                                     RotVel
00218  181-184  F4.1   mag     Dmag     ? Magnitude difference of double,
00219                                     or brightest multiple
00220  185-190  F6.1   arcsec  Sep      ? Separation of components in Dmag
00221                                     if occultation binary.
00222  191-194  A4     ---     MultID   Identifications of components in Dmag
00223  195-196  I2     ---     MultCnt  ? Number of components assigned to a multiple
00224      197  A1     ---     NoteFlag [*] a star indicates that there is a note
00225                                     (see file notes)
00226 */
00227 void CStarImages::LoadBSCDatabase (const char* bscFilename, float limit)
00228 {
00229   // Clear existing star database
00230   stars.erase (stars.begin(), stars.end());
00231 
00232   // Open Bright Star Catalog file
00233   PODFILE* p = popen (&globals->pfs, bscFilename);
00234   if (p) {
00235     
00236     // Define a buffer for each BSC record.
00237     char rec[512];
00238 
00239     while (!peof(p)) {
00240       // Read record
00241       pgets (rec, 512, p);
00242 
00243       // Get HR index number
00244       char hrString[16];
00245       memset (hrString, 0, 16);
00246       strncpy (hrString, rec, 4);
00247       int hr = atoi (hrString);
00248 
00249       // Get common name
00250       char name[16];
00251       memset (name, 0, 16);
00252       strncpy (name, rec+4, 10);
00253 
00254       // Get J2000.0 right ascension values
00255       char raHrs[8], raMin[8], raSec[8];
00256       memset (raHrs, 0, 8);
00257       memset (raMin, 0, 8);
00258       memset (raSec, 0, 8);
00259       strncpy (raHrs, rec+75, 2);
00260       strncpy (raMin, rec+77, 2);
00261       strncpy (raSec, rec+79, 4);
00262 
00263       // Convert right ascension to radians
00264       float rah = atof (raHrs);
00265       float ram = atof (raMin);
00266       float ras = atof (raSec);
00267       float ra = rah + (ram / 60.0) + (ras / 3600.0);   // RA in hours
00268       ra = (ra / 24.0) * 2 * SGD_PI;            // RA in radians
00269 
00270       // Get J2000.0 declination values
00271       char decDeg[8], decMin[8], decSec[8];
00272       memset (decDeg, 0, 8);
00273       memset (decMin, 0, 8);
00274       memset (decSec, 0, 8);
00275       strncpy (decDeg, rec+83, 3);
00276       strncpy (decMin, rec+86, 2);
00277       strncpy (decSec, rec+88, 2);
00278 
00279       // Convert declination to radians
00280       float decd = atof (decDeg);
00281       float decm = atof (decMin);
00282       float decs = atof (decSec);
00283       float dec = decd + (decm / 60.0) + (decs / 3600.0); // Dec in degrees
00284       dec = DegToRad (dec);               // Dec in radians
00285 
00286       // Get magnitude
00287       char mvText[8];
00288       memset (mvText, 0, 8);
00289       strncpy (mvText, rec+102, 5);
00290       float mv = atof (mvText);
00291 
00292       // Get colour (BV)
00293       char bvText[8];
00294       memset (bvText, 0, 8);
00295       strncpy (bvText, rec+109, 5);
00296       float bv = atof (bvText);
00297 
00298       // Check against limiting magnitude, and store this star in the database
00299       if (mv <= limit) {
00300         SStarData* star = new SStarData;
00301         strcpy (star->name, name);
00302         star->ra = WrapTwoPi (ra - (SGD_PI / 2));
00303         star->dec = dec;
00304         star->mv = mv;
00305         star->bv = bv;
00306         star->hr = hr;
00307         stars.push_back (star);
00308       }
00309     }
00310 
00311     pclose (p);
00312   } else {
00313     gtfo ("CStarImages : Could not open BSC5 database %s", bscFilename);
00314   }
00315 }
00316 
00317 
00318 //
00319 // Load constellation stick figures.  The figures consists of a set of line
00320 //   segments between stars.  The format of the constellation line database
00321 //   file is:
00322 //      1-3   C   3   Constellation abbreviation
00323 //      4-11  I   8   HR1, first line endpoint
00324 //      12-19 I   8   HR2, second line endpoint
00325 //
00326 // Since the HR index number is included in both the Fly! and BSC databases,
00327 //   constellation figures can be displayed regardless of which database is
00328 //   used.
00329 //
00330 void CStarImages::LoadConstellations (const char* txtFilename)
00331 {
00332   // Clear existing star database
00333   constellations.erase (constellations.begin(), constellations.end());
00334 
00335   // Open constellation data file
00336   PODFILE* p = popen (&globals->pfs, txtFilename);
00337   if (p) {
00338     // Define a buffer for each record.
00339     char rec[512];
00340 
00341     while (!peof(p)) {
00342       // Read record
00343       pgets (rec, 512, p);
00344       TrimTrailingWhitespace (rec);
00345 
00346       // Skip comments lines beginning with semicolon
00347       if ((rec[0] == ';') || (strlen(rec) == 0)) continue;
00348 
00349       // Get constellation name (abbreviated)
00350       char name[16];
00351       memset (name, 0, 16);
00352       strncpy (name, rec, 3);
00353 
00354       // Get HR index for first endpoint
00355       char hr1String[16];
00356       memset (hr1String, 0, 16);
00357       strncpy (hr1String, rec+3, 8);
00358       int hr1 = atoi (hr1String);
00359 
00360       // Get HR index for second endpoint
00361       char hr2String[16];
00362       memset (hr2String, 0, 16);
00363       strncpy (hr2String, rec+11, 8);
00364       int hr2 = atoi (hr2String);
00365 
00366       // Check that both endpoint stars are in the set of loaded stars
00367       int star1 = FindStarByHR (hr1);
00368       int star2 = FindStarByHR (hr2);
00369       if ((star1 != 1) && (star2!= -1)) {
00370         // Store in the constellation database
00371         SConstellationLine* line = new SConstellationLine;
00372         strcpy (line->name, name);
00373         line->star1 = star1;
00374         line->star2 = star2;
00375         constellations.push_back (line);
00376       }
00377     }
00378 
00379     pclose (p);
00380 
00381   } else {
00382     gtfo ("CStarImages : Could not open constellation database %s", txtFilename);
00383   }
00384 }
00385 
00386 
00387 
00388 //
00389 // This function provides a simplified mapping of a star's B-V colour index
00390 //   to an RGB colour triplet.  The data is taken from the web page,
00391 //   "What color are the stars?" at:
00392 //   http://www.vendian.org/mncharity/dir3/starcolor/details.html
00393 //
00394 
00395 typedef struct {
00396   unsigned char r;
00397   unsigned char g;
00398   unsigned char b;
00399 } SBVToRGB;
00400 
00401 
00402 SBVToRGB bvMapping[] =
00403 {
00404   {0x9b,  0xb2, 0xff},    // -0.40
00405   {0x9e,  0xb5, 0xff},    // -0.35
00406   {0xa3,  0xb9, 0xff},    // -0.30
00407   {0xaa,  0xbf, 0xff},    // -0.25
00408   {0xb2,  0xc5, 0xff},    // -0.20
00409   {0xbb,  0xcc, 0xff},    // -0.15
00410   {0xc4,  0xd2, 0xff},    // -0.10
00411   {0xcc,  0xd8, 0xff},    // -0.05
00412   {0xd3,  0xdd, 0xff},    //  0.00
00413   {0xda,  0xe2, 0xff},    //  0.05
00414   {0xdf,  0xe5, 0xff},    //  0.10
00415   {0xe4,  0xe9, 0xff},    //  0.15
00416   {0xe9,  0xec, 0xff},    //  0.20
00417   {0xee,  0xef, 0xff},    //  0.25
00418   {0xf3,  0xf2, 0xff},    //  0.30
00419   {0xf8,  0xf6, 0xff},    //  0.35
00420   {0xfe,  0xf9, 0xff},    //  0.40
00421   {0xff,  0xf9, 0xfb},    //  0.45
00422   {0xff,  0xf7, 0xf5},    //  0.50
00423   {0xff,  0xf5, 0xef},    //  0.55
00424   {0xff,  0xf3, 0xea},    //  0.60
00425   {0xff,  0xf1, 0xe5},    //  0.65
00426   {0xff,  0xef, 0xe0},    //  0.70
00427   {0xff,  0xed, 0xdb},    //  0.75
00428   {0xff,  0xeb, 0xd6},    //  0.80
00429   {0xff,  0xe9, 0xd2},    //  0.85
00430   {0xff,  0xe8, 0xce},    //  0.90
00431   {0xff,  0xe6, 0xca},    //  0.95
00432   {0xff,  0xe5, 0xc6},    //  1.00
00433   {0xff,  0xe3, 0xc3},    //  1.05
00434   {0xff,  0xe2, 0xbf},    //  1.10
00435   {0xff,  0xe0, 0xbb},    //  1.15
00436   {0xff,  0xdf, 0xb8},    //  1.20
00437   {0xff,  0xdd, 0xb4},    //  1.25
00438   {0xff,  0xdb, 0xb0},    //  1.30
00439   {0xff,  0xda, 0xad},    //  1.35
00440   {0xff,  0xd8, 0xa9},    //  1.40
00441   {0xff,  0xd6, 0xa5},    //  1.45
00442   {0xff,  0xd5, 0xa1},    //  1.50
00443   {0xff,  0xd2, 0x9c},    //  1.55
00444   {0xff,  0xd0, 0x96},    //  1.60
00445   {0xff,  0xcc, 0x8f},    //  1.65
00446   {0xff,  0xc8, 0x85},    //  1.70
00447   {0xff,  0xc1, 0x78},    //  1.75
00448   {0xff,  0xb7, 0x65},    //  1.80
00449   {0xff,  0xa9, 0x4b},    //  1.85
00450   {0xff,  0x95, 0x23},    // 1.90
00451   {0xff,  0x7b, 0x00},    // 1.95
00452   {0xff,  0x52, 0x00}   // 2.00
00453 };
00454 
00455 const int nMappings = sizeof(bvMapping) / sizeof(SBVToRGB);
00456 
00457 static void bv_to_rgb (float bv, float& r, float& g, float& b)
00458 {
00459   // Convert B-V colour to table index
00460   int i = (int)(floor (bv + 0.40) * 20.0);
00461 
00462   // Clamp to table bounds
00463   if (i < 0) i = 0;
00464   if (i > nMappings) i = nMappings;
00465 
00466   // Extract char values from mapping table and convert to float
00467   SBVToRGB *p = &bvMapping[i];
00468   r = (float)(p->r) / 255.0;
00469   g = (float)(p->g) / 255.0;
00470   b = (float)(p->b) / 255.0;
00471 }
00472 
00473 //
00474 // Find the index of a star with the given HR index number.  If the HR index
00475 //   does not exist, the function returns -1.
00476 //
00477 int CStarImages::FindStarByHR (int hr)
00478 {
00479   int rc = -1;
00480 
00481   vector<SStarData*>::iterator i;
00482   int count = 0;
00483   for (i=stars.begin(); i!=stars.end(); i++, count++) {
00484     SStarData *s = *i;
00485     if (s->hr == hr) {
00486       // Found a match
00487       rc = count;
00488       break;
00489     }
00490   }
00491   return rc;
00492 }
00493 
00494 
00495 CStarImages::CStarImages (double distance)
00496 {
00497   int i;
00498   
00499   // Construct the top-level transform
00500   top = new ssgTransform;
00501 
00502   // Get INI setting to determine format of star database to use
00503   char dbType[64];
00504   strcpy (dbType, "");
00505   GetIniString ("Graphics", "starDatabaseType", dbType, 64);
00506   if (stricmp (dbType, "FLY") == 0) {
00507     // Fly! II star database
00508     char flyDbName[64];
00509     strcpy (flyDbName, "DATA\\VISFK5.TXT");
00510     GetIniString ("Graphics", "starDatabaseName", flyDbName, 64);
00511     LoadFlyDatabase (flyDbName);
00512   } else {
00513     // BSC (Bright Star Catalog) database
00514     char bscDbName[64];
00515     strcpy (bscDbName, "");
00516     GetIniString ("Graphics", "starDatabaseName", bscDbName, 64);
00517     if (strlen (bscDbName) == 0) {
00518       strcpy (bscDbName, "SYSTEM\\BSC5.CAT");
00519     }
00520 
00521     // Get INI setting for limiting magnitude
00522     float limit = 0;
00523     GetIniFloat ("Graphics", "starLimitingMagnitude", &limit);
00524     if (limit == 0) {
00525       limit = 6.5;
00526     }
00527 
00528     LoadBSCDatabase (bscDbName, limit);
00529   }
00530 
00531   //
00532   // Construct SSG leaf for stars as a set of points
00533   //
00534 
00535   // Construct SSG state for drawing stars
00536   ssgSimpleState *stars_state = new ssgSimpleState();
00537   stars_state->disable( GL_LIGHTING );
00538   stars_state->disable( GL_CULL_FACE );
00539   stars_state->disable( GL_TEXTURE_2D );
00540   stars_state->enable( GL_COLOR_MATERIAL );
00541   stars_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
00542   stars_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
00543   stars_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
00544   stars_state->enable( GL_BLEND );
00545   stars_state->disable( GL_ALPHA_TEST );
00546 
00547   // Allocate vertex and colour arrays
00548   int nStars = stars.size();
00549   stars_va = new ssgVertexArray (nStars);
00550   stars_rgba = new ssgColourArray (nStars);
00551 
00552   // Build ssg structure
00553   sgVec3 v;
00554   sgVec4 c;
00555   for (i=0; i<nStars; i++) {
00556     // Vertex position
00557     SStarData *s = stars[i];
00558     sgSetVec3 (v,
00559                distance * cos (s->ra) * cos (s->dec),
00560                distance * sin (s->ra) * cos (s->dec),
00561                distance * sin (s->dec));
00562     stars_va->add (v);
00563 
00564     // Assign RGB colour based on B-V colour index
00565     float r, g, b;
00566     bv_to_rgb (s->bv, r, g, b);
00567     sgSetVec4 (c, r, g, b, 1.0 );
00568     stars_rgba->add (c);
00569   }
00570 
00571   // Construct the leaf object for the stars and set attributes
00572   ssgLeaf *stars_leaf =
00573       new ssgVtxTable ( GL_POINTS, stars_va, NULL, NULL, stars_rgba );
00574   stars_leaf->setState( stars_state );
00575   stars_leaf->setCallback( SSG_CALLBACK_PREDRAW, star_predraw );
00576   stars_leaf->setCallback( SSG_CALLBACK_POSTDRAW, star_postdraw );
00577   top->addKid (stars_leaf);
00578 
00579   // Check INI setting to see if constellation figures are to be drawn
00580   i = 0;
00581   GetIniVar ("Graphics", "starShowConstellations", &i);
00582   if (i != 0) {
00583     // Get filename of constellation figures fron INI settings
00584     char constDbName[64];
00585     strcpy (constDbName, "");
00586     GetIniString ("Graphics", "starConstellationsName", constDbName, 64);
00587     if (strlen (constDbName) == 0) {
00588       strcpy (constDbName, "SYSTEM\\DefaultConstL.txt");
00589     }
00590     
00591     // Load the constellation stick figures.  This will update the class
00592     //   member 'constellations', a vector of star endpoints.  Only those
00593     //   lines that can be drawn, given the set of stars that were loaded,
00594     //   will be added to the vector.
00595     LoadConstellations (constDbName);
00596 
00597     // Construct SSG state for drawing constellations
00598     ssgSimpleState *constellations_state = new ssgSimpleState();
00599     constellations_state->disable( GL_LIGHTING );
00600     constellations_state->disable( GL_CULL_FACE );
00601     constellations_state->disable( GL_TEXTURE_2D );
00602     constellations_state->enable( GL_COLOR_MATERIAL );
00603     constellations_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
00604     constellations_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
00605     constellations_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
00606     constellations_state->enable( GL_BLEND );
00607     constellations_state->disable( GL_ALPHA_TEST );
00608 
00609     // Allocate vertex and colour arrays
00610     int nLines = constellations.size();
00611     constellations_va = new ssgVertexArray (nLines * 2);
00612     constellations_rgba = new ssgColourArray (1);
00613 
00614     // Default constellation line colour
00615     sgSetVec4 (c, 1.0, 1.0, 1.0, 0.5);
00616     constellations_rgba->add (c);
00617 
00618     // Build ssg structure
00619     vector<SConstellationLine*>::iterator i;
00620     for (i=constellations.begin(); i!=constellations.end(); i++) {
00621       SConstellationLine *s = *i;
00622 
00623       // Copy each star vertex into constellation endpoint list
00624       float *v1 = stars_va->get (s->star1);
00625       float *v2 = stars_va->get (s->star2);
00626 
00627       sgVec3 p1, p2;
00628       sgCopyVec3 (p1, v1);
00629       sgCopyVec3 (p2, v2);
00630 
00631       constellations_va->add (p1);
00632       constellations_va->add (p2);
00633     
00634       // Construct leaf object for constellations
00635       ssgLeaf *constellations_leaf =
00636         new ssgVtxTable (GL_LINES, constellations_va, NULL, NULL, constellations_rgba);
00637       constellations_leaf->setState( constellations_state );
00638       constellations_leaf->setCallback( SSG_CALLBACK_PREDRAW, star_predraw );
00639       constellations_leaf->setCallback( SSG_CALLBACK_POSTDRAW, star_postdraw );
00640       top->addKid (constellations_leaf);
00641     }
00642   }
00643 }
00644 
00645 CStarImages::~CStarImages (void)
00646 {
00647   vector<SStarData*>::iterator i;
00648   for (i=stars.begin(); i!=stars.end(); i++) delete *i;
00649 
00650   vector<SConstellationLine*>::iterator j;
00651   for (j=constellations.begin(); j!=constellations.end(); j++) delete *j;
00652 }
00653 
00654 //
00655 // Return the top-level SSG entity for the star images
00656 //
00657 ssgEntity* CStarImages::GetSSGEntity (void)
00658 {
00659   return top;
00660 }
00661 
00662 void CStarImages::Repaint (float limit, float factor)
00663 {
00664   int i;
00665 
00666   int nStars = stars.size();
00667   for (i=0; i<nStars; i++) {
00668     SStarData* star = stars[i];
00669 
00670     // Determine visual brightness of the star
00671     double mv = star->mv;
00672     double nmag, alpha;
00673     if (mv < limit) {
00674       // This star is brighter than the limiting magnitude
00675       nmag = (limit - mv) / limit;    // Scale brightness to 0.0..1.0
00676       alpha = (nmag * 0.85);        // Scale alpha to 0.0..0.85
00677       alpha += 0.25;            // Bias +25%
00678       alpha *= factor;          // Adjust alpha for ambient light
00679     } else {
00680       // Star is dimmer than the limiting magnitude
00681       alpha = 0.0;
00682     }
00683 
00684     // Clamp alpha to 0.0..1.0
00685     if (alpha > 1.0) { alpha = 1.0; }
00686     if (alpha < 0.0) { alpha = 0.0; }
00687 
00688     float* c = stars_rgba->get (i);
00689     c[3] = alpha;
00690   }
00691 }
00692 
00693 //
00694 // Reposition the image of the stars in the sky
00695 //
00696 // Arguments:
00697 //  pos     3D position of the eye (cartesian coordinates??)
00698 //  lst     Local Sidereal Time, in degrees
00699 //
00700 void CStarImages::Reposition (sgVec3 pos, double lon, double lat, double lst)
00701 {
00702   sgMat4 LAT, LST;
00703   sgVec3 axis;
00704 
00705   // Rotate for eye position latitude
00706   sgSetVec3 (axis, -1, 0, 0);
00707   sgMakeRotMat4 (LAT, 90 - lat, axis);
00708 
00709   // Rotation matrix for local sidereal time, converted from h to deg
00710   sgSetVec3 (axis, 0, 0, -1);
00711   sgMakeRotMat4 (LST, (lst * 15), axis);
00712 
00713   sgMat4 T;
00714   sgMakeIdentMat4 (T);
00715   sgPreMultMat4 (T, LAT);
00716   sgPreMultMat4 (T, LST);
00717 
00718   top->setTransform (T);
00719 }
00720 
SourceForge.net Logo Documentation generated by doxygen