]> git.mxchange.org Git - flightgear.git/blob - src/Main/views.cxx
"sg-izing" the matrix/vector code so that I can hopefully eventually remove
[flightgear.git] / src / Main / views.cxx
1 // views.cxx -- data structures and routines for managing and view
2 //               parameters.
3 //
4 // Written by Curtis Olson, started August 1997.
5 //
6 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <ssg.h>                // plib include
30
31 #include <Aircraft/aircraft.hxx>
32 #include <Cockpit/panel.hxx>
33 #include <Debug/logstream.hxx>
34 #include <Include/fg_constants.h>
35 #include <Math/point3d.hxx>
36 #include <Math/polar3d.hxx>
37 #include <Math/vector.hxx>
38 #include <Scenery/scenery.hxx>
39 #include <Time/fg_time.hxx>
40
41 #include "options.hxx"
42 #include "views.hxx"
43
44
45 // temporary (hopefully) hack
46 static int panel_hist = 0;
47
48
49 // This is a record containing current view parameters for the current
50 // aircraft position
51 FGView pilot_view;
52
53 // This is a record containing current view parameters for the current
54 // view position
55 FGView current_view;
56
57
58 // Constructor
59 FGView::FGView( void ) {
60 }
61
62
63 // Initialize a view structure
64 void FGView::Init( void ) {
65     FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
66
67     view_offset = 0.0;
68     goal_view_offset = 0.0;
69
70     winWidth = current_options.get_xsize();
71     winHeight = current_options.get_ysize();
72
73     if ( ! current_options.get_panel_status() ) {
74         current_view.set_win_ratio( (GLfloat) winWidth / (GLfloat) winHeight );
75     } else {
76         current_view.set_win_ratio( (GLfloat) winWidth / 
77                                     ((GLfloat) (winHeight)*0.4232) );
78     }
79
80     // This never changes -- NHV
81     LARC_TO_SSG[0][0] = 0.0; 
82     LARC_TO_SSG[0][1] = 1.0; 
83     LARC_TO_SSG[0][2] = -0.0; 
84     LARC_TO_SSG[0][3] = 0.0; 
85
86     LARC_TO_SSG[1][0] = 0.0; 
87     LARC_TO_SSG[1][1] = 0.0; 
88     LARC_TO_SSG[1][2] = 1.0; 
89     LARC_TO_SSG[1][3] = 0.0;
90         
91     LARC_TO_SSG[2][0] = 1.0; 
92     LARC_TO_SSG[2][1] = -0.0; 
93     LARC_TO_SSG[2][2] = 0.0; 
94     LARC_TO_SSG[2][3] = 0.0;
95         
96     LARC_TO_SSG[3][0] = 0.0; 
97     LARC_TO_SSG[3][1] = 0.0; 
98     LARC_TO_SSG[3][2] = 0.0; 
99     LARC_TO_SSG[3][3] = 1.0; 
100         
101     force_update_fov_math();
102 }
103
104 // Update the view volume, position, and orientation
105 void FGView::UpdateViewParams( const FGInterface& f ) {
106     UpdateViewMath(f);
107     
108     if ((current_options.get_panel_status() != panel_hist) &&                          (current_options.get_panel_status()))
109     {
110         FGPanel::OurPanel->ReInit( 0, 0, 1024, 768);
111     }
112
113     if ( ! current_options.get_panel_status() ) {
114         xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
115     } else {
116         xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth), 
117                     (GLint)((winHeight)*0.4232) );
118     }
119
120     panel_hist = current_options.get_panel_status();
121 }
122
123
124 // convert sgMat4 to MAT3 and print
125 static void print_sgMat4( sgMat4 &in) {
126     int i, j;
127     for ( i = 0; i < 4; i++ ) {
128         for ( j = 0; j < 4; j++ ) {
129             printf("%10.4f ", in[i][j]);
130         }
131         cout << endl;
132     }
133 }
134
135
136 // Update the view parameters
137 void FGView::UpdateViewMath( const FGInterface& f ) {
138     Point3D p;
139     sgVec3 v0, minus_z, sgvec, forward;
140     sgMat4 VIEWo, TMP;
141
142     if ( update_fov ) {
143         ssgSetFOV( current_options.get_fov(), 
144                    current_options.get_fov() * win_ratio );
145         update_fov = false;
146     }
147                 
148     scenery.center = scenery.next_center;
149
150     // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
151     //        scenery.center.y, scenery.center.z);
152
153     // calculate the cartesion coords of the current lat/lon/0 elev
154     p = Point3D( f.get_Longitude(), 
155                  f.get_Lat_geocentric(), 
156                  f.get_Sea_level_radius() * FEET_TO_METER );
157
158     cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
159
160     // calculate view position in current FG view coordinate system
161     // p.lon & p.lat are already defined earlier, p.radius was set to
162     // the sea level radius, so now we add in our altitude.
163     if ( f.get_Altitude() * FEET_TO_METER > 
164          (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
165         p.setz( p.radius() + f.get_Altitude() * FEET_TO_METER );
166     } else {
167         p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
168     }
169
170     abs_view_pos = fgPolarToCart3d(p);
171         
172     view_pos = abs_view_pos - scenery.center;
173
174     FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
175     FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
176     FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
177
178     // code to calculate LOCAL matrix calculated from Phi, Theta, and
179     // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
180     // flight model
181
182     sgVec3 rollvec;
183     sgSetVec3( rollvec, 0.0, 0.0, 1.0 );
184     sgMat4 PHI;         // roll
185     sgMakeRotMat4( PHI, f.get_Phi() * RAD_TO_DEG, rollvec );
186
187     sgVec3 pitchvec;
188     sgSetVec3( pitchvec, 0.0, 1.0, 0.0 );
189     sgMat4 THETA;               // pitch
190     sgMakeRotMat4( THETA, f.get_Theta() * RAD_TO_DEG, pitchvec );
191
192     sgMat4 ROT;
193     sgMultMat4( ROT, PHI, THETA );
194
195     sgVec3 yawvec;
196     sgSetVec3( yawvec, 1.0, 0.0, 0.0 );
197     sgMat4 PSI;         // pitch
198     sgMakeRotMat4( PSI, -f.get_Psi() * RAD_TO_DEG, yawvec );
199
200     sgMultMat4( LOCAL, ROT, PSI );
201     // cout << "LOCAL matrix" << endl;
202     // print_sgMat4( LOCAL );
203         
204     sgMakeRotMat4( UP, 
205                    f.get_Longitude() * RAD_TO_DEG,
206                    0.0,
207                    -f.get_Latitude() * RAD_TO_DEG );
208
209     sgSetVec3( local_up, 1.0, 0.0, 0.0 );
210     sgXformVec3( local_up, UP );
211     // cout << "Local Up = " << local_up[0] << "," << local_up[1] << ","
212     //      << local_up[2] << endl;
213     
214     // Alternative method to Derive local up vector based on
215     // *geodetic* coordinates
216     // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
217     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
218     //         alt_up.x, alt_up.y, alt_up.z);
219
220     sgMat4 TMP2;
221     sgMultMat4( VIEWo, LOCAL, UP );
222     // cout << "VIEWo matrix" << endl;
223     // print_sgMat4( VIEWo );
224
225     // generate the sg view up vector
226     sgVec3 vec1;
227     sgSetVec3( vec1, 1.0, 0.0, 0.0 );
228     sgXformVec3( view_up, vec1, VIEWo );
229
230     // generate the view offset matrix
231     sgMakeRotMat4( VIEW_OFFSET, view_offset * RAD_TO_DEG, view_up );
232     // cout << "VIEW_OFFSET matrix" << endl;
233     // print_sgMat4( VIEW_OFFSET );
234         
235     sgMultMat4( TMP2, VIEWo, VIEW_OFFSET );
236     sgMultMat4( VIEW_ROT, LARC_TO_SSG, TMP2 );
237     // cout << "VIEW_ROT matrix" << endl;
238     // print_sgMat4( VIEW_ROT );
239
240     sgMakeTransMat4( TRANS, view_pos.x(), view_pos.y(), view_pos.z() );
241
242     sgMultMat4( VIEW, VIEW_ROT, TRANS );
243
244     sgSetVec3( sgvec, 0.0, 0.0, 1.0 );
245     sgXformVec3( forward, sgvec, VIEWo );
246     // cout << "forward = " << forward[0] << ","
247     //      << forward[1] << "," << forward[2] << endl;
248
249     sgMakeRotMat4( TMP, view_offset * RAD_TO_DEG, view_up );
250     sgXformVec3( view_forward, forward, TMP );
251     // cout << "view_forward = " << view_forward[0] << ","
252     //      << view_forward[1] << "," << view_forward[2] << endl;
253     
254     // make a vector to the current view position
255     sgSetVec3( v0, view_pos.x(), view_pos.y(), view_pos.z() );
256
257     // Given a vector pointing straight down (-Z), map into onto the
258     // local plane representing "horizontal".  This should give us the
259     // local direction for moving "south".
260     sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
261
262     sgmap_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
263     sgNormalizeVec3(surface_south);
264     // cout << "Surface direction directly south " << surface_south[0] << ","
265     //      << surface_south[1] << "," << surface_south[2] << endl;
266
267     // now calculate the surface east vector
268     sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, view_up );
269     // cout << "sgMat4 TMP" << endl;
270     // print_sgMat4( TMP );
271     sgXformVec3(surface_east, surface_south, TMP);
272     // cout << "Surface direction directly east" << surface_east[0] << ","
273     //      << surface_east[1] << "," << surface_east[2] << endl;
274     // cout << "Should be close to zero = "
275     //      << sgScalarProductVec3(surface_south, surface_east) << endl;
276 }
277
278
279 // Destructor
280 FGView::~FGView( void ) {
281 }