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/misc/sg_path.hxx>
46 #include "ApplicationProperties.hxx"
47 ////////////////////////////////////////////////////////////////////////
49 ////////////////////////////////////////////////////////////////////////
51 class FGDummyTextureLoader : public FGTextureLoaderInterface {
53 virtual GLuint loadTexture( const string & filename );
56 GLuint FGDummyTextureLoader::loadTexture( const string & filename )
59 glGenTextures( 1, &_texture );
60 glBindTexture( GL_TEXTURE_2D, _texture );
62 // glTexEnvi ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
63 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) ;
64 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ) ;
65 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
66 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;
68 GLubyte image[ 2 * 2 * 3 ] ;
70 /* Red and white chequerboard */
71 image [ 0 ] = 255 ; image [ 1 ] = 0 ; image [ 2 ] = 0 ;
72 image [ 3 ] = 255 ; image [ 4 ] = 255 ; image [ 5 ] = 255 ;
73 image [ 6 ] = 255 ; image [ 7 ] = 255 ; image [ 8 ] = 255 ;
74 image [ 9 ] = 255 ; image [ 10] = 0 ; image [ 11] = 0 ;
76 glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, 2, 2, 0,
77 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image);
82 ////////////////////////////////////////////////////////////////////////
83 // Implementation of FGCropped Texture.
84 ////////////////////////////////////////////////////////////////////////
86 GLuint FGCroppedTexture::current_bound_texture = 0;
87 map<string,GLuint> FGCroppedTexture::cache;
88 map<string,FGTextureLoaderInterface*> FGCroppedTexture::textureLoader;
89 static FGDummyTextureLoader dummyTextureLoader;
91 FGCroppedTexture::FGCroppedTexture (const string &path,
92 float minX, float minY,
93 float maxX, float maxY)
95 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY), _texture(0)
99 FGCroppedTexture::~FGCroppedTexture ()
103 void FGCroppedTexture::bind( bool doGLBind )
105 if( _texture == 0 ) {
106 SG_LOG( SG_COCKPIT, SG_DEBUG, "First bind of texture " << _path );
107 if( cache.count(_path) > 0 ) {
108 _texture = cache[_path];
109 SG_LOG( SG_COCKPIT, SG_DEBUG, "Using texture " << _path << " from cache (#" << _texture << ")" );
111 SGPath tpath = ApplicationProperties::GetRootPath(_path.c_str());
112 string extension = tpath.extension();
113 FGTextureLoaderInterface * loader = &dummyTextureLoader;
114 if( textureLoader.count( extension ) == 0 ) {
115 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't handle textures of type " << extension );
117 loader = textureLoader[extension];
120 _texture = loader->loadTexture( tpath.c_str() );
121 SG_LOG( SG_COCKPIT, SG_DEBUG, "Texture " << tpath.c_str() << " loaded from file as #" << _texture );
123 cache[_path] = _texture;
127 if( !doGLBind || current_bound_texture == _texture )
130 glBindTexture( GL_TEXTURE_2D, _texture );
131 current_bound_texture = _texture;
135 ////////////////////////////////////////////////////////////////////////
136 // Implementation of FGPanel.
137 ////////////////////////////////////////////////////////////////////////
142 FGPanel::FGPanel ( SGPropertyNode_ptr root)
144 _flipx(root->getNode("/sim/panel/flip-x", true)),
145 _rotate(root->getNode("/sim/panel/rotate-deg", true)),
146 _bg_width(1.0), _bg_height(1.0),
157 for (instrument_list_type::iterator it = _instruments.begin();
158 it != _instruments.end();
167 * Add an instrument to the panel.
170 FGPanel::addInstrument (FGPanelInstrument * instrument)
172 _instruments.push_back(instrument);
177 * Initialize the panel.
186 * Bind panel properties.
195 * Unbind panel properties.
202 GLuint FGPanel::getInitDisplayList()
204 if( initDisplayList != 0 ) return initDisplayList;
205 glMatrixMode(GL_PROJECTION);
207 if ( _flipx->getBoolValue() ) {
208 gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
210 gluOrtho2D( 0, _width, 0, _height ); /* right side up */
213 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
215 glMatrixMode(GL_MODELVIEW);
218 glClear( GL_COLOR_BUFFER_BIT);
221 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
222 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE
223 | GL_DEPTH_BUFFER_BIT );
225 // Draw the background
226 glEnable(GL_TEXTURE_2D);
228 glDisable(GL_LIGHTING);
230 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
231 glEnable(GL_ALPHA_TEST);
232 glEnable(GL_COLOR_MATERIAL);
233 glEnable(GL_CULL_FACE);
235 glDisable(GL_DEPTH_TEST);
239 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
241 glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
242 glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
243 glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
244 glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
246 } else if( _mbg[0] != NULL ) {
247 for (int i = 0; i < 4; i ++) {
248 // top row of textures...(1,3,5,7)
250 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
252 glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4, _height/2);
253 glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
254 glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
255 glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4, _height);
257 // bottom row of textures...(2,4,6,8)
259 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
261 glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4, 0);
262 glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
263 glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
264 glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4, _height/2);
269 glGetFloatv( GL_CURRENT_COLOR, c );
270 glColor4f( 0.0, 0.0, 0.0, 1.0 );
273 glVertex2f(_width, 0);
274 glVertex2f(_width, _height);
275 glVertex2f(0, _height);
281 return initDisplayList;
285 FGPanel::update (double dt)
287 /*glCallList*/(getInitDisplayList());
289 // Draw the instruments.
290 // Syd Adams: added instrument clipping
291 instrument_list_type::const_iterator current = _instruments.begin();
292 instrument_list_type::const_iterator end = _instruments.end();
294 GLdouble blx[4]={1.0,0.0,0.0,0.0};
295 GLdouble bly[4]={0.0,1.0,0.0,0.0};
296 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
297 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
299 for ( ; current != end; current++) {
300 FGPanelInstrument * instr = *current;
302 glTranslated(instr->getXPos(), instr->getYPos(), 0);
304 int ix= instr->getWidth();
305 int iy= instr->getHeight();
307 glTranslated(-ix/2,-iy/2,0);
308 glClipPlane(GL_CLIP_PLANE0,blx);
309 glClipPlane(GL_CLIP_PLANE1,bly);
310 glEnable(GL_CLIP_PLANE0);
311 glEnable(GL_CLIP_PLANE1);
313 glTranslated(ix,iy,0);
314 glClipPlane(GL_CLIP_PLANE2,urx);
315 glClipPlane(GL_CLIP_PLANE3,ury);
316 glEnable(GL_CLIP_PLANE2);
317 glEnable(GL_CLIP_PLANE3);
324 glDisable(GL_CLIP_PLANE0);
325 glDisable(GL_CLIP_PLANE1);
326 glDisable(GL_CLIP_PLANE2);
327 glDisable(GL_CLIP_PLANE3);
329 // restore some original state
338 FGPanel::update (double dt)
340 glMatrixMode(GL_PROJECTION);
342 if ( _flipx->getBoolValue() ) {
343 gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
345 gluOrtho2D( 0, _width, 0, _height ); /* right side up */
348 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
350 glMatrixMode(GL_MODELVIEW);
358 glClear( GL_COLOR_BUFFER_BIT);
361 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
362 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE
363 | GL_DEPTH_BUFFER_BIT );
365 // Draw the background
366 glEnable(GL_TEXTURE_2D);
368 glDisable(GL_LIGHTING);
370 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
371 glEnable(GL_ALPHA_TEST);
372 glEnable(GL_COLOR_MATERIAL);
373 glEnable(GL_CULL_FACE);
375 glDisable(GL_DEPTH_TEST);
379 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
381 glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
382 glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
383 glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
384 glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
386 } else if( _mbg[0] != NULL ) {
387 for (int i = 0; i < 4; i ++) {
388 // top row of textures...(1,3,5,7)
390 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
392 glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4, _height/2);
393 glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
394 glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
395 glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4, _height);
397 // bottom row of textures...(2,4,6,8)
399 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
401 glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4, 0);
402 glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
403 glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
404 glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4, _height/2);
409 glGetFloatv( GL_CURRENT_COLOR, c );
410 glColor4f( 0.0, 0.0, 0.0, 1.0 );
413 glVertex2f(_width, 0);
414 glVertex2f(_width, _height);
415 glVertex2f(0, _height);
420 // Draw the instruments.
421 // Syd Adams: added instrument clipping
422 instrument_list_type::const_iterator current = _instruments.begin();
423 instrument_list_type::const_iterator end = _instruments.end();
425 GLdouble blx[4]={1.0,0.0,0.0,0.0};
426 GLdouble bly[4]={0.0,1.0,0.0,0.0};
427 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
428 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
430 for ( ; current != end; current++) {
431 FGPanelInstrument * instr = *current;
433 glTranslated(instr->getXPos(), instr->getYPos(), 0);
435 int ix= instr->getWidth();
436 int iy= instr->getHeight();
438 glTranslated(-ix/2,-iy/2,0);
439 glClipPlane(GL_CLIP_PLANE0,blx);
440 glClipPlane(GL_CLIP_PLANE1,bly);
441 glEnable(GL_CLIP_PLANE0);
442 glEnable(GL_CLIP_PLANE1);
444 glTranslated(ix,iy,0);
445 glClipPlane(GL_CLIP_PLANE2,urx);
446 glClipPlane(GL_CLIP_PLANE3,ury);
447 glEnable(GL_CLIP_PLANE2);
448 glEnable(GL_CLIP_PLANE3);
455 glDisable(GL_CLIP_PLANE0);
456 glDisable(GL_CLIP_PLANE1);
457 glDisable(GL_CLIP_PLANE2);
458 glDisable(GL_CLIP_PLANE3);
460 // restore some original state
466 * Set the panel's background texture.
469 FGPanel::setBackground (FGCroppedTexture_ptr texture)
475 * Set the panel's multiple background textures.
478 FGPanel::setMultiBackground (FGCroppedTexture_ptr texture, int idx)
484 ////////////////////////////////////////////////////////////////////////
485 // Implementation of FGPanelTransformation.
486 ////////////////////////////////////////////////////////////////////////
488 FGPanelTransformation::FGPanelTransformation ()
493 FGPanelTransformation::~FGPanelTransformation ()
500 ////////////////////////////////////////////////////////////////////////
501 // Implementation of FGPanelInstrument.
502 ////////////////////////////////////////////////////////////////////////
505 FGPanelInstrument::FGPanelInstrument ()
511 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
517 FGPanelInstrument::~FGPanelInstrument ()
522 FGPanelInstrument::setPosition (int x, int y)
529 FGPanelInstrument::setSize (int w, int h)
536 FGPanelInstrument::getXPos () const
542 FGPanelInstrument::getYPos () const
548 FGPanelInstrument::getWidth () const
554 FGPanelInstrument::getHeight () const
560 ////////////////////////////////////////////////////////////////////////
561 // Implementation of FGLayeredInstrument.
562 ////////////////////////////////////////////////////////////////////////
564 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
565 : FGPanelInstrument(x, y, w, h)
569 FGLayeredInstrument::~FGLayeredInstrument ()
571 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
578 FGLayeredInstrument::draw ()
583 for (int i = 0; i < (int)_layers.size(); i++) {
591 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
593 int n = _layers.size();
594 if (layer->getWidth() == -1) {
595 layer->setWidth(getWidth());
597 if (layer->getHeight() == -1) {
598 layer->setHeight(getHeight());
600 _layers.push_back(layer);
605 FGLayeredInstrument::addLayer (FGCroppedTexture_ptr texture, int w, int h)
607 return addLayer(new FGTexturedLayer(texture, w, h));
611 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
613 int layer = _layers.size() - 1;
614 _layers[layer]->addTransformation(transformation);
619 ////////////////////////////////////////////////////////////////////////
620 // Implementation of FGInstrumentLayer.
621 ////////////////////////////////////////////////////////////////////////
623 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
629 FGInstrumentLayer::~FGInstrumentLayer ()
631 for (transformation_list::iterator it = _transformations.begin();
632 it != _transformations.end();
640 FGInstrumentLayer::transform () const
642 transformation_list::const_iterator it = _transformations.begin();
643 transformation_list::const_iterator last = _transformations.end();
645 FGPanelTransformation *t = *it;
647 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
650 val = fmod(val, t->mod);
653 } else if (val > t->max) {
658 val = val * t->factor + t->offset;
660 val = t->table->interpolate(val) * t->factor + t->offset;
664 case FGPanelTransformation::XSHIFT:
665 glTranslatef(val, 0.0, 0.0);
667 case FGPanelTransformation::YSHIFT:
668 glTranslatef(0.0, val, 0.0);
670 case FGPanelTransformation::ROTATION:
671 glRotatef(-val, 0.0, 0.0, 1.0);
680 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
682 _transformations.push_back(transformation);
687 ////////////////////////////////////////////////////////////////////////
688 // Implementation of FGGroupLayer.
689 ////////////////////////////////////////////////////////////////////////
691 FGGroupLayer::FGGroupLayer ()
695 FGGroupLayer::~FGGroupLayer ()
697 for (unsigned int i = 0; i < _layers.size(); i++)
702 FGGroupLayer::draw ()
706 int nLayers = _layers.size();
707 for (int i = 0; i < nLayers; i++)
713 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
715 _layers.push_back(layer);
720 ////////////////////////////////////////////////////////////////////////
721 // Implementation of FGTexturedLayer.
722 ////////////////////////////////////////////////////////////////////////
725 FGTexturedLayer::FGTexturedLayer (FGCroppedTexture_ptr texture, int w, int h)
726 : FGInstrumentLayer(w, h),
734 FGTexturedLayer::~FGTexturedLayer ()
739 FGTexturedLayer::getDisplayList()
741 if( displayList != 0 )
747 _texture->bind( false );
748 displayList = glGenLists(1);
749 glNewList(displayList,GL_COMPILE_AND_EXECUTE);
750 glBindTexture( GL_TEXTURE_2D, _texture->getTexture() );
752 glTexCoord2f(_texture->getMinX(), _texture->getMinY()); glVertex2f(-w2, -h2);
753 glTexCoord2f(_texture->getMaxX(), _texture->getMinY()); glVertex2f(w2, -h2);
754 glTexCoord2f(_texture->getMaxX(), _texture->getMaxY()); glVertex2f(w2, h2);
755 glTexCoord2f(_texture->getMinX(), _texture->getMaxY()); glVertex2f(-w2, h2);
763 FGTexturedLayer::draw ( )
767 glCallList(getDisplayList());
773 ////////////////////////////////////////////////////////////////////////
774 // Implementation of FGTextLayer.
775 ////////////////////////////////////////////////////////////////////////
777 fntRenderer FGTextLayer::text_renderer;
779 FGTextLayer::FGTextLayer (int w, int h)
780 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
783 _color[0] = _color[1] = _color[2] = 0.0;
787 FGTextLayer::~FGTextLayer ()
789 chunk_list::iterator it = _chunks.begin();
790 chunk_list::iterator last = _chunks.end();
791 for ( ; it != last; it++) {
801 glGetFloatv( GL_CURRENT_COLOR, c );
805 text_renderer.setFont(ApplicationProperties::fontCache.getTexFont(_font_name.c_str()));
806 if (!text_renderer.getFont())
808 SG_LOG( SG_COCKPIT, SG_ALERT, "Missing font file: " << _font_name );
812 text_renderer.setPointSize(_pointSize);
813 text_renderer.begin();
814 text_renderer.start3f(0, 0, 0);
817 long diff = (_now - _then).toUSecs();
819 if (diff > 100000 || diff < 0 ) {
820 // ( diff < 0 ) is a sanity check and indicates our time stamp
821 // difference math probably overflowed. We can handle a max
822 // difference of 35.8 minutes since the returned value is in
823 // usec. So if the panel is left off longer than that we can
824 // over flow the math with it is turned back on. This (diff <
825 // 0) catches that situation, get's us out of trouble, and
831 // Something is goofy. The code in this file renders only CCW
832 // polygons, and I have verified that the font code in plib
833 // renders only CCW trianbles. Yet they come out backwards.
834 // Something around here or in plib is either changing the winding
835 // order or (more likely) pushing a left-handed matrix onto the
836 // stack. But I can't find it; get out the chainsaw...
838 text_renderer.puts((char *)(_value.c_str()));
847 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
849 _chunks.push_back(chunk);
853 FGTextLayer::setColor (float r, float g, float b)
862 FGTextLayer::setPointSize (float size)
868 FGTextLayer::setFontName(const string &name)
870 _font_name = name + ".txf";
875 FGTextLayer::setFont(fntFont * font)
877 FGTextLayer::text_renderer.setFont(font);
882 FGTextLayer::recalc_value () const
885 chunk_list::const_iterator it = _chunks.begin();
886 chunk_list::const_iterator last = _chunks.end();
887 for ( ; it != last; it++) {
888 _value += (*it)->getValue();
894 ////////////////////////////////////////////////////////////////////////
895 // Implementation of FGTextLayer::Chunk.
896 ////////////////////////////////////////////////////////////////////////
898 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
899 : _type(FGTextLayer::TEXT), _fmt(fmt)
906 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
907 const string &fmt, float mult, float offs,
909 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
912 if (type == TEXT_VALUE)
921 FGTextLayer::Chunk::getValue () const
927 sprintf(_buf, _fmt.c_str(), _text.c_str());
930 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
933 double d = _offs + _node->getFloatValue() * _mult;
934 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
935 sprintf(_buf, _fmt.c_str(), d);
946 ////////////////////////////////////////////////////////////////////////
947 // Implementation of FGSwitchLayer.
948 ////////////////////////////////////////////////////////////////////////
950 FGSwitchLayer::FGSwitchLayer ()
956 FGSwitchLayer::draw ()
960 int nLayers = _layers.size();
961 for (int i = 0; i < nLayers; i++) {
962 if (_layers[i]->test()) {