1 // panel.cxx - default, 2D single-engine prop instrument panel
3 // Written by David Megginson, started January 2000.
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 // $Id: panel.cxx,v 1.44 2006/09/05 20:28:48 curt Exp $
29 #include <stdio.h> // sprintf
32 #include <simgear/compiler.h>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sg_path.hxx>
42 #include "ApplicationProperties.hxx"
43 ////////////////////////////////////////////////////////////////////////
45 ////////////////////////////////////////////////////////////////////////
47 class FGDummyTextureLoader : public FGTextureLoaderInterface {
49 virtual GLuint loadTexture( const string & filename );
52 GLuint FGDummyTextureLoader::loadTexture( const string & filename )
55 glGenTextures( 1, &_texture );
56 glBindTexture( GL_TEXTURE_2D, _texture );
58 // glTexEnvi ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
59 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) ;
60 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ) ;
61 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
62 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;
64 GLubyte image[ 2 * 2 * 3 ] ;
66 /* Red and white chequerboard */
67 image [ 0 ] = 255 ; image [ 1 ] = 0 ; image [ 2 ] = 0 ;
68 image [ 3 ] = 255 ; image [ 4 ] = 255 ; image [ 5 ] = 255 ;
69 image [ 6 ] = 255 ; image [ 7 ] = 255 ; image [ 8 ] = 255 ;
70 image [ 9 ] = 255 ; image [ 10] = 0 ; image [ 11] = 0 ;
72 glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, 2, 2, 0,
73 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image);
78 ////////////////////////////////////////////////////////////////////////
79 // Implementation of FGCropped Texture.
80 ////////////////////////////////////////////////////////////////////////
82 GLuint FGCroppedTexture::current_bound_texture = 0;
83 map<string,GLuint> FGCroppedTexture::cache;
84 map<string,FGTextureLoaderInterface*> FGCroppedTexture::textureLoader;
85 static FGDummyTextureLoader dummyTextureLoader;
87 FGCroppedTexture::FGCroppedTexture (const string &path,
88 float minX, float minY,
89 float maxX, float maxY)
91 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY), _texture(0)
95 FGCroppedTexture::~FGCroppedTexture ()
99 void FGCroppedTexture::bind( bool doGLBind )
101 if( _texture == 0 ) {
102 SG_LOG( SG_COCKPIT, SG_DEBUG, "First bind of texture " << _path );
103 if( cache.count(_path) > 0 ) {
104 _texture = cache[_path];
105 SG_LOG( SG_COCKPIT, SG_DEBUG, "Using texture " << _path << " from cache (#" << _texture << ")" );
107 SGPath tpath = ApplicationProperties::GetRootPath(_path.c_str());
108 string extension = tpath.extension();
109 FGTextureLoaderInterface * loader = &dummyTextureLoader;
110 if( textureLoader.count( extension ) == 0 ) {
111 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't handle textures of type " << extension );
113 loader = textureLoader[extension];
116 _texture = loader->loadTexture( tpath.c_str() );
117 SG_LOG( SG_COCKPIT, SG_DEBUG, "Texture " << tpath.c_str() << " loaded from file as #" << _texture );
119 cache[_path] = _texture;
123 if( !doGLBind || current_bound_texture == _texture )
126 glBindTexture( GL_TEXTURE_2D, _texture );
127 current_bound_texture = _texture;
131 ////////////////////////////////////////////////////////////////////////
132 // Implementation of FGPanel.
133 ////////////////////////////////////////////////////////////////////////
138 FGPanel::FGPanel ( SGPropertyNode_ptr root)
140 _flipx(root->getNode("/sim/panel/flip-x", true)),
141 _rotate(root->getNode("/sim/panel/rotate-deg", true)),
142 _bg_width(1.0), _bg_height(1.0),
153 for (instrument_list_type::iterator it = _instruments.begin();
154 it != _instruments.end();
163 * Add an instrument to the panel.
166 FGPanel::addInstrument (FGPanelInstrument * instrument)
168 _instruments.push_back(instrument);
173 * Initialize the panel.
182 * Bind panel properties.
191 * Unbind panel properties.
198 GLuint FGPanel::getInitDisplayList()
200 if( initDisplayList != 0 ) return initDisplayList;
201 glMatrixMode(GL_PROJECTION);
203 if ( _flipx->getBoolValue() ) {
204 gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
206 gluOrtho2D( 0, _width, 0, _height ); /* right side up */
209 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
211 glMatrixMode(GL_MODELVIEW);
214 glClear( GL_COLOR_BUFFER_BIT);
217 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
218 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE
219 | GL_DEPTH_BUFFER_BIT );
221 // Draw the background
222 glEnable(GL_TEXTURE_2D);
224 glDisable(GL_LIGHTING);
226 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
227 glEnable(GL_ALPHA_TEST);
228 glEnable(GL_COLOR_MATERIAL);
229 glEnable(GL_CULL_FACE);
231 glDisable(GL_DEPTH_TEST);
235 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
237 glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
238 glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
239 glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
240 glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
242 } else if( _mbg[0] != NULL ) {
243 for (int i = 0; i < 4; i ++) {
244 // top row of textures...(1,3,5,7)
246 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
248 glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4, _height/2);
249 glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
250 glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
251 glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4, _height);
253 // bottom row of textures...(2,4,6,8)
255 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
257 glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4, 0);
258 glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
259 glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
260 glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4, _height/2);
265 glGetFloatv( GL_CURRENT_COLOR, c );
266 glColor4f( 0.0, 0.0, 0.0, 1.0 );
269 glVertex2f(_width, 0);
270 glVertex2f(_width, _height);
271 glVertex2f(0, _height);
277 return initDisplayList;
281 FGPanel::update (double dt)
283 glCallList(getInitDisplayList());
285 // Draw the instruments.
286 // Syd Adams: added instrument clipping
287 instrument_list_type::const_iterator current = _instruments.begin();
288 instrument_list_type::const_iterator end = _instruments.end();
290 GLdouble blx[4]={1.0,0.0,0.0,0.0};
291 GLdouble bly[4]={0.0,1.0,0.0,0.0};
292 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
293 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
295 for ( ; current != end; current++) {
296 FGPanelInstrument * instr = *current;
298 glTranslated(instr->getXPos(), instr->getYPos(), 0);
300 int ix= instr->getWidth();
301 int iy= instr->getHeight();
303 glTranslated(-ix/2,-iy/2,0);
304 glClipPlane(GL_CLIP_PLANE0,blx);
305 glClipPlane(GL_CLIP_PLANE1,bly);
306 glEnable(GL_CLIP_PLANE0);
307 glEnable(GL_CLIP_PLANE1);
309 glTranslated(ix,iy,0);
310 glClipPlane(GL_CLIP_PLANE2,urx);
311 glClipPlane(GL_CLIP_PLANE3,ury);
312 glEnable(GL_CLIP_PLANE2);
313 glEnable(GL_CLIP_PLANE3);
320 glDisable(GL_CLIP_PLANE0);
321 glDisable(GL_CLIP_PLANE1);
322 glDisable(GL_CLIP_PLANE2);
323 glDisable(GL_CLIP_PLANE3);
325 // restore some original state
334 FGPanel::update (double dt)
336 glMatrixMode(GL_PROJECTION);
338 if ( _flipx->getBoolValue() ) {
339 gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
341 gluOrtho2D( 0, _width, 0, _height ); /* right side up */
344 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
346 glMatrixMode(GL_MODELVIEW);
354 glClear( GL_COLOR_BUFFER_BIT);
357 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
358 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE
359 | GL_DEPTH_BUFFER_BIT );
361 // Draw the background
362 glEnable(GL_TEXTURE_2D);
364 glDisable(GL_LIGHTING);
366 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
367 glEnable(GL_ALPHA_TEST);
368 glEnable(GL_COLOR_MATERIAL);
369 glEnable(GL_CULL_FACE);
371 glDisable(GL_DEPTH_TEST);
375 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
377 glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
378 glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
379 glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
380 glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
382 } else if( _mbg[0] != NULL ) {
383 for (int i = 0; i < 4; i ++) {
384 // top row of textures...(1,3,5,7)
386 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
388 glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4, _height/2);
389 glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
390 glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
391 glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4, _height);
393 // bottom row of textures...(2,4,6,8)
395 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
397 glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4, 0);
398 glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
399 glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
400 glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4, _height/2);
405 glGetFloatv( GL_CURRENT_COLOR, c );
406 glColor4f( 0.0, 0.0, 0.0, 1.0 );
409 glVertex2f(_width, 0);
410 glVertex2f(_width, _height);
411 glVertex2f(0, _height);
416 // Draw the instruments.
417 // Syd Adams: added instrument clipping
418 instrument_list_type::const_iterator current = _instruments.begin();
419 instrument_list_type::const_iterator end = _instruments.end();
421 GLdouble blx[4]={1.0,0.0,0.0,0.0};
422 GLdouble bly[4]={0.0,1.0,0.0,0.0};
423 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
424 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
426 for ( ; current != end; current++) {
427 FGPanelInstrument * instr = *current;
429 glTranslated(instr->getXPos(), instr->getYPos(), 0);
431 int ix= instr->getWidth();
432 int iy= instr->getHeight();
434 glTranslated(-ix/2,-iy/2,0);
435 glClipPlane(GL_CLIP_PLANE0,blx);
436 glClipPlane(GL_CLIP_PLANE1,bly);
437 glEnable(GL_CLIP_PLANE0);
438 glEnable(GL_CLIP_PLANE1);
440 glTranslated(ix,iy,0);
441 glClipPlane(GL_CLIP_PLANE2,urx);
442 glClipPlane(GL_CLIP_PLANE3,ury);
443 glEnable(GL_CLIP_PLANE2);
444 glEnable(GL_CLIP_PLANE3);
451 glDisable(GL_CLIP_PLANE0);
452 glDisable(GL_CLIP_PLANE1);
453 glDisable(GL_CLIP_PLANE2);
454 glDisable(GL_CLIP_PLANE3);
456 // restore some original state
462 * Set the panel's background texture.
465 FGPanel::setBackground (FGCroppedTexture_ptr texture)
471 * Set the panel's multiple background textures.
474 FGPanel::setMultiBackground (FGCroppedTexture_ptr texture, int idx)
480 ////////////////////////////////////////////////////////////////////////
481 // Implementation of FGPanelTransformation.
482 ////////////////////////////////////////////////////////////////////////
484 FGPanelTransformation::FGPanelTransformation ()
489 FGPanelTransformation::~FGPanelTransformation ()
496 ////////////////////////////////////////////////////////////////////////
497 // Implementation of FGPanelInstrument.
498 ////////////////////////////////////////////////////////////////////////
501 FGPanelInstrument::FGPanelInstrument ()
507 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
513 FGPanelInstrument::~FGPanelInstrument ()
518 FGPanelInstrument::setPosition (int x, int y)
525 FGPanelInstrument::setSize (int w, int h)
532 FGPanelInstrument::getXPos () const
538 FGPanelInstrument::getYPos () const
544 FGPanelInstrument::getWidth () const
550 FGPanelInstrument::getHeight () const
556 ////////////////////////////////////////////////////////////////////////
557 // Implementation of FGLayeredInstrument.
558 ////////////////////////////////////////////////////////////////////////
560 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
561 : FGPanelInstrument(x, y, w, h)
565 FGLayeredInstrument::~FGLayeredInstrument ()
567 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
574 FGLayeredInstrument::draw ()
579 for (int i = 0; i < (int)_layers.size(); i++) {
587 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
589 int n = _layers.size();
590 if (layer->getWidth() == -1) {
591 layer->setWidth(getWidth());
593 if (layer->getHeight() == -1) {
594 layer->setHeight(getHeight());
596 _layers.push_back(layer);
601 FGLayeredInstrument::addLayer (FGCroppedTexture_ptr texture, int w, int h)
603 return addLayer(new FGTexturedLayer(texture, w, h));
607 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
609 int layer = _layers.size() - 1;
610 _layers[layer]->addTransformation(transformation);
615 ////////////////////////////////////////////////////////////////////////
616 // Implementation of FGInstrumentLayer.
617 ////////////////////////////////////////////////////////////////////////
619 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
625 FGInstrumentLayer::~FGInstrumentLayer ()
627 for (transformation_list::iterator it = _transformations.begin();
628 it != _transformations.end();
636 FGInstrumentLayer::transform () const
638 transformation_list::const_iterator it = _transformations.begin();
639 transformation_list::const_iterator last = _transformations.end();
641 FGPanelTransformation *t = *it;
643 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
646 val = fmod(val, t->mod);
649 } else if (val > t->max) {
654 val = val * t->factor + t->offset;
656 val = t->table->interpolate(val) * t->factor + t->offset;
660 case FGPanelTransformation::XSHIFT:
661 glTranslatef(val, 0.0, 0.0);
663 case FGPanelTransformation::YSHIFT:
664 glTranslatef(0.0, val, 0.0);
666 case FGPanelTransformation::ROTATION:
667 glRotatef(-val, 0.0, 0.0, 1.0);
676 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
678 _transformations.push_back(transformation);
683 ////////////////////////////////////////////////////////////////////////
684 // Implementation of FGGroupLayer.
685 ////////////////////////////////////////////////////////////////////////
687 FGGroupLayer::FGGroupLayer ()
691 FGGroupLayer::~FGGroupLayer ()
693 for (unsigned int i = 0; i < _layers.size(); i++)
698 FGGroupLayer::draw ()
702 int nLayers = _layers.size();
703 for (int i = 0; i < nLayers; i++)
709 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
711 _layers.push_back(layer);
716 ////////////////////////////////////////////////////////////////////////
717 // Implementation of FGTexturedLayer.
718 ////////////////////////////////////////////////////////////////////////
721 FGTexturedLayer::FGTexturedLayer (FGCroppedTexture_ptr texture, int w, int h)
722 : FGInstrumentLayer(w, h),
730 FGTexturedLayer::~FGTexturedLayer ()
735 FGTexturedLayer::getDisplayList()
737 if( displayList != 0 )
743 _texture->bind( false );
744 displayList = glGenLists(1);
745 glNewList(displayList,GL_COMPILE_AND_EXECUTE);
746 glBindTexture( GL_TEXTURE_2D, _texture->getTexture() );
748 glTexCoord2f(_texture->getMinX(), _texture->getMinY()); glVertex2f(-w2, -h2);
749 glTexCoord2f(_texture->getMaxX(), _texture->getMinY()); glVertex2f(w2, -h2);
750 glTexCoord2f(_texture->getMaxX(), _texture->getMaxY()); glVertex2f(w2, h2);
751 glTexCoord2f(_texture->getMinX(), _texture->getMaxY()); glVertex2f(-w2, h2);
759 FGTexturedLayer::draw ( )
763 glCallList(getDisplayList());
769 ////////////////////////////////////////////////////////////////////////
770 // Implementation of FGTextLayer.
771 ////////////////////////////////////////////////////////////////////////
773 fntRenderer FGTextLayer::text_renderer;
775 FGTextLayer::FGTextLayer (int w, int h)
776 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
779 _color[0] = _color[1] = _color[2] = 0.0;
783 FGTextLayer::~FGTextLayer ()
785 chunk_list::iterator it = _chunks.begin();
786 chunk_list::iterator last = _chunks.end();
787 for ( ; it != last; it++) {
797 glGetFloatv( GL_CURRENT_COLOR, c );
801 text_renderer.setFont(ApplicationProperties::fontCache.getTexFont(_font_name.c_str()));
803 text_renderer.setPointSize(_pointSize);
804 text_renderer.begin();
805 text_renderer.start3f(0, 0, 0);
808 long diff = (_now - _then).toUSecs();
810 if (diff > 100000 || diff < 0 ) {
811 // ( diff < 0 ) is a sanity check and indicates our time stamp
812 // difference math probably overflowed. We can handle a max
813 // difference of 35.8 minutes since the returned value is in
814 // usec. So if the panel is left off longer than that we can
815 // over flow the math with it is turned back on. This (diff <
816 // 0) catches that situation, get's us out of trouble, and
822 // Something is goofy. The code in this file renders only CCW
823 // polygons, and I have verified that the font code in plib
824 // renders only CCW trianbles. Yet they come out backwards.
825 // Something around here or in plib is either changing the winding
826 // order or (more likely) pushing a left-handed matrix onto the
827 // stack. But I can't find it; get out the chainsaw...
829 text_renderer.puts((char *)(_value.c_str()));
838 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
840 _chunks.push_back(chunk);
844 FGTextLayer::setColor (float r, float g, float b)
853 FGTextLayer::setPointSize (float size)
859 FGTextLayer::setFontName(const string &name)
861 _font_name = name + ".txf";
866 FGTextLayer::setFont(fntFont * font)
868 FGTextLayer::text_renderer.setFont(font);
873 FGTextLayer::recalc_value () const
876 chunk_list::const_iterator it = _chunks.begin();
877 chunk_list::const_iterator last = _chunks.end();
878 for ( ; it != last; it++) {
879 _value += (*it)->getValue();
885 ////////////////////////////////////////////////////////////////////////
886 // Implementation of FGTextLayer::Chunk.
887 ////////////////////////////////////////////////////////////////////////
889 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
890 : _type(FGTextLayer::TEXT), _fmt(fmt)
897 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
898 const string &fmt, float mult, float offs,
900 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
903 if (type == TEXT_VALUE)
912 FGTextLayer::Chunk::getValue () const
918 sprintf(_buf, _fmt.c_str(), _text.c_str());
921 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
924 double d = _offs + _node->getFloatValue() * _mult;
925 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
926 sprintf(_buf, _fmt.c_str(), d);
937 ////////////////////////////////////////////////////////////////////////
938 // Implementation of FGSwitchLayer.
939 ////////////////////////////////////////////////////////////////////////
941 FGSwitchLayer::FGSwitchLayer ()
947 FGSwitchLayer::draw ()
951 int nLayers = _layers.size();
952 for (int i = 0; i < nLayers; i++) {
953 if (_layers[i]->test()) {