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

WorldObjects.cpp

Go to the documentation of this file.
00001 /*
00002  * WorldObjects.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 
00043 #include "../Include/Globals.h"
00044 #include "../Include/FlyLegacy.h"
00045 #include "../Include/WorldObjects.h"
00046 #include "../Include/Utility.h"
00047 
00048 //
00049 // CWorldObject
00050 //
00051 
00052 CWorldObject::CWorldObject (void) : geopRead (false), iangRead (false)
00053 {
00054   type = TYPE_FLY_WORLDOBJECT;
00055   geop.lat = geop.lon = geop.alt = 0;
00056   iang.h = iang.p = iang.r = 0;
00057 }
00058 
00059 int   CWorldObject::Read (SStream *stream, Tag tag)
00060 {
00061   int rc = TAG_IGNORED;
00062 
00063   switch (tag) {
00064   case 'type':
00065     // Object type
00066     ReadTag (&type, stream);
00067     break;
00068 
00069   case 'geop':
00070     // Geographical position, in WGS84 geodetic coordinates
00071     ReadPosition (&geop, stream);
00073     geop.alt += 50.0;
00074     geopRead = true;
00075     rc = TAG_READ;
00076     break;
00077 
00078   case 'iang':
00079     // Inertial angular position, in radians (heading relative to true north)
00080     ReadVector (&iang, stream);
00081     iangRead = true;
00082     rc = TAG_READ;
00083     break;
00084 
00085   default:
00086     // This is the end of the line...if the tag is not recognized then
00087     //   generate a warning
00088     char s[16];
00089     TagToString (s, tag);
00090     globals->logWarning->Write ("CWorldObject::Read : Unrecognized tag <%s>", s);
00091   }
00092 
00093   return rc;
00094 }
00095 
00096 
00097 void  CWorldObject::ReadFinished (void)
00098 {
00099   if (!geopRead) {
00100     // No <geop> tag was specified, set default position to KSFO runway ??
00101     geop.lat = 0;
00102     geop.lon = 0;
00103     geop.alt = 0;
00104   }
00105   SetPosition (geop);
00106 
00107   if (!iangRead) {
00108     // No <iang> tag was specified, set default orientation to true north
00109     iang.h = 0;
00110     iang.p = 0;
00111     iang.r = 0;
00112   }
00113 }
00114 
00115 Tag CWorldObject::GetType (void)
00116 {
00117   return type;
00118 }
00119 
00120 SPosition CWorldObject::GetPosition (void)
00121 {
00122   return geop;
00123 }
00124 
00125 void CWorldObject::SetPosition (SPosition pos)
00126 {
00127   // Clamp altitude to 100K
00128   const double altClamp = 1.0E+5;
00129   if (pos.alt > altClamp) pos.alt = altClamp;
00130 
00131   // Clamp latitude to globe tile maximum latitude
00132   const double latClamp = 85.199627944116 * 3600.0;
00133   if (pos.lat > latClamp)  pos.lat = latClamp;
00134   if (pos.lat < -latClamp) pos.lat = latClamp;
00135 
00136   geop = pos;
00137 }
00138 
00139 SVector   CWorldObject::GetOrientation (void)
00140 {
00141   return iang;
00142 }
00143 
00144 void      CWorldObject::SetOrientation (SVector v)
00145 {
00146   iang = v;
00147 }
00148 
00149 
00150 //
00151 // CModelManager
00152 //
00153 
00154 CModelManager::CModelManager (void)
00155 {
00156   tag = 0;
00157   model = NULL;
00158 }
00159 
00160 CModelManager::~CModelManager (void)
00161 {
00162   if (model != NULL) delete model;
00163 }
00164 
00165 int CModelManager::Read (SStream *stream, Tag tag)
00166 {
00167   int rc = TAG_IGNORED;
00168 
00169   switch (tag) {
00170   case 'modl':
00171     {
00172       // Static model
00173       ReadTag (&tag, stream);
00174       char modelName[64];
00175       ReadString (modelName, 64, stream);
00176 
00178       char fullName[64];
00179       strcpy (fullName, "MODELS\\");
00180       strcat (fullName, modelName);
00181       char *p = strrchr (modelName, '.');
00182       if (p != NULL) {
00183         if (stricmp (p, ".SMF") == 0) {
00184           if (model == NULL) {
00186             char *mark1 = new char[32];
00187             strcpy (mark1, "CModelSMF Start");
00188             model = new CModelSMF (fullName);
00189             char *mark2 = new char[32];
00190             strcpy (mark2, "CModelSMF End");
00191           } else {
00192             gtfo ("CModelManager : Duplicated <modl> tag : %s", fullName);
00193           }
00194         }
00195       }
00196     }
00197     rc = TAG_READ;
00198     break;
00199   }
00200 
00201   return rc;
00202 }
00203 
00204 ssgEntity *CModelManager::GetSSGEntity (void)
00205 {
00206   ssgEntity *rc = NULL;
00207 
00208   if (model != NULL) rc = model->GetSSGEntity ();
00209 
00210   return rc;
00211 }
00212 
00213 //
00214 // CModelObject
00215 //
00216 
00217 CModelObject::CModelObject (void)
00218 : CWorldObject ()
00219 {
00220   type = TYPE_FLY_MODELOBJECT;
00221   flags = 0;
00222   detail = 0;
00223   dayModel = NULL;
00224   nightModel = NULL;
00225   daynight = NULL;
00226 
00227   top = new ssgTransform;
00228   top->setName ("CModelObject Top");
00229 }
00230 
00231 CModelObject::~CModelObject (void)
00232 {
00233   if (dayModel != NULL) delete dayModel;
00234   if (nightModel != NULL) delete nightModel;
00235 }
00236 
00237 int CModelObject::Read (SStream *stream, Tag tag)
00238 {
00239   int rc = TAG_IGNORED;
00240 
00241   switch (tag) {
00242   case 'flag':
00243     // Gestalt flags
00244     ReadUInt (&flags, stream);
00245     rc = TAG_READ;
00246     break;
00247 
00248   case 'detl':
00249     // Detail level
00250     ReadInt (&detail, stream);
00251     rc = TAG_READ;
00252     break;
00253 
00254   case 'kmmd':
00255     // Day model
00256     dayModel = new CModelManager;
00257     ReadFrom (dayModel, stream);
00258     rc = TAG_READ;
00259     break;
00260 
00261   case 'kmmn':
00262     // Night model
00263     nightModel = new CModelManager;
00264     ReadFrom (nightModel, stream);
00265     rc = TAG_READ;
00266     break;
00267   }
00268 
00269   if (rc == TAG_IGNORED) {
00270     // Allow parent class to process the tag
00271     rc = CWorldObject::Read (stream, tag);
00272   }
00273 
00274   return rc;
00275 }
00276 
00277 void  CModelObject::ReadFinished (void)
00278 {
00279   // Add day and/or night models
00280   if ((dayModel != NULL) || (nightModel != NULL)){
00281     // Create selector for day/night models
00282     daynight = new ssgSelector;
00283     daynight->setName ("Day/Night Selector");
00284     daynight->selectStep(0);
00285     top->addKid (daynight);
00286     
00287     if (dayModel != NULL) {
00288       daynight->addKid (dayModel->GetSSGEntity());
00289     }
00290     if (nightModel != NULL) {
00291       daynight->addKid (nightModel->GetSSGEntity());
00292     }
00293   }
00294 
00295   // Call ReadFinished() method of parent
00296   CWorldObject::ReadFinished ();
00297 }
00298 
00299 void CModelObject::SetPosition (SPosition geop)
00300 {
00301   CWorldObject::SetPosition (geop);
00302 
00303   // Get model rotation Euler angles, convert to degrees
00304   SVector rot = GetOrientation ();
00305   rot.h = RadToDeg (rot.h);
00306   rot.p = RadToDeg (rot.p);
00307   rot.r = RadToDeg (rot.r);
00308 
00309   // Update transform
00310   sgMat4 pos;
00311   SVector v = PosToScaledFlatCartesianQgt (geop);
00312 
00313   sgMakeTransMat4 (pos, v.x, v.y, v.z);
00314 
00315   sgMat4 orient;
00316   sgMakeRotMat4 (orient, rot.r, rot.p, rot.h);
00317 
00318   // Apply transform matrix to top-level transform
00319   sgMat4 T;
00320   sgCopyMat4 (T, pos);
00321   sgPreMultMat4 (T, orient);
00322   top->setTransform (T);
00323 }
00324 
00325 ssgEntity *CModelObject::GetSSGEntity (void)
00326 {
00327   return top;
00328 }
00329 
00330 //
00331 // CSimulatedObject
00332 //
00333 
00334 CSimulatedObject::CSimulatedObject (void)
00335 : CModelObject ()
00336 {
00337   type = TYPE_FLY_SIMULATEDOBJECT;
00338 }
00339 
00340 int CSimulatedObject::Read (SStream *stream, Tag tag)
00341 {
00342   int rc = TAG_IGNORED;
00343 
00344   if (rc != TAG_READ) {
00345     // Allow parent class to process the tag
00346     rc = CModelObject::Read (stream, tag);
00347   }
00348 
00349   return rc;
00350 }
00351 
00352 void  CSimulatedObject::ReadFinished (void)
00353 {
00354   // Call ReadFinished() method of parent
00355   CModelObject::ReadFinished ();
00356 }
00357 
00358 //
00359 // CVehicleObject
00360 //
00361 
00362 CVehicleObject::CVehicleObject (void)
00363 : CSimulatedObject ()
00364 {
00365   type = TYPE_FLY_VEHICLE;
00366   user = false;
00367   vmode = CAMERA_INVALID;
00368 }
00369 
00370 CVehicleObject::~CVehicleObject (void)
00371 {
00372 }
00373 
00374 int CVehicleObject::Read (SStream *stream, Tag tag)
00375 {
00376   int rc = TAG_IGNORED;
00377 
00378   switch (tag) {
00379   case 'user':
00380     // No arguments, this tag indicates that this is the user vehicle
00381     user = true;
00382     rc = TAG_READ;
00383 
00384   case 'vmod':
00385     {
00386       // View mode, references one of the pre-defined cameras
00387       char s[256];
00388       ReadString (s, sizeof(s), stream);
00389       rc = TAG_READ;
00390     }
00391     break;
00392 
00393   default:
00394     // Allow parent class to process the tag
00395     rc = CSimulatedObject::Read (stream, tag);
00396   }
00397 
00398   return rc;
00399 }
00400 
00401 void CVehicleObject::ReadFinished (void)
00402 {
00403   // Call ReadFinished() method of parent
00404   CSimulatedObject::ReadFinished ();
00405 }
00406 
00407 bool  CVehicleObject::isUserVehicle (void)
00408 {
00409   return user;
00410 }
00411 
00412 CCameraManager* CVehicleObject::GetCameraManager (void)
00413 {
00414   return NULL;
00415 }
00416 
00417 CCockpitManager* CVehicleObject::GetCockpitManager (void)
00418 {
00419   return NULL;
00420 }
00421 
00422 //
00423 // CGroundVehicleObject
00424 //
00425 
00426 CGroundVehicleObject::CGroundVehicleObject (void)
00427 {
00428   type = TYPE_FLY_GROUNDVEHICLE;
00429 }
00430 
00431 int CGroundVehicleObject::Read (SStream *stream, Tag tag)
00432 {
00433   int rc = TAG_IGNORED;
00434 
00435   if (rc != TAG_READ) {
00436     // Allow parent class to process the tag
00437     rc = CVehicleObject::Read (stream, tag);
00438   }
00439 
00440   return rc;
00441 }
00442 
00443 
00444 //
00445 // CAirplaneObject
00446 //
00447 
00448 CAirplaneObject::CAirplaneObject (void)
00449 {
00450   type = TYPE_FLY_AIRPLANE;
00451   strcpy (nfoFilename, "");
00452   pAirplane = NULL;
00453 }
00454 
00455 CAirplaneObject::~CAirplaneObject (void)
00456 {
00457   if (pAirplane != NULL) delete pAirplane;
00458 }
00459 
00460 int   CAirplaneObject::Read (SStream *stream, Tag tag)
00461 {
00462   int rc = TAG_IGNORED;
00463 
00464   switch (tag) {
00465   case '_NFO':
00466     // Aircraft Info (.NFO) file
00467     ReadString (nfoFilename, sizeof(nfoFilename), stream);
00468     rc = TAG_READ;
00469     break;
00470   }
00471 
00472   if (rc != TAG_READ) {
00473     // Allow parent class to process the tag
00474     rc = CVehicleObject::Read (stream, tag);
00475   }
00476 
00477   return rc;
00478 }
00479 
00480 void  CAirplaneObject::ReadFinished (void)
00481 {
00482   if (strlen(nfoFilename) == 0) {
00483     // Error, no <_NFO> tag was specified
00484     gtfo ("CAirplaneObject : Missing <_NFO> tag");
00485   } else {
00487     char *mark1 = new char[32];
00488     strcpy (mark1, "airplane start");
00489     pAirplane = new CAirplane (nfoFilename);
00490     char *mark2 = new char[32];
00491     strcpy (mark2, "airplane end");
00492   }
00493 
00494   // Call ReadFinished() method of parent
00495   CVehicleObject::ReadFinished ();
00496 
00497   // Add top-level SSG Entity for aircraft visual model
00498   top->addKid (pAirplane->lod->GetSSGEntity ());
00499 }
00500 
00501 CCameraManager* CAirplaneObject::GetCameraManager (void)
00502 {
00503   CCameraManager *rc = NULL;
00504 
00505   if (pAirplane) {
00506     rc = pAirplane->cam;
00507   }
00508 
00509   return rc;
00510 }
00511 
00512 
00513 CCockpitManager*  CAirplaneObject::GetCockpitManager (void)
00514 {
00515   CCockpitManager *rc = NULL;
00516 
00517   if (pAirplane) {
00518     rc = pAirplane->pit;
00519   }
00520 
00521   return rc;
00522 }
00523 
00524 
00525 Tag CAirplaneObject::GetPanel (void)
00526 {
00527   return pAirplane->pit->GetPanel ();
00528 }
00529 
00530 
00531 void CAirplaneObject::SetPanel (Tag tag)
00532 {
00533   pAirplane->pit->SetPanel (tag);
00534 }
00535 
00536 
00537 void CAirplaneObject::DrawPanel (void)
00538 {
00539   pAirplane->pit->Draw ();
00540 }
00541 
00542 
00543 //
00544 // CHelicopterObject
00545 //
00546 
00547 CHelicopterObject::CHelicopterObject (void)
00548 {
00549   type = TYPE_FLY_HELICOPTER;
00550   strcpy (nfoFilename, "");
00551   pHelicopter = NULL;
00552 }
00553 
00554 CHelicopterObject::~CHelicopterObject (void)
00555 {
00556   if (pHelicopter != NULL) delete pHelicopter;
00557 }
00558 
00559 int   CHelicopterObject::Read (SStream *stream, Tag tag)
00560 {
00561   int rc = TAG_IGNORED;
00562 
00563   switch (tag) {
00564   case '_NFO':
00565     // Helicopter Info (.NFO) file
00566     ReadString (nfoFilename, sizeof(nfoFilename), stream);
00567     rc = TAG_READ;
00568     break;
00569   }
00570 
00571   if (rc != TAG_READ) {
00572     // Allow parent class to process the tag
00573     rc = CVehicleObject::Read (stream, tag);
00574   }
00575 
00576   return rc;
00577 }
00578 
SourceForge.net Logo Documentation generated by doxygen