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