]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/Renderer.cxx
fgviewer: Import hla based viewer application.
[flightgear.git] / utils / fgviewer / Renderer.cxx
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 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include "Renderer.hxx"
24
25 #include <osgViewer/Renderer>
26 #include <simgear/scene/material/EffectCullVisitor.hxx>
27
28 #include "Drawable.hxx"
29 #include "Viewer.hxx"
30 #include "SlaveCamera.hxx"
31
32 namespace fgviewer  {
33
34 // FIXME I understand why we currently need this, but this seems like
35 // weird to require an own cull visitor.
36 static void installCullVisitor(osg::Camera& camera)
37 {
38     osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(camera.getRenderer());
39     for (int i = 0; i < 2; ++i) {
40         osgUtil::SceneView* sceneView = renderer->getSceneView(i);
41         osg::ref_ptr<osgUtil::CullVisitor::Identifier> identifier;
42
43         identifier = sceneView->getCullVisitor()->getIdentifier();
44         sceneView->setCullVisitor(new simgear::EffectCullVisitor);
45         sceneView->getCullVisitor()->setIdentifier(identifier.get());
46
47         identifier = sceneView->getCullVisitorLeft()->getIdentifier();
48         sceneView->setCullVisitorLeft(sceneView->getCullVisitor()->clone());
49         sceneView->getCullVisitorLeft()->setIdentifier(identifier.get());
50
51         identifier = sceneView->getCullVisitorRight()->getIdentifier();
52         sceneView->setCullVisitorRight(sceneView->getCullVisitor()->clone());
53         sceneView->getCullVisitorRight()->setIdentifier(identifier.get());
54     }
55 }
56
57 class Renderer::_SlaveCamera : public SlaveCamera {
58 public:
59     _SlaveCamera(const std::string& name) :
60         SlaveCamera(name)
61     { 
62     }
63     virtual ~_SlaveCamera()
64     {
65     }
66     virtual osg::Camera* _realizeImplementation(Viewer& viewer)
67     {
68         osg::Camera* camera = SlaveCamera::_realizeImplementation(viewer);
69         if (!camera)
70             return 0;
71
72         camera->addChild(viewer.getSceneDataGroup());
73
74         viewer.addSlave(camera, osg::Matrix::identity(), getViewOffset(), false /*useMastersSceneData*/);
75         installCullVisitor(*camera);
76     
77         return camera;
78     }
79     virtual bool _updateImplementation(Viewer& viewer)
80     {
81         getCamera()->setViewMatrix(getEffectiveViewOffset(viewer));
82         getCamera()->setProjectionMatrix(getEffectiveFrustum(viewer).getMatrix(osg::Vec2(10, 8e4)));
83         return SlaveCamera::_updateImplementation(viewer);
84     }
85 };
86
87 Renderer::Renderer()
88 {
89 }
90
91 Renderer::~Renderer()
92 {
93 }
94
95 Drawable*
96 Renderer::createDrawable(Viewer&, const std::string& name)
97 {
98     return new Drawable(name);
99 }
100
101 SlaveCamera*
102 Renderer::createSlaveCamera(Viewer&, const std::string& name)
103 {
104     return new _SlaveCamera(name);
105 }
106
107 bool
108 Renderer::realize(Viewer& viewer)
109 {
110     if (!viewer.realizeDrawables())
111         return false;
112     return viewer.realizeSlaveCameras();
113 }
114
115 bool
116 Renderer::update(Viewer& viewer)
117 {
118     return viewer.updateSlaveCameras();
119 }
120
121 } // namespace fgviewer