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.
36 #include <stdio.h> // sprintf
40 #include <osg/CullFace>
42 #include <osg/Material>
43 #include <osg/Matrixf>
45 #include <osg/PolygonOffset>
47 #include <simgear/compiler.h>
51 #include <simgear/debug/logstream.hxx>
52 #include <simgear/misc/sg_path.hxx>
53 #include <simgear/scene/model/model.hxx>
56 #include <Main/globals.hxx>
57 #include <Main/fg_props.hxx>
58 #include <Main/viewmgr.hxx>
59 #include <Time/light.hxx>
60 #include <GUI/new_gui.hxx> // FGFontCache
61 #include <Main/viewer.hxx>
62 #include <Instrumentation/dclgps.hxx>
72 // The number of polygon-offset "units" to place between layers. In
73 // principle, one is supposed to be enough. In practice, I find that
74 // my hardware/driver requires many more.
77 ////////////////////////////////////////////////////////////////////////
79 ////////////////////////////////////////////////////////////////////////
83 * Calculate the aspect adjustment for the panel.
86 get_aspect_adjust (int xsize, int ysize)
88 float ideal_aspect = float(WIN_W) / float(WIN_H);
89 float real_aspect = float(xsize) / float(ysize);
90 return (real_aspect / ideal_aspect);
95 ////////////////////////////////////////////////////////////////////////
97 ////////////////////////////////////////////////////////////////////////
102 const FGPanel* current = globals->get_current_panel();
105 if (current->getVisibility() == 0)
107 if (globals->get_viewmgr()->get_current() != 0)
109 if (current->getAutohide() && globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS != 0)
115 ////////////////////////////////////////////////////////////////////////
116 // Implementation of FGTextureManager.
117 ////////////////////////////////////////////////////////////////////////
119 map<string,osg::ref_ptr<osg::Texture2D> > FGTextureManager::_textureMap;
122 FGTextureManager::createTexture (const string &relativePath, bool staticTexture)
124 osg::Texture2D* texture = _textureMap[relativePath].get();
126 SGPath tpath = globals->resolve_aircraft_path(relativePath);
127 texture = SGLoadTexture2D(staticTexture, tpath);
129 _textureMap[relativePath] = texture;
130 if (!_textureMap[relativePath].valid())
131 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
132 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath );
139 void FGTextureManager::addTexture(const string &relativePath,
140 osg::Texture2D* texture)
142 _textureMap[relativePath] = texture;
145 ////////////////////////////////////////////////////////////////////////
146 // Implementation of FGCropped Texture.
147 ////////////////////////////////////////////////////////////////////////
150 FGCroppedTexture::FGCroppedTexture ()
151 : _path(""), _texture(0),
152 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
157 FGCroppedTexture::FGCroppedTexture (const string &path,
158 float minX, float minY,
159 float maxX, float maxY)
160 : _path(path), _texture(0),
161 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
166 FGCroppedTexture::~FGCroppedTexture ()
172 FGCroppedTexture::getTexture ()
175 _texture = new osg::StateSet;
176 _texture->setTextureAttribute(0, FGTextureManager::createTexture(_path));
177 _texture->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
178 _texture->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
180 return _texture.get();
185 ////////////////////////////////////////////////////////////////////////
186 // Implementation of FGPanel.
187 ////////////////////////////////////////////////////////////////////////
189 static fntRenderer text_renderer;
190 static sgVec4 panel_color;
191 static sgVec4 emissive_panel_color = {1,1,1,1};
199 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
200 _view_height(int(WIN_H * 0.4232)),
201 _visibility(fgGetNode("/sim/panel/visibility", true)),
202 _x_offset(fgGetNode("/sim/panel/x-offset", true)),
203 _y_offset(fgGetNode("/sim/panel/y-offset", true)),
204 _jitter(fgGetNode("/sim/panel/jitter", true)),
205 _flipx(fgGetNode("/sim/panel/flip-x", true)),
206 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
207 _ysize_node(fgGetNode("/sim/startup/ysize", true)),
208 _enable_depth_test(false)
218 for (instrument_list_type::iterator it = _instruments.begin();
219 it != _instruments.end();
228 * Add an instrument to the panel.
231 FGPanel::addInstrument (FGPanelInstrument * instrument)
233 _instruments.push_back(instrument);
238 * Initialize the panel.
247 * Bind panel properties.
252 fgSetArchivable("/sim/panel/visibility");
253 fgSetArchivable("/sim/panel/x-offset");
254 fgSetArchivable("/sim/panel/y-offset");
255 fgSetArchivable("/sim/panel/jitter");
260 * Unbind panel properties.
269 FGPanel::update (double dt)
271 std::cout << "OSGFIXME" << std::endl;
275 FGPanel::update (osg::State& state, GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
278 // Calculate accelerations
279 // and jiggle the panel accordingly
280 // The factors and bounds are just
281 // initial guesses; using sqrt smooths
283 double x_offset = _x_offset->getIntValue();
284 double y_offset = _y_offset->getIntValue();
287 glMatrixMode(GL_PROJECTION);
290 if ( _flipx->getBoolValue() ) {
291 proj = Matrixf::ortho2D(winx + winw, winx, winy + winh, winy); /* up side down */
293 proj = Matrixf::ortho2D(winx, winx + winw, winy, winy + winh); /* right side up */
295 glLoadMatrix(proj.ptr());
297 glMatrixMode(GL_MODELVIEW);
301 glTranslated(x_offset, y_offset, 0);
305 glMatrixMode(GL_PROJECTION);
307 glMatrixMode(GL_MODELVIEW);
315 FGPanel::update (osg::State& state)
317 // Do nothing if the panel isn't visible.
318 if ( !fgPanelVisible() ) {
324 // Now, draw the panel
325 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
326 _ysize_node->getIntValue());
327 if (aspect_adjust <1.0)
328 update(state, WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
330 update(state, WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
334 * Handle repeatable mouse events. Called from update() and from
335 * fgUpdate3DPanels(). This functionality needs to move into the
336 * input subsystem. Counting a tick every two frames is clumsy...
338 void FGPanel::updateMouseDelay()
342 if (_mouseDelay < 0) {
343 _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
351 FGPanel::draw(osg::State& state)
353 // In 3D mode, it's possible that we are being drawn exactly on top
354 // of an existing polygon. Use an offset to prevent z-fighting. In
355 // 2D mode, this is a no-op.
356 static osg::ref_ptr<osg::StateSet> panelStateSet;
357 if (!panelStateSet.valid()) {
358 panelStateSet = new osg::StateSet;
359 panelStateSet->setAttributeAndModes(new osg::PolygonOffset(-1, -POFF_UNITS));
360 panelStateSet->setTextureAttribute(0, new osg::TexEnv);
362 // Draw the background
363 panelStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
364 panelStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
365 panelStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
366 panelStateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
367 osg::Material* material = new osg::Material;
368 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
369 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
370 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
371 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
372 material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
373 panelStateSet->setAttribute(material);
374 panelStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
375 panelStateSet->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK));
376 panelStateSet->setAttributeAndModes(new osg::Depth(osg::Depth::LEQUAL));
378 if ( _enable_depth_test )
379 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
381 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
382 state.pushStateSet(panelStateSet.get());
384 state.setActiveTextureUnit(0);
385 state.setClientActiveTextureUnit(0);
387 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
388 sgCopyVec4( panel_color, l->scene_diffuse().data());
389 if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
390 if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
391 if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
392 if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
394 glColor4fv( panel_color );
396 state.pushStateSet(_bg.get());
398 state.setActiveTextureUnit(0);
399 state.setClientActiveTextureUnit(0);
402 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
403 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
404 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
405 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
409 state.setActiveTextureUnit(0);
410 state.setClientActiveTextureUnit(0);
413 for (int i = 0; i < 4; i ++) {
414 // top row of textures...(1,3,5,7)
415 state.pushStateSet(_mbg[i*2].get());
417 state.setActiveTextureUnit(0);
418 state.setClientActiveTextureUnit(0);
421 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
422 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
423 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
424 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
428 state.setActiveTextureUnit(0);
429 state.setClientActiveTextureUnit(0);
431 // bottom row of textures...(2,4,6,8)
432 state.pushStateSet(_mbg[i*2+1].get());
434 state.setActiveTextureUnit(0);
435 state.setClientActiveTextureUnit(0);
438 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
439 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
440 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
441 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
445 state.setActiveTextureUnit(0);
446 state.setClientActiveTextureUnit(0);
451 // Draw the instruments.
452 // Syd Adams: added instrument clipping
453 instrument_list_type::const_iterator current = _instruments.begin();
454 instrument_list_type::const_iterator end = _instruments.end();
456 GLdouble blx[4]={1.0,0.0,0.0,0.0};
457 GLdouble bly[4]={0.0,1.0,0.0,0.0};
458 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
459 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
461 for ( ; current != end; current++) {
462 FGPanelInstrument * instr = *current;
464 glTranslated(instr->getXPos(), instr->getYPos(), 0);
466 int ix= instr->getWidth();
467 int iy= instr->getHeight();
469 glTranslated(-ix/2,-iy/2,0);
470 glClipPlane(GL_CLIP_PLANE0,blx);
471 glClipPlane(GL_CLIP_PLANE1,bly);
472 glEnable(GL_CLIP_PLANE0);
473 glEnable(GL_CLIP_PLANE1);
475 glTranslated(ix,iy,0);
476 glClipPlane(GL_CLIP_PLANE2,urx);
477 glClipPlane(GL_CLIP_PLANE3,ury);
478 glEnable(GL_CLIP_PLANE2);
479 glEnable(GL_CLIP_PLANE3);
486 glDisable(GL_CLIP_PLANE0);
487 glDisable(GL_CLIP_PLANE1);
488 glDisable(GL_CLIP_PLANE2);
489 glDisable(GL_CLIP_PLANE3);
493 state.setActiveTextureUnit(0);
494 state.setClientActiveTextureUnit(0);
497 // Draw yellow "hotspots" if directed to. This is a panel authoring
498 // feature; not intended to be high performance or to look good.
499 if ( fgGetBool("/sim/panel-hotspots") ) {
500 static osg::ref_ptr<osg::StateSet> hotspotStateSet;
501 if (!hotspotStateSet.valid()) {
502 hotspotStateSet = new osg::StateSet;
503 hotspotStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
504 hotspotStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
507 state.pushStateSet(hotspotStateSet.get());
509 state.setActiveTextureUnit(0);
510 state.setClientActiveTextureUnit(0);
513 glPushAttrib(GL_ENABLE_BIT);
514 glDisable(GL_COLOR_MATERIAL);
517 for ( unsigned int i = 0; i < _instruments.size(); i++ )
518 _instruments[i]->drawHotspots(state);
524 state.setActiveTextureUnit(0);
525 state.setClientActiveTextureUnit(0);
531 * Set the panel's visibility.
534 FGPanel::setVisibility (bool visibility)
536 _visibility->setBoolValue( visibility );
541 * Return true if the panel is visible.
544 FGPanel::getVisibility () const
546 return _visibility->getBoolValue();
551 * Set the panel's background texture.
554 FGPanel::setBackground (osg::Texture2D* texture)
556 osg::StateSet* stateSet = new osg::StateSet;
557 stateSet->setTextureAttribute(0, texture);
558 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
559 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
564 * Set the panel's multiple background textures.
567 FGPanel::setMultiBackground (osg::Texture2D* texture, int idx)
571 osg::StateSet* stateSet = new osg::StateSet;
572 stateSet->setTextureAttribute(0, texture);
573 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
574 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
575 _mbg[idx] = stateSet;
579 * Set the panel's x-offset.
582 FGPanel::setXOffset (int offset)
584 if (offset <= 0 && offset >= -_width + WIN_W)
585 _x_offset->setIntValue( offset );
590 * Set the panel's y-offset.
593 FGPanel::setYOffset (int offset)
595 if (offset <= 0 && offset >= -_height)
596 _y_offset->setIntValue( offset );
600 * Handle a mouse action in panel-local (not screen) coordinates.
601 * Used by the 3D panel code in Model/panelnode.cxx, in situations
602 * where the panel doesn't control its own screen location.
605 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
607 // Note a released button and return
609 if (_mouseInstrument != 0)
610 _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
612 _mouseInstrument = 0;
616 // Search for a matching instrument.
617 for (int i = 0; i < (int)_instruments.size(); i++) {
618 FGPanelInstrument *inst = _instruments[i];
619 int ix = inst->getXPos();
620 int iy = inst->getYPos();
621 int iw = inst->getWidth() / 2;
622 int ih = inst->getHeight() / 2;
623 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
626 _mouseInstrument = inst;
627 _mouseButton = button;
630 // Always do the action once.
631 return _mouseInstrument->doMouseAction(_mouseButton, 0,
639 * Perform a mouse action.
642 FGPanel::doMouseAction (int button, int updown, int x, int y)
644 // FIXME: this same code appears in update()
645 int xsize = _xsize_node->getIntValue();
646 int ysize = _ysize_node->getIntValue();
647 float aspect_adjust = get_aspect_adjust(xsize, ysize);
649 // Scale for the real window size.
650 if (aspect_adjust < 1.0) {
651 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
652 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
654 x = int(((float)x / xsize) * WIN_W);
655 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
658 // Adjust for offsets.
659 x -= _x_offset->getIntValue();
660 y -= _y_offset->getIntValue();
662 // Having fixed up the coordinates, fall through to the local
663 // coordinate handler.
664 return doLocalMouseAction(button, updown, x, y);
667 void FGPanel::setDepthTest (bool enable) {
668 _enable_depth_test = enable;
673 ////////////////////////////////////////////////////////////////////////.
674 // Implementation of FGPanelAction.
675 ////////////////////////////////////////////////////////////////////////
677 FGPanelAction::FGPanelAction ()
681 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
683 : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
687 FGPanelAction::~FGPanelAction ()
689 for (unsigned int i = 0; i < 2; i++) {
690 for (unsigned int j = 0; j < _bindings[i].size(); j++)
691 delete _bindings[i][j];
696 FGPanelAction::addBinding (SGBinding * binding, int updown)
698 _bindings[updown].push_back(binding);
702 FGPanelAction::doAction (int updown)
705 if ((updown != _last_state) || (updown == 0 && _repeatable)) {
706 int nBindings = _bindings[updown].size();
707 for (int i = 0; i < nBindings; i++)
708 _bindings[updown][i]->fire();
710 _last_state = updown;
719 ////////////////////////////////////////////////////////////////////////
720 // Implementation of FGPanelTransformation.
721 ////////////////////////////////////////////////////////////////////////
723 FGPanelTransformation::FGPanelTransformation ()
728 FGPanelTransformation::~FGPanelTransformation ()
735 ////////////////////////////////////////////////////////////////////////
736 // Implementation of FGPanelInstrument.
737 ////////////////////////////////////////////////////////////////////////
740 FGPanelInstrument::FGPanelInstrument ()
746 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
752 FGPanelInstrument::~FGPanelInstrument ()
754 for (action_list_type::iterator it = _actions.begin();
755 it != _actions.end();
763 FGPanelInstrument::drawHotspots(osg::State& state)
765 for ( unsigned int i = 0; i < _actions.size(); i++ ) {
766 FGPanelAction* a = _actions[i];
767 float x1 = getXPos() + a->getX();
768 float x2 = x1 + a->getWidth();
769 float y1 = getYPos() + a->getY();
770 float y2 = y1 + a->getHeight();
772 glBegin(GL_LINE_LOOP);
782 FGPanelInstrument::setPosition (int x, int y)
789 FGPanelInstrument::setSize (int w, int h)
796 FGPanelInstrument::getXPos () const
802 FGPanelInstrument::getYPos () const
808 FGPanelInstrument::getWidth () const
814 FGPanelInstrument::getHeight () const
820 FGPanelInstrument::addAction (FGPanelAction * action)
822 _actions.push_back(action);
825 // Coordinates relative to centre.
827 FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
830 action_list_type::iterator it = _actions.begin();
831 action_list_type::iterator last = _actions.end();
832 for ( ; it != last; it++) {
833 if ((*it)->inArea(button, x, y) &&
834 (*it)->doAction(updown))
843 ////////////////////////////////////////////////////////////////////////
844 // Implementation of FGLayeredInstrument.
845 ////////////////////////////////////////////////////////////////////////
847 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
848 : FGPanelInstrument(x, y, w, h)
852 FGLayeredInstrument::~FGLayeredInstrument ()
854 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
861 FGLayeredInstrument::draw (osg::State& state)
866 for (int i = 0; i < (int)_layers.size(); i++) {
868 _layers[i]->draw(state);
874 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
876 int n = _layers.size();
877 if (layer->getWidth() == -1) {
878 layer->setWidth(getWidth());
880 if (layer->getHeight() == -1) {
881 layer->setHeight(getHeight());
883 _layers.push_back(layer);
888 FGLayeredInstrument::addLayer (const FGCroppedTexture &texture,
891 return addLayer(new FGTexturedLayer(texture, w, h));
895 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
897 int layer = _layers.size() - 1;
898 _layers[layer]->addTransformation(transformation);
903 ////////////////////////////////////////////////////////////////////////
904 // Implementation of FGSpecialInstrument.
905 ////////////////////////////////////////////////////////////////////////
907 FGSpecialInstrument::FGSpecialInstrument (DCLGPS* sb)
908 : FGPanelInstrument()
913 FGSpecialInstrument::~FGSpecialInstrument ()
918 FGSpecialInstrument::draw (osg::State& state)
920 complex->draw(state);
925 ////////////////////////////////////////////////////////////////////////
926 // Implementation of FGInstrumentLayer.
927 ////////////////////////////////////////////////////////////////////////
929 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
935 FGInstrumentLayer::~FGInstrumentLayer ()
937 for (transformation_list::iterator it = _transformations.begin();
938 it != _transformations.end();
946 FGInstrumentLayer::transform () const
948 transformation_list::const_iterator it = _transformations.begin();
949 transformation_list::const_iterator last = _transformations.end();
951 FGPanelTransformation *t = *it;
953 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
956 val = fmod(val, t->mod);
959 } else if (val > t->max) {
964 val = val * t->factor + t->offset;
966 val = t->table->interpolate(val) * t->factor + t->offset;
970 case FGPanelTransformation::XSHIFT:
971 glTranslatef(val, 0.0, 0.0);
973 case FGPanelTransformation::YSHIFT:
974 glTranslatef(0.0, val, 0.0);
976 case FGPanelTransformation::ROTATION:
977 glRotatef(-val, 0.0, 0.0, 1.0);
986 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
988 _transformations.push_back(transformation);
993 ////////////////////////////////////////////////////////////////////////
994 // Implementation of FGGroupLayer.
995 ////////////////////////////////////////////////////////////////////////
997 FGGroupLayer::FGGroupLayer ()
1001 FGGroupLayer::~FGGroupLayer ()
1003 for (unsigned int i = 0; i < _layers.size(); i++)
1008 FGGroupLayer::draw (osg::State& state)
1012 int nLayers = _layers.size();
1013 for (int i = 0; i < nLayers; i++)
1014 _layers[i]->draw(state);
1019 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
1021 _layers.push_back(layer);
1026 ////////////////////////////////////////////////////////////////////////
1027 // Implementation of FGTexturedLayer.
1028 ////////////////////////////////////////////////////////////////////////
1031 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
1032 : FGInstrumentLayer(w, h),
1035 setTexture(texture);
1039 FGTexturedLayer::~FGTexturedLayer ()
1045 FGTexturedLayer::draw (osg::State& state)
1052 state.pushStateSet(_texture.getTexture());
1054 state.setActiveTextureUnit(0);
1055 state.setClientActiveTextureUnit(0);
1057 glBegin(GL_POLYGON);
1060 glColor4fv( emissive_panel_color );
1062 // From Curt: turn on the panel
1063 // lights after sundown.
1064 glColor4fv( panel_color );
1067 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
1068 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
1069 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
1070 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
1072 state.popStateSet();
1074 state.setActiveTextureUnit(0);
1075 state.setClientActiveTextureUnit(0);
1082 ////////////////////////////////////////////////////////////////////////
1083 // Implementation of FGTextLayer.
1084 ////////////////////////////////////////////////////////////////////////
1086 FGTextLayer::FGTextLayer (int w, int h)
1087 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
1090 _color[0] = _color[1] = _color[2] = 0.0;
1094 FGTextLayer::~FGTextLayer ()
1096 chunk_list::iterator it = _chunks.begin();
1097 chunk_list::iterator last = _chunks.end();
1098 for ( ; it != last; it++) {
1104 FGTextLayer::draw (osg::State& state)
1110 FGFontCache *fc = globals->get_fontcache();
1111 fntFont* font = fc->getTexFont(_font_name.c_str());
1113 return; // don't crash on missing fonts
1116 text_renderer.setFont(font);
1118 text_renderer.setPointSize(_pointSize);
1119 text_renderer.begin();
1120 text_renderer.start3f(0, 0, 0);
1123 double diff = (_now - _then).toUSecs();
1125 if (diff > 100000 || diff < 0 ) {
1126 // ( diff < 0 ) is a sanity check and indicates our time stamp
1127 // difference math probably overflowed. We can handle a max
1128 // difference of 35.8 minutes since the returned value is in
1129 // usec. So if the panel is left off longer than that we can
1130 // over flow the math with it is turned back on. This (diff <
1131 // 0) catches that situation, get's us out of trouble, and
1137 // Something is goofy. The code in this file renders only CCW
1138 // polygons, and I have verified that the font code in plib
1139 // renders only CCW trianbles. Yet they come out backwards.
1140 // Something around here or in plib is either changing the winding
1141 // order or (more likely) pushing a left-handed matrix onto the
1142 // stack. But I can't find it; get out the chainsaw...
1144 text_renderer.puts((char *)(_value.c_str()));
1145 glFrontFace(GL_CCW);
1147 text_renderer.end();
1148 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
1153 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1155 _chunks.push_back(chunk);
1159 FGTextLayer::setColor (float r, float g, float b)
1168 FGTextLayer::setPointSize (float size)
1174 FGTextLayer::setFontName(const string &name)
1176 _font_name = name + ".txf";
1177 FGFontCache *fc = globals->get_fontcache();
1178 fntFont* font = fc->getTexFont(_font_name.c_str());
1180 SG_LOG(SG_GENERAL, SG_WARN, "unable to find font:" << name);
1186 FGTextLayer::setFont(fntFont * font)
1188 text_renderer.setFont(font);
1193 FGTextLayer::recalc_value () const
1196 chunk_list::const_iterator it = _chunks.begin();
1197 chunk_list::const_iterator last = _chunks.end();
1198 for ( ; it != last; it++) {
1199 _value += (*it)->getValue();
1205 ////////////////////////////////////////////////////////////////////////
1206 // Implementation of FGTextLayer::Chunk.
1207 ////////////////////////////////////////////////////////////////////////
1209 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1210 : _type(FGTextLayer::TEXT), _fmt(fmt)
1217 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1218 const string &fmt, float mult, float offs,
1220 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
1223 if (type == TEXT_VALUE)
1232 FGTextLayer::Chunk::getValue () const
1238 sprintf(_buf, _fmt.c_str(), _text.c_str());
1241 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1244 double d = _offs + _node->getFloatValue() * _mult;
1245 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
1246 sprintf(_buf, _fmt.c_str(), d);
1257 ////////////////////////////////////////////////////////////////////////
1258 // Implementation of FGSwitchLayer.
1259 ////////////////////////////////////////////////////////////////////////
1261 FGSwitchLayer::FGSwitchLayer ()
1267 FGSwitchLayer::draw (osg::State& state)
1271 int nLayers = _layers.size();
1272 for (int i = 0; i < nLayers; i++) {
1273 if (_layers[i]->test()) {
1274 _layers[i]->draw(state);