| 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 | stateSet->setRenderBinDetails(1000, "RenderBin"); |
| 85 | |
| 86 | osg::Vec3Array* vertexArray = new osg::Vec3Array; |
| 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 | vertexArray->push_back(osg::Vec3(-1, 1, 0)); |
| 91 | geometry->setVertexArray(vertexArray); |
| 92 | osg::Vec4Array* colorArray = new osg::Vec4Array; |
| 93 | colorArray->push_back(osg::Vec4(1, 0, 0, 1)); |
| 94 | geometry->setColorArray(colorArray); |
| 95 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
| 96 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, 0, 4)); |
| 97 | |
| 98 | osg::Geode* geode = new osg::Geode; |
| 99 | geode->addDrawable(geometry); |
| 100 | |
| 101 | osg::Camera* camera = new osg::Camera; |
| 102 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
| 103 | camera->setProjectionMatrix(osg::Matrix::ortho2D(-1, 1, -1, 1)); |
| 104 | camera->setViewMatrix(osg::Matrix::identity()); |
| 105 | camera->setRenderOrder(osg::Camera::NESTED_RENDER); |
| 106 | camera->setClearMask(0); |
| 107 | camera->setAllowEventFocus(false); |
| 108 | camera->setCullingActive(false); |
| 109 | camera->addChild(geode); |
| 110 | |
| 111 | osg::Switch* sw = new osg::Switch; |
| 112 | sw->setUpdateCallback(new FGRedoutCallback(colorArray)); |
| 113 | sw->addChild(camera); |
| 114 | |
| 115 | return sw; |
| 116 | } |
| 117 |