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
41 #include <osg/CullFace>
43 #include <osg/Material>
44 #include <osg/Matrixf>
46 #include <osg/PolygonOffset>
48 #include <simgear/compiler.h>
52 #include <boost/foreach.hpp>
53 #include <simgear/debug/logstream.hxx>
54 #include <simgear/misc/sg_path.hxx>
55 #include <simgear/scene/model/model.hxx>
58 #include <Main/globals.hxx>
59 #include <Main/fg_props.hxx>
60 #include <Viewer/viewmgr.hxx>
61 #include <Viewer/viewer.hxx>
62 #include <Time/light.hxx>
63 #include <GUI/FGFontCache.hxx>
64 #include <Instrumentation/dclgps.hxx>
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 const double MOUSE_ACTION_REPEAT_DELAY = 0.5; // 500msec initial delay
77 const double MOUSE_ACTION_REPEAT_INTERVAL = 0.1; // 10Hz repeat rate
81 ////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////
87 * Calculate the aspect adjustment for the panel.
90 get_aspect_adjust (int xsize, int ysize)
92 float ideal_aspect = float(WIN_W) / float(WIN_H);
93 float real_aspect = float(xsize) / float(ysize);
94 return (real_aspect / ideal_aspect);
97 ////////////////////////////////////////////////////////////////////////
98 // Implementation of FGTextureManager.
99 ////////////////////////////////////////////////////////////////////////
101 map<string,osg::ref_ptr<osg::Texture2D> > FGTextureManager::_textureMap;
104 FGTextureManager::createTexture (const string &relativePath, bool staticTexture)
106 osg::Texture2D* texture = _textureMap[relativePath].get();
108 SGPath tpath = globals->resolve_aircraft_path(relativePath);
109 texture = SGLoadTexture2D(staticTexture, tpath);
111 _textureMap[relativePath] = texture;
112 if (!_textureMap[relativePath].valid())
113 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
114 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath );
121 void FGTextureManager::addTexture(const string &relativePath,
122 osg::Texture2D* texture)
124 _textureMap[relativePath] = texture;
127 ////////////////////////////////////////////////////////////////////////
128 // Implementation of FGCropped Texture.
129 ////////////////////////////////////////////////////////////////////////
132 FGCroppedTexture::FGCroppedTexture ()
133 : _path(""), _texture(0),
134 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
139 FGCroppedTexture::FGCroppedTexture (const string &path,
140 float minX, float minY,
141 float maxX, float maxY)
142 : _path(path), _texture(0),
143 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
148 FGCroppedTexture::~FGCroppedTexture ()
154 FGCroppedTexture::getTexture ()
157 _texture = new osg::StateSet;
158 _texture->setTextureAttribute(0, FGTextureManager::createTexture(_path));
159 _texture->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
160 _texture->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
162 return _texture.get();
167 ////////////////////////////////////////////////////////////////////////
168 // Implementation of FGPanel.
169 ////////////////////////////////////////////////////////////////////////
171 static fntRenderer text_renderer;
172 static sgVec4 panel_color;
173 static sgVec4 emissive_panel_color = {1,1,1,1};
181 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
182 _x_offset(fgGetNode("/sim/panel/x-offset", true)),
183 _y_offset(fgGetNode("/sim/panel/y-offset", true)),
184 _jitter(fgGetNode("/sim/panel/jitter", true)),
185 _flipx(fgGetNode("/sim/panel/flip-x", true)),
186 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
187 _ysize_node(fgGetNode("/sim/startup/ysize", true)),
188 _enable_depth_test(false),
190 _drawPanelHotspots("/sim/panel-hotspots")
200 for (instrument_list_type::iterator it = _instruments.begin();
201 it != _instruments.end();
210 * Add an instrument to the panel.
213 FGPanel::addInstrument (FGPanelInstrument * instrument)
215 _instruments.push_back(instrument);
219 FGPanel::getAspectScale() const
221 // set corner-coordinates correctly
223 int xsize = _xsize_node->getIntValue();
224 int ysize = _ysize_node->getIntValue();
225 float aspect_adjust = get_aspect_adjust(xsize, ysize);
227 if (aspect_adjust < 1.0)
228 return ysize / (double) WIN_H;
230 return xsize /(double) WIN_W;
234 * Handle repeatable mouse events. Called from update() and from
235 * fgUpdate3DPanels(). This functionality needs to move into the
236 * input subsystem. Counting a tick every two frames is clumsy...
238 void FGPanel::updateMouseDelay(double dt)
244 _mouseActionRepeat -= dt;
245 while (_mouseActionRepeat < 0.0) {
246 _mouseActionRepeat += MOUSE_ACTION_REPEAT_INTERVAL;
247 _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
253 FGPanel::draw(osg::State& state)
256 // In 3D mode, it's possible that we are being drawn exactly on top
257 // of an existing polygon. Use an offset to prevent z-fighting. In
258 // 2D mode, this is a no-op.
259 static osg::ref_ptr<osg::StateSet> panelStateSet;
260 if (!panelStateSet.valid()) {
261 panelStateSet = new osg::StateSet;
262 panelStateSet->setAttributeAndModes(new osg::PolygonOffset(-1, -POFF_UNITS));
263 panelStateSet->setTextureAttribute(0, new osg::TexEnv);
265 // Draw the background
266 panelStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
267 panelStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
268 panelStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
269 panelStateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
271 osg::Material* material = new osg::Material;
272 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
273 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
274 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
275 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
276 material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
277 panelStateSet->setAttribute(material);
279 panelStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
280 panelStateSet->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK));
281 panelStateSet->setAttributeAndModes(new osg::Depth(osg::Depth::LEQUAL));
283 if ( _enable_depth_test )
284 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
286 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
287 state.pushStateSet(panelStateSet.get());
289 state.setActiveTextureUnit(0);
290 state.setClientActiveTextureUnit(0);
292 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
293 sgCopyVec4( panel_color, l->scene_diffuse().data());
294 if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
295 if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
296 if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
297 if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
299 glColor4fv( panel_color );
301 state.pushStateSet(_bg.get());
303 state.setActiveTextureUnit(0);
304 state.setClientActiveTextureUnit(0);
307 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
308 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
309 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
310 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
314 state.setActiveTextureUnit(0);
315 state.setClientActiveTextureUnit(0);
318 for (int i = 0; i < 4; i ++) {
319 // top row of textures...(1,3,5,7)
320 state.pushStateSet(_mbg[i*2].get());
322 state.setActiveTextureUnit(0);
323 state.setClientActiveTextureUnit(0);
326 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
327 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
328 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
329 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
333 state.setActiveTextureUnit(0);
334 state.setClientActiveTextureUnit(0);
336 // bottom row of textures...(2,4,6,8)
337 state.pushStateSet(_mbg[i*2+1].get());
339 state.setActiveTextureUnit(0);
340 state.setClientActiveTextureUnit(0);
343 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
344 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
345 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
346 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
350 state.setActiveTextureUnit(0);
351 state.setClientActiveTextureUnit(0);
356 // Draw the instruments.
357 // Syd Adams: added instrument clipping
358 instrument_list_type::const_iterator current = _instruments.begin();
359 instrument_list_type::const_iterator end = _instruments.end();
361 GLdouble blx[4]={1.0,0.0,0.0,0.0};
362 GLdouble bly[4]={0.0,1.0,0.0,0.0};
363 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
364 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
366 for ( ; current != end; current++) {
367 FGPanelInstrument * instr = *current;
369 glTranslated(instr->getXPos(), instr->getYPos(), 0);
371 int ix= instr->getWidth();
372 int iy= instr->getHeight();
374 glTranslated(-ix/2,-iy/2,0);
375 glClipPlane(GL_CLIP_PLANE0,blx);
376 glClipPlane(GL_CLIP_PLANE1,bly);
377 glEnable(GL_CLIP_PLANE0);
378 glEnable(GL_CLIP_PLANE1);
380 glTranslated(ix,iy,0);
381 glClipPlane(GL_CLIP_PLANE2,urx);
382 glClipPlane(GL_CLIP_PLANE3,ury);
383 glEnable(GL_CLIP_PLANE2);
384 glEnable(GL_CLIP_PLANE3);
391 glDisable(GL_CLIP_PLANE0);
392 glDisable(GL_CLIP_PLANE1);
393 glDisable(GL_CLIP_PLANE2);
394 glDisable(GL_CLIP_PLANE3);
398 state.setActiveTextureUnit(0);
399 state.setClientActiveTextureUnit(0);
402 // Draw yellow "hotspots" if directed to. This is a panel authoring
403 // feature; not intended to be high performance or to look good.
404 if ( _drawPanelHotspots ) {
405 static osg::ref_ptr<osg::StateSet> hotspotStateSet;
406 if (!hotspotStateSet.valid()) {
407 hotspotStateSet = new osg::StateSet;
408 hotspotStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
409 hotspotStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
412 state.pushStateSet(hotspotStateSet.get());
414 state.setActiveTextureUnit(0);
415 state.setClientActiveTextureUnit(0);
418 glPushAttrib(GL_ENABLE_BIT);
419 glDisable(GL_COLOR_MATERIAL);
422 for ( unsigned int i = 0; i < _instruments.size(); i++ )
423 _instruments[i]->drawHotspots(state);
425 // disable drawing of panel extents for the 2.8 release, since it
426 // confused use of hot-spot drawing in tutorials.
427 #ifdef DRAW_PANEL_EXTENTS
431 getLogicalExtent(x0, y0, x1, y1);
433 glBegin(GL_LINE_LOOP);
445 state.setActiveTextureUnit(0);
446 state.setClientActiveTextureUnit(0);
452 * Set the panel's background texture.
455 FGPanel::setBackground (osg::Texture2D* texture)
457 osg::StateSet* stateSet = new osg::StateSet;
458 stateSet->setTextureAttribute(0, texture);
459 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
460 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
465 * Set the panel's multiple background textures.
468 FGPanel::setMultiBackground (osg::Texture2D* texture, int idx)
472 osg::StateSet* stateSet = new osg::StateSet;
473 stateSet->setTextureAttribute(0, texture);
474 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
475 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
476 _mbg[idx] = stateSet;
480 * Set the panel's x-offset.
483 FGPanel::setXOffset (int offset)
485 if (offset <= 0 && offset >= -_width + WIN_W)
486 _x_offset->setIntValue( offset );
491 * Set the panel's y-offset.
494 FGPanel::setYOffset (int offset)
496 if (offset <= 0 && offset >= -_height)
497 _y_offset->setIntValue( offset );
501 * Handle a mouse action in panel-local (not screen) coordinates.
502 * Used by the 3D panel code in Model/panelnode.cxx, in situations
503 * where the panel doesn't control its own screen location.
506 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
508 // Note a released button and return
510 if (_mouseInstrument != 0)
511 _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
513 _mouseInstrument = 0;
517 // Search for a matching instrument.
518 for (int i = 0; i < (int)_instruments.size(); i++) {
519 FGPanelInstrument *inst = _instruments[i];
520 int ix = inst->getXPos();
521 int iy = inst->getYPos();
522 int iw = inst->getWidth() / 2;
523 int ih = inst->getHeight() / 2;
524 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
526 _mouseActionRepeat = MOUSE_ACTION_REPEAT_DELAY;
527 _mouseInstrument = inst;
528 _mouseButton = button;
531 // Always do the action once.
532 return _mouseInstrument->doMouseAction(_mouseButton, 0,
540 * Perform a mouse action.
543 FGPanel::doMouseAction (int button, int updown, int x, int y)
545 // FIXME: this same code appears in update()
546 int xsize = _xsize_node->getIntValue();
547 int ysize = _ysize_node->getIntValue();
548 float aspect_adjust = get_aspect_adjust(xsize, ysize);
550 // Scale for the real window size.
551 if (aspect_adjust < 1.0) {
552 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
553 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
555 x = int(((float)x / xsize) * WIN_W);
556 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
559 // Adjust for offsets.
560 x -= _x_offset->getIntValue();
561 y -= _y_offset->getIntValue();
563 // Having fixed up the coordinates, fall through to the local
564 // coordinate handler.
565 return doLocalMouseAction(button, updown, x, y);
568 void FGPanel::setDepthTest (bool enable) {
569 _enable_depth_test = enable;
577 x0(std::numeric_limits<int>::max()),
578 y0(std::numeric_limits<int>::max()),
579 x1(std::numeric_limits<int>::min()),
580 y1(std::numeric_limits<int>::min())
583 IntRect(int x, int y, int w, int h) :
584 x0(x), y0(y), x1(x + w), y1( y + h)
598 void extend(const IntRect& r)
600 x0 = std::min(x0, r.x0);
601 y0 = std::min(y0, r.y0);
602 x1 = std::max(x1, r.x1);
603 y1 = std::max(y1, r.y1);
609 void FGPanel::getLogicalExtent(int &x0, int& y0, int& x1, int &y1)
612 BOOST_FOREACH(FGPanelInstrument *inst, _instruments) {
613 inst->extendRect(result);
622 ////////////////////////////////////////////////////////////////////////.
623 // Implementation of FGPanelAction.
624 ////////////////////////////////////////////////////////////////////////
626 FGPanelAction::FGPanelAction ()
630 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
632 : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
636 FGPanelAction::~FGPanelAction ()
638 for (unsigned int i = 0; i < 2; i++) {
639 for (unsigned int j = 0; j < _bindings[i].size(); j++)
640 delete _bindings[i][j];
645 FGPanelAction::addBinding (SGBinding * binding, int updown)
647 _bindings[updown].push_back(binding);
651 FGPanelAction::doAction (int updown)
654 if ((updown != _last_state) || (updown == 0 && _repeatable)) {
655 int nBindings = _bindings[updown].size();
656 for (int i = 0; i < nBindings; i++)
657 _bindings[updown][i]->fire();
659 _last_state = updown;
668 ////////////////////////////////////////////////////////////////////////
669 // Implementation of FGPanelTransformation.
670 ////////////////////////////////////////////////////////////////////////
672 FGPanelTransformation::FGPanelTransformation ()
677 FGPanelTransformation::~FGPanelTransformation ()
684 ////////////////////////////////////////////////////////////////////////
685 // Implementation of FGPanelInstrument.
686 ////////////////////////////////////////////////////////////////////////
689 FGPanelInstrument::FGPanelInstrument ()
695 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
701 FGPanelInstrument::~FGPanelInstrument ()
703 for (action_list_type::iterator it = _actions.begin();
704 it != _actions.end();
712 FGPanelInstrument::drawHotspots(osg::State& state)
714 for ( unsigned int i = 0; i < _actions.size(); i++ ) {
715 FGPanelAction* a = _actions[i];
716 float x1 = getXPos() + a->getX();
717 float x2 = x1 + a->getWidth();
718 float y1 = getYPos() + a->getY();
719 float y2 = y1 + a->getHeight();
721 glBegin(GL_LINE_LOOP);
731 FGPanelInstrument::setPosition (int x, int y)
738 FGPanelInstrument::setSize (int w, int h)
745 FGPanelInstrument::getXPos () const
751 FGPanelInstrument::getYPos () const
757 FGPanelInstrument::getWidth () const
763 FGPanelInstrument::getHeight () const
769 FGPanelInstrument::extendRect(IntRect& r) const
771 IntRect instRect(_x, _y, _w, _h);
774 BOOST_FOREACH(FGPanelAction* act, _actions) {
775 r.extend(IntRect(getXPos() + act->getX(),
776 getYPos() + act->getY(),
784 FGPanelInstrument::addAction (FGPanelAction * action)
786 _actions.push_back(action);
789 // Coordinates relative to centre.
791 FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
794 action_list_type::iterator it = _actions.begin();
795 action_list_type::iterator last = _actions.end();
796 for ( ; it != last; it++) {
797 if ((*it)->inArea(button, x, y) &&
798 (*it)->doAction(updown))
807 ////////////////////////////////////////////////////////////////////////
808 // Implementation of FGLayeredInstrument.
809 ////////////////////////////////////////////////////////////////////////
811 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
812 : FGPanelInstrument(x, y, w, h)
816 FGLayeredInstrument::~FGLayeredInstrument ()
818 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
825 FGLayeredInstrument::draw (osg::State& state)
830 for (int i = 0; i < (int)_layers.size(); i++) {
832 _layers[i]->draw(state);
838 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
840 int n = _layers.size();
841 if (layer->getWidth() == -1) {
842 layer->setWidth(getWidth());
844 if (layer->getHeight() == -1) {
845 layer->setHeight(getHeight());
847 _layers.push_back(layer);
852 FGLayeredInstrument::addLayer (const FGCroppedTexture &texture,
855 return addLayer(new FGTexturedLayer(texture, w, h));
859 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
861 int layer = _layers.size() - 1;
862 _layers[layer]->addTransformation(transformation);
867 ////////////////////////////////////////////////////////////////////////
868 // Implementation of FGSpecialInstrument.
869 ////////////////////////////////////////////////////////////////////////
871 FGSpecialInstrument::FGSpecialInstrument (DCLGPS* sb)
872 : FGPanelInstrument()
877 FGSpecialInstrument::~FGSpecialInstrument ()
882 FGSpecialInstrument::draw (osg::State& state)
884 complex->draw(state);
889 ////////////////////////////////////////////////////////////////////////
890 // Implementation of FGInstrumentLayer.
891 ////////////////////////////////////////////////////////////////////////
893 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
899 FGInstrumentLayer::~FGInstrumentLayer ()
901 for (transformation_list::iterator it = _transformations.begin();
902 it != _transformations.end();
910 FGInstrumentLayer::transform () const
912 transformation_list::const_iterator it = _transformations.begin();
913 transformation_list::const_iterator last = _transformations.end();
915 FGPanelTransformation *t = *it;
917 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
920 val = fmod(val, t->mod);
923 } else if (val > t->max) {
928 val = val * t->factor + t->offset;
930 val = t->table->interpolate(val) * t->factor + t->offset;
934 case FGPanelTransformation::XSHIFT:
935 glTranslatef(val, 0.0, 0.0);
937 case FGPanelTransformation::YSHIFT:
938 glTranslatef(0.0, val, 0.0);
940 case FGPanelTransformation::ROTATION:
941 glRotatef(-val, 0.0, 0.0, 1.0);
950 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
952 _transformations.push_back(transformation);
957 ////////////////////////////////////////////////////////////////////////
958 // Implementation of FGGroupLayer.
959 ////////////////////////////////////////////////////////////////////////
961 FGGroupLayer::FGGroupLayer ()
965 FGGroupLayer::~FGGroupLayer ()
967 for (unsigned int i = 0; i < _layers.size(); i++)
972 FGGroupLayer::draw (osg::State& state)
976 int nLayers = _layers.size();
977 for (int i = 0; i < nLayers; i++)
978 _layers[i]->draw(state);
983 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
985 _layers.push_back(layer);
990 ////////////////////////////////////////////////////////////////////////
991 // Implementation of FGTexturedLayer.
992 ////////////////////////////////////////////////////////////////////////
995 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
996 : FGInstrumentLayer(w, h),
1003 FGTexturedLayer::~FGTexturedLayer ()
1009 FGTexturedLayer::draw (osg::State& state)
1016 state.pushStateSet(_texture.getTexture());
1018 state.setActiveTextureUnit(0);
1019 state.setClientActiveTextureUnit(0);
1021 glBegin(GL_POLYGON);
1024 glColor4fv( emissive_panel_color );
1026 // From Curt: turn on the panel
1027 // lights after sundown.
1028 glColor4fv( panel_color );
1031 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
1032 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
1033 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
1034 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
1036 state.popStateSet();
1038 state.setActiveTextureUnit(0);
1039 state.setClientActiveTextureUnit(0);
1046 ////////////////////////////////////////////////////////////////////////
1047 // Implementation of FGTextLayer.
1048 ////////////////////////////////////////////////////////////////////////
1050 FGTextLayer::FGTextLayer (int w, int h)
1051 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
1054 _color[0] = _color[1] = _color[2] = 0.0;
1058 FGTextLayer::~FGTextLayer ()
1060 chunk_list::iterator it = _chunks.begin();
1061 chunk_list::iterator last = _chunks.end();
1062 for ( ; it != last; it++) {
1068 FGTextLayer::draw (osg::State& state)
1074 FGFontCache *fc = globals->get_fontcache();
1075 fntFont* font = fc->getTexFont(_font_name.c_str());
1077 return; // don't crash on missing fonts
1080 text_renderer.setFont(font);
1082 text_renderer.setPointSize(_pointSize);
1083 text_renderer.begin();
1084 text_renderer.start3f(0, 0, 0);
1087 double diff = (_now - _then).toUSecs();
1089 if (diff > 100000 || diff < 0 ) {
1090 // ( diff < 0 ) is a sanity check and indicates our time stamp
1091 // difference math probably overflowed. We can handle a max
1092 // difference of 35.8 minutes since the returned value is in
1093 // usec. So if the panel is left off longer than that we can
1094 // over flow the math with it is turned back on. This (diff <
1095 // 0) catches that situation, get's us out of trouble, and
1101 // Something is goofy. The code in this file renders only CCW
1102 // polygons, and I have verified that the font code in plib
1103 // renders only CCW trianbles. Yet they come out backwards.
1104 // Something around here or in plib is either changing the winding
1105 // order or (more likely) pushing a left-handed matrix onto the
1106 // stack. But I can't find it; get out the chainsaw...
1108 text_renderer.puts((char *)(_value.c_str()));
1109 glFrontFace(GL_CCW);
1111 text_renderer.end();
1112 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
1117 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1119 _chunks.push_back(chunk);
1123 FGTextLayer::setColor (float r, float g, float b)
1132 FGTextLayer::setPointSize (float size)
1138 FGTextLayer::setFontName(const string &name)
1140 _font_name = name + ".txf";
1141 FGFontCache *fc = globals->get_fontcache();
1142 fntFont* font = fc->getTexFont(_font_name.c_str());
1144 SG_LOG(SG_COCKPIT, SG_WARN, "unable to find font:" << name);
1150 FGTextLayer::setFont(fntFont * font)
1152 text_renderer.setFont(font);
1157 FGTextLayer::recalc_value () const
1160 chunk_list::const_iterator it = _chunks.begin();
1161 chunk_list::const_iterator last = _chunks.end();
1162 for ( ; it != last; it++) {
1163 _value += (*it)->getValue();
1169 ////////////////////////////////////////////////////////////////////////
1170 // Implementation of FGTextLayer::Chunk.
1171 ////////////////////////////////////////////////////////////////////////
1173 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1174 : _type(FGTextLayer::TEXT), _fmt(fmt)
1181 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1182 const string &fmt, float mult, float offs,
1184 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
1187 if (type == TEXT_VALUE)
1196 FGTextLayer::Chunk::getValue () const
1202 sprintf(_buf, _fmt.c_str(), _text.c_str());
1205 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1208 double d = _offs + _node->getFloatValue() * _mult;
1209 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
1210 sprintf(_buf, _fmt.c_str(), d);
1221 ////////////////////////////////////////////////////////////////////////
1222 // Implementation of FGSwitchLayer.
1223 ////////////////////////////////////////////////////////////////////////
1225 FGSwitchLayer::FGSwitchLayer ()
1231 FGSwitchLayer::draw (osg::State& state)
1235 int nLayers = _layers.size();
1236 for (int i = 0; i < nLayers; i++) {
1237 if (_layers[i]->test()) {
1238 _layers[i]->draw(state);