00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
00061
00062 static int star_postdraw (ssgEntity *e)
00063 {
00064 glPopAttrib ();
00065
00066 return true;
00067 }
00068
00069
00070
00071 static char* getToken (const char* s, char* token, int size)
00072 {
00073
00074 strcpy (token, "");
00075
00076
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
00085 p++;
00086 } else {
00087
00088 strncpy (token, s, size);
00089 }
00090
00091 return p;
00092 }
00093
00094
00095
00096
00097
00098 void CStarImages::LoadFlyDatabase (const char* dbFilename)
00099 {
00100
00101 stars.erase (stars.begin(), stars.end());
00102
00103
00104 PODFILE* p = popen (&globals->pfs, dbFilename);
00105 if (p) {
00106 char s[256];
00107
00108
00109 pgets (s, 256, p);
00110
00111 while (!peof(p)) {
00112
00113 pgets (s, 256, p);
00114
00115
00116 char dummy[64];
00117 char *t = getToken (s, dummy, 64);
00118 int hr = atoi (dummy);
00119
00120
00121 char name[64];
00122 t = getToken (t, dummy, 64);
00123 strcpy (name, dummy);
00124
00125
00126 t = getToken (t, dummy, 64);
00127
00128
00129 t = getToken (t, dummy, 64);
00130 double ra = atof (dummy);
00131 ra = (ra / 24.0) * SGD_PI * 2.0;
00132
00133
00134 t = getToken (t, dummy, 64);
00135 double dec = atof (dummy);
00136 dec = DegToRad (dec);
00137
00138
00139 t = getToken (t, dummy, 64);
00140 float mv = atof (dummy);
00141
00142
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
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 void CStarImages::LoadBSCDatabase (const char* bscFilename, float limit)
00228 {
00229
00230 stars.erase (stars.begin(), stars.end());
00231
00232
00233 PODFILE* p = popen (&globals->pfs, bscFilename);
00234 if (p) {
00235
00236
00237 char rec[512];
00238
00239 while (!peof(p)) {
00240
00241 pgets (rec, 512, p);
00242
00243
00244 char hrString[16];
00245 memset (hrString, 0, 16);
00246 strncpy (hrString, rec, 4);
00247 int hr = atoi (hrString);
00248
00249
00250 char name[16];
00251 memset (name, 0, 16);
00252 strncpy (name, rec+4, 10);
00253
00254
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
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);
00268 ra = (ra / 24.0) * 2 * SGD_PI;
00269
00270
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
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);
00284 dec = DegToRad (dec);
00285
00286
00287 char mvText[8];
00288 memset (mvText, 0, 8);
00289 strncpy (mvText, rec+102, 5);
00290 float mv = atof (mvText);
00291
00292
00293 char bvText[8];
00294 memset (bvText, 0, 8);
00295 strncpy (bvText, rec+109, 5);
00296 float bv = atof (bvText);
00297
00298
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
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 void CStarImages::LoadConstellations (const char* txtFilename)
00331 {
00332
00333 constellations.erase (constellations.begin(), constellations.end());
00334
00335
00336 PODFILE* p = popen (&globals->pfs, txtFilename);
00337 if (p) {
00338
00339 char rec[512];
00340
00341 while (!peof(p)) {
00342
00343 pgets (rec, 512, p);
00344 TrimTrailingWhitespace (rec);
00345
00346
00347 if ((rec[0] == ';') || (strlen(rec) == 0)) continue;
00348
00349
00350 char name[16];
00351 memset (name, 0, 16);
00352 strncpy (name, rec, 3);
00353
00354
00355 char hr1String[16];
00356 memset (hr1String, 0, 16);
00357 strncpy (hr1String, rec+3, 8);
00358 int hr1 = atoi (hr1String);
00359
00360
00361 char hr2String[16];
00362 memset (hr2String, 0, 16);
00363 strncpy (hr2String, rec+11, 8);
00364 int hr2 = atoi (hr2String);
00365
00366
00367 int star1 = FindStarByHR (hr1);
00368 int star2 = FindStarByHR (hr2);
00369 if ((star1 != 1) && (star2!= -1)) {
00370
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
00390
00391
00392
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},
00405 {0x9e, 0xb5, 0xff},
00406 {0xa3, 0xb9, 0xff},
00407 {0xaa, 0xbf, 0xff},
00408 {0xb2, 0xc5, 0xff},
00409 {0xbb, 0xcc, 0xff},
00410 {0xc4, 0xd2, 0xff},
00411 {0xcc, 0xd8, 0xff},
00412 {0xd3, 0xdd, 0xff},
00413 {0xda, 0xe2, 0xff},
00414 {0xdf, 0xe5, 0xff},
00415 {0xe4, 0xe9, 0xff},
00416 {0xe9, 0xec, 0xff},
00417 {0xee, 0xef, 0xff},
00418 {0xf3, 0xf2, 0xff},
00419 {0xf8, 0xf6, 0xff},
00420 {0xfe, 0xf9, 0xff},
00421 {0xff, 0xf9, 0xfb},
00422 {0xff, 0xf7, 0xf5},
00423 {0xff, 0xf5, 0xef},
00424 {0xff, 0xf3, 0xea},
00425 {0xff, 0xf1, 0xe5},
00426 {0xff, 0xef, 0xe0},
00427 {0xff, 0xed, 0xdb},
00428 {0xff, 0xeb, 0xd6},
00429 {0xff, 0xe9, 0xd2},
00430 {0xff, 0xe8, 0xce},
00431 {0xff, 0xe6, 0xca},
00432 {0xff, 0xe5, 0xc6},
00433 {0xff, 0xe3, 0xc3},
00434 {0xff, 0xe2, 0xbf},
00435 {0xff, 0xe0, 0xbb},
00436 {0xff, 0xdf, 0xb8},
00437 {0xff, 0xdd, 0xb4},
00438 {0xff, 0xdb, 0xb0},
00439 {0xff, 0xda, 0xad},
00440 {0xff, 0xd8, 0xa9},
00441 {0xff, 0xd6, 0xa5},
00442 {0xff, 0xd5, 0xa1},
00443 {0xff, 0xd2, 0x9c},
00444 {0xff, 0xd0, 0x96},
00445 {0xff, 0xcc, 0x8f},
00446 {0xff, 0xc8, 0x85},
00447 {0xff, 0xc1, 0x78},
00448 {0xff, 0xb7, 0x65},
00449 {0xff, 0xa9, 0x4b},
00450 {0xff, 0x95, 0x23},
00451 {0xff, 0x7b, 0x00},
00452 {0xff, 0x52, 0x00}
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
00460 int i = (int)(floor (bv + 0.40) * 20.0);
00461
00462
00463 if (i < 0) i = 0;
00464 if (i > nMappings) i = nMappings;
00465
00466
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
00475
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
00487 rc = count;
00488 break;
00489 }
00490 }
00491 return rc;
00492 }
00493
00494
00495 CStarImages::CStarImages (double distance)
00496 {
00497 int i;
00498
00499
00500 top = new ssgTransform;
00501
00502
00503 char dbType[64];
00504 strcpy (dbType, "");
00505 GetIniString ("Graphics", "starDatabaseType", dbType, 64);
00506 if (stricmp (dbType, "FLY") == 0) {
00507
00508 char flyDbName[64];
00509 strcpy (flyDbName, "DATA\\VISFK5.TXT");
00510 GetIniString ("Graphics", "starDatabaseName", flyDbName, 64);
00511 LoadFlyDatabase (flyDbName);
00512 } else {
00513
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
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
00533
00534
00535
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
00548 int nStars = stars.size();
00549 stars_va = new ssgVertexArray (nStars);
00550 stars_rgba = new ssgColourArray (nStars);
00551
00552
00553 sgVec3 v;
00554 sgVec4 c;
00555 for (i=0; i<nStars; i++) {
00556
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
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
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
00580 i = 0;
00581 GetIniVar ("Graphics", "starShowConstellations", &i);
00582 if (i != 0) {
00583
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
00592
00593
00594
00595 LoadConstellations (constDbName);
00596
00597
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
00610 int nLines = constellations.size();
00611 constellations_va = new ssgVertexArray (nLines * 2);
00612 constellations_rgba = new ssgColourArray (1);
00613
00614
00615 sgSetVec4 (c, 1.0, 1.0, 1.0, 0.5);
00616 constellations_rgba->add (c);
00617
00618
00619 vector<SConstellationLine*>::iterator i;
00620 for (i=constellations.begin(); i!=constellations.end(); i++) {
00621 SConstellationLine *s = *i;
00622
00623
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
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
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
00671 double mv = star->mv;
00672 double nmag, alpha;
00673 if (mv < limit) {
00674
00675 nmag = (limit - mv) / limit;
00676 alpha = (nmag * 0.85);
00677 alpha += 0.25;
00678 alpha *= factor;
00679 } else {
00680
00681 alpha = 0.0;
00682 }
00683
00684
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
00695
00696
00697
00698
00699
00700 void CStarImages::Reposition (sgVec3 pos, double lon, double lat, double lst)
00701 {
00702 sgMat4 LAT, LST;
00703 sgVec3 axis;
00704
00705
00706 sgSetVec3 (axis, -1, 0, 0);
00707 sgMakeRotMat4 (LAT, 90 - lat, axis);
00708
00709
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