]> git.mxchange.org Git - flightgear.git/blob - src/Model/panelnode.cxx
Port over remaining Point3D usage to the more type and unit safe SG* classes.
[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 <Cockpit/panel.hxx>
11 #include <Cockpit/panel_io.hxx>
12 #include "panelnode.hxx"
13
14 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     const char *path = props->getStringValue("path");
45     _panel = fgReadPanel(path);
46     if (!_panel)
47         throw sg_io_exception(string("Failed to load panel ") + path);
48
49     // Never mind.  We *have* to call init to make sure the static
50     // state is initialized (it's not, if there aren't any 2D
51     // panels).  This is a memory leak and should be fixed!`
52     // FIXME
53     _panel->init();
54
55     _panel->setDepthTest( props->getBoolValue("depth-test") );
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     osg::Vec3 a = _bottomLeft;
82     osg::Vec3 b = _bottomRight;
83     osg::Vec3 c = _topLeft;
84     osg::Vec3 u = b - a;
85     osg::Vec3 v = c - a;
86     osg::Vec3 w = u^v;
87
88     osg::Matrix& m = _xform;
89     // Now generate a trivial basis transformation matrix.  If we want
90     // to map the three unit vectors to three arbitrary vectors U, V,
91     // and W, then those just become the columns of the 3x3 matrix.
92     m(0,0) = u[0]; m(1,0) = v[0]; m(2,0) = w[0]; m(3,0) = a[0];//    |Ux Vx Wx|
93     m(0,1) = u[1]; m(1,1) = v[1]; m(2,1) = w[1]; m(3,1) = a[1];//m = |Uy Vy Wy|
94     m(0,2) = u[2]; m(1,2) = v[2]; m(2,2) = w[2]; m(3,2) = a[2];//    |Uz Vz Wz|
95     m(0,3) = 0;    m(1,3) = 0;    m(2,3) = 0;    m(3,3) = 1;
96
97     // The above matrix maps the unit (!) square to the panel
98     // rectangle.  Postmultiply scaling factors that match the
99     // pixel-space size of the panel.
100     for(i=0; i<4; ++i) {
101         m(0,i) *= 1.0/_xmax;
102         m(1,i) *= 1.0/_ymax;
103     }
104
105     dirtyBound();
106
107     // All done.  Add us to the list
108     all_3d_panels.push_back(this);
109
110     setUseDisplayList(false);
111     getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
112     getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
113 }
114
115 FGPanelNode::~FGPanelNode()
116 {
117     delete _panel;
118 }
119
120 void
121 FGPanelNode::drawImplementation(osg::State& state) const
122 {
123   osg::ref_ptr<osg::RefMatrix> mv = new osg::RefMatrix;
124   mv->set(_xform*state.getModelViewMatrix());
125   state.applyModelViewMatrix(mv.get());
126   
127   // Grab the matrix state, so that we can get back from screen
128   // coordinates to panel coordinates when the user clicks the
129   // mouse.
130   // OSGFIXME: we don't need that when we can really pick
131   _lastModelview = state.getModelViewMatrix();
132   _lastProjection = state.getProjectionMatrix();
133
134   const osg::Viewport* vp = state.getCurrentViewport();
135   _lastViewport[0] = vp->x();
136   _lastViewport[1] = vp->y();
137   _lastViewport[2] = vp->width();
138   _lastViewport[3] = vp->height();
139   
140   _panel->draw(state);
141 }
142
143 osg::BoundingBox
144 FGPanelNode::computeBound() const
145 {
146     osg::BoundingBox bb;
147     bb.expandBy(_bottomLeft);
148     bb.expandBy(_bottomRight);
149     bb.expandBy(_topLeft);
150     return bb;
151 }
152
153 bool FGPanelNode::doMouseAction(int button, int updown, int x, int y)
154 {
155     // Covert the screen coordinates to viewport coordinates in the
156     // range [0:1], then transform to OpenGL "post projection" coords
157     // in [-1:1].  Remember the difference in Y direction!
158     float vx = (x + 0.5 - _lastViewport[0]) / _lastViewport[2];
159     float vy = (y + 0.5 - _lastViewport[1]) / _lastViewport[3];
160     vx = 2*vx - 1;
161     vy = 1 - 2*vy;
162
163     // Make two vectors in post-projection coordinates at the given
164     // screen, one in the near field and one in the far field.
165     osg::Vec3 a, b;
166     a[0] = b[0] = vx;
167     a[1] = b[1] = vy;
168     a[2] =  0.75; // "Near" Z value
169     b[2] = -0.75; // "Far" Z value
170
171     // Run both vectors "backwards" through the OpenGL matrix
172     // transformation.  Remember to w-normalize the vectors!
173     osg::Matrix m = _lastModelview*_lastProjection;
174     m = osg::Matrix::inverse(m);
175
176     a = m.preMult(a);
177     b = m.preMult(b);
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