1 // Owner Drawn Gauge helper class
3 // Written by Harald JOHNSEN, started May 2005.
5 // Copyright (C) 2005 Harald JOHNSEN
7 // Ported to OSG by Tim Moore - Jun 2007
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <osg/AlphaFunc>
30 #include <osg/BlendFunc>
33 #include <osg/NodeVisitor>
35 #include <osg/PolygonMode>
36 #include <osg/ShadeModel>
37 #include <osg/StateSet>
38 #include <osgDB/FileNameUtils>
40 #include <simgear/scene/util/RenderConstants.hxx>
41 #include <simgear/screen/extensions.hxx>
42 #include <simgear/debug/logstream.hxx>
44 #include <Main/globals.hxx>
45 #include <Main/renderer.hxx>
46 #include <Scenery/scenery.hxx>
47 #include "od_gauge.hxx"
49 FGODGauge::FGODGauge() :
50 rtAvailable( false )// ,
55 void FGODGauge::allocRT () {
56 camera = new osg::Camera;
57 // Only the far camera should trigger this texture to be rendered.
58 camera->setNodeMask(simgear::BACKGROUND_BIT);
59 camera->setProjectionMatrix(osg::Matrix::ortho2D(-256.0, 256.0, -256.0,
61 camera->setViewport(0, 0, textureWH, textureWH);
62 camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
63 camera->setRenderOrder(osg::Camera::PRE_RENDER);
64 camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
65 camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 0.0f));
66 camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT, osg::Camera::FRAME_BUFFER);
67 osg::StateSet* stateSet = camera->getOrCreateStateSet();
68 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
69 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
70 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
71 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
72 stateSet->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,
73 osg::PolygonMode::FILL),
74 osg::StateAttribute::ON);
75 stateSet->setAttributeAndModes(new osg::AlphaFunc(osg::AlphaFunc::GREATER,
77 osg::StateAttribute::ON);
78 stateSet->setAttribute(new osg::ShadeModel(osg::ShadeModel::FLAT));
79 stateSet->setAttributeAndModes(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
80 osg::BlendFunc::ONE_MINUS_SRC_ALPHA),
81 osg::StateAttribute::ON);
82 if (!texture.valid()) {
83 texture = new osg::Texture2D;
84 texture->setTextureSize(textureWH, textureWH);
85 texture->setInternalFormat(GL_RGBA);
86 texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
87 texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
89 camera->attach(osg::Camera::COLOR_BUFFER, texture.get());
90 globals->get_renderer()->addCamera(camera.get(), false);
93 // GLint colorBits = 0;
94 // glGetIntegerv( GL_BLUE_BITS, &colorBits );
95 // rt = new RenderTexture();
96 // if( colorBits < 8 )
97 // rt->Reset("rgba=5,5,5,1 ctt");
99 // rt->Reset("rgba ctt");
101 // if( rt->Initialize(256, 256, true) ) {
102 // SG_LOG(SG_ALL, SG_INFO, "FGODGauge:Initialize sucessfull");
103 // if (rt->BeginCapture())
105 // SG_LOG(SG_ALL, SG_INFO, "FGODGauge:BeginCapture sucessfull, RTT available");
106 // rtAvailable = true;
107 // glViewport(0, 0, textureWH, textureWH);
108 // glMatrixMode(GL_PROJECTION);
110 // gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
111 // glMatrixMode(GL_MODELVIEW);
113 // glDisable(GL_LIGHTING);
114 // glEnable(GL_COLOR_MATERIAL);
115 // glDisable(GL_CULL_FACE);
116 // glDisable(GL_FOG);
117 // glDisable(GL_DEPTH_TEST);
118 // glClearColor(0.0, 0.0, 0.0, 0.0);
119 // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
120 // glBindTexture(GL_TEXTURE_2D, 0);
121 // glEnable(GL_TEXTURE_2D);
122 // glEnable(GL_ALPHA_TEST);
123 // glAlphaFunc(GL_GREATER, 0.0f);
124 // glDisable(GL_SMOOTH);
125 // glEnable(GL_BLEND);
126 // glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
129 // SG_LOG(SG_ALL, SG_WARN, "FGODGauge:BeginCapture failed, RTT not available, using backbuffer");
131 // SG_LOG(SG_ALL, SG_WARN, "FGODGauge:Initialize failed, RTT not available, using backbuffer");
134 FGODGauge::~FGODGauge() {
138 void FGODGauge::init () {
141 void FGODGauge::update (double dt) {
145 void FGODGauge::setSize(int viewSize) {
146 textureWH = viewSize;
147 // glViewport(0, 0, textureWH, textureWH);
150 bool FGODGauge::serviceable(void) {
155 * Replace a texture in the airplane model with the gauge texture.
158 class ReplaceStaticTextureVisitor : public osg::NodeVisitor
161 ReplaceStaticTextureVisitor(const std::string& name,
162 osg::Texture2D* _newTexture) :
163 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
164 newTexture(_newTexture)
166 textureFileName = osgDB::getSimpleFileName(name);
169 virtual void apply(osg::Node& node)
171 osg::StateSet* ss = node.getStateSet();
173 changeStateSetTexture(ss);
177 virtual void apply(osg::Geode& node)
179 int numDrawables = node.getNumDrawables();
180 for (int i = 0; i < numDrawables; i++) {
181 osg::Drawable* drawable = node.getDrawable(i);
182 osg::StateSet* ss = drawable->getStateSet();
184 changeStateSetTexture(ss);
189 void changeStateSetTexture(osg::StateSet *ss)
192 = dynamic_cast<osg::Texture2D*>(ss->getTextureAttribute(0,
193 osg::StateAttribute::TEXTURE));
194 if (!tex || tex == newTexture || !tex->getImage())
196 std::string fileName = tex->getImage()->getFileName();
197 std::string simpleName = osgDB::getSimpleFileName(fileName);
198 if (osgDB::equalCaseInsensitive(textureFileName, simpleName))
199 ss->setTextureAttribute(0, newTexture);
201 std::string textureFileName;
202 osg::Texture2D* newTexture;
205 void FGODGauge::set_texture(const char * name, osg::Texture2D* new_texture)
207 osg::Group* root = globals->get_scenery()->get_aircraft_branch();
208 ReplaceStaticTextureVisitor visitor(name, new_texture);
209 root->accept(visitor);