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.
28 #include <simgear/screen/extensions.hxx>
29 #include <simgear/screen/RenderTexture.h>
30 #include <simgear/debug/logstream.hxx>
33 #include <Main/globals.hxx>
34 #include <Scenery/scenery.hxx>
35 #include "od_gauge.hxx"
37 FGODGauge::FGODGauge() :
43 // done here and not in init() so we don't allocate a rendering context if it is
45 void FGODGauge::allocRT () {
47 glGetIntegerv( GL_BLUE_BITS, &colorBits );
49 rt = new RenderTexture();
51 rt->Reset("rgba=5,5,5,1 ctt");
53 rt->Reset("rgba ctt");
55 if( rt->Initialize(256, 256, true) ) {
56 SG_LOG(SG_ALL, SG_INFO, "FGODGauge:Initialize sucessfull");
57 if (rt->BeginCapture())
59 SG_LOG(SG_ALL, SG_INFO, "FGODGauge:BeginCapture sucessfull, RTT available");
61 glViewport(0, 0, textureWH, textureWH);
62 glMatrixMode(GL_PROJECTION);
64 gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
65 glMatrixMode(GL_MODELVIEW);
67 glDisable(GL_LIGHTING);
68 glEnable(GL_COLOR_MATERIAL);
69 glDisable(GL_CULL_FACE);
71 glDisable(GL_DEPTH_TEST);
72 glClearColor(0.0, 0.0, 0.0, 0.0);
73 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
74 glBindTexture(GL_TEXTURE_2D, 0);
75 glEnable(GL_TEXTURE_2D);
76 glEnable(GL_ALPHA_TEST);
77 glAlphaFunc(GL_GREATER, 0.0f);
80 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
83 SG_LOG(SG_ALL, SG_WARN, "FGODGauge:BeginCapture failed, RTT not available, using backbuffer");
85 SG_LOG(SG_ALL, SG_WARN, "FGODGauge:Initialize failed, RTT not available, using backbuffer");
88 FGODGauge::~FGODGauge() {
92 void FGODGauge::init () {
95 void FGODGauge::update (double dt) {
98 void FGODGauge::beginCapture(int viewSize) {
106 textureWH = viewSize;
107 glViewport(0, 0, textureWH, textureWH);
110 void FGODGauge::beginCapture(void) {
120 void FGODGauge::Clear(void) {
122 glClear(GL_COLOR_BUFFER_BIT);
126 glDisable(GL_ALPHA_TEST);
127 glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
128 glRectf(-256.0, -256.0, 256.0, 256.0);
130 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
131 glEnable(GL_ALPHA_TEST);
135 void FGODGauge::endCapture(GLuint 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 )
171 static ssgSimpleState *
172 find_texture_node (ssgEntity * node, const char * name)
174 if( node->isAKindOf( ssgTypeLeaf() ) ) {
175 ssgLeaf *leaf = (ssgLeaf *) node;
176 ssgSimpleState *state = (ssgSimpleState *) leaf->getState();
178 ssgTexture *tex = state->getTexture();
180 const char * texture_name = tex->getFilename();
182 texture_name = strip_path( texture_name );
183 if ( !strcmp(name, texture_name) )
190 int nKids = node->getNumKids();
191 for (int i = 0; i < nKids; i++) {
192 ssgSimpleState * result =
193 find_texture_node(((ssgBranch*)node)->getKid(i), name);
201 void FGODGauge::set_texture(const char * name, GLuint new_texture) {
202 ssgEntity * root = globals->get_scenery()->get_aircraft_branch();
203 name = strip_path( name );
204 ssgSimpleState * node = find_texture_node( root, name );
206 node->setTexture( new_texture );
209 void FGODGauge::set2D() {
210 glPushAttrib ( GL_ENABLE_BIT | GL_VIEWPORT_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT ) ;
212 glDisable(GL_LIGHTING);
213 glEnable(GL_COLOR_MATERIAL);
214 glDisable(GL_CULL_FACE);
216 glDisable(GL_DEPTH_TEST);
217 glClearColor(0.0, 0.0, 0.0, 0.0);
218 glEnable(GL_TEXTURE_2D);
219 glDisable(GL_SMOOTH);
220 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
221 glBindTexture(GL_TEXTURE_2D, 0);
223 glViewport ( 0, 0, textureWH, textureWH ) ;
224 glMatrixMode ( GL_PROJECTION ) ;
227 gluOrtho2D( -256.0, 256.0, -256.0, 256.0 );
228 glMatrixMode ( GL_MODELVIEW ) ;
232 glAlphaFunc(GL_GREATER, 0.0f);
236 void FGODGauge::set3D() {
237 glMatrixMode ( GL_PROJECTION ) ;
239 glMatrixMode ( GL_MODELVIEW ) ;
241 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;