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/math/SGMath.hxx>
40 #include <simgear/misc/sg_path.hxx>
43 #include "ApplicationProperties.hxx"
44 ////////////////////////////////////////////////////////////////////////
46 ////////////////////////////////////////////////////////////////////////
48 class FGDummyTextureLoader : public FGTextureLoaderInterface {
50 virtual GLuint loadTexture( const string & filename );
53 GLuint FGDummyTextureLoader::loadTexture( const string & filename )
56 glGenTextures( 1, &_texture );
57 glBindTexture( GL_TEXTURE_2D, _texture );
59 // glTexEnvi ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
60 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) ;
61 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ) ;
62 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
63 // glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;
65 GLubyte image[ 2 * 2 * 3 ] ;
67 /* Red and white chequerboard */
68 image [ 0 ] = 255 ; image [ 1 ] = 0 ; image [ 2 ] = 0 ;
69 image [ 3 ] = 255 ; image [ 4 ] = 255 ; image [ 5 ] = 255 ;
70 image [ 6 ] = 255 ; image [ 7 ] = 255 ; image [ 8 ] = 255 ;
71 image [ 9 ] = 255 ; image [ 10] = 0 ; image [ 11] = 0 ;
73 glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, 2, 2, 0,
74 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image);
79 ////////////////////////////////////////////////////////////////////////
80 // Implementation of FGCropped Texture.
81 ////////////////////////////////////////////////////////////////////////
83 GLuint FGCroppedTexture::current_bound_texture = 0;
84 map<string,GLuint> FGCroppedTexture::cache;
85 map<string,FGTextureLoaderInterface*> FGCroppedTexture::textureLoader;
86 static FGDummyTextureLoader dummyTextureLoader;
88 FGCroppedTexture::FGCroppedTexture (const string &path,
89 float minX, float minY,
90 float maxX, float maxY)
92 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY), _texture(0)
96 FGCroppedTexture::~FGCroppedTexture ()
100 void FGCroppedTexture::bind( bool doGLBind )
102 if( _texture == 0 ) {
103 SG_LOG( SG_COCKPIT, SG_DEBUG, "First bind of texture " << _path );
104 if( cache.count(_path) > 0 ) {
105 _texture = cache[_path];
106 SG_LOG( SG_COCKPIT, SG_DEBUG, "Using texture " << _path << " from cache (#" << _texture << ")" );
108 SGPath tpath = ApplicationProperties::GetRootPath(_path.c_str());
109 string extension = tpath.extension();
110 FGTextureLoaderInterface * loader = &dummyTextureLoader;
111 if( textureLoader.count( extension ) == 0 ) {
112 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't handle textures of type " << extension );
114 loader = textureLoader[extension];
117 _texture = loader->loadTexture( tpath.c_str() );
118 SG_LOG( SG_COCKPIT, SG_DEBUG, "Texture " << tpath.c_str() << " loaded from file as #" << _texture );
120 cache[_path] = _texture;
124 if( !doGLBind || current_bound_texture == _texture )
127 glBindTexture( GL_TEXTURE_2D, _texture );
128 current_bound_texture = _texture;
132 ////////////////////////////////////////////////////////////////////////
133 // Implementation of FGPanel.
134 ////////////////////////////////////////////////////////////////////////
139 FGPanel::FGPanel ( SGPropertyNode_ptr root)
141 _flipx(root->getNode("/sim/panel/flip-x", true)),
142 _rotate(root->getNode("/sim/panel/rotate-deg", true)),
143 _bg_width(1.0), _bg_height(1.0),
154 for (instrument_list_type::iterator it = _instruments.begin();
155 it != _instruments.end();
164 * Add an instrument to the panel.
167 FGPanel::addInstrument (FGPanelInstrument * instrument)
169 _instruments.push_back(instrument);
174 * Initialize the panel.
183 * Bind panel properties.
192 * Unbind panel properties.
199 GLuint FGPanel::getInitDisplayList()
201 if( initDisplayList != 0 ) return initDisplayList;
202 glMatrixMode(GL_PROJECTION);
204 if ( _flipx->getBoolValue() ) {
205 gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
207 gluOrtho2D( 0, _width, 0, _height ); /* right side up */
210 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
212 glMatrixMode(GL_MODELVIEW);
215 glClear( GL_COLOR_BUFFER_BIT);
218 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
219 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE
220 | GL_DEPTH_BUFFER_BIT );
222 // Draw the background
223 glEnable(GL_TEXTURE_2D);
225 glDisable(GL_LIGHTING);
227 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
228 glEnable(GL_ALPHA_TEST);
229 glEnable(GL_COLOR_MATERIAL);
230 glEnable(GL_CULL_FACE);
232 glDisable(GL_DEPTH_TEST);
236 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
238 glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
239 glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
240 glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
241 glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
243 } else if( _mbg[0] != NULL ) {
244 for (int i = 0; i < 4; i ++) {
245 // top row of textures...(1,3,5,7)
247 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
249 glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4, _height/2);
250 glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
251 glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
252 glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4, _height);
254 // bottom row of textures...(2,4,6,8)
256 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
258 glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4, 0);
259 glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
260 glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
261 glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4, _height/2);
266 glGetFloatv( GL_CURRENT_COLOR, c );
267 glColor4f( 0.0, 0.0, 0.0, 1.0 );
270 glVertex2f(_width, 0);
271 glVertex2f(_width, _height);
272 glVertex2f(0, _height);
278 return initDisplayList;
282 FGPanel::update (double dt)
284 glCallList(getInitDisplayList());
286 // Draw the instruments.
287 // Syd Adams: added instrument clipping
288 instrument_list_type::const_iterator current = _instruments.begin();
289 instrument_list_type::const_iterator end = _instruments.end();
291 GLdouble blx[4]={1.0,0.0,0.0,0.0};
292 GLdouble bly[4]={0.0,1.0,0.0,0.0};
293 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
294 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
296 for ( ; current != end; current++) {
297 FGPanelInstrument * instr = *current;
299 glTranslated(instr->getXPos(), instr->getYPos(), 0);
301 int ix= instr->getWidth();
302 int iy= instr->getHeight();
304 glTranslated(-ix/2,-iy/2,0);
305 glClipPlane(GL_CLIP_PLANE0,blx);
306 glClipPlane(GL_CLIP_PLANE1,bly);
307 glEnable(GL_CLIP_PLANE0);
308 glEnable(GL_CLIP_PLANE1);
310 glTranslated(ix,iy,0);
311 glClipPlane(GL_CLIP_PLANE2,urx);
312 glClipPlane(GL_CLIP_PLANE3,ury);
313 glEnable(GL_CLIP_PLANE2);
314 glEnable(GL_CLIP_PLANE3);
321 glDisable(GL_CLIP_PLANE0);
322 glDisable(GL_CLIP_PLANE1);
323 glDisable(GL_CLIP_PLANE2);
324 glDisable(GL_CLIP_PLANE3);
326 // restore some original state
335 FGPanel::update (double dt)
337 glMatrixMode(GL_PROJECTION);
339 if ( _flipx->getBoolValue() ) {
340 gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
342 gluOrtho2D( 0, _width, 0, _height ); /* right side up */
345 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
347 glMatrixMode(GL_MODELVIEW);
355 glClear( GL_COLOR_BUFFER_BIT);
358 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
359 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE
360 | GL_DEPTH_BUFFER_BIT );
362 // Draw the background
363 glEnable(GL_TEXTURE_2D);
365 glDisable(GL_LIGHTING);
367 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
368 glEnable(GL_ALPHA_TEST);
369 glEnable(GL_COLOR_MATERIAL);
370 glEnable(GL_CULL_FACE);
372 glDisable(GL_DEPTH_TEST);
376 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
378 glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
379 glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
380 glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
381 glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
383 } else if( _mbg[0] != NULL ) {
384 for (int i = 0; i < 4; i ++) {
385 // top row of textures...(1,3,5,7)
387 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
389 glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4, _height/2);
390 glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
391 glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
392 glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4, _height);
394 // bottom row of textures...(2,4,6,8)
396 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
398 glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4, 0);
399 glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
400 glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
401 glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4, _height/2);
406 glGetFloatv( GL_CURRENT_COLOR, c );
407 glColor4f( 0.0, 0.0, 0.0, 1.0 );
410 glVertex2f(_width, 0);
411 glVertex2f(_width, _height);
412 glVertex2f(0, _height);
417 // Draw the instruments.
418 // Syd Adams: added instrument clipping
419 instrument_list_type::const_iterator current = _instruments.begin();
420 instrument_list_type::const_iterator end = _instruments.end();
422 GLdouble blx[4]={1.0,0.0,0.0,0.0};
423 GLdouble bly[4]={0.0,1.0,0.0,0.0};
424 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
425 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
427 for ( ; current != end; current++) {
428 FGPanelInstrument * instr = *current;
430 glTranslated(instr->getXPos(), instr->getYPos(), 0);
432 int ix= instr->getWidth();
433 int iy= instr->getHeight();
435 glTranslated(-ix/2,-iy/2,0);
436 glClipPlane(GL_CLIP_PLANE0,blx);
437 glClipPlane(GL_CLIP_PLANE1,bly);
438 glEnable(GL_CLIP_PLANE0);
439 glEnable(GL_CLIP_PLANE1);
441 glTranslated(ix,iy,0);
442 glClipPlane(GL_CLIP_PLANE2,urx);
443 glClipPlane(GL_CLIP_PLANE3,ury);
444 glEnable(GL_CLIP_PLANE2);
445 glEnable(GL_CLIP_PLANE3);
452 glDisable(GL_CLIP_PLANE0);
453 glDisable(GL_CLIP_PLANE1);
454 glDisable(GL_CLIP_PLANE2);
455 glDisable(GL_CLIP_PLANE3);
457 // restore some original state
463 * Set the panel's background texture.
466 FGPanel::setBackground (FGCroppedTexture_ptr texture)
472 * Set the panel's multiple background textures.
475 FGPanel::setMultiBackground (FGCroppedTexture_ptr texture, int idx)
481 ////////////////////////////////////////////////////////////////////////
482 // Implementation of FGPanelTransformation.
483 ////////////////////////////////////////////////////////////////////////
485 FGPanelTransformation::FGPanelTransformation ()
490 FGPanelTransformation::~FGPanelTransformation ()
497 ////////////////////////////////////////////////////////////////////////
498 // Implementation of FGPanelInstrument.
499 ////////////////////////////////////////////////////////////////////////
502 FGPanelInstrument::FGPanelInstrument ()
508 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
514 FGPanelInstrument::~FGPanelInstrument ()
519 FGPanelInstrument::setPosition (int x, int y)
526 FGPanelInstrument::setSize (int w, int h)
533 FGPanelInstrument::getXPos () const
539 FGPanelInstrument::getYPos () const
545 FGPanelInstrument::getWidth () const
551 FGPanelInstrument::getHeight () const
557 ////////////////////////////////////////////////////////////////////////
558 // Implementation of FGLayeredInstrument.
559 ////////////////////////////////////////////////////////////////////////
561 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
562 : FGPanelInstrument(x, y, w, h)
566 FGLayeredInstrument::~FGLayeredInstrument ()
568 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
575 FGLayeredInstrument::draw ()
580 for (int i = 0; i < (int)_layers.size(); i++) {
588 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
590 int n = _layers.size();
591 if (layer->getWidth() == -1) {
592 layer->setWidth(getWidth());
594 if (layer->getHeight() == -1) {
595 layer->setHeight(getHeight());
597 _layers.push_back(layer);
602 FGLayeredInstrument::addLayer (FGCroppedTexture_ptr texture, int w, int h)
604 return addLayer(new FGTexturedLayer(texture, w, h));
608 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
610 int layer = _layers.size() - 1;
611 _layers[layer]->addTransformation(transformation);
616 ////////////////////////////////////////////////////////////////////////
617 // Implementation of FGInstrumentLayer.
618 ////////////////////////////////////////////////////////////////////////
620 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
626 FGInstrumentLayer::~FGInstrumentLayer ()
628 for (transformation_list::iterator it = _transformations.begin();
629 it != _transformations.end();
637 FGInstrumentLayer::transform () const
639 transformation_list::const_iterator it = _transformations.begin();
640 transformation_list::const_iterator last = _transformations.end();
642 FGPanelTransformation *t = *it;
644 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
647 val = fmod(val, t->mod);
650 } else if (val > t->max) {
655 val = val * t->factor + t->offset;
657 val = t->table->interpolate(val) * t->factor + t->offset;
661 case FGPanelTransformation::XSHIFT:
662 glTranslatef(val, 0.0, 0.0);
664 case FGPanelTransformation::YSHIFT:
665 glTranslatef(0.0, val, 0.0);
667 case FGPanelTransformation::ROTATION:
668 glRotatef(-val, 0.0, 0.0, 1.0);
677 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
679 _transformations.push_back(transformation);
684 ////////////////////////////////////////////////////////////////////////
685 // Implementation of FGGroupLayer.
686 ////////////////////////////////////////////////////////////////////////
688 FGGroupLayer::FGGroupLayer ()
692 FGGroupLayer::~FGGroupLayer ()
694 for (unsigned int i = 0; i < _layers.size(); i++)
699 FGGroupLayer::draw ()
703 int nLayers = _layers.size();
704 for (int i = 0; i < nLayers; i++)
710 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
712 _layers.push_back(layer);
717 ////////////////////////////////////////////////////////////////////////
718 // Implementation of FGTexturedLayer.
719 ////////////////////////////////////////////////////////////////////////
722 FGTexturedLayer::FGTexturedLayer (FGCroppedTexture_ptr texture, int w, int h)
723 : FGInstrumentLayer(w, h),
731 FGTexturedLayer::~FGTexturedLayer ()
736 FGTexturedLayer::getDisplayList()
738 if( displayList != 0 )
744 _texture->bind( false );
745 displayList = glGenLists(1);
746 glNewList(displayList,GL_COMPILE_AND_EXECUTE);
747 glBindTexture( GL_TEXTURE_2D, _texture->getTexture() );
749 glTexCoord2f(_texture->getMinX(), _texture->getMinY()); glVertex2f(-w2, -h2);
750 glTexCoord2f(_texture->getMaxX(), _texture->getMinY()); glVertex2f(w2, -h2);
751 glTexCoord2f(_texture->getMaxX(), _texture->getMaxY()); glVertex2f(w2, h2);
752 glTexCoord2f(_texture->getMinX(), _texture->getMaxY()); glVertex2f(-w2, h2);
760 FGTexturedLayer::draw ( )
764 glCallList(getDisplayList());
770 ////////////////////////////////////////////////////////////////////////
771 // Implementation of FGTextLayer.
772 ////////////////////////////////////////////////////////////////////////
774 fntRenderer FGTextLayer::text_renderer;
776 FGTextLayer::FGTextLayer (int w, int h)
777 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
780 _color[0] = _color[1] = _color[2] = 0.0;
784 FGTextLayer::~FGTextLayer ()
786 chunk_list::iterator it = _chunks.begin();
787 chunk_list::iterator last = _chunks.end();
788 for ( ; it != last; it++) {
798 glGetFloatv( GL_CURRENT_COLOR, c );
802 text_renderer.setFont(ApplicationProperties::fontCache.getTexFont(_font_name.c_str()));
804 text_renderer.setPointSize(_pointSize);
805 text_renderer.begin();
806 text_renderer.start3f(0, 0, 0);
809 long diff = (_now - _then).toUSecs();
811 if (diff > 100000 || diff < 0 ) {
812 // ( diff < 0 ) is a sanity check and indicates our time stamp
813 // difference math probably overflowed. We can handle a max
814 // difference of 35.8 minutes since the returned value is in
815 // usec. So if the panel is left off longer than that we can
816 // over flow the math with it is turned back on. This (diff <
817 // 0) catches that situation, get's us out of trouble, and
823 // Something is goofy. The code in this file renders only CCW
824 // polygons, and I have verified that the font code in plib
825 // renders only CCW trianbles. Yet they come out backwards.
826 // Something around here or in plib is either changing the winding
827 // order or (more likely) pushing a left-handed matrix onto the
828 // stack. But I can't find it; get out the chainsaw...
830 text_renderer.puts((char *)(_value.c_str()));
839 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
841 _chunks.push_back(chunk);
845 FGTextLayer::setColor (float r, float g, float b)
854 FGTextLayer::setPointSize (float size)
860 FGTextLayer::setFontName(const string &name)
862 _font_name = name + ".txf";
867 FGTextLayer::setFont(fntFont * font)
869 FGTextLayer::text_renderer.setFont(font);
874 FGTextLayer::recalc_value () const
877 chunk_list::const_iterator it = _chunks.begin();
878 chunk_list::const_iterator last = _chunks.end();
879 for ( ; it != last; it++) {
880 _value += (*it)->getValue();
886 ////////////////////////////////////////////////////////////////////////
887 // Implementation of FGTextLayer::Chunk.
888 ////////////////////////////////////////////////////////////////////////
890 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
891 : _type(FGTextLayer::TEXT), _fmt(fmt)
898 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
899 const string &fmt, float mult, float offs,
901 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
904 if (type == TEXT_VALUE)
913 FGTextLayer::Chunk::getValue () const
919 sprintf(_buf, _fmt.c_str(), _text.c_str());
922 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
925 double d = _offs + _node->getFloatValue() * _mult;
926 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
927 sprintf(_buf, _fmt.c_str(), d);
938 ////////////////////////////////////////////////////////////////////////
939 // Implementation of FGSwitchLayer.
940 ////////////////////////////////////////////////////////////////////////
942 FGSwitchLayer::FGSwitchLayer ()
948 FGSwitchLayer::draw ()
952 int nLayers = _layers.size();
953 for (int i = 0; i < nLayers; i++) {
954 if (_layers[i]->test()) {