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.
23 #include "Renderer.hxx"
25 #include <osgViewer/Renderer>
26 #include <simgear/scene/material/EffectCullVisitor.hxx>
28 #include "Drawable.hxx"
30 #include "SlaveCamera.hxx"
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)
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;
43 identifier = sceneView->getCullVisitor()->getIdentifier();
44 sceneView->setCullVisitor(new simgear::EffectCullVisitor);
45 sceneView->getCullVisitor()->setIdentifier(identifier.get());
47 identifier = sceneView->getCullVisitorLeft()->getIdentifier();
48 sceneView->setCullVisitorLeft(sceneView->getCullVisitor()->clone());
49 sceneView->getCullVisitorLeft()->setIdentifier(identifier.get());
51 identifier = sceneView->getCullVisitorRight()->getIdentifier();
52 sceneView->setCullVisitorRight(sceneView->getCullVisitor()->clone());
53 sceneView->getCullVisitorRight()->setIdentifier(identifier.get());
57 class Renderer::_SlaveCamera : public SlaveCamera {
59 _SlaveCamera(const std::string& name) :
63 virtual ~_SlaveCamera()
66 virtual osg::Camera* _realizeImplementation(Viewer& viewer)
68 osg::Camera* camera = SlaveCamera::_realizeImplementation(viewer);
72 camera->addChild(viewer.getSceneDataGroup());
74 viewer.addSlave(camera, osg::Matrix::identity(), getViewOffset(), false /*useMastersSceneData*/);
75 installCullVisitor(*camera);
79 virtual bool _updateImplementation(Viewer& viewer)
81 getCamera()->setViewMatrix(getEffectiveViewOffset(viewer));
82 getCamera()->setProjectionMatrix(getEffectiveFrustum(viewer).getMatrix(osg::Vec2(10, 8e4)));
83 return SlaveCamera::_updateImplementation(viewer);
96 Renderer::createDrawable(Viewer&, const std::string& name)
98 return new Drawable(name);
102 Renderer::createSlaveCamera(Viewer&, const std::string& name)
104 return new _SlaveCamera(name);
108 Renderer::realize(Viewer& viewer)
110 if (!viewer.realizeDrawables())
112 return viewer.realizeSlaveCameras();
116 Renderer::update(Viewer& viewer)
118 return viewer.updateSlaveCameras();
121 } // namespace fgviewer