1 // Owner Drawn Gauge helper class
3 // Written by Harald JOHNSEN, started May 2005.
5 // Copyright (C) 2005 Harald JOHNSEN
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.
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.
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.
27 #include <simgear/screen/extensions.hxx>
28 #include <simgear/screen/RenderTexture.h>
29 #include <simgear/debug/logstream.hxx>
32 #include <Main/globals.hxx>
33 #include <Scenery/scenery.hxx>
34 #include "od_gauge.hxx"
36 FGODGauge::FGODGauge() :
37 rtAvailable( false )// ,
42 // done here and not in init() so we don't allocate a rendering context if it is
44 void FGODGauge::allocRT () {
46 // glGetIntegerv( GL_BLUE_BITS, &colorBits );
48 // rt = new RenderTexture();
49 // if( colorBits < 8 )
50 // rt->Reset("rgba=5,5,5,1 ctt");
52 // rt->Reset("rgba ctt");
54 // if( rt->Initialize(256, 256, true) ) {
55 // SG_LOG(SG_ALL, SG_INFO, "FGODGauge:Initialize sucessfull");
56 // if (rt->BeginCapture())
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);
63 // gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
64 // glMatrixMode(GL_MODELVIEW);
66 // glDisable(GL_LIGHTING);
67 // glEnable(GL_COLOR_MATERIAL);
68 // glDisable(GL_CULL_FACE);
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 );
82 // SG_LOG(SG_ALL, SG_WARN, "FGODGauge:BeginCapture failed, RTT not available, using backbuffer");
84 // SG_LOG(SG_ALL, SG_WARN, "FGODGauge:Initialize failed, RTT not available, using backbuffer");
87 FGODGauge::~FGODGauge() {
91 void FGODGauge::init () {
94 void FGODGauge::update (double dt) {
97 void FGODGauge::beginCapture(int viewSize) {
101 // rt->BeginCapture();
105 // textureWH = viewSize;
106 // glViewport(0, 0, textureWH, textureWH);
109 void FGODGauge::beginCapture(void) {
113 // rt->BeginCapture();
119 void FGODGauge::Clear(void) {
121 // glClear(GL_COLOR_BUFFER_BIT);
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);
134 void FGODGauge::endCapture(osg::Texture2D* texID) {
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);
140 // glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureWH, textureWH);
145 // glBindTexture(GL_TEXTURE_2D, 0);
148 void FGODGauge::setSize(int viewSize) {
149 textureWH = viewSize;
150 // glViewport(0, 0, textureWH, textureWH);
153 bool FGODGauge::serviceable(void) {
158 * Locate a texture SSG node in a branch.
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 )
172 static osg::StateSet*
173 find_texture_node(osg::Node* node, const char * name)
176 if( node->isAKindOf( ssgTypeLeaf() ) ) {
177 ssgLeaf *leaf = (ssgLeaf *) node;
178 ssgSimpleState *state = (ssgSimpleState *) leaf->getState();
180 ssgTexture *tex = state->getTexture();
182 const char * texture_name = tex->getFilename();
184 texture_name = strip_path( texture_name );
185 if ( !strcmp(name, texture_name) )
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);
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 );
208 // osg::StateSet* node = find_texture_node( root, name );
210 // node->setTexture( new_texture );
213 void FGODGauge::set2D() {
214 // glPushAttrib ( GL_ENABLE_BIT | GL_VIEWPORT_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT ) ;
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);
227 // glViewport ( 0, 0, textureWH, textureWH ) ;
228 // glMatrixMode ( GL_PROJECTION ) ;
230 // glLoadIdentity () ;
231 // gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
232 // glMatrixMode ( GL_MODELVIEW ) ;
234 // glLoadIdentity () ;
236 // glAlphaFunc(GL_GREATER, 0.0f);
240 void FGODGauge::set3D() {
241 // glMatrixMode ( GL_PROJECTION ) ;
243 // glMatrixMode ( GL_MODELVIEW ) ;
245 // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;