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.
22 // On 2D panels all instruments include light sources were in night displayed
23 // with a red mask (instrument light). It is not correct for light sources
24 // (bulbs). There is added new layer property "emissive" (boolean) (only for
26 // If a layer has to shine set it in the "instrument_def_file.xml" inside the
27 // <layer> tag by adding <emissive>true</emissive> tag. When omitted the default
28 // value is for backward compatibility set to false.
34 #include <stdio.h> // sprintf
37 #include <osg/CullFace>
39 #include <osg/Material>
41 #include <osg/PolygonOffset>
43 #include <simgear/compiler.h>
47 #include <simgear/debug/logstream.hxx>
48 #include <simgear/misc/sg_path.hxx>
49 #include <simgear/scene/model/model.hxx>
51 #include <Main/globals.hxx>
52 #include <Main/fg_props.hxx>
53 #include <Main/viewmgr.hxx>
54 #include <Time/light.hxx>
55 #include <GUI/new_gui.hxx> // FGFontCache
65 // The number of polygon-offset "units" to place between layers. In
66 // principle, one is supposed to be enough. In practice, I find that
67 // my hardware/driver requires many more.
70 ////////////////////////////////////////////////////////////////////////
72 ////////////////////////////////////////////////////////////////////////
76 * Calculate the aspect adjustment for the panel.
79 get_aspect_adjust (int xsize, int ysize)
81 float ideal_aspect = float(WIN_W) / float(WIN_H);
82 float real_aspect = float(xsize) / float(ysize);
83 return (real_aspect / ideal_aspect);
88 ////////////////////////////////////////////////////////////////////////
90 ////////////////////////////////////////////////////////////////////////
95 const FGPanel* current = globals->get_current_panel();
98 if (current->getVisibility() == 0)
100 if (globals->get_viewmgr()->get_current() != 0)
102 if (current->getAutohide() && globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS != 0)
108 ////////////////////////////////////////////////////////////////////////
109 // Implementation of FGTextureManager.
110 ////////////////////////////////////////////////////////////////////////
112 map<string,osg::ref_ptr<osg::Texture2D> > FGTextureManager::_textureMap;
115 FGTextureManager::createTexture (const string &relativePath, bool staticTexture)
117 osg::Texture2D* texture = _textureMap[relativePath].get();
119 SG_LOG( SG_COCKPIT, SG_DEBUG,
120 "Texture " << relativePath << " does not yet exist" );
121 SGPath tpath(globals->get_fg_root());
122 tpath.append(relativePath);
124 texture = SGLoadTexture2D(staticTexture, tpath);
126 _textureMap[relativePath] = texture;
127 if (!_textureMap[relativePath].valid())
128 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
129 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath );
136 void FGTextureManager::addTexture(const string &relativePath,
137 osg::Texture2D* texture)
139 _textureMap[relativePath] = texture;
142 ////////////////////////////////////////////////////////////////////////
143 // Implementation of FGCropped Texture.
144 ////////////////////////////////////////////////////////////////////////
147 FGCroppedTexture::FGCroppedTexture ()
148 : _path(""), _texture(0),
149 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
154 FGCroppedTexture::FGCroppedTexture (const string &path,
155 float minX, float minY,
156 float maxX, float maxY)
157 : _path(path), _texture(0),
158 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
163 FGCroppedTexture::~FGCroppedTexture ()
169 FGCroppedTexture::getTexture ()
172 _texture = new osg::StateSet;
173 _texture->setTextureAttribute(0, FGTextureManager::createTexture(_path));
174 _texture->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
175 _texture->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
177 return _texture.get();
182 ////////////////////////////////////////////////////////////////////////
183 // Implementation of FGPanel.
184 ////////////////////////////////////////////////////////////////////////
186 static fntRenderer text_renderer;
187 static sgVec4 panel_color;
188 static sgVec4 emissive_panel_color = {1,1,1,1};
196 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
197 _view_height(int(WIN_H * 0.4232)),
198 _visibility(fgGetNode("/sim/panel/visibility", true)),
199 _x_offset(fgGetNode("/sim/panel/x-offset", true)),
200 _y_offset(fgGetNode("/sim/panel/y-offset", true)),
201 _jitter(fgGetNode("/sim/panel/jitter", true)),
202 _flipx(fgGetNode("/sim/panel/flip-x", true)),
203 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
204 _ysize_node(fgGetNode("/sim/startup/ysize", true)),
205 _enable_depth_test(false)
215 for (instrument_list_type::iterator it = _instruments.begin();
216 it != _instruments.end();
225 * Add an instrument to the panel.
228 FGPanel::addInstrument (FGPanelInstrument * instrument)
230 _instruments.push_back(instrument);
235 * Initialize the panel.
244 * Bind panel properties.
249 fgSetArchivable("/sim/panel/visibility");
250 fgSetArchivable("/sim/panel/x-offset");
251 fgSetArchivable("/sim/panel/y-offset");
252 fgSetArchivable("/sim/panel/jitter");
257 * Unbind panel properties.
266 FGPanel::update (double dt)
268 std::cout << "OSGFIXME" << std::endl;
272 FGPanel::update (osg::State& state, GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
274 // Calculate accelerations
275 // and jiggle the panel accordingly
276 // The factors and bounds are just
277 // initial guesses; using sqrt smooths
279 double x_offset = _x_offset->getIntValue();
280 double y_offset = _y_offset->getIntValue();
283 glMatrixMode(GL_PROJECTION);
286 if ( _flipx->getBoolValue() ) {
287 gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
289 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
292 glMatrixMode(GL_MODELVIEW);
296 glTranslated(x_offset, y_offset, 0);
300 glMatrixMode(GL_PROJECTION);
302 glMatrixMode(GL_MODELVIEW);
310 FGPanel::update (osg::State& state)
312 // Do nothing if the panel isn't visible.
313 if ( !fgPanelVisible() ) {
319 // Now, draw the panel
320 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
321 _ysize_node->getIntValue());
322 if (aspect_adjust <1.0)
323 update(state, WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
325 update(state, WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
329 * Handle repeatable mouse events. Called from update() and from
330 * fgUpdate3DPanels(). This functionality needs to move into the
331 * input subsystem. Counting a tick every two frames is clumsy...
333 void FGPanel::updateMouseDelay()
337 if (_mouseDelay < 0) {
338 _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
346 FGPanel::draw(osg::State& state)
348 // In 3D mode, it's possible that we are being drawn exactly on top
349 // of an existing polygon. Use an offset to prevent z-fighting. In
350 // 2D mode, this is a no-op.
351 static osg::ref_ptr<osg::StateSet> panelStateSet;
352 if (!panelStateSet.valid()) {
353 panelStateSet = new osg::StateSet;
354 panelStateSet->setAttributeAndModes(new osg::PolygonOffset(-1, -POFF_UNITS));
355 panelStateSet->setTextureAttribute(0, new osg::TexEnv);
357 // Draw the background
358 panelStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
359 panelStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
360 panelStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
361 panelStateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
362 osg::Material* material = new osg::Material;
363 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
364 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
365 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
366 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
367 material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
368 panelStateSet->setAttribute(material);
369 panelStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
370 panelStateSet->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK));
371 panelStateSet->setAttributeAndModes(new osg::Depth(osg::Depth::LEQUAL));
373 if ( _enable_depth_test )
374 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
376 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
377 state.pushStateSet(panelStateSet.get());
379 state.setActiveTextureUnit(0);
380 state.setClientActiveTextureUnit(0);
382 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
383 sgCopyVec4( panel_color, l->scene_diffuse().data());
384 if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
385 if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
386 if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
387 if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
389 glColor4fv( panel_color );
391 state.pushStateSet(_bg.get());
393 state.setActiveTextureUnit(0);
394 state.setClientActiveTextureUnit(0);
397 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
398 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
399 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
400 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
404 state.setActiveTextureUnit(0);
405 state.setClientActiveTextureUnit(0);
408 for (int i = 0; i < 4; i ++) {
409 // top row of textures...(1,3,5,7)
410 state.pushStateSet(_mbg[i*2].get());
412 state.setActiveTextureUnit(0);
413 state.setClientActiveTextureUnit(0);
416 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
417 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
418 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
419 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
423 state.setActiveTextureUnit(0);
424 state.setClientActiveTextureUnit(0);
426 // bottom row of textures...(2,4,6,8)
427 state.pushStateSet(_mbg[i*2+1].get());
429 state.setActiveTextureUnit(0);
430 state.setClientActiveTextureUnit(0);
433 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
434 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
435 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
436 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
440 state.setActiveTextureUnit(0);
441 state.setClientActiveTextureUnit(0);
446 // Draw the instruments.
447 // Syd Adams: added instrument clipping
448 instrument_list_type::const_iterator current = _instruments.begin();
449 instrument_list_type::const_iterator end = _instruments.end();
451 GLdouble blx[4]={1.0,0.0,0.0,0.0};
452 GLdouble bly[4]={0.0,1.0,0.0,0.0};
453 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
454 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
456 for ( ; current != end; current++) {
457 FGPanelInstrument * instr = *current;
459 glTranslated(instr->getXPos(), instr->getYPos(), 0);
461 int ix= instr->getWidth();
462 int iy= instr->getHeight();
464 glTranslated(-ix/2,-iy/2,0);
465 glClipPlane(GL_CLIP_PLANE0,blx);
466 glClipPlane(GL_CLIP_PLANE1,bly);
467 glEnable(GL_CLIP_PLANE0);
468 glEnable(GL_CLIP_PLANE1);
470 glTranslated(ix,iy,0);
471 glClipPlane(GL_CLIP_PLANE2,urx);
472 glClipPlane(GL_CLIP_PLANE3,ury);
473 glEnable(GL_CLIP_PLANE2);
474 glEnable(GL_CLIP_PLANE3);
481 glDisable(GL_CLIP_PLANE0);
482 glDisable(GL_CLIP_PLANE1);
483 glDisable(GL_CLIP_PLANE2);
484 glDisable(GL_CLIP_PLANE3);
488 state.setActiveTextureUnit(0);
489 state.setClientActiveTextureUnit(0);
492 // Draw yellow "hotspots" if directed to. This is a panel authoring
493 // feature; not intended to be high performance or to look good.
494 if ( fgGetBool("/sim/panel-hotspots") ) {
495 static osg::ref_ptr<osg::StateSet> hotspotStateSet;
496 if (!hotspotStateSet.valid()) {
497 hotspotStateSet = new osg::StateSet;
498 hotspotStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
499 hotspotStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
502 state.pushStateSet(hotspotStateSet.get());
504 state.setActiveTextureUnit(0);
505 state.setClientActiveTextureUnit(0);
508 glPushAttrib(GL_ENABLE_BIT);
509 glDisable(GL_COLOR_MATERIAL);
512 for ( unsigned int i = 0; i < _instruments.size(); i++ )
513 _instruments[i]->drawHotspots(state);
519 state.setActiveTextureUnit(0);
520 state.setClientActiveTextureUnit(0);
526 * Set the panel's visibility.
529 FGPanel::setVisibility (bool visibility)
531 _visibility->setBoolValue( visibility );
536 * Return true if the panel is visible.
539 FGPanel::getVisibility () const
541 return _visibility->getBoolValue();
546 * Set the panel's background texture.
549 FGPanel::setBackground (osg::Texture2D* texture)
551 osg::StateSet* stateSet = new osg::StateSet;
552 stateSet->setTextureAttribute(0, texture);
553 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
554 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
559 * Set the panel's multiple background textures.
562 FGPanel::setMultiBackground (osg::Texture2D* texture, int idx)
566 osg::StateSet* stateSet = new osg::StateSet;
567 stateSet->setTextureAttribute(0, texture);
568 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
569 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
570 _mbg[idx] = stateSet;
574 * Set the panel's x-offset.
577 FGPanel::setXOffset (int offset)
579 if (offset <= 0 && offset >= -_width + WIN_W)
580 _x_offset->setIntValue( offset );
585 * Set the panel's y-offset.
588 FGPanel::setYOffset (int offset)
590 if (offset <= 0 && offset >= -_height)
591 _y_offset->setIntValue( offset );
595 * Handle a mouse action in panel-local (not screen) coordinates.
596 * Used by the 3D panel code in Model/panelnode.cxx, in situations
597 * where the panel doesn't control its own screen location.
600 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
602 // Note a released button and return
604 if (_mouseInstrument != 0)
605 _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
607 _mouseInstrument = 0;
611 // Search for a matching instrument.
612 for (int i = 0; i < (int)_instruments.size(); i++) {
613 FGPanelInstrument *inst = _instruments[i];
614 int ix = inst->getXPos();
615 int iy = inst->getYPos();
616 int iw = inst->getWidth() / 2;
617 int ih = inst->getHeight() / 2;
618 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
621 _mouseInstrument = inst;
622 _mouseButton = button;
625 // Always do the action once.
626 return _mouseInstrument->doMouseAction(_mouseButton, 0,
634 * Perform a mouse action.
637 FGPanel::doMouseAction (int button, int updown, int x, int y)
639 // FIXME: this same code appears in update()
640 int xsize = _xsize_node->getIntValue();
641 int ysize = _ysize_node->getIntValue();
642 float aspect_adjust = get_aspect_adjust(xsize, ysize);
644 // Scale for the real window size.
645 if (aspect_adjust < 1.0) {
646 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
647 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
649 x = int(((float)x / xsize) * WIN_W);
650 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
653 // Adjust for offsets.
654 x -= _x_offset->getIntValue();
655 y -= _y_offset->getIntValue();
657 // Having fixed up the coordinates, fall through to the local
658 // coordinate handler.
659 return doLocalMouseAction(button, updown, x, y);
662 void FGPanel::setDepthTest (bool enable) {
663 _enable_depth_test = enable;
668 ////////////////////////////////////////////////////////////////////////.
669 // Implementation of FGPanelAction.
670 ////////////////////////////////////////////////////////////////////////
672 FGPanelAction::FGPanelAction ()
676 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
678 : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
682 FGPanelAction::~FGPanelAction ()
684 for (unsigned int i = 0; i < 2; i++) {
685 for (unsigned int j = 0; j < _bindings[i].size(); j++)
686 delete _bindings[i][j];
691 FGPanelAction::addBinding (SGBinding * binding, int updown)
693 _bindings[updown].push_back(binding);
697 FGPanelAction::doAction (int updown)
700 if ((updown != _last_state) || (updown == 0 && _repeatable)) {
701 int nBindings = _bindings[updown].size();
702 for (int i = 0; i < nBindings; i++)
703 _bindings[updown][i]->fire();
705 _last_state = updown;
714 ////////////////////////////////////////////////////////////////////////
715 // Implementation of FGPanelTransformation.
716 ////////////////////////////////////////////////////////////////////////
718 FGPanelTransformation::FGPanelTransformation ()
723 FGPanelTransformation::~FGPanelTransformation ()
730 ////////////////////////////////////////////////////////////////////////
731 // Implementation of FGPanelInstrument.
732 ////////////////////////////////////////////////////////////////////////
735 FGPanelInstrument::FGPanelInstrument ()
741 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
747 FGPanelInstrument::~FGPanelInstrument ()
749 for (action_list_type::iterator it = _actions.begin();
750 it != _actions.end();
758 FGPanelInstrument::drawHotspots(osg::State& state)
760 for ( unsigned int i = 0; i < _actions.size(); i++ ) {
761 FGPanelAction* a = _actions[i];
762 float x1 = getXPos() + a->getX();
763 float x2 = x1 + a->getWidth();
764 float y1 = getYPos() + a->getY();
765 float y2 = y1 + a->getHeight();
767 glBegin(GL_LINE_LOOP);
777 FGPanelInstrument::setPosition (int x, int y)
784 FGPanelInstrument::setSize (int w, int h)
791 FGPanelInstrument::getXPos () const
797 FGPanelInstrument::getYPos () const
803 FGPanelInstrument::getWidth () const
809 FGPanelInstrument::getHeight () const
815 FGPanelInstrument::addAction (FGPanelAction * action)
817 _actions.push_back(action);
820 // Coordinates relative to centre.
822 FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
825 action_list_type::iterator it = _actions.begin();
826 action_list_type::iterator last = _actions.end();
827 for ( ; it != last; it++) {
828 if ((*it)->inArea(button, x, y) &&
829 (*it)->doAction(updown))
838 ////////////////////////////////////////////////////////////////////////
839 // Implementation of FGLayeredInstrument.
840 ////////////////////////////////////////////////////////////////////////
842 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
843 : FGPanelInstrument(x, y, w, h)
847 FGLayeredInstrument::~FGLayeredInstrument ()
849 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
856 FGLayeredInstrument::draw (osg::State& state)
861 for (int i = 0; i < (int)_layers.size(); i++) {
863 _layers[i]->draw(state);
869 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
871 int n = _layers.size();
872 if (layer->getWidth() == -1) {
873 layer->setWidth(getWidth());
875 if (layer->getHeight() == -1) {
876 layer->setHeight(getHeight());
878 _layers.push_back(layer);
883 FGLayeredInstrument::addLayer (const FGCroppedTexture &texture,
886 return addLayer(new FGTexturedLayer(texture, w, h));
890 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
892 int layer = _layers.size() - 1;
893 _layers[layer]->addTransformation(transformation);
898 ////////////////////////////////////////////////////////////////////////
899 // Implementation of FGSpecialInstrument.
900 ////////////////////////////////////////////////////////////////////////
902 FGSpecialInstrument::FGSpecialInstrument (DCLGPS* sb)
903 : FGPanelInstrument()
908 FGSpecialInstrument::~FGSpecialInstrument ()
913 FGSpecialInstrument::draw (osg::State& state)
920 ////////////////////////////////////////////////////////////////////////
921 // Implementation of FGInstrumentLayer.
922 ////////////////////////////////////////////////////////////////////////
924 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
930 FGInstrumentLayer::~FGInstrumentLayer ()
932 for (transformation_list::iterator it = _transformations.begin();
933 it != _transformations.end();
941 FGInstrumentLayer::transform () const
943 transformation_list::const_iterator it = _transformations.begin();
944 transformation_list::const_iterator last = _transformations.end();
946 FGPanelTransformation *t = *it;
948 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
951 val = fmod(val, t->mod);
954 } else if (val > t->max) {
959 val = val * t->factor + t->offset;
961 val = t->table->interpolate(val) * t->factor + t->offset;
965 case FGPanelTransformation::XSHIFT:
966 glTranslatef(val, 0.0, 0.0);
968 case FGPanelTransformation::YSHIFT:
969 glTranslatef(0.0, val, 0.0);
971 case FGPanelTransformation::ROTATION:
972 glRotatef(-val, 0.0, 0.0, 1.0);
981 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
983 _transformations.push_back(transformation);
988 ////////////////////////////////////////////////////////////////////////
989 // Implementation of FGGroupLayer.
990 ////////////////////////////////////////////////////////////////////////
992 FGGroupLayer::FGGroupLayer ()
996 FGGroupLayer::~FGGroupLayer ()
998 for (unsigned int i = 0; i < _layers.size(); i++)
1003 FGGroupLayer::draw (osg::State& state)
1007 int nLayers = _layers.size();
1008 for (int i = 0; i < nLayers; i++)
1009 _layers[i]->draw(state);
1014 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
1016 _layers.push_back(layer);
1021 ////////////////////////////////////////////////////////////////////////
1022 // Implementation of FGTexturedLayer.
1023 ////////////////////////////////////////////////////////////////////////
1026 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
1027 : FGInstrumentLayer(w, h),
1030 setTexture(texture);
1034 FGTexturedLayer::~FGTexturedLayer ()
1040 FGTexturedLayer::draw (osg::State& state)
1047 state.pushStateSet(_texture.getTexture());
1049 state.setActiveTextureUnit(0);
1050 state.setClientActiveTextureUnit(0);
1052 glBegin(GL_POLYGON);
1055 glColor4fv( emissive_panel_color );
1057 // From Curt: turn on the panel
1058 // lights after sundown.
1059 glColor4fv( panel_color );
1062 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
1063 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
1064 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
1065 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
1067 state.popStateSet();
1069 state.setActiveTextureUnit(0);
1070 state.setClientActiveTextureUnit(0);
1077 ////////////////////////////////////////////////////////////////////////
1078 // Implementation of FGTextLayer.
1079 ////////////////////////////////////////////////////////////////////////
1081 FGTextLayer::FGTextLayer (int w, int h)
1082 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
1085 _color[0] = _color[1] = _color[2] = 0.0;
1089 FGTextLayer::~FGTextLayer ()
1091 chunk_list::iterator it = _chunks.begin();
1092 chunk_list::iterator last = _chunks.end();
1093 for ( ; it != last; it++) {
1099 FGTextLayer::draw (osg::State& state)
1105 FGFontCache *fc = globals->get_fontcache();
1106 text_renderer.setFont(fc->getTexFont(_font_name.c_str()));
1108 text_renderer.setPointSize(_pointSize);
1109 text_renderer.begin();
1110 text_renderer.start3f(0, 0, 0);
1113 long diff = _now - _then;
1115 if (diff > 100000 || diff < 0 ) {
1116 // ( diff < 0 ) is a sanity check and indicates our time stamp
1117 // difference math probably overflowed. We can handle a max
1118 // difference of 35.8 minutes since the returned value is in
1119 // usec. So if the panel is left off longer than that we can
1120 // over flow the math with it is turned back on. This (diff <
1121 // 0) catches that situation, get's us out of trouble, and
1127 // Something is goofy. The code in this file renders only CCW
1128 // polygons, and I have verified that the font code in plib
1129 // renders only CCW trianbles. Yet they come out backwards.
1130 // Something around here or in plib is either changing the winding
1131 // order or (more likely) pushing a left-handed matrix onto the
1132 // stack. But I can't find it; get out the chainsaw...
1134 text_renderer.puts((char *)(_value.c_str()));
1135 glFrontFace(GL_CCW);
1137 text_renderer.end();
1138 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
1143 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1145 _chunks.push_back(chunk);
1149 FGTextLayer::setColor (float r, float g, float b)
1158 FGTextLayer::setPointSize (float size)
1164 FGTextLayer::setFontName(const string &name)
1166 _font_name = name + ".txf";
1171 FGTextLayer::setFont(fntFont * font)
1173 text_renderer.setFont(font);
1178 FGTextLayer::recalc_value () const
1181 chunk_list::const_iterator it = _chunks.begin();
1182 chunk_list::const_iterator last = _chunks.end();
1183 for ( ; it != last; it++) {
1184 _value += (*it)->getValue();
1190 ////////////////////////////////////////////////////////////////////////
1191 // Implementation of FGTextLayer::Chunk.
1192 ////////////////////////////////////////////////////////////////////////
1194 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1195 : _type(FGTextLayer::TEXT), _fmt(fmt)
1202 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1203 const string &fmt, float mult, float offs,
1205 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
1208 if (type == TEXT_VALUE)
1217 FGTextLayer::Chunk::getValue () const
1223 sprintf(_buf, _fmt.c_str(), _text.c_str());
1226 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1229 double d = _offs + _node->getFloatValue() * _mult;
1230 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
1231 sprintf(_buf, _fmt.c_str(), d);
1242 ////////////////////////////////////////////////////////////////////////
1243 // Implementation of FGSwitchLayer.
1244 ////////////////////////////////////////////////////////////////////////
1246 FGSwitchLayer::FGSwitchLayer ()
1252 FGSwitchLayer::draw (osg::State& state)
1256 int nLayers = _layers.size();
1257 for (int i = 0; i < nLayers; i++) {
1258 if (_layers[i]->test()) {
1259 _layers[i]->draw(state);