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>
52 #include <Main/globals.hxx>
53 #include <Main/fg_props.hxx>
54 #include <Main/viewmgr.hxx>
55 #include <Time/light.hxx>
56 #include <GUI/new_gui.hxx> // FGFontCache
66 // The number of polygon-offset "units" to place between layers. In
67 // principle, one is supposed to be enough. In practice, I find that
68 // my hardware/driver requires many more.
71 ////////////////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////////////////
77 * Calculate the aspect adjustment for the panel.
80 get_aspect_adjust (int xsize, int ysize)
82 float ideal_aspect = float(WIN_W) / float(WIN_H);
83 float real_aspect = float(xsize) / float(ysize);
84 return (real_aspect / ideal_aspect);
89 ////////////////////////////////////////////////////////////////////////
91 ////////////////////////////////////////////////////////////////////////
96 const FGPanel* current = globals->get_current_panel();
99 if (current->getVisibility() == 0)
101 if (globals->get_viewmgr()->get_current() != 0)
103 if (current->getAutohide() && globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS != 0)
109 ////////////////////////////////////////////////////////////////////////
110 // Implementation of FGTextureManager.
111 ////////////////////////////////////////////////////////////////////////
113 map<string,osg::ref_ptr<osg::Texture2D> > FGTextureManager::_textureMap;
116 FGTextureManager::createTexture (const string &relativePath, bool staticTexture)
118 osg::Texture2D* texture = _textureMap[relativePath].get();
120 SG_LOG( SG_COCKPIT, SG_DEBUG,
121 "Texture " << relativePath << " does not yet exist" );
122 SGPath tpath(globals->get_fg_root());
123 tpath.append(relativePath);
125 texture = SGLoadTexture2D(staticTexture, tpath);
127 _textureMap[relativePath] = texture;
128 if (!_textureMap[relativePath].valid())
129 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
130 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath );
137 void FGTextureManager::addTexture(const string &relativePath,
138 osg::Texture2D* texture)
140 _textureMap[relativePath] = texture;
143 ////////////////////////////////////////////////////////////////////////
144 // Implementation of FGCropped Texture.
145 ////////////////////////////////////////////////////////////////////////
148 FGCroppedTexture::FGCroppedTexture ()
149 : _path(""), _texture(0),
150 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
155 FGCroppedTexture::FGCroppedTexture (const string &path,
156 float minX, float minY,
157 float maxX, float maxY)
158 : _path(path), _texture(0),
159 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
164 FGCroppedTexture::~FGCroppedTexture ()
170 FGCroppedTexture::getTexture ()
173 _texture = new osg::StateSet;
174 _texture->setTextureAttribute(0, FGTextureManager::createTexture(_path));
175 _texture->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
176 _texture->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
178 return _texture.get();
183 ////////////////////////////////////////////////////////////////////////
184 // Implementation of FGPanel.
185 ////////////////////////////////////////////////////////////////////////
187 static fntRenderer text_renderer;
188 static sgVec4 panel_color;
189 static sgVec4 emissive_panel_color = {1,1,1,1};
197 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
198 _view_height(int(WIN_H * 0.4232)),
199 _visibility(fgGetNode("/sim/panel/visibility", true)),
200 _x_offset(fgGetNode("/sim/panel/x-offset", true)),
201 _y_offset(fgGetNode("/sim/panel/y-offset", true)),
202 _jitter(fgGetNode("/sim/panel/jitter", true)),
203 _flipx(fgGetNode("/sim/panel/flip-x", true)),
204 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
205 _ysize_node(fgGetNode("/sim/startup/ysize", true)),
206 _enable_depth_test(false)
216 for (instrument_list_type::iterator it = _instruments.begin();
217 it != _instruments.end();
226 * Add an instrument to the panel.
229 FGPanel::addInstrument (FGPanelInstrument * instrument)
231 _instruments.push_back(instrument);
236 * Initialize the panel.
245 * Bind panel properties.
250 fgSetArchivable("/sim/panel/visibility");
251 fgSetArchivable("/sim/panel/x-offset");
252 fgSetArchivable("/sim/panel/y-offset");
253 fgSetArchivable("/sim/panel/jitter");
258 * Unbind panel properties.
267 FGPanel::update (double dt)
269 std::cout << "OSGFIXME" << std::endl;
273 FGPanel::update (osg::State& state, GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
275 // Calculate accelerations
276 // and jiggle the panel accordingly
277 // The factors and bounds are just
278 // initial guesses; using sqrt smooths
280 double x_offset = _x_offset->getIntValue();
281 double y_offset = _y_offset->getIntValue();
284 glMatrixMode(GL_PROJECTION);
287 if ( _flipx->getBoolValue() ) {
288 gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
290 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
293 glMatrixMode(GL_MODELVIEW);
297 glTranslated(x_offset, y_offset, 0);
301 glMatrixMode(GL_PROJECTION);
303 glMatrixMode(GL_MODELVIEW);
311 FGPanel::update (osg::State& state)
313 // Do nothing if the panel isn't visible.
314 if ( !fgPanelVisible() ) {
320 // Now, draw the panel
321 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
322 _ysize_node->getIntValue());
323 if (aspect_adjust <1.0)
324 update(state, WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
326 update(state, WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
330 * Handle repeatable mouse events. Called from update() and from
331 * fgUpdate3DPanels(). This functionality needs to move into the
332 * input subsystem. Counting a tick every two frames is clumsy...
334 void FGPanel::updateMouseDelay()
338 if (_mouseDelay < 0) {
339 _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
347 FGPanel::draw(osg::State& state)
349 // In 3D mode, it's possible that we are being drawn exactly on top
350 // of an existing polygon. Use an offset to prevent z-fighting. In
351 // 2D mode, this is a no-op.
352 static osg::ref_ptr<osg::StateSet> panelStateSet;
353 if (!panelStateSet.valid()) {
354 panelStateSet = new osg::StateSet;
355 panelStateSet->setAttributeAndModes(new osg::PolygonOffset(-1, -POFF_UNITS));
356 panelStateSet->setTextureAttribute(0, new osg::TexEnv);
358 // Draw the background
359 panelStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
360 panelStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
361 panelStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
362 panelStateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
363 osg::Material* material = new osg::Material;
364 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
365 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
366 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
367 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
368 material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
369 panelStateSet->setAttribute(material);
370 panelStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
371 panelStateSet->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK));
372 panelStateSet->setAttributeAndModes(new osg::Depth(osg::Depth::LEQUAL));
374 if ( _enable_depth_test )
375 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
377 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
378 state.pushStateSet(panelStateSet.get());
380 state.setActiveTextureUnit(0);
381 state.setClientActiveTextureUnit(0);
383 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
384 sgCopyVec4( panel_color, l->scene_diffuse().data());
385 if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
386 if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
387 if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
388 if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
390 glColor4fv( panel_color );
392 state.pushStateSet(_bg.get());
394 state.setActiveTextureUnit(0);
395 state.setClientActiveTextureUnit(0);
398 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
399 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
400 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
401 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
405 state.setActiveTextureUnit(0);
406 state.setClientActiveTextureUnit(0);
409 for (int i = 0; i < 4; i ++) {
410 // top row of textures...(1,3,5,7)
411 state.pushStateSet(_mbg[i*2].get());
413 state.setActiveTextureUnit(0);
414 state.setClientActiveTextureUnit(0);
417 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
418 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
419 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
420 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
424 state.setActiveTextureUnit(0);
425 state.setClientActiveTextureUnit(0);
427 // bottom row of textures...(2,4,6,8)
428 state.pushStateSet(_mbg[i*2+1].get());
430 state.setActiveTextureUnit(0);
431 state.setClientActiveTextureUnit(0);
434 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
435 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
436 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
437 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
441 state.setActiveTextureUnit(0);
442 state.setClientActiveTextureUnit(0);
447 // Draw the instruments.
448 // Syd Adams: added instrument clipping
449 instrument_list_type::const_iterator current = _instruments.begin();
450 instrument_list_type::const_iterator end = _instruments.end();
452 GLdouble blx[4]={1.0,0.0,0.0,0.0};
453 GLdouble bly[4]={0.0,1.0,0.0,0.0};
454 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
455 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
457 for ( ; current != end; current++) {
458 FGPanelInstrument * instr = *current;
460 glTranslated(instr->getXPos(), instr->getYPos(), 0);
462 int ix= instr->getWidth();
463 int iy= instr->getHeight();
465 glTranslated(-ix/2,-iy/2,0);
466 glClipPlane(GL_CLIP_PLANE0,blx);
467 glClipPlane(GL_CLIP_PLANE1,bly);
468 glEnable(GL_CLIP_PLANE0);
469 glEnable(GL_CLIP_PLANE1);
471 glTranslated(ix,iy,0);
472 glClipPlane(GL_CLIP_PLANE2,urx);
473 glClipPlane(GL_CLIP_PLANE3,ury);
474 glEnable(GL_CLIP_PLANE2);
475 glEnable(GL_CLIP_PLANE3);
482 glDisable(GL_CLIP_PLANE0);
483 glDisable(GL_CLIP_PLANE1);
484 glDisable(GL_CLIP_PLANE2);
485 glDisable(GL_CLIP_PLANE3);
489 state.setActiveTextureUnit(0);
490 state.setClientActiveTextureUnit(0);
493 // Draw yellow "hotspots" if directed to. This is a panel authoring
494 // feature; not intended to be high performance or to look good.
495 if ( fgGetBool("/sim/panel-hotspots") ) {
496 static osg::ref_ptr<osg::StateSet> hotspotStateSet;
497 if (!hotspotStateSet.valid()) {
498 hotspotStateSet = new osg::StateSet;
499 hotspotStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
500 hotspotStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
503 state.pushStateSet(hotspotStateSet.get());
505 state.setActiveTextureUnit(0);
506 state.setClientActiveTextureUnit(0);
509 glPushAttrib(GL_ENABLE_BIT);
510 glDisable(GL_COLOR_MATERIAL);
513 for ( unsigned int i = 0; i < _instruments.size(); i++ )
514 _instruments[i]->drawHotspots(state);
520 state.setActiveTextureUnit(0);
521 state.setClientActiveTextureUnit(0);
527 * Set the panel's visibility.
530 FGPanel::setVisibility (bool visibility)
532 _visibility->setBoolValue( visibility );
537 * Return true if the panel is visible.
540 FGPanel::getVisibility () const
542 return _visibility->getBoolValue();
547 * Set the panel's background texture.
550 FGPanel::setBackground (osg::Texture2D* texture)
552 osg::StateSet* stateSet = new osg::StateSet;
553 stateSet->setTextureAttribute(0, texture);
554 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
555 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
560 * Set the panel's multiple background textures.
563 FGPanel::setMultiBackground (osg::Texture2D* texture, int idx)
567 osg::StateSet* stateSet = new osg::StateSet;
568 stateSet->setTextureAttribute(0, texture);
569 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
570 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
571 _mbg[idx] = stateSet;
575 * Set the panel's x-offset.
578 FGPanel::setXOffset (int offset)
580 if (offset <= 0 && offset >= -_width + WIN_W)
581 _x_offset->setIntValue( offset );
586 * Set the panel's y-offset.
589 FGPanel::setYOffset (int offset)
591 if (offset <= 0 && offset >= -_height)
592 _y_offset->setIntValue( offset );
596 * Handle a mouse action in panel-local (not screen) coordinates.
597 * Used by the 3D panel code in Model/panelnode.cxx, in situations
598 * where the panel doesn't control its own screen location.
601 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
603 // Note a released button and return
605 if (_mouseInstrument != 0)
606 _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
608 _mouseInstrument = 0;
612 // Search for a matching instrument.
613 for (int i = 0; i < (int)_instruments.size(); i++) {
614 FGPanelInstrument *inst = _instruments[i];
615 int ix = inst->getXPos();
616 int iy = inst->getYPos();
617 int iw = inst->getWidth() / 2;
618 int ih = inst->getHeight() / 2;
619 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
622 _mouseInstrument = inst;
623 _mouseButton = button;
626 // Always do the action once.
627 return _mouseInstrument->doMouseAction(_mouseButton, 0,
635 * Perform a mouse action.
638 FGPanel::doMouseAction (int button, int updown, int x, int y)
640 // FIXME: this same code appears in update()
641 int xsize = _xsize_node->getIntValue();
642 int ysize = _ysize_node->getIntValue();
643 float aspect_adjust = get_aspect_adjust(xsize, ysize);
645 // Scale for the real window size.
646 if (aspect_adjust < 1.0) {
647 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
648 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
650 x = int(((float)x / xsize) * WIN_W);
651 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
654 // Adjust for offsets.
655 x -= _x_offset->getIntValue();
656 y -= _y_offset->getIntValue();
658 // Having fixed up the coordinates, fall through to the local
659 // coordinate handler.
660 return doLocalMouseAction(button, updown, x, y);
663 void FGPanel::setDepthTest (bool enable) {
664 _enable_depth_test = enable;
669 ////////////////////////////////////////////////////////////////////////.
670 // Implementation of FGPanelAction.
671 ////////////////////////////////////////////////////////////////////////
673 FGPanelAction::FGPanelAction ()
677 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
679 : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
683 FGPanelAction::~FGPanelAction ()
685 for (unsigned int i = 0; i < 2; i++) {
686 for (unsigned int j = 0; j < _bindings[i].size(); j++)
687 delete _bindings[i][j];
692 FGPanelAction::addBinding (SGBinding * binding, int updown)
694 _bindings[updown].push_back(binding);
698 FGPanelAction::doAction (int updown)
701 if ((updown != _last_state) || (updown == 0 && _repeatable)) {
702 int nBindings = _bindings[updown].size();
703 for (int i = 0; i < nBindings; i++)
704 _bindings[updown][i]->fire();
706 _last_state = updown;
715 ////////////////////////////////////////////////////////////////////////
716 // Implementation of FGPanelTransformation.
717 ////////////////////////////////////////////////////////////////////////
719 FGPanelTransformation::FGPanelTransformation ()
724 FGPanelTransformation::~FGPanelTransformation ()
731 ////////////////////////////////////////////////////////////////////////
732 // Implementation of FGPanelInstrument.
733 ////////////////////////////////////////////////////////////////////////
736 FGPanelInstrument::FGPanelInstrument ()
742 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
748 FGPanelInstrument::~FGPanelInstrument ()
750 for (action_list_type::iterator it = _actions.begin();
751 it != _actions.end();
759 FGPanelInstrument::drawHotspots(osg::State& state)
761 for ( unsigned int i = 0; i < _actions.size(); i++ ) {
762 FGPanelAction* a = _actions[i];
763 float x1 = getXPos() + a->getX();
764 float x2 = x1 + a->getWidth();
765 float y1 = getYPos() + a->getY();
766 float y2 = y1 + a->getHeight();
768 glBegin(GL_LINE_LOOP);
778 FGPanelInstrument::setPosition (int x, int y)
785 FGPanelInstrument::setSize (int w, int h)
792 FGPanelInstrument::getXPos () const
798 FGPanelInstrument::getYPos () const
804 FGPanelInstrument::getWidth () const
810 FGPanelInstrument::getHeight () const
816 FGPanelInstrument::addAction (FGPanelAction * action)
818 _actions.push_back(action);
821 // Coordinates relative to centre.
823 FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
826 action_list_type::iterator it = _actions.begin();
827 action_list_type::iterator last = _actions.end();
828 for ( ; it != last; it++) {
829 if ((*it)->inArea(button, x, y) &&
830 (*it)->doAction(updown))
839 ////////////////////////////////////////////////////////////////////////
840 // Implementation of FGLayeredInstrument.
841 ////////////////////////////////////////////////////////////////////////
843 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
844 : FGPanelInstrument(x, y, w, h)
848 FGLayeredInstrument::~FGLayeredInstrument ()
850 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
857 FGLayeredInstrument::draw (osg::State& state)
862 for (int i = 0; i < (int)_layers.size(); i++) {
864 _layers[i]->draw(state);
870 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
872 int n = _layers.size();
873 if (layer->getWidth() == -1) {
874 layer->setWidth(getWidth());
876 if (layer->getHeight() == -1) {
877 layer->setHeight(getHeight());
879 _layers.push_back(layer);
884 FGLayeredInstrument::addLayer (const FGCroppedTexture &texture,
887 return addLayer(new FGTexturedLayer(texture, w, h));
891 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
893 int layer = _layers.size() - 1;
894 _layers[layer]->addTransformation(transformation);
899 ////////////////////////////////////////////////////////////////////////
900 // Implementation of FGSpecialInstrument.
901 ////////////////////////////////////////////////////////////////////////
903 FGSpecialInstrument::FGSpecialInstrument (DCLGPS* sb)
904 : FGPanelInstrument()
909 FGSpecialInstrument::~FGSpecialInstrument ()
914 FGSpecialInstrument::draw (osg::State& state)
921 ////////////////////////////////////////////////////////////////////////
922 // Implementation of FGInstrumentLayer.
923 ////////////////////////////////////////////////////////////////////////
925 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
931 FGInstrumentLayer::~FGInstrumentLayer ()
933 for (transformation_list::iterator it = _transformations.begin();
934 it != _transformations.end();
942 FGInstrumentLayer::transform () const
944 transformation_list::const_iterator it = _transformations.begin();
945 transformation_list::const_iterator last = _transformations.end();
947 FGPanelTransformation *t = *it;
949 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
952 val = fmod(val, t->mod);
955 } else if (val > t->max) {
960 val = val * t->factor + t->offset;
962 val = t->table->interpolate(val) * t->factor + t->offset;
966 case FGPanelTransformation::XSHIFT:
967 glTranslatef(val, 0.0, 0.0);
969 case FGPanelTransformation::YSHIFT:
970 glTranslatef(0.0, val, 0.0);
972 case FGPanelTransformation::ROTATION:
973 glRotatef(-val, 0.0, 0.0, 1.0);
982 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
984 _transformations.push_back(transformation);
989 ////////////////////////////////////////////////////////////////////////
990 // Implementation of FGGroupLayer.
991 ////////////////////////////////////////////////////////////////////////
993 FGGroupLayer::FGGroupLayer ()
997 FGGroupLayer::~FGGroupLayer ()
999 for (unsigned int i = 0; i < _layers.size(); i++)
1004 FGGroupLayer::draw (osg::State& state)
1008 int nLayers = _layers.size();
1009 for (int i = 0; i < nLayers; i++)
1010 _layers[i]->draw(state);
1015 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
1017 _layers.push_back(layer);
1022 ////////////////////////////////////////////////////////////////////////
1023 // Implementation of FGTexturedLayer.
1024 ////////////////////////////////////////////////////////////////////////
1027 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
1028 : FGInstrumentLayer(w, h),
1031 setTexture(texture);
1035 FGTexturedLayer::~FGTexturedLayer ()
1041 FGTexturedLayer::draw (osg::State& state)
1048 state.pushStateSet(_texture.getTexture());
1050 state.setActiveTextureUnit(0);
1051 state.setClientActiveTextureUnit(0);
1053 glBegin(GL_POLYGON);
1056 glColor4fv( emissive_panel_color );
1058 // From Curt: turn on the panel
1059 // lights after sundown.
1060 glColor4fv( panel_color );
1063 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
1064 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
1065 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
1066 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
1068 state.popStateSet();
1070 state.setActiveTextureUnit(0);
1071 state.setClientActiveTextureUnit(0);
1078 ////////////////////////////////////////////////////////////////////////
1079 // Implementation of FGTextLayer.
1080 ////////////////////////////////////////////////////////////////////////
1082 FGTextLayer::FGTextLayer (int w, int h)
1083 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
1086 _color[0] = _color[1] = _color[2] = 0.0;
1090 FGTextLayer::~FGTextLayer ()
1092 chunk_list::iterator it = _chunks.begin();
1093 chunk_list::iterator last = _chunks.end();
1094 for ( ; it != last; it++) {
1100 FGTextLayer::draw (osg::State& state)
1106 FGFontCache *fc = globals->get_fontcache();
1107 text_renderer.setFont(fc->getTexFont(_font_name.c_str()));
1109 text_renderer.setPointSize(_pointSize);
1110 text_renderer.begin();
1111 text_renderer.start3f(0, 0, 0);
1114 long diff = _now - _then;
1116 if (diff > 100000 || diff < 0 ) {
1117 // ( diff < 0 ) is a sanity check and indicates our time stamp
1118 // difference math probably overflowed. We can handle a max
1119 // difference of 35.8 minutes since the returned value is in
1120 // usec. So if the panel is left off longer than that we can
1121 // over flow the math with it is turned back on. This (diff <
1122 // 0) catches that situation, get's us out of trouble, and
1128 // Something is goofy. The code in this file renders only CCW
1129 // polygons, and I have verified that the font code in plib
1130 // renders only CCW trianbles. Yet they come out backwards.
1131 // Something around here or in plib is either changing the winding
1132 // order or (more likely) pushing a left-handed matrix onto the
1133 // stack. But I can't find it; get out the chainsaw...
1135 text_renderer.puts((char *)(_value.c_str()));
1136 glFrontFace(GL_CCW);
1138 text_renderer.end();
1139 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
1144 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1146 _chunks.push_back(chunk);
1150 FGTextLayer::setColor (float r, float g, float b)
1159 FGTextLayer::setPointSize (float size)
1165 FGTextLayer::setFontName(const string &name)
1167 _font_name = name + ".txf";
1172 FGTextLayer::setFont(fntFont * font)
1174 text_renderer.setFont(font);
1179 FGTextLayer::recalc_value () const
1182 chunk_list::const_iterator it = _chunks.begin();
1183 chunk_list::const_iterator last = _chunks.end();
1184 for ( ; it != last; it++) {
1185 _value += (*it)->getValue();
1191 ////////////////////////////////////////////////////////////////////////
1192 // Implementation of FGTextLayer::Chunk.
1193 ////////////////////////////////////////////////////////////////////////
1195 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1196 : _type(FGTextLayer::TEXT), _fmt(fmt)
1203 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1204 const string &fmt, float mult, float offs,
1206 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
1209 if (type == TEXT_VALUE)
1218 FGTextLayer::Chunk::getValue () const
1224 sprintf(_buf, _fmt.c_str(), _text.c_str());
1227 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1230 double d = _offs + _node->getFloatValue() * _mult;
1231 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
1232 sprintf(_buf, _fmt.c_str(), d);
1243 ////////////////////////////////////////////////////////////////////////
1244 // Implementation of FGSwitchLayer.
1245 ////////////////////////////////////////////////////////////////////////
1247 FGSwitchLayer::FGSwitchLayer ()
1253 FGSwitchLayer::draw (osg::State& state)
1257 int nLayers = _layers.size();
1258 for (int i = 0; i < nLayers; i++) {
1259 if (_layers[i]->test()) {
1260 _layers[i]->draw(state);