]> git.mxchange.org Git - flightgear.git/blob - src/Model/panelnode.cxx
55973b0035ec670cc3220f4a8c5649522abb9173
[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.  Very
18 // clumsy.  Replace with per-aircraft handling.
19 vector<FGPanelNode*> all_3d_panels;
20 bool fgHandle3DPanelMouseEvent( int button, int updown, int x, int y )
21 {
22     for ( unsigned int i = 0; i < all_3d_panels.size(); i++ ) {
23         if ( all_3d_panels[i]->doMouseAction(button, updown, x, y) ) {
24             return true;
25         }
26     }
27     return false;
28 }
29
30 void fgUpdate3DPanels()
31 {
32     for ( unsigned int i = 0; i < all_3d_panels.size(); i++ ) {
33         all_3d_panels[i]->getPanel()->updateMouseDelay();
34     }
35 }
36
37 FGPanelNode::FGPanelNode(SGPropertyNode* props)
38 {
39     int i;
40
41     // Make an FGPanel object.  But *don't* call init() or bind() on
42     // it -- those methods touch static state.
43     _panel = fgReadPanel(props->getStringValue("path"));
44
45     // Never mind.  We *have* to call init to make sure the static
46     // state is initialized (it's not, if there aren't any 2D
47     // panels).  This is a memory leak and should be fixed!`
48     _panel->init();
49
50     // Initialize the matrices to the identity.  PLib prints warnings
51     // when trying to invert singular matrices (e.g. when not using a
52     // 3D panel).
53     for(i=0; i<4; i++)
54         for(int j=0; j<4; j++)
55             _lastModelview[4*i+j] = _lastProjection[4*i+j] = i==j ? 1 : 0;
56
57     // Read out the pixel-space info
58     _xmax = _panel->getWidth();
59     _ymax = _panel->getHeight();
60
61     // And the corner points
62     SGPropertyNode* pt = props->getChild("bottom-left");
63     _bottomLeft[0] = pt->getFloatValue("x-m");
64     _bottomLeft[1] = pt->getFloatValue("y-m");
65     _bottomLeft[2] = pt->getFloatValue("z-m");
66
67     pt = props->getChild("top-left");
68     _topLeft[0] = pt->getFloatValue("x-m");
69     _topLeft[1] = pt->getFloatValue("y-m");
70     _topLeft[2] = pt->getFloatValue("z-m");
71
72     pt = props->getChild("bottom-right");
73     _bottomRight[0] = pt->getFloatValue("x-m");
74     _bottomRight[1] = pt->getFloatValue("y-m");
75     _bottomRight[2] = pt->getFloatValue("z-m");
76
77     // Now generate our transformation matrix.  For shorthand, use
78     // "a", "b", and "c" as our corners and "m" as the matrix. The
79     // vector u goes from a to b, v from a to c, and w is a
80     // perpendicular cross product.
81     float *a = _bottomLeft, *b = _bottomRight, *c = _topLeft, *m = _xform;
82     float u[3], v[3], w[3];
83     for(i=0; i<3; i++) u[i] = b[i] - a[i]; // U = B - A
84     for(i=0; i<3; i++) v[i] = c[i] - a[i]; // V = C - A
85
86     w[0] = u[1]*v[2] - v[1]*u[2];          // W = U x V
87     w[1] = u[2]*v[0] - v[2]*u[0];
88     w[2] = u[0]*v[1] - v[0]*u[1];
89
90     // Now generate a trivial basis transformation matrix.  If we want
91     // to map the three unit vectors to three arbitrary vectors U, V,
92     // and W, then those just become the columns of the 3x3 matrix.
93     m[0] = u[0]; m[4] = v[0]; m[8]  = w[0]; m[12] = a[0]; //     |Ux Vx Wx|
94     m[1] = u[1]; m[5] = v[1]; m[9]  = w[1]; m[13] = a[1]; // m = |Uy Vy Wy|
95     m[2] = u[2]; m[6] = v[2]; m[10] = w[2]; m[14] = a[2]; //     |Uz Vz Wz|
96     m[3] = 0;    m[7] = 0;    m[11] = 0;    m[15] = 1;
97
98     // The above matrix maps the unit (!) square to the panel
99     // rectangle.  Postmultiply scaling factors that match the
100     // pixel-space size of the panel.
101     for(i=0; i<4; i++) {
102         m[0+i] *= 1.0/_xmax;
103         m[4+i] *= 1.0/_ymax;
104     }
105
106     // Now plib initialization.  The bounding sphere is defined nicely
107     // by our corner points:
108     float cx = (b[0]+c[0])/2;
109     float cy = (b[1]+c[1])/2;
110     float cz = (b[2]+c[2])/2;
111     float r = sqrt((cx-a[0])*(cx-a[0]) +
112                    (cy-a[1])*(cy-a[1]) +
113                    (cz-a[2])*(cz-a[2]));
114     bsphere.setCenter(cx, cy, cz);
115     bsphere.setRadius(r);
116
117     // All done.  Add us to the list
118     all_3d_panels.push_back(this);
119 }
120
121 FGPanelNode::~FGPanelNode()
122 {
123     delete _panel;
124 }
125
126 void FGPanelNode::draw()
127 {
128     // What's the difference?
129     draw_geometry();
130 }
131
132 void FGPanelNode::draw_geometry()
133 {
134     glMatrixMode(GL_MODELVIEW);
135     glPushMatrix();
136     glMultMatrixf(_xform);
137
138     // Grab the matrix state, so that we can get back from screen
139     // coordinates to panel coordinates when the user clicks the
140     // mouse.
141     glGetFloatv(GL_MODELVIEW_MATRIX, _lastModelview);
142     glGetFloatv(GL_PROJECTION_MATRIX, _lastProjection);
143     glGetIntegerv(GL_VIEWPORT, _lastViewport);
144
145     _panel->draw();
146
147
148     glPopMatrix();
149 }
150
151 bool FGPanelNode::doMouseAction(int button, int updown, int x, int y)
152 {
153     // Covert the screen coordinates to viewport coordinates in the
154     // range [0:1], then transform to OpenGL "post projection" coords
155     // in [-1:1].  Remember the difference in Y direction!
156     float vx = (x + 0.5 - _lastViewport[0]) / _lastViewport[2];
157     float vy = (y + 0.5 - _lastViewport[1]) / _lastViewport[3];
158     vx = 2*vx - 1;
159     vy = 1 - 2*vy;
160
161     // Make two vectors in post-projection coordinates at the given
162     // screen, one in the near field and one in the far field.
163     sgVec3 a, b;
164     a[0] = b[0] = vx;
165     a[1] = b[1] = vy;
166     a[2] =  0.75; // "Near" Z value
167     b[2] = -0.75; // "Far" Z value
168
169     // Run both vectors "backwards" through the OpenGL matrix
170     // transformation.  Remember to w-normalize the vectors!
171     sgMat4 m;
172     sgMultMat4(m, *(sgMat4*)_lastProjection, *(sgMat4*)_lastModelview);
173     sgInvertMat4(m);
174
175     sgFullXformPnt3(a, m);
176     sgFullXformPnt3(b, m);
177
178     // And find their intersection on the z=0 plane.  The resulting X
179     // and Y coordinates are the hit location in panel coordinates.
180     float dxdz = (b[0] - a[0]) / (b[2] - a[2]);
181     float dydz = (b[1] - a[1]) / (b[2] - a[2]);
182     int panelX = (int)(a[0] - a[2]*dxdz + 0.5);
183     int panelY = (int)(a[1] - a[2]*dydz + 0.5);
184
185     return _panel->doLocalMouseAction(button, updown, panelX, panelY);
186 }
187
188 void FGPanelNode::die()
189 {
190     SG_LOG(SG_ALL,SG_ALERT,"Unimplemented function called on FGPanelNode");
191     exit(1);
192 }
193