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