]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/od_gauge.cxx
GPS data validity clean-up; it was a mess, now it's more robust. Thanks to Dave Luff...
[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(-textureWH/2.0, textureWH/2.0, -textureWH/2.0, textureWH/2.0));
60     camera->setViewport(0, 0, textureWH, textureWH);
61     camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
62     camera->setRenderOrder(osg::Camera::PRE_RENDER);
63     camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
64     camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 0.0f));
65     camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT, osg::Camera::FRAME_BUFFER);
66     osg::StateSet* stateSet = camera->getOrCreateStateSet();
67     stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
68     stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
69     stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
70     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
71     stateSet->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,
72             osg::PolygonMode::FILL),
73             osg::StateAttribute::ON);
74     stateSet->setAttributeAndModes(new osg::AlphaFunc(osg::AlphaFunc::GREATER,
75             0.0f),
76             osg::StateAttribute::ON);
77     stateSet->setAttribute(new osg::ShadeModel(osg::ShadeModel::FLAT));
78     stateSet->setAttributeAndModes(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
79             osg::BlendFunc::ONE_MINUS_SRC_ALPHA),
80             osg::StateAttribute::ON);
81     if (!texture.valid()) {
82         texture = new osg::Texture2D;
83         texture->setTextureSize(textureWH, textureWH);
84         texture->setInternalFormat(GL_RGBA);
85         texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
86         texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
87     }
88     camera->attach(osg::Camera::COLOR_BUFFER, texture.get());
89     globals->get_renderer()->addCamera(camera.get(), false);
90     rtAvailable = true;
91
92     // GLint colorBits = 0;
93 //     glGetIntegerv( GL_BLUE_BITS, &colorBits );
94 //     rt = new RenderTexture();
95 //     if( colorBits < 8 )
96 //         rt->Reset("rgba=5,5,5,1 ctt");
97 //     else
98 //         rt->Reset("rgba ctt");
99
100 //     if( rt->Initialize(256, 256, true) ) {
101 //         SG_LOG(SG_ALL, SG_INFO, "FGODGauge:Initialize sucessfull");
102 //         if (rt->BeginCapture())
103 //         {
104 //             SG_LOG(SG_ALL, SG_INFO, "FGODGauge:BeginCapture sucessfull, RTT available");
105 //             rtAvailable = true;
106 //             glViewport(0, 0, textureWH, textureWH);
107 //             glMatrixMode(GL_PROJECTION);
108 //             glLoadIdentity();
109 //             gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
110 //             glMatrixMode(GL_MODELVIEW);
111 //             glLoadIdentity();
112 //             glDisable(GL_LIGHTING);
113 //             glEnable(GL_COLOR_MATERIAL);
114 //             glDisable(GL_CULL_FACE);
115 //             glDisable(GL_FOG);
116 //             glDisable(GL_DEPTH_TEST);
117 //             glClearColor(0.0, 0.0, 0.0, 0.0);
118 //             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
119 //             glBindTexture(GL_TEXTURE_2D, 0);
120 //             glEnable(GL_TEXTURE_2D);
121 //             glEnable(GL_ALPHA_TEST);
122 //             glAlphaFunc(GL_GREATER, 0.0f);
123 //             glDisable(GL_SMOOTH);
124 //             glEnable(GL_BLEND);
125 //             glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
126 //             rt->EndCapture();
127 //         } else
128 //             SG_LOG(SG_ALL, SG_WARN, "FGODGauge:BeginCapture failed, RTT not available, using backbuffer");
129 //     } else
130 //         SG_LOG(SG_ALL, SG_WARN, "FGODGauge:Initialize failed, RTT not available, using backbuffer");
131 }
132
133 FGODGauge::~FGODGauge() {
134 //     delete rt;
135 }
136
137 void FGODGauge::init () {
138 }
139
140 void FGODGauge::update (double dt) {
141 }
142
143
144 void FGODGauge::setSize(int viewSize) {
145     textureWH = viewSize;
146 //     glViewport(0, 0, textureWH, textureWH);
147 }
148
149 bool FGODGauge::serviceable(void) {
150     return rtAvailable;
151 }
152
153 /**
154  * Replace a texture in the airplane model with the gauge texture.
155  */
156
157 class ReplaceStaticTextureVisitor : public osg::NodeVisitor
158 {
159 public:
160     ReplaceStaticTextureVisitor(const std::string& name,
161         osg::Texture2D* _newTexture) :
162         osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
163         newTexture(_newTexture)
164     {
165         textureFileName = osgDB::getSimpleFileName(name);
166     }
167
168     virtual void apply(osg::Node& node)
169     {
170         osg::StateSet* ss = node.getStateSet();
171         if (ss)
172             changeStateSetTexture(ss);
173         traverse(node);
174     }
175
176     virtual void apply(osg::Geode& node)
177     {
178         int numDrawables = node.getNumDrawables();
179         for (int i = 0; i < numDrawables; i++) {
180             osg::Drawable* drawable = node.getDrawable(i);
181             osg::StateSet* ss = drawable->getStateSet();
182             if (ss)
183                 changeStateSetTexture(ss);
184         }
185         traverse(node);
186 }
187 protected:
188     void changeStateSetTexture(osg::StateSet *ss)
189     {
190         osg::Texture2D* tex
191                 = dynamic_cast<osg::Texture2D*>(ss->getTextureAttribute(0,
192                 osg::StateAttribute::TEXTURE));
193         if (!tex || tex == newTexture || !tex->getImage())
194             return;
195         std::string fileName = tex->getImage()->getFileName();
196         std::string simpleName = osgDB::getSimpleFileName(fileName);
197         if (osgDB::equalCaseInsensitive(textureFileName, simpleName))
198             ss->setTextureAttribute(0, newTexture);
199     }
200     std::string textureFileName;
201     osg::Texture2D* newTexture;
202 };
203
204 void FGODGauge::set_texture(const char * name, osg::Texture2D* new_texture)
205 {
206     osg::Group* root = globals->get_scenery()->get_aircraft_branch();
207     ReplaceStaticTextureVisitor visitor(name, new_texture);
208     root->accept(visitor);
209 }
210