1 // Viewer.hxx -- alternative flightgear viewer application
3 // Copyright (C) 2009 - 2012 Mathias Froehlich
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.
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.
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.
19 #ifndef _FGVIEWER_VIEWER_HXX
20 #define _FGVIEWER_VIEWER_HXX
22 #include <osg/ArgumentParser>
23 #include <osgViewer/Viewer>
25 #include <simgear/math/SGMath.hxx>
26 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
27 #include <simgear/timing/timestamp.hxx>
28 #include <simgear/props/props.hxx>
30 #include "Drawable.hxx"
31 #include "Frustum.hxx"
32 #include "Renderer.hxx"
33 #include "SlaveCamera.hxx"
36 #include "HLAViewerFederate.hxx"
41 class Viewer : public osgViewer::Viewer {
43 Viewer(osg::ArgumentParser& arguments);
46 bool readCameraConfig(const SGPropertyNode& viewerNode);
47 void setupDefaultCameraConfigIfUnset();
48 /// Short circuit osg config files.
49 virtual bool readConfiguration(const std::string& filename);
51 /// Callback class that cares for the camera and drawable setup
52 void setRenderer(Renderer* renderer);
53 Renderer* getRenderer();
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;
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;
69 /// Realize the contexts and attach the cameras there
70 virtual void realize();
71 bool realizeDrawables();
72 bool realizeSlaveCameras();
75 virtual void advance(double simTime);
76 virtual void updateTraversal();
77 bool updateSlaveCameras();
79 /// Store this per viewer instead of global.
80 void setReaderWriterOptions(simgear::SGReaderWriterOptions* readerWriterOptions);
81 simgear::SGReaderWriterOptions* getReaderWriterOptions();
83 /// Puts the scene data under the renderer.
84 /// Replaces the whole renderer independent scene.
85 virtual void setSceneData(osg::Node* node);
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();
94 /// Traverse the scenegraph and throw out all child nodes that can be loaded again.
95 void purgeLevelOfDetailNodes();
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);
109 /// The federate if configured, can only be set once
110 const HLAViewerFederate* getViewerFederate() const;
111 HLAViewerFederate* getViewerFederate();
112 void setViewerFederate(HLAViewerFederate* viewerFederate);
116 Viewer(const Viewer&);
117 Viewer& operator=(const Viewer&);
120 class _PurgeLevelOfDetailNodesVisitor;
122 /// Under linux make sure that the screen saver does not jump in
123 class _ResetScreenSaverSwapCallback;
126 /// The renderer used to setup the higher level camera/scenegraph structure
127 SGSharedPtr<Renderer> _renderer;
129 /// The drawables managed by this viewer.
130 typedef std::vector<SGSharedPtr<Drawable> > DrawableVector;
131 DrawableVector _drawableVector;
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;
138 /// The top level options struct
139 osg::ref_ptr<simgear::SGReaderWriterOptions> _readerWriterOptions;
141 /// The top level scenegraph structure that is used for drawing
142 osg::ref_ptr<osg::Group> _sceneDataGroup;
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;
151 /// The federate if configured
152 SGSharedPtr<HLAViewerFederate> _viewerFederate;
156 } // namespace fgviewer