| 1 | // Viewer.hxx -- alternative flightgear viewer application |
| 2 | // |
| 3 | // Copyright (C) 2009 - 2012 Mathias Froehlich |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or |
| 6 | // modify it under the terms of the GNU General Public License as |
| 7 | // published by the Free Software Foundation; either version 2 of the |
| 8 | // License, or (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, but |
| 11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | // General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | |
| 19 | #ifndef _FGVIEWER_VIEWER_HXX |
| 20 | #define _FGVIEWER_VIEWER_HXX |
| 21 | |
| 22 | #include <osgViewer/Viewer> |
| 23 | |
| 24 | #include <simgear/math/SGMath.hxx> |
| 25 | #include <simgear/scene/util/SGReaderWriterOptions.hxx> |
| 26 | #include <simgear/timing/timestamp.hxx> |
| 27 | #include <simgear/props/props.hxx> |
| 28 | |
| 29 | #include "Drawable.hxx" |
| 30 | #include "Frustum.hxx" |
| 31 | #include "Renderer.hxx" |
| 32 | #include "SlaveCamera.hxx" |
| 33 | #include "ArgumentParser.hxx" |
| 34 | |
| 35 | #if FG_HAVE_HLA |
| 36 | #include "HLAViewerFederate.hxx" |
| 37 | #endif |
| 38 | |
| 39 | namespace fgviewer { |
| 40 | |
| 41 | class Viewer : public osgViewer::Viewer { |
| 42 | public: |
| 43 | Viewer(ArgumentParser& arguments); |
| 44 | virtual ~Viewer(); |
| 45 | |
| 46 | bool readCameraConfig(const SGPropertyNode& viewerNode); |
| 47 | void setupDefaultCameraConfigIfUnset(); |
| 48 | /// Short circuit osg config files. |
| 49 | virtual bool readConfiguration(const std::string& filename); |
| 50 | |
| 51 | /// Callback class that cares for the camera and drawable setup |
| 52 | void setRenderer(Renderer* renderer); |
| 53 | Renderer* getRenderer(); |
| 54 | |
| 55 | /// Access and create drawables |
| 56 | Drawable* getOrCreateDrawable(const std::string& name); |
| 57 | Drawable* getDrawable(const std::string& name); |
| 58 | unsigned getDrawableIndex(const std::string& name); |
| 59 | Drawable* getDrawable(unsigned index); |
| 60 | unsigned getNumDrawables() const; |
| 61 | |
| 62 | /// Access and create slave cameras |
| 63 | SlaveCamera* getOrCreateSlaveCamera(const std::string& name); |
| 64 | SlaveCamera* getSlaveCamera(const std::string& name); |
| 65 | unsigned getSlaveCameraIndex(const std::string& name); |
| 66 | SlaveCamera* getSlaveCamera(unsigned index); |
| 67 | unsigned getNumSlaveCameras() const; |
| 68 | |
| 69 | /// Realize the contexts and attach the cameras there |
| 70 | virtual void realize(); |
| 71 | bool realizeDrawables(); |
| 72 | bool realizeSlaveCameras(); |
| 73 | |
| 74 | /// exec methods |
| 75 | virtual void advance(double simTime); |
| 76 | virtual void updateTraversal(); |
| 77 | bool updateSlaveCameras(); |
| 78 | |
| 79 | /// Store this per viewer instead of global. |
| 80 | void setReaderWriterOptions(simgear::SGReaderWriterOptions* readerWriterOptions); |
| 81 | simgear::SGReaderWriterOptions* getReaderWriterOptions(); |
| 82 | |
| 83 | /// Puts the scene data under the renderer. |
| 84 | /// Replaces the whole renderer independent scene. |
| 85 | virtual void setSceneData(osg::Node* node); |
| 86 | |
| 87 | /// Adds the scene node to the global scene |
| 88 | /// Use this to add something to the displayed scene. |
| 89 | void insertSceneData(osg::Node* node); |
| 90 | bool insertSceneData(const std::string& fileName, const osgDB::Options* options = 0); |
| 91 | /// Return the scene data group. |
| 92 | osg::Group* getSceneDataGroup(); |
| 93 | |
| 94 | /// Traverse the scenegraph and throw out all child nodes that can be loaded again. |
| 95 | void purgeLevelOfDetailNodes(); |
| 96 | |
| 97 | /// Return a default screen identifier. Under UNIX the default DISPLAY environment variable. |
| 98 | static osg::GraphicsContext::ScreenIdentifier getDefaultScreenIdentifier(); |
| 99 | /// Interpret the given display. Is mergend with the default screen identifier. |
| 100 | static osg::GraphicsContext::ScreenIdentifier getScreenIdentifier(const std::string& display); |
| 101 | /// Return screen settings, mostly the resolution of the given screen. |
| 102 | osg::GraphicsContext::ScreenSettings getScreenSettings(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier); |
| 103 | /// Return traits struct for the given screen identifier. The size and position is already set up for a fullscreen window. |
| 104 | osg::GraphicsContext::Traits* getTraits(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier); |
| 105 | /// Helper to create an new graphics context from traits. |
| 106 | osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits); |
| 107 | |
| 108 | #if FG_HAVE_HLA |
| 109 | /// The federate if configured, can only be set once |
| 110 | const HLAViewerFederate* getViewerFederate() const; |
| 111 | HLAViewerFederate* getViewerFederate(); |
| 112 | void setViewerFederate(HLAViewerFederate* viewerFederate); |
| 113 | #endif |
| 114 | |
| 115 | private: |
| 116 | Viewer(const Viewer&); |
| 117 | Viewer& operator=(const Viewer&); |
| 118 | |
| 119 | /// Unload all lod's |
| 120 | class _PurgeLevelOfDetailNodesVisitor; |
| 121 | #ifdef __linux__ |
| 122 | /// Under linux make sure that the screen saver does not jump in |
| 123 | class _ResetScreenSaverSwapCallback; |
| 124 | #endif |
| 125 | |
| 126 | /// The renderer used to setup the higher level camera/scenegraph structure |
| 127 | SGSharedPtr<Renderer> _renderer; |
| 128 | |
| 129 | /// The drawables managed by this viewer. |
| 130 | typedef std::vector<SGSharedPtr<Drawable> > DrawableVector; |
| 131 | DrawableVector _drawableVector; |
| 132 | |
| 133 | /// The slave cameras for this viewer. |
| 134 | /// Since we support more complex renderers, we can not only use osg::View::Slave. |
| 135 | typedef std::vector<SGSharedPtr<SlaveCamera> > SlaveCameraVector; |
| 136 | SlaveCameraVector _slaveCameraVector; |
| 137 | |
| 138 | /// The top level options struct |
| 139 | osg::ref_ptr<simgear::SGReaderWriterOptions> _readerWriterOptions; |
| 140 | |
| 141 | /// The top level scenegraph structure that is used for drawing |
| 142 | osg::ref_ptr<osg::Group> _sceneDataGroup; |
| 143 | |
| 144 | /// Stores the time increment for each frame. |
| 145 | /// If zero, the time advance is done to the current real time. |
| 146 | SGTimeStamp _timeIncrement; |
| 147 | /// The current simulation time of the viewer |
| 148 | SGTimeStamp _simTime; |
| 149 | |
| 150 | #if FG_HAVE_HLA |
| 151 | /// The federate if configured |
| 152 | SGSharedPtr<HLAViewerFederate> _viewerFederate; |
| 153 | #endif |
| 154 | }; |
| 155 | |
| 156 | } // namespace fgviewer |
| 157 | |
| 158 | #endif |