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.
38 #include <stdio.h> // sprintf
41 #include <osg/CullFace>
43 #include <osg/Material>
45 #include <osg/PolygonOffset>
47 #include <simgear/compiler.h>
53 #include <simgear/debug/logstream.hxx>
54 #include <simgear/misc/sg_path.hxx>
55 #include <simgear/scene/model/model.hxx>
57 #include <Main/globals.hxx>
58 #include <Main/fg_props.hxx>
59 #include <Main/viewmgr.hxx>
60 #include <Time/light.hxx>
61 #include <GUI/new_gui.hxx> // FGFontCache
71 // The number of polygon-offset "units" to place between layers. In
72 // principle, one is supposed to be enough. In practice, I find that
73 // my hardware/driver requires many more.
76 ////////////////////////////////////////////////////////////////////////
78 ////////////////////////////////////////////////////////////////////////
82 * Calculate the aspect adjustment for the panel.
85 get_aspect_adjust (int xsize, int ysize)
87 float ideal_aspect = float(WIN_W) / float(WIN_H);
88 float real_aspect = float(xsize) / float(ysize);
89 return (real_aspect / ideal_aspect);
94 ////////////////////////////////////////////////////////////////////////
96 ////////////////////////////////////////////////////////////////////////
101 if (globals->get_current_panel() == 0)
103 if (globals->get_current_panel()->getVisibility() == 0)
105 if (globals->get_viewmgr()->get_current() != 0)
107 if (globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS != 0)
114 ////////////////////////////////////////////////////////////////////////
115 // Implementation of FGTextureManager.
116 ////////////////////////////////////////////////////////////////////////
118 map<string,osg::ref_ptr<osg::Texture2D> > FGTextureManager::_textureMap;
121 FGTextureManager::createTexture (const string &relativePath)
123 osg::Texture2D* texture = _textureMap[relativePath].get();
125 SG_LOG( SG_COCKPIT, SG_DEBUG,
126 "Texture " << relativePath << " does not yet exist" );
127 SGPath tpath(globals->get_fg_root());
128 tpath.append(relativePath);
130 texture = SGLoadTexture2D(tpath);
132 _textureMap[relativePath] = texture;
133 if (!_textureMap[relativePath].valid())
134 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
135 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath );
144 ////////////////////////////////////////////////////////////////////////
145 // Implementation of FGCropped Texture.
146 ////////////////////////////////////////////////////////////////////////
149 FGCroppedTexture::FGCroppedTexture ()
150 : _path(""), _texture(0),
151 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
156 FGCroppedTexture::FGCroppedTexture (const string &path,
157 float minX, float minY,
158 float maxX, float maxY)
159 : _path(path), _texture(0),
160 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
165 FGCroppedTexture::~FGCroppedTexture ()
171 FGCroppedTexture::getTexture ()
174 _texture = new osg::StateSet;
175 _texture->setTextureAttribute(0, FGTextureManager::createTexture(_path));
176 _texture->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
177 _texture->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
179 return _texture.get();
184 ////////////////////////////////////////////////////////////////////////
185 // Implementation of FGPanel.
186 ////////////////////////////////////////////////////////////////////////
188 static fntRenderer text_renderer;
189 static sgVec4 panel_color;
190 static sgVec4 emissive_panel_color = {1,1,1,1};
198 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
199 _view_height(int(WIN_H * 0.4232)),
200 _visibility(fgGetNode("/sim/panel/visibility", true)),
201 _x_offset(fgGetNode("/sim/panel/x-offset", true)),
202 _y_offset(fgGetNode("/sim/panel/y-offset", true)),
203 _jitter(fgGetNode("/sim/panel/jitter", true)),
204 _flipx(fgGetNode("/sim/panel/flip-x", true)),
205 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
206 _ysize_node(fgGetNode("/sim/startup/ysize", true)),
207 _enable_depth_test(false)
217 for (instrument_list_type::iterator it = _instruments.begin();
218 it != _instruments.end();
227 * Add an instrument to the panel.
230 FGPanel::addInstrument (FGPanelInstrument * instrument)
232 _instruments.push_back(instrument);
237 * Initialize the panel.
246 * Bind panel properties.
251 fgSetArchivable("/sim/panel/visibility");
252 fgSetArchivable("/sim/panel/x-offset");
253 fgSetArchivable("/sim/panel/y-offset");
254 fgSetArchivable("/sim/panel/jitter");
259 * Unbind panel properties.
268 FGPanel::update (double dt)
270 std::cout << "OSGFIXME" << std::endl;
274 FGPanel::update (osg::State& state, GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
276 // Calculate accelerations
277 // and jiggle the panel accordingly
278 // The factors and bounds are just
279 // initial guesses; using sqrt smooths
281 double x_offset = _x_offset->getIntValue();
282 double y_offset = _y_offset->getIntValue();
285 glMatrixMode(GL_PROJECTION);
288 if ( _flipx->getBoolValue() ) {
289 gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
291 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
294 glMatrixMode(GL_MODELVIEW);
298 glTranslated(x_offset, y_offset, 0);
302 glMatrixMode(GL_PROJECTION);
304 glMatrixMode(GL_MODELVIEW);
312 FGPanel::update (osg::State& state)
314 // Do nothing if the panel isn't visible.
315 if ( !fgPanelVisible() ) {
321 // Now, draw the panel
322 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
323 _ysize_node->getIntValue());
324 if (aspect_adjust <1.0)
325 update(state, WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
327 update(state, WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
331 * Handle repeatable mouse events. Called from update() and from
332 * fgUpdate3DPanels(). This functionality needs to move into the
333 * input subsystem. Counting a tick every two frames is clumsy...
335 void FGPanel::updateMouseDelay()
339 if (_mouseDelay < 0) {
340 _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
348 FGPanel::draw(osg::State& state)
350 // In 3D mode, it's possible that we are being drawn exactly on top
351 // of an existing polygon. Use an offset to prevent z-fighting. In
352 // 2D mode, this is a no-op.
353 static osg::ref_ptr<osg::StateSet> panelStateSet;
354 if (!panelStateSet.valid()) {
355 panelStateSet = new osg::StateSet;
356 panelStateSet->setAttributeAndModes(new osg::PolygonOffset(-1, -POFF_UNITS));
357 panelStateSet->setTextureAttribute(0, new osg::TexEnv);
359 // Draw the background
360 panelStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
361 panelStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
362 panelStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
363 panelStateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
364 osg::Material* material = new osg::Material;
365 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
366 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
367 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
368 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
369 material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
370 panelStateSet->setAttribute(material);
371 panelStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
372 panelStateSet->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK));
373 if ( _enable_depth_test )
374 panelStateSet->setAttributeAndModes(new osg::Depth(osg::Depth::ALWAYS));
376 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
378 state.pushStateSet(panelStateSet.get());
381 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
382 sgCopyVec4( panel_color, l->scene_diffuse().data());
383 if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
384 if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
385 if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
386 if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
388 glColor4fv( panel_color );
390 state.pushStateSet(_bg.get());
393 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
394 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
395 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
396 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
401 for (int i = 0; i < 4; i ++) {
402 // top row of textures...(1,3,5,7)
403 state.pushStateSet(_mbg[i*2].get());
406 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
407 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
408 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
409 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
413 // bottom row of textures...(2,4,6,8)
414 state.pushStateSet(_mbg[i*2+1].get());
417 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
418 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
419 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
420 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
427 // Draw the instruments.
428 // Syd Adams: added instrument clipping
429 instrument_list_type::const_iterator current = _instruments.begin();
430 instrument_list_type::const_iterator end = _instruments.end();
432 GLdouble blx[4]={1.0,0.0,0.0,0.0};
433 GLdouble bly[4]={0.0,1.0,0.0,0.0};
434 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
435 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
437 for ( ; current != end; current++) {
438 FGPanelInstrument * instr = *current;
440 glTranslated(instr->getXPos(), instr->getYPos(), 0);
442 int ix= instr->getWidth();
443 int iy= instr->getHeight();
445 glTranslated(-ix/2,-iy/2,0);
446 glClipPlane(GL_CLIP_PLANE0,blx);
447 glClipPlane(GL_CLIP_PLANE1,bly);
448 glEnable(GL_CLIP_PLANE0);
449 glEnable(GL_CLIP_PLANE1);
451 glTranslated(ix,iy,0);
452 glClipPlane(GL_CLIP_PLANE2,urx);
453 glClipPlane(GL_CLIP_PLANE3,ury);
454 glEnable(GL_CLIP_PLANE2);
455 glEnable(GL_CLIP_PLANE3);
462 glDisable(GL_CLIP_PLANE0);
463 glDisable(GL_CLIP_PLANE1);
464 glDisable(GL_CLIP_PLANE2);
465 glDisable(GL_CLIP_PLANE3);
470 // Draw yellow "hotspots" if directed to. This is a panel authoring
471 // feature; not intended to be high performance or to look good.
472 if ( fgGetBool("/sim/panel-hotspots") ) {
473 static osg::ref_ptr<osg::StateSet> hotspotStateSet = new osg::StateSet;
474 hotspotStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
475 state.pushStateSet(hotspotStateSet.get());
480 for ( unsigned int i = 0; i < _instruments.size(); i++ )
481 _instruments[i]->drawHotspots(state);
488 * Set the panel's visibility.
491 FGPanel::setVisibility (bool visibility)
493 _visibility->setBoolValue( visibility );
498 * Return true if the panel is visible.
501 FGPanel::getVisibility () const
503 return _visibility->getBoolValue();
508 * Set the panel's background texture.
511 FGPanel::setBackground (osg::Texture2D* texture)
513 osg::StateSet* stateSet = new osg::StateSet;
514 stateSet->setTextureAttribute(0, texture);
515 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
516 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
521 * Set the panel's multiple background textures.
524 FGPanel::setMultiBackground (osg::Texture2D* texture, int idx)
528 osg::StateSet* stateSet = new osg::StateSet;
529 stateSet->setTextureAttribute(0, texture);
530 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
531 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
532 _mbg[idx] = stateSet;
536 * Set the panel's x-offset.
539 FGPanel::setXOffset (int offset)
541 if (offset <= 0 && offset >= -_width + WIN_W)
542 _x_offset->setIntValue( offset );
547 * Set the panel's y-offset.
550 FGPanel::setYOffset (int offset)
552 if (offset <= 0 && offset >= -_height)
553 _y_offset->setIntValue( offset );
557 * Handle a mouse action in panel-local (not screen) coordinates.
558 * Used by the 3D panel code in Model/panelnode.cxx, in situations
559 * where the panel doesn't control its own screen location.
562 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
564 // Note a released button and return
566 if (_mouseInstrument != 0)
567 _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
569 _mouseInstrument = 0;
573 // Search for a matching instrument.
574 for (int i = 0; i < (int)_instruments.size(); i++) {
575 FGPanelInstrument *inst = _instruments[i];
576 int ix = inst->getXPos();
577 int iy = inst->getYPos();
578 int iw = inst->getWidth() / 2;
579 int ih = inst->getHeight() / 2;
580 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
583 _mouseInstrument = inst;
584 _mouseButton = button;
587 // Always do the action once.
588 return _mouseInstrument->doMouseAction(_mouseButton, 0,
596 * Perform a mouse action.
599 FGPanel::doMouseAction (int button, int updown, int x, int y)
601 // FIXME: this same code appears in update()
602 int xsize = _xsize_node->getIntValue();
603 int ysize = _ysize_node->getIntValue();
604 float aspect_adjust = get_aspect_adjust(xsize, ysize);
606 // Scale for the real window size.
607 if (aspect_adjust < 1.0) {
608 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
609 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
611 x = int(((float)x / xsize) * WIN_W);
612 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
615 // Adjust for offsets.
616 x -= _x_offset->getIntValue();
617 y -= _y_offset->getIntValue();
619 // Having fixed up the coordinates, fall through to the local
620 // coordinate handler.
621 return doLocalMouseAction(button, updown, x, y);
624 void FGPanel::setDepthTest (bool enable) {
625 _enable_depth_test = enable;
630 ////////////////////////////////////////////////////////////////////////.
631 // Implementation of FGPanelAction.
632 ////////////////////////////////////////////////////////////////////////
634 FGPanelAction::FGPanelAction ()
638 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
640 : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
644 FGPanelAction::~FGPanelAction ()
646 for (unsigned int i = 0; i < 2; i++) {
647 for (unsigned int j = 0; j < _bindings[i].size(); j++)
648 delete _bindings[i][j];
653 FGPanelAction::addBinding (FGBinding * binding, int updown)
655 _bindings[updown].push_back(binding);
659 FGPanelAction::doAction (int updown)
662 if ((updown != _last_state) || (updown == 0 && _repeatable)) {
663 int nBindings = _bindings[updown].size();
664 for (int i = 0; i < nBindings; i++)
665 _bindings[updown][i]->fire();
667 _last_state = updown;
676 ////////////////////////////////////////////////////////////////////////
677 // Implementation of FGPanelTransformation.
678 ////////////////////////////////////////////////////////////////////////
680 FGPanelTransformation::FGPanelTransformation ()
685 FGPanelTransformation::~FGPanelTransformation ()
692 ////////////////////////////////////////////////////////////////////////
693 // Implementation of FGPanelInstrument.
694 ////////////////////////////////////////////////////////////////////////
697 FGPanelInstrument::FGPanelInstrument ()
703 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
709 FGPanelInstrument::~FGPanelInstrument ()
711 for (action_list_type::iterator it = _actions.begin();
712 it != _actions.end();
720 FGPanelInstrument::drawHotspots(osg::State& state)
722 for ( unsigned int i = 0; i < _actions.size(); i++ ) {
723 FGPanelAction* a = _actions[i];
724 float x1 = getXPos() + a->getX();
725 float x2 = x1 + a->getWidth();
726 float y1 = getYPos() + a->getY();
727 float y2 = y1 + a->getHeight();
729 glBegin(GL_LINE_LOOP);
739 FGPanelInstrument::setPosition (int x, int y)
746 FGPanelInstrument::setSize (int w, int h)
753 FGPanelInstrument::getXPos () const
759 FGPanelInstrument::getYPos () const
765 FGPanelInstrument::getWidth () const
771 FGPanelInstrument::getHeight () const
777 FGPanelInstrument::addAction (FGPanelAction * action)
779 _actions.push_back(action);
782 // Coordinates relative to centre.
784 FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
787 action_list_type::iterator it = _actions.begin();
788 action_list_type::iterator last = _actions.end();
789 for ( ; it != last; it++) {
790 if ((*it)->inArea(button, x, y) &&
791 (*it)->doAction(updown))
800 ////////////////////////////////////////////////////////////////////////
801 // Implementation of FGLayeredInstrument.
802 ////////////////////////////////////////////////////////////////////////
804 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
805 : FGPanelInstrument(x, y, w, h)
809 FGLayeredInstrument::~FGLayeredInstrument ()
811 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
818 FGLayeredInstrument::draw (osg::State& state)
823 for (int i = 0; i < (int)_layers.size(); i++) {
825 _layers[i]->draw(state);
831 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
833 int n = _layers.size();
834 if (layer->getWidth() == -1) {
835 layer->setWidth(getWidth());
837 if (layer->getHeight() == -1) {
838 layer->setHeight(getHeight());
840 _layers.push_back(layer);
845 FGLayeredInstrument::addLayer (const FGCroppedTexture &texture,
848 return addLayer(new FGTexturedLayer(texture, w, h));
852 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
854 int layer = _layers.size() - 1;
855 _layers[layer]->addTransformation(transformation);
860 ////////////////////////////////////////////////////////////////////////
861 // Implementation of FGSpecialInstrument.
862 ////////////////////////////////////////////////////////////////////////
864 FGSpecialInstrument::FGSpecialInstrument (DCLGPS* sb)
865 : FGPanelInstrument()
870 FGSpecialInstrument::~FGSpecialInstrument ()
875 FGSpecialInstrument::draw (osg::State& state)
882 ////////////////////////////////////////////////////////////////////////
883 // Implementation of FGInstrumentLayer.
884 ////////////////////////////////////////////////////////////////////////
886 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
892 FGInstrumentLayer::~FGInstrumentLayer ()
894 for (transformation_list::iterator it = _transformations.begin();
895 it != _transformations.end();
903 FGInstrumentLayer::transform () const
905 transformation_list::const_iterator it = _transformations.begin();
906 transformation_list::const_iterator last = _transformations.end();
908 FGPanelTransformation *t = *it;
910 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
913 val = fmod(val, t->mod);
916 } else if (val > t->max) {
921 val = val * t->factor + t->offset;
923 val = t->table->interpolate(val) * t->factor + t->offset;
927 case FGPanelTransformation::XSHIFT:
928 glTranslatef(val, 0.0, 0.0);
930 case FGPanelTransformation::YSHIFT:
931 glTranslatef(0.0, val, 0.0);
933 case FGPanelTransformation::ROTATION:
934 glRotatef(-val, 0.0, 0.0, 1.0);
943 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
945 _transformations.push_back(transformation);
950 ////////////////////////////////////////////////////////////////////////
951 // Implementation of FGGroupLayer.
952 ////////////////////////////////////////////////////////////////////////
954 FGGroupLayer::FGGroupLayer ()
958 FGGroupLayer::~FGGroupLayer ()
960 for (unsigned int i = 0; i < _layers.size(); i++)
965 FGGroupLayer::draw (osg::State& state)
969 int nLayers = _layers.size();
970 for (int i = 0; i < nLayers; i++)
971 _layers[i]->draw(state);
976 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
978 _layers.push_back(layer);
983 ////////////////////////////////////////////////////////////////////////
984 // Implementation of FGTexturedLayer.
985 ////////////////////////////////////////////////////////////////////////
988 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
989 : FGInstrumentLayer(w, h),
996 FGTexturedLayer::~FGTexturedLayer ()
1002 FGTexturedLayer::draw (osg::State& state)
1009 state.pushStateSet(_texture.getTexture());
1011 glBegin(GL_POLYGON);
1014 glColor4fv( emissive_panel_color );
1016 // From Curt: turn on the panel
1017 // lights after sundown.
1018 glColor4fv( panel_color );
1021 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
1022 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
1023 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
1024 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
1026 state.popStateSet();
1033 ////////////////////////////////////////////////////////////////////////
1034 // Implementation of FGTextLayer.
1035 ////////////////////////////////////////////////////////////////////////
1037 FGTextLayer::FGTextLayer (int w, int h)
1038 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
1041 _color[0] = _color[1] = _color[2] = 0.0;
1045 FGTextLayer::~FGTextLayer ()
1047 chunk_list::iterator it = _chunks.begin();
1048 chunk_list::iterator last = _chunks.end();
1049 for ( ; it != last; it++) {
1055 FGTextLayer::draw (osg::State& state)
1061 FGFontCache *fc = globals->get_fontcache();
1062 text_renderer.setFont(fc->getTexFont(_font_name.c_str()));
1064 text_renderer.setPointSize(_pointSize);
1065 text_renderer.begin();
1066 text_renderer.start3f(0, 0, 0);
1069 long diff = _now - _then;
1071 if (diff > 100000 || diff < 0 ) {
1072 // ( diff < 0 ) is a sanity check and indicates our time stamp
1073 // difference math probably overflowed. We can handle a max
1074 // difference of 35.8 minutes since the returned value is in
1075 // usec. So if the panel is left off longer than that we can
1076 // over flow the math with it is turned back on. This (diff <
1077 // 0) catches that situation, get's us out of trouble, and
1083 // Something is goofy. The code in this file renders only CCW
1084 // polygons, and I have verified that the font code in plib
1085 // renders only CCW trianbles. Yet they come out backwards.
1086 // Something around here or in plib is either changing the winding
1087 // order or (more likely) pushing a left-handed matrix onto the
1088 // stack. But I can't find it; get out the chainsaw...
1090 text_renderer.puts((char *)(_value.c_str()));
1091 glFrontFace(GL_CCW);
1093 text_renderer.end();
1094 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
1099 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1101 _chunks.push_back(chunk);
1105 FGTextLayer::setColor (float r, float g, float b)
1114 FGTextLayer::setPointSize (float size)
1120 FGTextLayer::setFontName(const string &name)
1122 _font_name = name + ".txf";
1127 FGTextLayer::setFont(fntFont * font)
1129 text_renderer.setFont(font);
1134 FGTextLayer::recalc_value () const
1137 chunk_list::const_iterator it = _chunks.begin();
1138 chunk_list::const_iterator last = _chunks.end();
1139 for ( ; it != last; it++) {
1140 _value += (*it)->getValue();
1146 ////////////////////////////////////////////////////////////////////////
1147 // Implementation of FGTextLayer::Chunk.
1148 ////////////////////////////////////////////////////////////////////////
1150 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1151 : _type(FGTextLayer::TEXT), _fmt(fmt)
1158 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1159 const string &fmt, float mult, float offs,
1161 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
1164 if (type == TEXT_VALUE)
1173 FGTextLayer::Chunk::getValue () const
1179 sprintf(_buf, _fmt.c_str(), _text.c_str());
1182 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1185 double d = _offs + _node->getFloatValue() * _mult;
1186 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
1187 sprintf(_buf, _fmt.c_str(), d);
1198 ////////////////////////////////////////////////////////////////////////
1199 // Implementation of FGSwitchLayer.
1200 ////////////////////////////////////////////////////////////////////////
1202 FGSwitchLayer::FGSwitchLayer ()
1208 FGSwitchLayer::draw (osg::State& state)
1212 int nLayers = _layers.size();
1213 for (int i = 0; i < nLayers; i++) {
1214 if (_layers[i]->test()) {
1215 _layers[i]->draw(state);