]> git.mxchange.org Git - flightgear.git/blob - src/Model/panelnode.cxx
e49f3197bcb92914759d817bdb8ef05b41dd749c
[flightgear.git] / src / Model / panelnode.cxx
1 #include <GL/gl.h>
2 #include <Main/fg_props.hxx>
3 #include <Cockpit/panel.hxx>
4 #include <Cockpit/panel_io.hxx>
5 #include "panelnode.hxx"
6
7 FGPanelNode::FGPanelNode(SGPropertyNode* props)
8 {
9     // Make an FGPanel object.  But *don't* call init() or bind() on
10     // it -- those methods touch static state.
11     _panel = fgReadPanel(props->getStringValue("path"));
12
13     // Read out the pixel-space info
14     _xmax = _panel->getWidth();
15     _ymax = _panel->getHeight();
16
17     // And the corner points
18     SGPropertyNode* pt = props->getChild("bottom-left");
19     _bottomLeft[0] = pt->getFloatValue("x-m");
20     _bottomLeft[1] = pt->getFloatValue("y-m");
21     _bottomLeft[2] = pt->getFloatValue("z-m");
22
23     pt = props->getChild("top-left");
24     _topLeft[0] = pt->getFloatValue("x-m");
25     _topLeft[1] = pt->getFloatValue("y-m");
26     _topLeft[2] = pt->getFloatValue("z-m");
27
28     pt = props->getChild("bottom-right");
29     _bottomRight[0] = pt->getFloatValue("x-m");
30     _bottomRight[1] = pt->getFloatValue("y-m");
31     _bottomRight[2] = pt->getFloatValue("z-m");
32
33     // Now generate out transformation matrix.  For shorthand, use
34     // "a", "b", and "c" as our corners and "m" as the matrix. The
35     // vector u goes from a to b, v from a to c, and w is a
36     // perpendicular cross product.
37     float *a = _bottomLeft, *b = _bottomRight, *c = _topLeft, *m = _xform;
38     float u[3], v[3], w[3];
39     int i;
40     for(i=0; i<3; i++) u[i] = b[i] - a[i]; // U = B - A
41     for(i=0; i<3; i++) v[i] = c[i] - a[i]; // V = C - A
42
43     w[0] = u[1]*v[2] - v[1]*u[2];          // W = U x V
44     w[1] = u[2]*v[0] - v[2]*u[0];
45     w[2] = u[0]*v[1] - v[0]*u[1];
46
47     // Now generate a trivial basis transformation matrix.  If we want
48     // to map the three unit vectors to three arbitrary vectors U, V,
49     // and W, then those just become the columns of the 3x3 matrix.
50     m[0] = u[0]; m[4] = v[0]; m[8]  = w[0]; m[12] = a[0]; //     |Ux Vx Wx|
51     m[1] = u[1]; m[5] = v[1]; m[9]  = w[1]; m[13] = a[1]; // m = |Uy Vy Wy|
52     m[2] = u[2]; m[6] = v[2]; m[10] = w[2]; m[14] = a[2]; //     |Uz Vz Wz|
53     m[3] = 0;    m[7] = 0;    m[11] = 0;    m[15] = 1;
54
55     // The above matrix maps the unit (!) square to the panel
56     // rectangle.  Postmultiply scaling factors that match the
57     // pixel-space size of the panel.
58     for(i=0; i<4; i++) {
59         m[0+i] *= 1.0/_xmax;
60         m[4+i] *= 1.0/_ymax;
61     }
62
63     // Now plib initialization.  The bounding sphere is defined nicely
64     // by our corner points:
65     float cx = (b[0]+c[0])/2;
66     float cy = (b[1]+c[1])/2;
67     float cz = (b[2]+c[2])/2;
68     float r = sqrt((cx-a[0])*(cx-a[0]) +
69                    (cy-a[1])*(cy-a[1]) +
70                    (cz-a[2])*(cz-a[2]));
71     bsphere.setCenter(cx, cy, cz);
72     bsphere.setRadius(r);
73 }
74
75 FGPanelNode::~FGPanelNode()
76 {
77     delete _panel;
78 }
79
80 void FGPanelNode::draw()
81 {
82     // What's the difference?
83     draw_geometry();
84 }
85
86 void FGPanelNode::draw_geometry()
87 {
88     glMatrixMode(GL_MODELVIEW);
89     glPushMatrix();
90     glMultMatrixf(_xform);
91     _panel->draw();
92     glPopMatrix();
93 }
94
95 void FGPanelNode::die()
96 {
97     SG_LOG(SG_ALL,SG_ALERT,"Unimplemented function called on FGPanelNode");
98     *(int*)0=0;
99 }
100