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>
35 #include <GLUT/glut.h>
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/math/SGMath.hxx>
44 #include <simgear/misc/sg_path.hxx>
47 #include "ApplicationProperties.hxx"
48 ////////////////////////////////////////////////////////////////////////
50 ////////////////////////////////////////////////////////////////////////
52 class FGDummyTextureLoader : public FGTextureLoaderInterface {
54 virtual GLuint loadTexture( const string & filename );
57 GLuint FGDummyTextureLoader::loadTexture( const string & filename )
60 glGenTextures( 1, &_texture );
61 glBindTexture( GL_TEXTURE_2D, _texture );
63 // glTexEnvi ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
64 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) ;
65 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ) ;
66 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
67 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;
69 GLubyte image[ 2 * 2 * 3 ] ;
71 /* Red and white chequerboard */
72 image [ 0 ] = 255 ; image [ 1 ] = 0 ; image [ 2 ] = 0 ;
73 image [ 3 ] = 255 ; image [ 4 ] = 255 ; image [ 5 ] = 255 ;
74 image [ 6 ] = 255 ; image [ 7 ] = 255 ; image [ 8 ] = 255 ;
75 image [ 9 ] = 255 ; image [ 10] = 0 ; image [ 11] = 0 ;
77 glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, 2, 2, 0,
78 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image);
83 ////////////////////////////////////////////////////////////////////////
84 // Implementation of FGCropped Texture.
85 ////////////////////////////////////////////////////////////////////////
87 GLuint FGCroppedTexture::current_bound_texture = 0;
88 map<string,GLuint> FGCroppedTexture::cache;
89 map<string,FGTextureLoaderInterface*> FGCroppedTexture::textureLoader;
90 static FGDummyTextureLoader dummyTextureLoader;
92 FGCroppedTexture::FGCroppedTexture (const string &path,
93 float minX, float minY,
94 float maxX, float maxY)
96 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY), _texture(0)
100 FGCroppedTexture::~FGCroppedTexture ()
104 void FGCroppedTexture::bind( bool doGLBind )
106 if( _texture == 0 ) {
107 SG_LOG( SG_COCKPIT, SG_DEBUG, "First bind of texture " << _path );
108 if( cache.count(_path) > 0 ) {
109 _texture = cache[_path];
110 SG_LOG( SG_COCKPIT, SG_DEBUG, "Using texture " << _path << " from cache (#" << _texture << ")" );
112 SGPath tpath = ApplicationProperties::GetRootPath(_path.c_str());
113 string extension = tpath.extension();
114 FGTextureLoaderInterface * loader = &dummyTextureLoader;
115 if( textureLoader.count( extension ) == 0 ) {
116 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't handle textures of type " << extension );
118 loader = textureLoader[extension];
121 _texture = loader->loadTexture( tpath.c_str() );
122 SG_LOG( SG_COCKPIT, SG_DEBUG, "Texture " << tpath.c_str() << " loaded from file as #" << _texture );
124 cache[_path] = _texture;
128 if( !doGLBind || current_bound_texture == _texture )
131 glBindTexture( GL_TEXTURE_2D, _texture );
132 current_bound_texture = _texture;
136 ////////////////////////////////////////////////////////////////////////
137 // Implementation of FGPanel.
138 ////////////////////////////////////////////////////////////////////////
143 FGPanel::FGPanel ( SGPropertyNode_ptr root)
145 _flipx(root->getNode("/sim/panel/flip-x", true)),
146 _rotate(root->getNode("/sim/panel/rotate-deg", true)),
147 _bg_width(1.0), _bg_height(1.0),
158 for (instrument_list_type::iterator it = _instruments.begin();
159 it != _instruments.end();
168 * Add an instrument to the panel.
171 FGPanel::addInstrument (FGPanelInstrument * instrument)
173 _instruments.push_back(instrument);
178 * Initialize the panel.
187 * Bind panel properties.
196 * Unbind panel properties.
203 GLuint FGPanel::getInitDisplayList()
205 if( initDisplayList != 0 ) return initDisplayList;
206 glMatrixMode(GL_PROJECTION);
208 if ( _flipx->getBoolValue() ) {
209 gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
211 gluOrtho2D( 0, _width, 0, _height ); /* right side up */
214 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
216 glMatrixMode(GL_MODELVIEW);
219 glClear( GL_COLOR_BUFFER_BIT);
222 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
223 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE
224 | GL_DEPTH_BUFFER_BIT );
226 // Draw the background
227 glEnable(GL_TEXTURE_2D);
229 glDisable(GL_LIGHTING);
231 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
232 glEnable(GL_ALPHA_TEST);
233 glEnable(GL_COLOR_MATERIAL);
234 glEnable(GL_CULL_FACE);
236 glDisable(GL_DEPTH_TEST);
240 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
242 glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
243 glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
244 glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
245 glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
247 } else if( _mbg[0] != NULL ) {
248 for (int i = 0; i < 4; i ++) {
249 // top row of textures...(1,3,5,7)
251 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
253 glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4, _height/2);
254 glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
255 glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
256 glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4, _height);
258 // bottom row of textures...(2,4,6,8)
260 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
262 glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4, 0);
263 glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
264 glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
265 glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4, _height/2);
270 glGetFloatv( GL_CURRENT_COLOR, c );
271 glColor4f( 0.0, 0.0, 0.0, 1.0 );
274 glVertex2f(_width, 0);
275 glVertex2f(_width, _height);
276 glVertex2f(0, _height);
282 return initDisplayList;
286 FGPanel::update (double dt)
288 glCallList(getInitDisplayList());
290 // Draw the instruments.
291 // Syd Adams: added instrument clipping
292 instrument_list_type::const_iterator current = _instruments.begin();
293 instrument_list_type::const_iterator end = _instruments.end();
295 GLdouble blx[4]={1.0,0.0,0.0,0.0};
296 GLdouble bly[4]={0.0,1.0,0.0,0.0};
297 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
298 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
300 for ( ; current != end; current++) {
301 FGPanelInstrument * instr = *current;
303 glTranslated(instr->getXPos(), instr->getYPos(), 0);
305 int ix= instr->getWidth();
306 int iy= instr->getHeight();
308 glTranslated(-ix/2,-iy/2,0);
309 glClipPlane(GL_CLIP_PLANE0,blx);
310 glClipPlane(GL_CLIP_PLANE1,bly);
311 glEnable(GL_CLIP_PLANE0);
312 glEnable(GL_CLIP_PLANE1);
314 glTranslated(ix,iy,0);
315 glClipPlane(GL_CLIP_PLANE2,urx);
316 glClipPlane(GL_CLIP_PLANE3,ury);
317 glEnable(GL_CLIP_PLANE2);
318 glEnable(GL_CLIP_PLANE3);
325 glDisable(GL_CLIP_PLANE0);
326 glDisable(GL_CLIP_PLANE1);
327 glDisable(GL_CLIP_PLANE2);
328 glDisable(GL_CLIP_PLANE3);
330 // restore some original state
339 FGPanel::update (double dt)
341 glMatrixMode(GL_PROJECTION);
343 if ( _flipx->getBoolValue() ) {
344 gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
346 gluOrtho2D( 0, _width, 0, _height ); /* right side up */
349 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
351 glMatrixMode(GL_MODELVIEW);
359 glClear( GL_COLOR_BUFFER_BIT);
362 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
363 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE
364 | GL_DEPTH_BUFFER_BIT );
366 // Draw the background
367 glEnable(GL_TEXTURE_2D);
369 glDisable(GL_LIGHTING);
371 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
372 glEnable(GL_ALPHA_TEST);
373 glEnable(GL_COLOR_MATERIAL);
374 glEnable(GL_CULL_FACE);
376 glDisable(GL_DEPTH_TEST);
380 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
382 glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
383 glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
384 glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
385 glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
387 } else if( _mbg[0] != NULL ) {
388 for (int i = 0; i < 4; i ++) {
389 // top row of textures...(1,3,5,7)
391 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
393 glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4, _height/2);
394 glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
395 glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
396 glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4, _height);
398 // bottom row of textures...(2,4,6,8)
400 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
402 glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4, 0);
403 glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
404 glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
405 glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4, _height/2);
410 glGetFloatv( GL_CURRENT_COLOR, c );
411 glColor4f( 0.0, 0.0, 0.0, 1.0 );
414 glVertex2f(_width, 0);
415 glVertex2f(_width, _height);
416 glVertex2f(0, _height);
421 // Draw the instruments.
422 // Syd Adams: added instrument clipping
423 instrument_list_type::const_iterator current = _instruments.begin();
424 instrument_list_type::const_iterator end = _instruments.end();
426 GLdouble blx[4]={1.0,0.0,0.0,0.0};
427 GLdouble bly[4]={0.0,1.0,0.0,0.0};
428 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
429 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
431 for ( ; current != end; current++) {
432 FGPanelInstrument * instr = *current;
434 glTranslated(instr->getXPos(), instr->getYPos(), 0);
436 int ix= instr->getWidth();
437 int iy= instr->getHeight();
439 glTranslated(-ix/2,-iy/2,0);
440 glClipPlane(GL_CLIP_PLANE0,blx);
441 glClipPlane(GL_CLIP_PLANE1,bly);
442 glEnable(GL_CLIP_PLANE0);
443 glEnable(GL_CLIP_PLANE1);
445 glTranslated(ix,iy,0);
446 glClipPlane(GL_CLIP_PLANE2,urx);
447 glClipPlane(GL_CLIP_PLANE3,ury);
448 glEnable(GL_CLIP_PLANE2);
449 glEnable(GL_CLIP_PLANE3);
456 glDisable(GL_CLIP_PLANE0);
457 glDisable(GL_CLIP_PLANE1);
458 glDisable(GL_CLIP_PLANE2);
459 glDisable(GL_CLIP_PLANE3);
461 // restore some original state
467 * Set the panel's background texture.
470 FGPanel::setBackground (FGCroppedTexture_ptr texture)
476 * Set the panel's multiple background textures.
479 FGPanel::setMultiBackground (FGCroppedTexture_ptr texture, int idx)
485 ////////////////////////////////////////////////////////////////////////
486 // Implementation of FGPanelTransformation.
487 ////////////////////////////////////////////////////////////////////////
489 FGPanelTransformation::FGPanelTransformation ()
494 FGPanelTransformation::~FGPanelTransformation ()
501 ////////////////////////////////////////////////////////////////////////
502 // Implementation of FGPanelInstrument.
503 ////////////////////////////////////////////////////////////////////////
506 FGPanelInstrument::FGPanelInstrument ()
512 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
518 FGPanelInstrument::~FGPanelInstrument ()
523 FGPanelInstrument::setPosition (int x, int y)
530 FGPanelInstrument::setSize (int w, int h)
537 FGPanelInstrument::getXPos () const
543 FGPanelInstrument::getYPos () const
549 FGPanelInstrument::getWidth () const
555 FGPanelInstrument::getHeight () const
561 ////////////////////////////////////////////////////////////////////////
562 // Implementation of FGLayeredInstrument.
563 ////////////////////////////////////////////////////////////////////////
565 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
566 : FGPanelInstrument(x, y, w, h)
570 FGLayeredInstrument::~FGLayeredInstrument ()
572 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
579 FGLayeredInstrument::draw ()
584 for (int i = 0; i < (int)_layers.size(); i++) {
592 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
594 int n = _layers.size();
595 if (layer->getWidth() == -1) {
596 layer->setWidth(getWidth());
598 if (layer->getHeight() == -1) {
599 layer->setHeight(getHeight());
601 _layers.push_back(layer);
606 FGLayeredInstrument::addLayer (FGCroppedTexture_ptr texture, int w, int h)
608 return addLayer(new FGTexturedLayer(texture, w, h));
612 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
614 int layer = _layers.size() - 1;
615 _layers[layer]->addTransformation(transformation);
620 ////////////////////////////////////////////////////////////////////////
621 // Implementation of FGInstrumentLayer.
622 ////////////////////////////////////////////////////////////////////////
624 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
630 FGInstrumentLayer::~FGInstrumentLayer ()
632 for (transformation_list::iterator it = _transformations.begin();
633 it != _transformations.end();
641 FGInstrumentLayer::transform () const
643 transformation_list::const_iterator it = _transformations.begin();
644 transformation_list::const_iterator last = _transformations.end();
646 FGPanelTransformation *t = *it;
648 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
651 val = fmod(val, t->mod);
654 } else if (val > t->max) {
659 val = val * t->factor + t->offset;
661 val = t->table->interpolate(val) * t->factor + t->offset;
665 case FGPanelTransformation::XSHIFT:
666 glTranslatef(val, 0.0, 0.0);
668 case FGPanelTransformation::YSHIFT:
669 glTranslatef(0.0, val, 0.0);
671 case FGPanelTransformation::ROTATION:
672 glRotatef(-val, 0.0, 0.0, 1.0);
681 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
683 _transformations.push_back(transformation);
688 ////////////////////////////////////////////////////////////////////////
689 // Implementation of FGGroupLayer.
690 ////////////////////////////////////////////////////////////////////////
692 FGGroupLayer::FGGroupLayer ()
696 FGGroupLayer::~FGGroupLayer ()
698 for (unsigned int i = 0; i < _layers.size(); i++)
703 FGGroupLayer::draw ()
707 int nLayers = _layers.size();
708 for (int i = 0; i < nLayers; i++)
714 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
716 _layers.push_back(layer);
721 ////////////////////////////////////////////////////////////////////////
722 // Implementation of FGTexturedLayer.
723 ////////////////////////////////////////////////////////////////////////
726 FGTexturedLayer::FGTexturedLayer (FGCroppedTexture_ptr texture, int w, int h)
727 : FGInstrumentLayer(w, h),
735 FGTexturedLayer::~FGTexturedLayer ()
740 FGTexturedLayer::getDisplayList()
742 if( displayList != 0 )
748 _texture->bind( false );
749 displayList = glGenLists(1);
750 glNewList(displayList,GL_COMPILE_AND_EXECUTE);
751 glBindTexture( GL_TEXTURE_2D, _texture->getTexture() );
753 glTexCoord2f(_texture->getMinX(), _texture->getMinY()); glVertex2f(-w2, -h2);
754 glTexCoord2f(_texture->getMaxX(), _texture->getMinY()); glVertex2f(w2, -h2);
755 glTexCoord2f(_texture->getMaxX(), _texture->getMaxY()); glVertex2f(w2, h2);
756 glTexCoord2f(_texture->getMinX(), _texture->getMaxY()); glVertex2f(-w2, h2);
764 FGTexturedLayer::draw ( )
768 glCallList(getDisplayList());
774 ////////////////////////////////////////////////////////////////////////
775 // Implementation of FGTextLayer.
776 ////////////////////////////////////////////////////////////////////////
778 fntRenderer FGTextLayer::text_renderer;
780 FGTextLayer::FGTextLayer (int w, int h)
781 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
784 _color[0] = _color[1] = _color[2] = 0.0;
788 FGTextLayer::~FGTextLayer ()
790 chunk_list::iterator it = _chunks.begin();
791 chunk_list::iterator last = _chunks.end();
792 for ( ; it != last; it++) {
802 glGetFloatv( GL_CURRENT_COLOR, c );
806 text_renderer.setFont(ApplicationProperties::fontCache.getTexFont(_font_name.c_str()));
807 if (!text_renderer.getFont())
809 SG_LOG( SG_COCKPIT, SG_ALERT, "Missing font file: " << _font_name );
813 text_renderer.setPointSize(_pointSize);
814 text_renderer.begin();
815 text_renderer.start3f(0, 0, 0);
818 long diff = (_now - _then).toUSecs();
820 if (diff > 100000 || diff < 0 ) {
821 // ( diff < 0 ) is a sanity check and indicates our time stamp
822 // difference math probably overflowed. We can handle a max
823 // difference of 35.8 minutes since the returned value is in
824 // usec. So if the panel is left off longer than that we can
825 // over flow the math with it is turned back on. This (diff <
826 // 0) catches that situation, get's us out of trouble, and
832 // Something is goofy. The code in this file renders only CCW
833 // polygons, and I have verified that the font code in plib
834 // renders only CCW trianbles. Yet they come out backwards.
835 // Something around here or in plib is either changing the winding
836 // order or (more likely) pushing a left-handed matrix onto the
837 // stack. But I can't find it; get out the chainsaw...
839 text_renderer.puts((char *)(_value.c_str()));
848 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
850 _chunks.push_back(chunk);
854 FGTextLayer::setColor (float r, float g, float b)
863 FGTextLayer::setPointSize (float size)
869 FGTextLayer::setFontName(const string &name)
871 _font_name = name + ".txf";
876 FGTextLayer::setFont(fntFont * font)
878 FGTextLayer::text_renderer.setFont(font);
883 FGTextLayer::recalc_value () const
886 chunk_list::const_iterator it = _chunks.begin();
887 chunk_list::const_iterator last = _chunks.end();
888 for ( ; it != last; it++) {
889 _value += (*it)->getValue();
895 ////////////////////////////////////////////////////////////////////////
896 // Implementation of FGTextLayer::Chunk.
897 ////////////////////////////////////////////////////////////////////////
899 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
900 : _type(FGTextLayer::TEXT), _fmt(fmt)
907 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
908 const string &fmt, float mult, float offs,
910 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
913 if (type == TEXT_VALUE)
922 FGTextLayer::Chunk::getValue () const
928 sprintf(_buf, _fmt.c_str(), _text.c_str());
931 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
934 double d = _offs + _node->getFloatValue() * _mult;
935 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
936 sprintf(_buf, _fmt.c_str(), d);
947 ////////////////////////////////////////////////////////////////////////
948 // Implementation of FGSwitchLayer.
949 ////////////////////////////////////////////////////////////////////////
951 FGSwitchLayer::FGSwitchLayer ()
957 FGSwitchLayer::draw ()
961 int nLayers = _layers.size();
962 for (int i = 0; i < nLayers; i++) {
963 if (_layers[i]->test()) {