]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/od_gauge.cxx
Modified Files:
[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 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 //
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <simgear/screen/extensions.hxx>
28 #include <simgear/screen/RenderTexture.h>
29 #include <simgear/debug/logstream.hxx>
30 #include SG_GLU_H
31
32 #include <Main/globals.hxx>
33 #include <Scenery/scenery.hxx>
34 #include "od_gauge.hxx"
35
36 FGODGauge::FGODGauge() :
37     rtAvailable( false )// ,
38 //     rt( 0 )
39 {
40 }
41
42 // done here and not in init() so we don't allocate a rendering context if it is
43 // never used
44 void FGODGauge::allocRT () {
45     GLint colorBits = 0;
46 //     glGetIntegerv( GL_BLUE_BITS, &colorBits );
47     textureWH = 256;
48 //     rt = new RenderTexture();
49 //     if( colorBits < 8 )
50 //         rt->Reset("rgba=5,5,5,1 ctt");
51 //     else
52 //         rt->Reset("rgba ctt");
53
54 //     if( rt->Initialize(256, 256, true) ) {
55 //         SG_LOG(SG_ALL, SG_INFO, "FGODGauge:Initialize sucessfull");
56 //         if (rt->BeginCapture())
57 //         {
58 //             SG_LOG(SG_ALL, SG_INFO, "FGODGauge:BeginCapture sucessfull, RTT available");
59 //             rtAvailable = true;
60 //             glViewport(0, 0, textureWH, textureWH);
61 //             glMatrixMode(GL_PROJECTION);
62 //             glLoadIdentity();
63 //             gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
64 //             glMatrixMode(GL_MODELVIEW);
65 //             glLoadIdentity();
66 //             glDisable(GL_LIGHTING);
67 //             glEnable(GL_COLOR_MATERIAL);
68 //             glDisable(GL_CULL_FACE);
69 //             glDisable(GL_FOG);
70 //             glDisable(GL_DEPTH_TEST);
71 //             glClearColor(0.0, 0.0, 0.0, 0.0);
72 //             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
73 //             glBindTexture(GL_TEXTURE_2D, 0);
74 //             glEnable(GL_TEXTURE_2D);
75 //             glEnable(GL_ALPHA_TEST);
76 //             glAlphaFunc(GL_GREATER, 0.0f);
77 //             glDisable(GL_SMOOTH);
78 //             glEnable(GL_BLEND);
79 //             glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
80 //             rt->EndCapture();
81 //         } else
82 //             SG_LOG(SG_ALL, SG_WARN, "FGODGauge:BeginCapture failed, RTT not available, using backbuffer");
83 //     } else
84 //         SG_LOG(SG_ALL, SG_WARN, "FGODGauge:Initialize failed, RTT not available, using backbuffer");
85 }
86
87 FGODGauge::~FGODGauge() {
88 //     delete rt;
89 }
90
91 void FGODGauge::init () {
92 }
93
94 void FGODGauge::update (double dt) {
95 }
96
97 void FGODGauge::beginCapture(int viewSize) {
98 //     if( ! rt )
99 //         allocRT();
100 //     if(rtAvailable) {
101 //         rt->BeginCapture();
102 //     }
103 //     else
104 //         set2D();
105 //      textureWH = viewSize;
106 //     glViewport(0, 0, textureWH, textureWH);
107 }
108
109 void FGODGauge::beginCapture(void) {
110 //     if( ! rt )
111 //         allocRT();
112 //     if(rtAvailable) {
113 //         rt->BeginCapture();
114 //     }
115 //     else
116 //         set2D();
117 }
118
119 void FGODGauge::Clear(void) {
120 //     if(rtAvailable) {
121 //         glClear(GL_COLOR_BUFFER_BIT);
122 //     }
123 //     else {
124 //         glDisable(GL_BLEND);
125 //         glDisable(GL_ALPHA_TEST);
126 //           glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
127 //         glRectf(-256.0, -256.0, 256.0, 256.0);
128 //         glEnable(GL_BLEND);
129 //         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
130 //         glEnable(GL_ALPHA_TEST);
131 //     }
132 }
133
134 void FGODGauge::endCapture(osg::Texture2D* texID) {
135   // OSGFIXME
136 //     glBindTexture(GL_TEXTURE_2D, texID);
137 //     // don't use mimaps if we don't update them
138 //     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
139
140 //     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureWH, textureWH);
141 //     if(rtAvailable)
142 //         rt->EndCapture();
143 //     else
144 //         set3D();
145 //     glBindTexture(GL_TEXTURE_2D, 0);
146 }
147
148 void FGODGauge::setSize(int viewSize) {
149     textureWH = viewSize;
150 //     glViewport(0, 0, textureWH, textureWH);
151 }
152
153 bool FGODGauge::serviceable(void) {
154     return rtAvailable;
155 }
156
157 /**
158  * Locate a texture SSG node in a branch.
159  */
160 static const char *strip_path(const char *name) {
161     /* Remove all leading path information. */
162     const char* seps = "\\/" ;
163     const char* fn = & name [ strlen ( name ) - 1 ] ;
164     for ( ; fn != name && strchr(seps,*fn) == NULL ; fn-- )
165         /* Search back for a seperator */ ;
166     if ( strchr(seps,*fn) != NULL )
167         fn++ ;
168     return fn ;
169 }
170
171 // OSGFIXME
172 static osg::StateSet*
173 find_texture_node(osg::Node* node, const char * name)
174 {
175 #if 0
176   if( node->isAKindOf( ssgTypeLeaf() ) ) {
177     ssgLeaf *leaf = (ssgLeaf *) node;
178     ssgSimpleState *state = (ssgSimpleState *) leaf->getState();
179     if( state ) {
180         ssgTexture *tex = state->getTexture();
181         if( tex ) {
182             const char * texture_name = tex->getFilename();
183             if (texture_name) {
184                 texture_name = strip_path( texture_name );
185                 if ( !strcmp(name, texture_name) )
186                     return state;
187             }
188         }
189     }
190   }
191   else {
192     int nKids = node->getNumKids();
193     for (int i = 0; i < nKids; i++) {
194       ssgSimpleState * result =
195         find_texture_node(((ssgBranch*)node)->getKid(i), name);
196       if (result != 0)
197         return result;
198     }
199   } 
200 #endif
201   return 0;
202 }
203
204 void FGODGauge::set_texture(const char * name, osg::Texture2D* new_texture) {
205     osg::Group* root = globals->get_scenery()->get_aircraft_branch();
206     name = strip_path( name );
207     // OSGFIXME
208 //     osg::StateSet* node = find_texture_node( root, name );
209 //     if( node )
210 //         node->setTexture( new_texture );
211 }
212
213 void FGODGauge::set2D() {
214 //     glPushAttrib ( GL_ENABLE_BIT | GL_VIEWPORT_BIT  | GL_TRANSFORM_BIT | GL_LIGHTING_BIT ) ;
215
216 //     glDisable(GL_LIGHTING);
217 //     glEnable(GL_COLOR_MATERIAL);
218 //     glDisable(GL_CULL_FACE);
219 //     glDisable(GL_FOG);
220 //     glDisable(GL_DEPTH_TEST);
221 //     glClearColor(0.0, 0.0, 0.0, 0.0);
222 //     glEnable(GL_TEXTURE_2D);
223 //     glDisable(GL_SMOOTH);
224 //     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
225 //     glBindTexture(GL_TEXTURE_2D, 0);
226
227 //     glViewport ( 0, 0, textureWH, textureWH ) ;
228 //     glMatrixMode   ( GL_PROJECTION ) ;
229 //     glPushMatrix   () ;
230 //     glLoadIdentity () ;
231 //     gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
232 //     glMatrixMode   ( GL_MODELVIEW ) ;
233 //     glPushMatrix   () ;
234 //     glLoadIdentity () ;
235
236 //     glAlphaFunc(GL_GREATER, 0.0f);
237
238 }
239
240 void FGODGauge::set3D() {
241 //     glMatrixMode   ( GL_PROJECTION ) ;
242 //     glPopMatrix    () ;
243 //     glMatrixMode   ( GL_MODELVIEW ) ;
244 //     glPopMatrix    () ;
245 //     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
246 //     glPopAttrib    () ;
247 }