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