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