00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <iostream>
00011 #include <celutil/util.h>
00012 #include <celestia/celestiacore.h>
00013 #include <celestia/imagecapture.h>
00014 #include "astro.h"
00015 #include "command.h"
00016 #include "execution.h"
00017 #include "glcontext.h"
00018
00019 using namespace std;
00020
00021
00023
00024
00025 CommandWait::CommandWait(double _duration) : TimedCommand(_duration)
00026 {
00027 }
00028
00029 CommandWait::~CommandWait()
00030 {
00031 }
00032
00033 void CommandWait::process(ExecutionEnvironment& env, double t, double dt)
00034 {
00035 }
00036
00037
00039
00040
00041 CommandSelect::CommandSelect(string _target) : target(_target)
00042 {
00043 }
00044
00045 CommandSelect::~CommandSelect()
00046 {
00047 }
00048
00049 void CommandSelect::process(ExecutionEnvironment& env)
00050 {
00051 Selection sel = env.getSimulation()->findObjectFromPath(target);
00052 env.getSimulation()->setSelection(sel);
00053 }
00054
00055
00056
00058
00059
00060 CommandGoto::CommandGoto(double t,
00061 double dist,
00062 Vec3f _up,
00063 astro::CoordinateSystem _upFrame) :
00064 gotoTime(t), distance(dist), up(_up), upFrame(_upFrame)
00065 {
00066 }
00067
00068 CommandGoto::~CommandGoto()
00069 {
00070 }
00071
00072 void CommandGoto::process(ExecutionEnvironment& env)
00073 {
00074 Selection sel = env.getSimulation()->getSelection();
00075 env.getSimulation()->gotoSelection(gotoTime,
00076 astro::kilometersToLightYears(sel.radius() * distance),
00077 up, upFrame);
00078 }
00079
00080
00082
00083
00084 CommandGotoLongLat::CommandGotoLongLat(double t,
00085 double dist,
00086 float _longitude,
00087 float _latitude,
00088 Vec3f _up) :
00089 gotoTime(t),
00090 distance(dist),
00091 longitude(_longitude),
00092 latitude(_latitude),
00093 up(_up)
00094 {
00095 }
00096
00097 CommandGotoLongLat::~CommandGotoLongLat()
00098 {
00099 }
00100
00101 void CommandGotoLongLat::process(ExecutionEnvironment& env)
00102 {
00103 Selection sel = env.getSimulation()->getSelection();
00104 env.getSimulation()->gotoSelectionLongLat(gotoTime,
00105 astro::kilometersToLightYears(sel.radius() * distance),
00106 longitude, latitude,
00107 up);
00108 }
00109
00110
00112
00113
00114 CommandGotoLocation::CommandGotoLocation(double t,
00115 const Point3d& _translation,
00116 const Quatf& _rotation) :
00117 gotoTime(t), translation(_translation), rotation(_rotation)
00118 {
00119 }
00120
00121 CommandGotoLocation::~CommandGotoLocation()
00122 {
00123 }
00124
00125 void CommandGotoLocation::process(ExecutionEnvironment& env)
00126 {
00127 RigidTransform to;
00128 to.rotation = Quatd(rotation.w, rotation.x, rotation.y, rotation.z);
00129 to.translation = translation;
00130 env.getSimulation()->gotoLocation(to, gotoTime);
00131 }
00132
00134
00135
00136 CommandSetUrl::CommandSetUrl(const std::string& _url) :
00137 url(_url)
00138 {
00139 }
00140
00141 void CommandSetUrl::process(ExecutionEnvironment& env)
00142 {
00143 env.getCelestiaCore()->goToUrl(url);
00144 }
00145
00146
00148
00149
00150 CommandCenter::CommandCenter(double t) : centerTime(t)
00151 {
00152 }
00153
00154 CommandCenter::~CommandCenter()
00155 {
00156 }
00157
00158 void CommandCenter::process(ExecutionEnvironment& env)
00159 {
00160 env.getSimulation()->centerSelection(centerTime);
00161 }
00162
00163
00165
00166
00167 CommandFollow::CommandFollow()
00168 {
00169 }
00170
00171 void CommandFollow::process(ExecutionEnvironment& env)
00172 {
00173 env.getSimulation()->follow();
00174 }
00175
00176
00178
00179
00180
00181 CommandSynchronous::CommandSynchronous()
00182 {
00183 }
00184
00185 void CommandSynchronous::process(ExecutionEnvironment& env)
00186 {
00187 env.getSimulation()->geosynchronousFollow();
00188 }
00189
00190
00192
00193
00194 CommandChase::CommandChase()
00195 {
00196 }
00197
00198 void CommandChase::process(ExecutionEnvironment& env)
00199 {
00200 env.getSimulation()->chase();
00201 }
00202
00203
00205
00206
00207 CommandTrack::CommandTrack()
00208 {
00209 }
00210
00211 void CommandTrack::process(ExecutionEnvironment& env)
00212 {
00213 env.getSimulation()->setTrackedObject(env.getSimulation()->getSelection());
00214 }
00215
00216
00218
00219
00220 CommandLock::CommandLock()
00221 {
00222 }
00223
00224 void CommandLock::process(ExecutionEnvironment& env)
00225 {
00226 env.getSimulation()->phaseLock();
00227 }
00228
00229
00230
00232
00233
00234 CommandSetFrame::CommandSetFrame(astro::CoordinateSystem _coordSys,
00235 const string& refName,
00236 const string& targetName) :
00237 coordSys(_coordSys), refObjectName(refName), targetObjectName(targetName)
00238 {
00239 }
00240
00241 void CommandSetFrame::process(ExecutionEnvironment& env)
00242 {
00243 Selection ref = env.getSimulation()->findObjectFromPath(refObjectName);
00244 Selection target;
00245 if (coordSys == astro::PhaseLock)
00246 target = env.getSimulation()->findObjectFromPath(targetObjectName);
00247 env.getSimulation()->setFrame(FrameOfReference(coordSys, ref, target));
00248 }
00249
00250
00252
00253
00254 CommandSetSurface::CommandSetSurface(const string& _surfaceName) :
00255 surfaceName(_surfaceName)
00256 {
00257 }
00258
00259 void CommandSetSurface::process(ExecutionEnvironment& env)
00260 {
00261 env.getSimulation()->getActiveObserver()->setDisplayedSurface(surfaceName);
00262 }
00263
00264
00266
00267
00268
00269 CommandCancel::CommandCancel()
00270 {
00271 }
00272
00273 void CommandCancel::process(ExecutionEnvironment& env)
00274 {
00275 env.getSimulation()->cancelMotion();
00276 env.getSimulation()->setFrame(FrameOfReference());
00277 env.getSimulation()->setTrackedObject(Selection());
00278 }
00279
00280
00282
00283
00284 CommandPrint::CommandPrint(string _text,
00285 int horig, int vorig, int hoff, int voff,
00286 double _duration
00287 ) : text(_text),
00288 hOrigin(horig), vOrigin(vorig),
00289 hOffset(hoff), vOffset(voff),
00290 duration(_duration)
00291 {
00292 }
00293
00294 void CommandPrint::process(ExecutionEnvironment& env)
00295 {
00296 env.showText(text, hOrigin, vOrigin, hOffset, vOffset, duration);
00297 }
00298
00299
00301
00302
00303 CommandClearScreen::CommandClearScreen()
00304 {
00305 }
00306
00307 void CommandClearScreen::process(ExecutionEnvironment& env)
00308 {
00309 }
00310
00311
00313
00314
00315 CommandExit::CommandExit()
00316 {
00317 }
00318
00319 void CommandExit::process(ExecutionEnvironment& env)
00320 {
00321 exit(0);
00322 }
00323
00325
00326
00327 CommandSetTime::CommandSetTime(double _jd) : jd(_jd)
00328 {
00329 }
00330
00331 void CommandSetTime::process(ExecutionEnvironment& env)
00332 {
00333 env.getSimulation()->setTime(jd);
00334 }
00335
00336
00337
00339
00340
00341 CommandSetTimeRate::CommandSetTimeRate(double _rate) : rate(_rate)
00342 {
00343 }
00344
00345 void CommandSetTimeRate::process(ExecutionEnvironment& env)
00346 {
00347 env.getSimulation()->setTimeScale(rate);
00348 }
00349
00350
00352
00353
00354 CommandChangeDistance::CommandChangeDistance(double _duration, double _rate) :
00355 TimedCommand(_duration),
00356 rate(_rate)
00357 {
00358 }
00359
00360 void CommandChangeDistance::process(ExecutionEnvironment& env, double t, double dt)
00361 {
00362 env.getSimulation()->changeOrbitDistance((float) (rate * dt));
00363 }
00364
00365
00367
00368
00369 CommandOrbit::CommandOrbit(double _duration, const Vec3f& axis, float rate) :
00370 TimedCommand(_duration),
00371 spin(axis * rate)
00372 {
00373 }
00374
00375 void CommandOrbit::process(ExecutionEnvironment& env, double t, double dt)
00376 {
00377 float v = spin.length();
00378 if (v != 0.0f)
00379 {
00380 Quatf q;
00381 q.setAxisAngle(spin / v, (float) (v * dt));
00382 env.getSimulation()->orbit(q);
00383 }
00384 }
00385
00386
00387 CommandRotate::CommandRotate(double _duration, const Vec3f& axis, float rate) :
00388 TimedCommand(_duration),
00389 spin(axis * rate)
00390 {
00391 }
00392
00393 void CommandRotate::process(ExecutionEnvironment& env, double t, double dt)
00394 {
00395 float v = spin.length();
00396 if (v != 0.0f)
00397 {
00398 Quatf q;
00399 q.setAxisAngle(spin / v, (float) (v * dt));
00400 env.getSimulation()->rotate(q);
00401 }
00402 }
00403
00404
00405 CommandMove::CommandMove(double _duration, const Vec3d& _velocity) :
00406 TimedCommand(_duration),
00407 velocity(_velocity)
00408 {
00409 }
00410
00411 void CommandMove::process(ExecutionEnvironment& env, double t, double dt)
00412 {
00413 env.getSimulation()->setObserverPosition(env.getSimulation()->getObserver().getPosition() + (velocity * dt));
00414 }
00415
00416
00418
00419
00420 CommandSetPosition::CommandSetPosition(const UniversalCoord& uc) : pos(uc)
00421 {
00422 }
00423
00424 void CommandSetPosition::process(ExecutionEnvironment& env)
00425 {
00426 env.getSimulation()->setObserverPosition(pos);
00427 }
00428
00429
00431
00432
00433 CommandSetOrientation::CommandSetOrientation(const Vec3f& _axis, float _angle) :
00434 axis(_axis), angle(_angle)
00435 {
00436 }
00437
00438 void CommandSetOrientation::process(ExecutionEnvironment& env)
00439 {
00440 Quatf q(1);
00441 q.setAxisAngle(axis, angle);
00442 env.getSimulation()->setObserverOrientation(q);
00443 }
00444
00446
00447
00448 CommandLookBack::CommandLookBack()
00449 {
00450 }
00451
00452 void CommandLookBack::process(ExecutionEnvironment& env)
00453 {
00454 env.getSimulation()->reverseObserverOrientation();
00455 }
00456
00458
00459
00460 CommandRenderFlags::CommandRenderFlags(int _setFlags, int _clearFlags) :
00461 setFlags(_setFlags), clearFlags(_clearFlags)
00462 {
00463 }
00464
00465 void CommandRenderFlags::process(ExecutionEnvironment& env)
00466 {
00467 Renderer* r = env.getRenderer();
00468 if (r != NULL)
00469 {
00470 r->setRenderFlags(r->getRenderFlags() | setFlags);
00471 r->setRenderFlags(r->getRenderFlags() & ~clearFlags);
00472 }
00473 }
00474
00475
00477
00478
00479 CommandLabels::CommandLabels(int _setFlags, int _clearFlags) :
00480 setFlags(_setFlags), clearFlags(_clearFlags)
00481 {
00482 }
00483
00484 void CommandLabels::process(ExecutionEnvironment& env)
00485 {
00486 Renderer* r = env.getRenderer();
00487 if (r != NULL)
00488 {
00489 r->setLabelMode(r->getLabelMode() | setFlags);
00490 r->setLabelMode(r->getLabelMode() & ~clearFlags);
00491 }
00492 }
00493
00494
00496
00497
00498 CommandOrbitFlags::CommandOrbitFlags(int _setFlags, int _clearFlags) :
00499 setFlags(_setFlags), clearFlags(_clearFlags)
00500 {
00501 }
00502
00503 void CommandOrbitFlags::process(ExecutionEnvironment& env)
00504 {
00505 Renderer* r = env.getRenderer();
00506 if (r != NULL)
00507 {
00508 r->setOrbitMask(r->getOrbitMask() | setFlags);
00509 r->setOrbitMask(r->getOrbitMask() & ~clearFlags);
00510 }
00511 }
00512
00513
00514
00516
00517
00518 CommandSetVisibilityLimit::CommandSetVisibilityLimit(double mag) :
00519 magnitude(mag)
00520 {
00521 }
00522
00523 void CommandSetVisibilityLimit::process(ExecutionEnvironment& env)
00524 {
00525 Renderer* r = env.getRenderer();
00526 if (r != NULL)
00527 {
00528 r->setBrightnessBias(0.05f);
00529 r->setSaturationMagnitude(1.0f);
00530 }
00531 env.getSimulation()->setFaintestVisible((float) magnitude);
00532 }
00534
00535
00536 CommandSetFaintestAutoMag45deg::CommandSetFaintestAutoMag45deg(double mag) :
00537 magnitude(mag)
00538 {
00539 }
00540
00541 void CommandSetFaintestAutoMag45deg::process(ExecutionEnvironment& env)
00542 {
00543 Renderer* r = env.getRenderer();
00544 if (r != NULL)
00545 r->setFaintestAM45deg((float) magnitude);
00546 }
00547
00549
00550
00551 CommandSetAmbientLight::CommandSetAmbientLight(float level) :
00552 lightLevel(level)
00553 {
00554 }
00555
00556 void CommandSetAmbientLight::process(ExecutionEnvironment& env)
00557 {
00558 Renderer* r = env.getRenderer();
00559 if (r != NULL)
00560 r->setAmbientLightLevel(lightLevel);
00561 }
00562
00563
00565
00566
00567 CommandSet::CommandSet(const std::string& _name, double _value) :
00568 name(_name), value(_value)
00569 {
00570 }
00571
00572 void CommandSet::process(ExecutionEnvironment& env)
00573 {
00574 if (compareIgnoringCase(name, "MinOrbitSize") == 0)
00575 {
00576 if (env.getRenderer() != NULL)
00577 env.getRenderer()->setMinimumOrbitSize((float) value);
00578 }
00579 else if (compareIgnoringCase(name, "AmbientLightLevel") == 0)
00580 {
00581 if (env.getRenderer() != NULL)
00582 env.getRenderer()->setAmbientLightLevel((float) value);
00583 }
00584 else if (compareIgnoringCase(name, "FOV") == 0)
00585 {
00586 if (env.getRenderer() != NULL)
00587 env.getSimulation()->getActiveObserver()->setFOV(degToRad((float) value));
00588 }
00589 else if (compareIgnoringCase(name, "StarDistanceLimit") == 0)
00590 {
00591 if (env.getRenderer() != NULL)
00592 env.getRenderer()->setDistanceLimit((float) value);
00593 }
00594 else if (compareIgnoringCase(name, "StarStyle") == 0)
00595 {
00596
00597
00598
00599 if (env.getRenderer() != NULL)
00600 env.getRenderer()->setStarStyle((Renderer::StarStyle) (int) value);
00601 }
00602 }
00603
00604
00606
00607
00608 CommandMark::CommandMark(const string& _target, Color _color, float _size,
00609 Marker::Symbol _symbol) :
00610 target(_target),
00611 color(_color),
00612 size(_size),
00613 symbol(_symbol)
00614 {
00615 }
00616
00617 void CommandMark::process(ExecutionEnvironment& env)
00618 {
00619 Selection sel = env.getSimulation()->findObjectFromPath(target);
00620 if (sel.empty())
00621 return;
00622
00623 if (env.getSimulation()->getUniverse() != NULL)
00624 env.getSimulation()->getUniverse()->markObject(sel, size, color, symbol, 1);
00625 }
00626
00627
00628
00630
00631
00632 CommandUnmark::CommandUnmark(const string& _target) :
00633 target(_target)
00634 {
00635 }
00636
00637 void CommandUnmark::process(ExecutionEnvironment& env)
00638 {
00639 Selection sel = env.getSimulation()->findObjectFromPath(target);
00640 if (sel.empty())
00641 return;
00642
00643 if (env.getSimulation()->getUniverse() != NULL)
00644 env.getSimulation()->getUniverse()->unmarkObject(sel, 1);
00645 }
00646
00647
00648
00650
00651
00652 CommandUnmarkAll::CommandUnmarkAll()
00653 {
00654 }
00655
00656 void CommandUnmarkAll::process(ExecutionEnvironment& env)
00657 {
00658 if (env.getSimulation()->getUniverse() != NULL)
00659 env.getSimulation()->getUniverse()->unmarkAll();
00660 }
00661
00662
00664
00665
00666 CommandPreloadTextures::CommandPreloadTextures(const string& _name) :
00667 name(_name)
00668 {
00669 }
00670
00671 void CommandPreloadTextures::process(ExecutionEnvironment& env)
00672 {
00673 Selection target = env.getSimulation()->findObjectFromPath(name);
00674 if (target.body() == NULL)
00675 return;
00676
00677 if (env.getRenderer() != NULL)
00678 env.getRenderer()->loadTextures(target.body());
00679 }
00680
00681
00683
00684
00685 CommandCapture::CommandCapture(const std::string& _type,
00686 const std::string& _filename) : type(_type), filename(_filename)
00687 {
00688 }
00689
00690 void CommandCapture::process(ExecutionEnvironment& env)
00691 {
00692 bool success = false;
00693 #ifndef MACOSX
00694
00695
00696 int viewport[4];
00697 glGetIntegerv(GL_VIEWPORT, viewport);
00698
00699 if (compareIgnoringCase(type, "jpeg") == 0)
00700 {
00701 success = CaptureGLBufferToJPEG(filename,
00702 viewport[0], viewport[1],
00703 viewport[2], viewport[3]);
00704 }
00705 if (compareIgnoringCase(type, "png") == 0)
00706 {
00707 success = CaptureGLBufferToPNG(filename,
00708 viewport[0], viewport[1],
00709 viewport[2], viewport[3]);
00710 }
00711 #endif
00712 }
00713
00714
00716
00717
00718 CommandRenderPath::CommandRenderPath(GLContext::GLRenderPath _path) :
00719 path(_path)
00720 {
00721 }
00722
00723 void CommandRenderPath::process(ExecutionEnvironment& env)
00724 {
00725 GLContext* context = env.getRenderer()->getGLContext();
00726
00727 if (context != NULL)
00728 {
00729 context->setRenderPath(path);
00730 env.getCelestiaCore()->notifyWatchers(CelestiaCore::RenderFlagsChanged);
00731 }
00732 }
00733
00734
00736
00737
00738 RepeatCommand::RepeatCommand(CommandSequence* _body, int _repeatCount) :
00739 body(_body),
00740 repeatCount(_repeatCount),
00741 bodyDuration(0.0),
00742 execution(NULL)
00743 {
00744 for (CommandSequence::const_iterator iter = body->begin();
00745 iter != body->end(); iter++)
00746 {
00747 bodyDuration += (*iter)->getDuration();
00748 }
00749 }
00750
00751 RepeatCommand::~RepeatCommand()
00752 {
00753 if (execution != NULL)
00754 delete execution;
00755
00756 }
00757
00758 void RepeatCommand::process(ExecutionEnvironment& env, double t, double dt)
00759 {
00760 double t0 = t - dt;
00761 int loop0 = (int) (t0 / bodyDuration);
00762 int loop1 = (int) (t / bodyDuration);
00763
00764
00765
00766 if (execution == NULL)
00767 execution = new Execution(*body, env);
00768
00769 if (loop0 == loop1)
00770 {
00771 execution->tick(dt);
00772 }
00773 else
00774 {
00775 double timeLeft = (loop0 + 1) * bodyDuration - t0;
00776 execution->tick(timeLeft);
00777
00778 for (int i = loop0 + 1; i < loop1; i++)
00779 {
00780 execution->reset(*body);
00781 execution->tick(bodyDuration);
00782 }
00783
00784 execution->reset(*body);
00785 execution->tick(t - loop1 * bodyDuration);
00786 }
00787 }
00788
00789 double RepeatCommand::getDuration()
00790 {
00791 return bodyDuration * repeatCount;
00792 }