]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/od_gauge.cxx
Make hardcoded error values configurable.
[flightgear.git] / src / Instrumentation / od_gauge.cxx
1 // Owner Drawn Gauge helper class
2 //
3 // Written by Harald JOHNSEN, started May 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN
6 //
7 // Ported to OSG by Tim Moore - Jun 2007
8 //
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.
13 //
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.
18 //
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.
22 //
23 //
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include <osg/AlphaFunc>
30 #include <osg/BlendFunc>
31 #include <osg/Camera>
32 #include <osg/Geode>
33 #include <osg/NodeVisitor>
34 #include <osg/Matrix>
35 #include <osg/PolygonMode>
36 #include <osg/ShadeModel>
37 #include <osg/StateSet>
38 #include <osgDB/FileNameUtils>
39
40 #include <simgear/scene/util/RenderConstants.hxx>
41 #include <simgear/screen/extensions.hxx>
42 #include <simgear/debug/logstream.hxx>
43
44 #include <Main/globals.hxx>
45 #include <Main/renderer.hxx>
46 #include <Scenery/scenery.hxx>
47 #include "od_gauge.hxx"
48
49 FGODGauge::FGODGauge() :
50     rtAvailable( false )// ,
51 //     rt( 0 )
52 {
53 }
54
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,
60             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,
76             0.0f),
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);
88     }
89     camera->attach(osg::Camera::COLOR_BUFFER, texture.get());
90     globals->get_renderer()->addCamera(camera.get(), false);
91     rtAvailable = true;
92
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");
98 //     else
99 //         rt->Reset("rgba ctt");
100
101 //     if( rt->Initialize(256, 256, true) ) {
102 //         SG_LOG(SG_ALL, SG_INFO, "FGODGauge:Initialize sucessfull");
103 //         if (rt->BeginCapture())
104 //         {
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);
109 //             glLoadIdentity();
110 //             gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
111 //             glMatrixMode(GL_MODELVIEW);
112 //             glLoadIdentity();
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 );
127 //             rt->EndCapture();
128 //         } else
129 //             SG_LOG(SG_ALL, SG_WARN, "FGODGauge:BeginCapture failed, RTT not available, using backbuffer");
130 //     } else
131 //         SG_LOG(SG_ALL, SG_WARN, "FGODGauge:Initialize failed, RTT not available, using backbuffer");
132 }
133
134 FGODGauge::~FGODGauge() {
135 //     delete rt;
136 }
137
138 void FGODGauge::init () {
139 }
140
141 void FGODGauge::update (double dt) {
142 }
143
144
145 void FGODGauge::setSize(int viewSize) {
146     textureWH = viewSize;
147 //     glViewport(0, 0, textureWH, textureWH);
148 }
149
150 bool FGODGauge::serviceable(void) {
151     return rtAvailable;
152 }
153
154 /**
155  * Replace a texture in the airplane model with the gauge texture.
156  */
157
158 class ReplaceStaticTextureVisitor : public osg::NodeVisitor
159 {
160 public:
161     ReplaceStaticTextureVisitor(const std::string& name,
162         osg::Texture2D* _newTexture) :
163         osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
164         newTexture(_newTexture)
165     {
166         textureFileName = osgDB::getSimpleFileName(name);
167     }
168
169     virtual void apply(osg::Node& node)
170     {
171         osg::StateSet* ss = node.getStateSet();
172         if (ss)
173             changeStateSetTexture(ss);
174         traverse(node);
175     }
176
177     virtual void apply(osg::Geode& node)
178     {
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();
183             if (ss)
184                 changeStateSetTexture(ss);
185         }
186         traverse(node);
187 }
188 protected:
189     void changeStateSetTexture(osg::StateSet *ss)
190     {
191         osg::Texture2D* tex
192                 = dynamic_cast<osg::Texture2D*>(ss->getTextureAttribute(0,
193                 osg::StateAttribute::TEXTURE));
194         if (!tex || tex == newTexture || !tex->getImage())
195             return;
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);
200     }
201     std::string textureFileName;
202     osg::Texture2D* newTexture;
203 };
204
205 void FGODGauge::set_texture(const char * name, osg::Texture2D* new_texture)
206 {
207     osg::Group* root = globals->get_scenery()->get_aircraft_branch();
208     ReplaceStaticTextureVisitor visitor(name, new_texture);
209     root->accept(visitor);
210 }
211