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, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
24 #include <simgear/screen/extensions.hxx>
25 #include <simgear/screen/RenderTexture.h>
26 #include <simgear/debug/logstream.hxx>
29 #include <Main/globals.hxx>
30 #include <Scenery/scenery.hxx>
31 #include "od_gauge.hxx"
33 FGODGauge::FGODGauge() :
39 // done here and not in init() so we don't allocate a rendering context if it is
41 void FGODGauge::allocRT () {
43 glGetIntegerv( GL_BLUE_BITS, &colorBits );
45 rt = new RenderTexture();
47 rt->Reset("rgba=5,5,5,1 ctt");
49 rt->Reset("rgba ctt");
51 if( rt->Initialize(256, 256, true) ) {
52 SG_LOG(SG_ALL, SG_INFO, "FGODGauge:Initialize sucessfull");
53 if (rt->BeginCapture())
55 SG_LOG(SG_ALL, SG_INFO, "FGODGauge:BeginCapture sucessfull, RTT available");
57 glViewport(0, 0, textureWH, textureWH);
58 glMatrixMode(GL_PROJECTION);
60 gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
61 glMatrixMode(GL_MODELVIEW);
63 glDisable(GL_LIGHTING);
64 glEnable(GL_COLOR_MATERIAL);
65 glDisable(GL_CULL_FACE);
67 glDisable(GL_DEPTH_TEST);
68 glClearColor(0.0, 0.0, 0.0, 0.0);
69 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
70 glBindTexture(GL_TEXTURE_2D, 0);
71 glEnable(GL_TEXTURE_2D);
72 glEnable(GL_ALPHA_TEST);
73 glAlphaFunc(GL_GREATER, 0.0f);
76 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
79 SG_LOG(SG_ALL, SG_WARN, "FGODGauge:BeginCapture failed, RTT not available, using backbuffer");
81 SG_LOG(SG_ALL, SG_WARN, "FGODGauge:Initialize failed, RTT not available, using backbuffer");
84 FGODGauge::~FGODGauge() {
88 void FGODGauge::init () {
91 void FGODGauge::update (double dt) {
94 void FGODGauge::beginCapture(int viewSize) {
102 textureWH = viewSize;
103 glViewport(0, 0, textureWH, textureWH);
106 void FGODGauge::beginCapture(void) {
116 void FGODGauge::Clear(void) {
118 glClear(GL_COLOR_BUFFER_BIT);
122 glDisable(GL_ALPHA_TEST);
123 glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
124 glRectf(-256.0, -256.0, 256.0, 256.0);
126 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
127 glEnable(GL_ALPHA_TEST);
131 void FGODGauge::endCapture(GLuint texID) {
132 glBindTexture(GL_TEXTURE_2D, texID);
133 // don't use mimaps if we don't update them
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
136 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureWH, textureWH);
141 glBindTexture(GL_TEXTURE_2D, 0);
144 void FGODGauge::setSize(int viewSize) {
145 textureWH = viewSize;
146 glViewport(0, 0, textureWH, textureWH);
149 bool FGODGauge::serviceable(void) {
154 * Locate a texture SSG node in a branch.
156 static const char *strip_path(const char *name) {
157 /* Remove all leading path information. */
158 const char* seps = "\\/" ;
159 const char* fn = & name [ strlen ( name ) - 1 ] ;
160 for ( ; fn != name && strchr(seps,*fn) == NULL ; fn-- )
161 /* Search back for a seperator */ ;
162 if ( strchr(seps,*fn) != NULL )
167 static ssgSimpleState *
168 find_texture_node (ssgEntity * node, const char * name)
170 if( node->isAKindOf( ssgTypeLeaf() ) ) {
171 ssgLeaf *leaf = (ssgLeaf *) node;
172 ssgSimpleState *state = (ssgSimpleState *) leaf->getState();
174 ssgTexture *tex = state->getTexture();
176 const char * texture_name = tex->getFilename();
178 texture_name = strip_path( texture_name );
179 if ( !strcmp(name, texture_name) )
186 int nKids = node->getNumKids();
187 for (int i = 0; i < nKids; i++) {
188 ssgSimpleState * result =
189 find_texture_node(((ssgBranch*)node)->getKid(i), name);
197 void FGODGauge::set_texture(const char * name, GLuint new_texture) {
198 ssgEntity * root = globals->get_scenery()->get_aircraft_branch();
199 name = strip_path( name );
200 ssgSimpleState * node = find_texture_node( root, name );
202 node->setTexture( new_texture );
205 void FGODGauge::set2D() {
206 glPushAttrib ( GL_ENABLE_BIT | GL_VIEWPORT_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT ) ;
208 glDisable(GL_LIGHTING);
209 glEnable(GL_COLOR_MATERIAL);
210 glDisable(GL_CULL_FACE);
212 glDisable(GL_DEPTH_TEST);
213 glClearColor(0.0, 0.0, 0.0, 0.0);
214 glEnable(GL_TEXTURE_2D);
215 glDisable(GL_SMOOTH);
216 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
217 glBindTexture(GL_TEXTURE_2D, 0);
219 glViewport ( 0, 0, textureWH, textureWH ) ;
220 glMatrixMode ( GL_PROJECTION ) ;
223 gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
224 glMatrixMode ( GL_MODELVIEW ) ;
228 glAlphaFunc(GL_GREATER, 0.0f);
232 void FGODGauge::set3D() {
233 glMatrixMode ( GL_PROJECTION ) ;
235 glMatrixMode ( GL_MODELVIEW ) ;
237 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;