]> git.mxchange.org Git - flightgear.git/blob - src/Model/panelnode.cxx
Olaf Flebbe: remove unused variable.
[flightgear.git] / src / Model / panelnode.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include <simgear/compiler.h>
6 #include <vector>
7
8 #include <plib/sg.h>
9
10 #include <Cockpit/panel.hxx>
11 #include <Cockpit/panel_io.hxx>
12 #include "panelnode.hxx"
13
14 SG_USING_STD(vector);
15
16
17 // Static (!) handling for all 3D panels in the program.
18 // OSGFIXME: Put the panel as different elements in the scenegraph.
19 // Then just pick in that scenegraph.
20 vector<FGPanelNode*> all_3d_panels;
21 bool fgHandle3DPanelMouseEvent( int button, int updown, int x, int y )
22 {
23     for ( unsigned int i = 0; i < all_3d_panels.size(); i++ ) {
24         if ( all_3d_panels[i]->doMouseAction(button, updown, x, y) ) {
25             return true;
26         }
27     }
28     return false;
29 }
30
31 void fgUpdate3DPanels()
32 {
33     for ( unsigned int i = 0; i < all_3d_panels.size(); i++ ) {
34         all_3d_panels[i]->getPanel()->updateMouseDelay();
35     }
36 }
37
38 FGPanelNode::FGPanelNode(SGPropertyNode* props)
39 {
40     int i;
41
42     // Make an FGPanel object.  But *don't* call init() or bind() on
43     // it -- those methods touch static state.
44     _panel = fgReadPanel(props->getStringValue("path"));
45
46     // Never mind.  We *have* to call init to make sure the static
47     // state is initialized (it's not, if there aren't any 2D
48     // panels).  This is a memory leak and should be fixed!`
49     // FIXME
50     _panel->init();
51
52     _panel->setDepthTest( props->getBoolValue("depth-test") );
53
54     // Read out the pixel-space info
55     _xmax = _panel->getWidth();
56     _ymax = _panel->getHeight();
57
58     // And the corner points
59     SGPropertyNode* pt = props->getChild("bottom-left");
60     _bottomLeft[0] = pt->getFloatValue("x-m");
61     _bottomLeft[1] = pt->getFloatValue("y-m");
62     _bottomLeft[2] = pt->getFloatValue("z-m");
63
64     pt = props->getChild("top-left");
65     _topLeft[0] = pt->getFloatValue("x-m");
66     _topLeft[1] = pt->getFloatValue("y-m");
67     _topLeft[2] = pt->getFloatValue("z-m");
68
69     pt = props->getChild("bottom-right");
70     _bottomRight[0] = pt->getFloatValue("x-m");
71     _bottomRight[1] = pt->getFloatValue("y-m");
72     _bottomRight[2] = pt->getFloatValue("z-m");
73
74     // Now generate our transformation matrix.  For shorthand, use
75     // "a", "b", and "c" as our corners and "m" as the matrix. The
76     // vector u goes from a to b, v from a to c, and w is a
77     // perpendicular cross product.
78     osg::Vec3 a = _bottomLeft;
79     osg::Vec3 b = _bottomRight;
80     osg::Vec3 c = _topLeft;
81     osg::Vec3 u = b - a;
82     osg::Vec3 v = c - a;
83     osg::Vec3 w = u^v;
84
85     osg::Matrix& m = _xform;
86     // Now generate a trivial basis transformation matrix.  If we want
87     // to map the three unit vectors to three arbitrary vectors U, V,
88     // and W, then those just become the columns of the 3x3 matrix.
89     m(0,0) = u[0]; m(1,0) = v[0]; m(2,0) = w[0]; m(3,0) = a[0];//    |Ux Vx Wx|
90     m(0,1) = u[1]; m(1,1) = v[1]; m(2,1) = w[1]; m(3,1) = a[1];//m = |Uy Vy Wy|
91     m(0,2) = u[2]; m(1,2) = v[2]; m(2,2) = w[2]; m(3,2) = a[2];//    |Uz Vz Wz|
92     m(0,3) = 0;    m(1,3) = 0;    m(2,3) = 0;    m(3,3) = 1;
93
94     // The above matrix maps the unit (!) square to the panel
95     // rectangle.  Postmultiply scaling factors that match the
96     // pixel-space size of the panel.
97     for(i=0; i<4; ++i) {
98         m(0,i) *= 1.0/_xmax;
99         m(1,i) *= 1.0/_ymax;
100     }
101
102     dirtyBound();
103
104     // All done.  Add us to the list
105     all_3d_panels.push_back(this);
106
107     setUseDisplayList(false);
108     getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
109     getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
110 }
111
112 FGPanelNode::~FGPanelNode()
113 {
114     delete _panel;
115 }
116
117 void
118 FGPanelNode::drawImplementation(osg::State& state) const
119 {
120   osg::ref_ptr<osg::RefMatrix> mv = new osg::RefMatrix;
121   mv->set(_xform*state.getModelViewMatrix());
122   state.applyModelViewMatrix(mv.get());
123   
124   // Grab the matrix state, so that we can get back from screen
125   // coordinates to panel coordinates when the user clicks the
126   // mouse.
127   // OSGFIXME: we don't need that when we can really pick
128   const_cast<osg::Matrix&>(_lastModelview) = state.getModelViewMatrix();
129   const_cast<osg::Matrix&>(_lastProjection) = state.getProjectionMatrix();
130   state.getCurrentViewport()->getViewport(const_cast<int&>(_lastViewport[0]),
131                                           const_cast<int&>(_lastViewport[1]),
132                                           const_cast<int&>(_lastViewport[2]),
133                                           const_cast<int&>(_lastViewport[3]));
134   
135   _panel->draw(state);
136 }
137
138 osg::BoundingBox
139 FGPanelNode::computeBound() const
140 {
141     osg::BoundingBox bb;
142     bb.expandBy(_bottomLeft);
143     bb.expandBy(_bottomRight);
144     bb.expandBy(_topLeft);
145     return bb;
146 }
147
148 bool FGPanelNode::doMouseAction(int button, int updown, int x, int y)
149 {
150     // Covert the screen coordinates to viewport coordinates in the
151     // range [0:1], then transform to OpenGL "post projection" coords
152     // in [-1:1].  Remember the difference in Y direction!
153     float vx = (x + 0.5 - _lastViewport[0]) / _lastViewport[2];
154     float vy = (y + 0.5 - _lastViewport[1]) / _lastViewport[3];
155     vx = 2*vx - 1;
156     vy = 1 - 2*vy;
157
158     // Make two vectors in post-projection coordinates at the given
159     // screen, one in the near field and one in the far field.
160     osg::Vec3 a, b;
161     a[0] = b[0] = vx;
162     a[1] = b[1] = vy;
163     a[2] =  0.75; // "Near" Z value
164     b[2] = -0.75; // "Far" Z value
165
166     // Run both vectors "backwards" through the OpenGL matrix
167     // transformation.  Remember to w-normalize the vectors!
168     osg::Matrix m = _lastModelview*_lastProjection;
169     m = osg::Matrix::inverse(m);
170
171     a = m.preMult(a);
172     b = m.preMult(b);
173
174     // And find their intersection on the z=0 plane.  The resulting X
175     // and Y coordinates are the hit location in panel coordinates.
176     float dxdz = (b[0] - a[0]) / (b[2] - a[2]);
177     float dydz = (b[1] - a[1]) / (b[2] - a[2]);
178     int panelX = (int)(a[0] - a[2]*dxdz + 0.5);
179     int panelY = (int)(a[1] - a[2]*dydz + 0.5);
180
181     return _panel->doLocalMouseAction(button, updown, panelX, panelY);
182 }
183