]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/redout.cxx
Fix native protocol crashes.
[flightgear.git] / src / Scenery / redout.cxx
1 // redout.hxx
2 //
3 // Written by Mathias Froehlich,
4 //
5 // Copyright (C) 2007  Mathias Froehlich
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include "redout.hxx"
27
28 #include <assert.h>
29
30 #include <osg/BlendFunc>
31 #include <osg/Depth>
32 #include <osg/Geometry>
33 #include <osg/Geode>
34 #include <osg/Switch>
35 #include <osg/StateSet>
36
37 #include <simgear/props/props.hxx>
38 #include <Main/fg_props.hxx>
39
40 class FGRedoutCallback : public osg::NodeCallback {
41 public:
42   FGRedoutCallback(osg::Vec4Array* colorArray) :
43     _colorArray(colorArray),
44     _redoutNode(fgGetNode("/sim/rendering/redout", true))
45   {
46     fgGetNode("/sim/rendering/redout/alpha", true);
47   }
48   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
49   {
50     assert(dynamic_cast<osg::Switch*>(node));
51     osg::Switch* sw = static_cast<osg::Switch*>(node);
52
53     // Check if we need to do something further ...
54     float alpha = _redoutNode->getFloatValue("alpha", 0);
55     bool enabled = (0 < alpha);
56     sw->setValue(0, enabled);
57     if (!enabled)
58       return;
59
60     (*_colorArray)[0][0] = _redoutNode->getFloatValue("red", 1);
61     (*_colorArray)[0][1] = _redoutNode->getFloatValue("green", 0);
62     (*_colorArray)[0][2] = _redoutNode->getFloatValue("blue", 0);
63     (*_colorArray)[0][3] = alpha;
64     _colorArray->dirty();
65   }
66 private:
67   osg::ref_ptr<osg::Vec4Array> _colorArray;
68   SGSharedPtr<SGPropertyNode> _redoutNode;
69 };
70
71 osg::Node* FGCreateRedoutNode()
72 {
73   osg::Geometry* geometry = new osg::Geometry;
74   geometry->setUseDisplayList(false);
75
76   osg::StateSet* stateSet = geometry->getOrCreateStateSet();
77   stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
78   stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
79   stateSet->setAttribute(new osg::BlendFunc);
80   stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
81   stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
82   stateSet->setAttribute(new osg::Depth(osg::Depth::ALWAYS, 0, 1, false));
83   stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
84
85   osg::Vec3Array* vertexArray = new osg::Vec3Array;
86   vertexArray->push_back(osg::Vec3(-1, -1, 0));
87   vertexArray->push_back(osg::Vec3( 1, -1, 0));
88   vertexArray->push_back(osg::Vec3( 1,  1, 0));
89   vertexArray->push_back(osg::Vec3(-1,  1, 0));
90   geometry->setVertexArray(vertexArray);
91   osg::Vec4Array* colorArray = new osg::Vec4Array;
92   colorArray->push_back(osg::Vec4(1, 0, 0, 1));
93   geometry->setColorArray(colorArray);
94   geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
95   geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, 0, 4));
96
97   osg::Geode* geode = new osg::Geode;
98   geode->addDrawable(geometry);
99
100   osg::Camera* camera = new osg::Camera;
101   camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
102   camera->setProjectionMatrix(osg::Matrix::ortho2D(-1, 1, -1, 1));
103   camera->setViewMatrix(osg::Matrix::identity());
104   camera->setRenderOrder(osg::Camera::POST_RENDER, 99);
105   camera->setClearMask(0);
106   camera->setAllowEventFocus(false);
107   camera->setCullingActive(false);
108   camera->addChild(geode);
109
110   osg::Switch* sw = new osg::Switch;
111   sw->setUpdateCallback(new FGRedoutCallback(colorArray));
112   sw->addChild(camera);
113
114   return sw;
115 }
116