Renderer: get rid of static scene references.
[fg:hoorays-flightgear.git] / src / Viewer / renderer.hxx
1
2 #ifndef __FG_RENDERER_HXX
3 #define __FG_RENDERER_HXX 1
4
5 #include <simgear/scene/util/SGPickCallback.hxx>
6 #include <simgear/props/props.hxx>
7 #include <simgear/timing/timestamp.hxx>
8
9 #include <osg/ref_ptr>
10 #include <osg/Matrix>
11 #include <osg/Vec3>
12
13 #include "renderingpipeline.hxx"
14
15 namespace osg
16 {
17 class Camera;
18 class Group;
19 class GraphicsContext;
20 }
21
22 namespace osgGA
23 {
24 class GUIEventAdapter;
25 }
26
27 namespace osgShadow
28 {
29 class ShadowedScene;
30 }
31
32 namespace osgViewer
33 {
34 class Viewer;
35 }
36
37 namespace flightgear
38 {
39 class FGEventHandler;
40 struct CameraInfo;
41 class CameraGroup;
42 }
43
44 class SGSky;
45 class SGUpdateVisitor;
46
47 class FGRenderer {
48
49 public:
50
51     FGRenderer();
52     ~FGRenderer();
53
54     void splashinit();
55     void init();
56
57     void setupView();
58
59     void resize(int width, int height );
60
61     void update();
62   
63     /** Just pick into the scene and return the pick callbacks on the way ...
64      */
65     bool pick( std::vector<SGSceneryPick>& pickList, const osg::Vec2& windowPos);
66
67     /** Get and set the OSG Viewer object, if any.
68      */
69     osgViewer::Viewer* getViewer() { return viewer.get(); }
70     const osgViewer::Viewer* getViewer() const { return viewer.get(); }
71     void setViewer(osgViewer::Viewer* viewer);
72     /** Get and set the manipulator object, if any.
73      */
74     flightgear::FGEventHandler* getEventHandler() { return eventHandler.get(); }
75     const flightgear::FGEventHandler* getEventHandler() const { return eventHandler.get(); }
76     void setEventHandler(flightgear::FGEventHandler* manipulator);
77
78     /** Add a top level camera.
79      */
80     void addCamera(osg::Camera* camera, bool useSceneData);
81
82     void removeCamera(osg::Camera* camera);
83   
84     /** Add a camera to the group. The camera is added to the viewer
85      * as a slave. See osgViewer::Viewer::addSlave.
86      * @param flags properties of the camera; see CameraGroup::Flags
87      * @param projection slave projection matrix
88      * @param view slave view matrix
89      * @param useMasterSceneData whether the camera displays the
90      * viewer's scene data.
91      * @return a CameraInfo object for the camera.
92      */
93         flightgear::CameraInfo* buildRenderingPipeline(flightgear::CameraGroup* cgroup, unsigned flags, osg::Camera* camera,
94                                    const osg::Matrix& view,
95                                    const osg::Matrix& projection,
96                                                                    osg::GraphicsContext* gc,
97                                    bool useMasterSceneData);
98
99         /**
100          */
101         flightgear::CameraInfo* buildClassicalPipeline(flightgear::CameraGroup* cgroup, unsigned flags, osg::Camera* camera,
102                                    const osg::Matrix& view,
103                                    const osg::Matrix& projection,
104                                    bool useMasterSceneData);
105
106         /**
107          */
108         flightgear::CameraInfo* buildDeferredPipeline(flightgear::CameraGroup* cgroup, unsigned flags, osg::Camera* camera,
109                                    const osg::Matrix& view, const osg::Matrix& projection, osg::GraphicsContext* gc);
110
111     void updateShadowCamera(const flightgear::CameraInfo* info, const osg::Vec3d& position);
112     void updateShadowMapSize(int mapSize);
113     void enableShadows(bool enabled);
114     void updateCascadeFar(int index, float far_m);
115     void updateCascadeNumber(size_t num);
116
117     SGSky* getSky() const { return _sky; }
118
119         void setPlanes( double zNear, double zFar );
120
121 protected:
122     osg::ref_ptr<osgViewer::Viewer> viewer;
123     osg::ref_ptr<flightgear::FGEventHandler> eventHandler;
124     
125     osg::ref_ptr<osg::FrameStamp> _frameStamp;
126     osg::ref_ptr<SGUpdateVisitor> _updateVisitor;
127     osg::ref_ptr<osg::Group> _viewerSceneRoot;
128     osg::ref_ptr<osg::Group> _deferredRealRoot;
129     osg::ref_ptr<osg::Group> _root;
130     
131     SGPropertyNode_ptr _scenery_loaded, _position_finalized;
132     
133     
134     SGPropertyNode_ptr _skyblend, _splash_alpha;
135     SGPropertyNode_ptr _point_sprites, _enhanced_lighting, _distance_attenuation;
136     SGPropertyNode_ptr _textures;
137     SGPropertyNode_ptr _cloud_status, _visibility_m; 
138     SGPropertyNode_ptr _xsize, _ysize;
139     SGPropertyNode_ptr _panel_hotspots, _sim_delta_sec, _horizon_effect, _altitude_ft;
140     SGPropertyNode_ptr _virtual_cockpit;
141     SGTimeStamp _splash_time;
142     SGSky* _sky;
143     bool _classicalRenderer;
144     std::string _renderer;
145     int _shadowMapSize;
146     size_t _numCascades;
147     float _cascadeFar[4];
148     bool _useColorForDepth;
149
150     typedef std::vector<SGPropertyChangeListener*> SGPropertyChangeListenerVec;
151     SGPropertyChangeListenerVec _listeners;
152     
153     flightgear::CameraInfo* buildCameraFromRenderingPipeline(FGRenderingPipeline* rpipe, flightgear::CameraGroup* cgroup, unsigned flags, osg::Camera* camera,
154                                         const osg::Matrix& view, const osg::Matrix& projection, osg::GraphicsContext* gc);
155
156     void buildBuffers(FGRenderingPipeline* rpipe, flightgear::CameraInfo* info);
157     void buildStage(flightgear::CameraInfo* info, FGRenderingPipeline::Stage* stage, flightgear::CameraGroup* cgroup, osg::Camera* mainCamera, const osg::Matrix& view, const osg::Matrix& projection, osg::GraphicsContext* gc);
158     osg::Node* buildPass(flightgear::CameraInfo* info, FGRenderingPipeline::Pass* pass);
159     osg::Node* buildLightingSkyCloudsPass(FGRenderingPipeline::Pass* pass);
160     osg::Node* buildLightingLightsPass(flightgear::CameraInfo* info, FGRenderingPipeline::Pass* pass);
161
162     osg::Camera* buildDeferredGeometryCamera( flightgear::CameraInfo* info, osg::GraphicsContext* gc, const std::string& name, const FGRenderingPipeline::AttachmentList& attachments );
163     osg::Camera* buildDeferredShadowCamera( flightgear::CameraInfo* info, osg::GraphicsContext* gc, const std::string& name, const FGRenderingPipeline::AttachmentList& attachments );
164     osg::Camera* buildDeferredLightingCamera( flightgear::CameraInfo* info, osg::GraphicsContext* gc, const FGRenderingPipeline::Stage* stage );
165     osg::Camera* buildDeferredFullscreenCamera( flightgear::CameraInfo* info, osg::GraphicsContext* gc, const FGRenderingPipeline::Stage* stage );
166     osg::Camera* buildDeferredFullscreenCamera( flightgear::CameraInfo* info, const FGRenderingPipeline::Pass* pass );
167     void buildDeferredDisplayCamera( osg::Camera* camera, flightgear::CameraInfo* info, const FGRenderingPipeline::Stage* stage, osg::GraphicsContext* gc );
168
169     void updateShadowCascade(const flightgear::CameraInfo* info, osg::Camera* camera, osg::Group* grp, int idx, double left, double right, double bottom, double top, double zNear, double f1, double f2);
170     osg::Vec3 getSunDirection() const;
171     osg::ref_ptr<osg::Uniform> _ambientFactor;
172     osg::ref_ptr<osg::Uniform> _sunDiffuse;
173     osg::ref_ptr<osg::Uniform> _sunSpecular;
174     osg::ref_ptr<osg::Uniform> _sunDirection;
175     osg::ref_ptr<osg::Uniform> _planes;
176     osg::ref_ptr<osg::Uniform> _fogColor;
177     osg::ref_ptr<osg::Uniform> _fogDensity;
178     osg::ref_ptr<osg::Uniform> _shadowNumber;
179     osg::ref_ptr<osg::Uniform> _shadowDistances;
180     osg::ref_ptr<osg::Uniform> _depthInColor;
181
182     osg::ref_ptr<FGRenderingPipeline> _pipeline;
183     
184     void addChangeListener(SGPropertyChangeListener* l, const char* path);
185 };
186
187 bool fgDumpSceneGraphToFile(const char* filename);
188 bool fgDumpTerrainBranchToFile(const char* filename);
189
190 namespace flightgear
191 {
192 bool printVisibleSceneInfo(FGRenderer* renderer);
193 }
194
195 #endif