]> git.mxchange.org Git - flightgear.git/blob - src/Main/views.cxx
Rewriting old mat3 stuff with plib's sg library.
[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/mat3.h>
36 #include <Math/point3d.hxx>
37 #include <Math/polar3d.hxx>
38 #include <Math/vector.hxx>
39 #include <Scenery/scenery.hxx>
40 #include <Time/fg_time.hxx>
41
42 #include "options.hxx"
43 #include "views.hxx"
44
45
46 // temporary (hopefully) hack
47 static int panel_hist = 0;
48
49
50 // This is a record containing current view parameters for the current
51 // aircraft position
52 FGView pilot_view;
53
54 // This is a record containing current view parameters for the current
55 // view position
56 FGView current_view;
57
58
59 // Constructor
60 FGView::FGView( void ) {
61 }
62
63
64 // Initialize a view structure
65 void FGView::Init( void ) {
66     FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
67
68     view_offset = 0.0;
69     goal_view_offset = 0.0;
70
71     winWidth = current_options.get_xsize();
72     winHeight = current_options.get_ysize();
73
74     if ( ! current_options.get_panel_status() ) {
75         current_view.set_win_ratio( (GLfloat) winWidth / (GLfloat) winHeight );
76     } else {
77         current_view.set_win_ratio( (GLfloat) winWidth / 
78                                     ((GLfloat) (winHeight)*0.4232) );
79     }
80
81     // This never changes -- NHV
82     sgLARC_TO_SSG[0][0] = 0.0; 
83     sgLARC_TO_SSG[0][1] = 1.0; 
84     sgLARC_TO_SSG[0][2] = -0.0; 
85     sgLARC_TO_SSG[0][3] = 0.0; 
86
87     sgLARC_TO_SSG[1][0] = 0.0; 
88     sgLARC_TO_SSG[1][1] = 0.0; 
89     sgLARC_TO_SSG[1][2] = 1.0; 
90     sgLARC_TO_SSG[1][3] = 0.0;
91         
92     sgLARC_TO_SSG[2][0] = 1.0; 
93     sgLARC_TO_SSG[2][1] = -0.0; 
94     sgLARC_TO_SSG[2][2] = 0.0; 
95     sgLARC_TO_SSG[2][3] = 0.0;
96         
97     sgLARC_TO_SSG[3][0] = 0.0; 
98     sgLARC_TO_SSG[3][1] = 0.0; 
99     sgLARC_TO_SSG[3][2] = 0.0; 
100     sgLARC_TO_SSG[3][3] = 1.0; 
101         
102     force_update_fov_math();
103 }
104
105 // Update the view volume, position, and orientation
106 void FGView::UpdateViewParams( const FGInterface& f ) {
107     UpdateViewMath(f);
108     
109     if ((current_options.get_panel_status() != panel_hist) &&                          (current_options.get_panel_status()))
110     {
111         FGPanel::OurPanel->ReInit( 0, 0, 1024, 768);
112     }
113
114     if ( ! current_options.get_panel_status() ) {
115         xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
116     } else {
117         xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth), 
118                     (GLint)((winHeight)*0.4232) );
119     }
120
121     panel_hist = current_options.get_panel_status();
122 }
123
124
125 // convert sgMat4 to MAT3 and print
126 static void print_sgMat4( sgMat4 &in) {
127     MAT3mat print;
128     int i;
129     int j;
130     for ( i = 0; i < 4; i++ ) {
131         for ( j = 0; j < 4; j++ ) {
132             print[i][j] = in[i][j];
133         }
134     }
135     MAT3print( print, stdout);
136 }
137
138
139 // convert convert MAT3 to sgMat4
140 static void MAT3mat_To_sgMat4( MAT3mat &in, sgMat4 &out ) {
141     out[0][0] = in[0][0];
142     out[0][1] = in[0][1];
143     out[0][2] = in[0][2];
144     out[0][3] = in[0][3];
145     out[1][0] = in[1][0];
146     out[1][1] = in[1][1];
147     out[1][2] = in[1][2];
148     out[1][3] = in[1][3];
149     out[2][0] = in[2][0];
150     out[2][1] = in[2][1];
151     out[2][2] = in[2][2];
152     out[2][3] = in[2][3];
153     out[3][0] = in[3][0];
154     out[3][1] = in[3][1];
155     out[3][2] = in[3][2];
156     out[3][3] = in[3][3];
157 }
158
159
160 // Update the view parameters
161 void FGView::UpdateViewMath( const FGInterface& f ) {
162     Point3D p;
163     sgVec3 v0, minus_z;
164     MAT3vec vec, forward;
165     MAT3mat R, TMP, UP, LOCAL, VIEW;
166     sgMat4 sgTMP;
167
168     if ( update_fov ) {
169         ssgSetFOV( current_options.get_fov(), 
170                    current_options.get_fov() * win_ratio );
171         update_fov = false;
172     }
173                 
174     scenery.center = scenery.next_center;
175
176     // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
177     //        scenery.center.y, scenery.center.z);
178
179     // calculate the cartesion coords of the current lat/lon/0 elev
180     p = Point3D( f.get_Longitude(), 
181                  f.get_Lat_geocentric(), 
182                  f.get_Sea_level_radius() * FEET_TO_METER );
183
184     cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
185
186     // calculate view position in current FG view coordinate system
187     // p.lon & p.lat are already defined earlier, p.radius was set to
188     // the sea level radius, so now we add in our altitude.
189     if ( f.get_Altitude() * FEET_TO_METER > 
190          (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
191         p.setz( p.radius() + f.get_Altitude() * FEET_TO_METER );
192     } else {
193         p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
194     }
195
196     abs_view_pos = fgPolarToCart3d(p);
197         
198     view_pos = abs_view_pos - scenery.center;
199
200     FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
201     FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
202     FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
203
204     // code to calculate LOCAL matrix calculated from Phi, Theta, and
205     // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
206     // flight model
207
208     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
209     MAT3rotate(R, vec, f.get_Phi());
210     // cout << "Roll matrix" << endl;
211     // MAT3print(R, stdout);
212
213     sgVec3 sgrollvec;
214     sgSetVec3( sgrollvec, 0.0, 0.0, 1.0 );
215     sgMat4 sgPHI;               // roll
216     sgMakeRotMat4( sgPHI, f.get_Phi() * RAD_TO_DEG, sgrollvec );
217
218     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
219     MAT3rotate(TMP, vec, f.get_Theta());
220     // cout << "Pitch matrix" << endl;;
221     // MAT3print(TMP, stdout);
222     MAT3mult(R, R, TMP);
223     // cout << "tmp rotation matrix, R:" << endl;;
224     // MAT3print(R, stdout);
225
226     sgVec3 sgpitchvec;
227     sgSetVec3( sgpitchvec, 0.0, 1.0, 0.0 );
228     sgMat4 sgTHETA;             // pitch
229     sgMakeRotMat4( sgTHETA, f.get_Theta() * RAD_TO_DEG,
230                    sgpitchvec );
231
232     sgMat4 sgROT;
233     sgMultMat4( sgROT, sgPHI, sgTHETA );
234
235     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
236     MAT3rotate(TMP, vec, -f.get_Psi());
237     // cout << "Yaw matrix" << endl;
238     // MAT3print(TMP, stdout);
239     MAT3mult(LOCAL, R, TMP);
240     // cout << "LOCAL matrix:" << endl;
241     // MAT3print(LOCAL, stdout);
242
243     sgVec3 sgyawvec;
244     sgSetVec3( sgyawvec, 1.0, 0.0, 0.0 );
245     sgMat4 sgPSI;               // pitch
246     sgMakeRotMat4( sgPSI, -f.get_Psi() * RAD_TO_DEG, sgyawvec );
247
248     sgMultMat4( sgLOCAL, sgROT, sgPSI );
249     // cout << "sgLOCAL matrix" << endl;
250     // print_sgMat4( sgLOCAL );
251         
252     // Derive the local UP transformation matrix based on *geodetic*
253     // coordinates
254     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
255     MAT3rotate(R, vec, f.get_Longitude());     // R = rotate about Z axis
256     // printf("Longitude matrix\n");
257     // MAT3print(R, stdout);
258
259     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
260     MAT3mult_vec(vec, vec, R);
261     MAT3rotate(TMP, vec, -f.get_Latitude());  // TMP = rotate about X axis
262     // printf("Latitude matrix\n");
263     // MAT3print(TMP, stdout);
264
265     MAT3mult(UP, R, TMP);
266     // cout << "Local up matrix" << endl;;
267     // MAT3print(UP, stdout);
268
269     sgMakeRotMat4( sgUP, 
270                    f.get_Longitude() * RAD_TO_DEG,
271                    0.0,
272                    -f.get_Latitude() * RAD_TO_DEG );
273     /*
274       cout << "FG derived UP matrix using sg routines" << endl;
275     MAT3mat print;
276     int i;
277     int j;
278     for ( i = 0; i < 4; i++ ) {
279         for ( j = 0; j < 4; j++ ) {
280         print[i][j] = sgUP[i][j];
281         }
282         }
283     MAT3print( print, stdout);
284     */
285
286     sgSetVec3( local_up, 1.0, 0.0, 0.0 );
287     sgXformVec3( local_up, sgUP );
288     // cout << "Local Up = " << local_up[0] << "," << local_up[1] << ","
289     //      << local_up[2] << endl;
290     
291     // Alternative method to Derive local up vector based on
292     // *geodetic* coordinates
293     // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
294     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
295     //         alt_up.x, alt_up.y, alt_up.z);
296
297     // Calculate the VIEW matrix
298     MAT3mult(VIEW, LOCAL, UP);
299     // cout << "VIEW matrix" << endl;;
300     // MAT3print(VIEW, stdout);
301
302     sgMat4 sgTMP2;
303     sgMultMat4( sgTMP, sgLOCAL, sgUP );
304
305     // generate the sg view up vector
306     sgVec3 vec1;
307     sgSetVec3( vec1, 1.0, 0.0, 0.0 );
308     sgXformVec3( sgview_up, vec1, sgTMP );
309
310     // generate the view offset matrix
311     sgMakeRotMat4( sgVIEW_OFFSET, view_offset * RAD_TO_DEG, sgview_up );
312     // cout << "sgVIEW_OFFSET matrix" << endl;
313     // print_sgMat4( sgVIEW_OFFSET );
314         
315     sgMultMat4( sgTMP2, sgTMP, sgVIEW_OFFSET );
316     sgMultMat4( sgVIEW_ROT, sgLARC_TO_SSG, sgTMP2 );
317
318     sgMakeTransMat4( sgTRANS, view_pos.x(), view_pos.y(), view_pos.z() );
319
320     sgMultMat4( sgVIEW, sgVIEW_ROT, sgTRANS );
321
322     // FGMat4Wrapper tmp;
323     // sgCopyMat4( tmp.m, sgVIEW );
324     // follow.push_back( tmp );
325
326     // generate the current up, forward, and fwrd-view vectors
327     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
328     MAT3mult_vec(view_up, vec, VIEW);
329
330     /*
331     cout << "FG derived VIEW matrix using sg routines" << endl;
332     MAT3mat print;
333     int i;
334     int j;
335     for ( i = 0; i < 4; i++ ) {
336         for ( j = 0; j < 4; j++ ) {
337             print[i][j] = sgVIEW[i][j];
338         }
339     }
340     MAT3print( print, stdout);
341     */
342
343     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
344     MAT3mult_vec(forward, vec, VIEW);
345     // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
346     //         forward[2]);
347
348     MAT3rotate(TMP, view_up, view_offset);
349     MAT3mult_vec(view_forward, forward, TMP);
350
351     // make a vector to the current view position
352     sgSetVec3( v0, view_pos.x(), view_pos.y(), view_pos.z() );
353
354     // Given a vector pointing straight down (-Z), map into onto the
355     // local plane representing "horizontal".  This should give us the
356     // local direction for moving "south".
357     sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
358
359     sgmap_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
360     sgNormalizeVec3(surface_south);
361     // cout << "Surface direction directly south " << surface_south[0] << ","
362     //      << surface_south[1] << "," << surface_south[2] << endl;
363
364     // now calculate the surface east vector
365     sgMakeRotMat4( sgTMP, FG_PI_2 * RAD_TO_DEG, sgview_up );
366     // cout << "sgMat4 sgTMP" << endl;
367     // print_sgMat4( sgTMP );
368     sgXformVec3(surface_east, surface_south, sgTMP);
369     // cout << "Surface direction directly east" << surface_east[0] << ","
370     //      << surface_east[1] << "," << surface_east[2] << endl;
371     // cout << "Should be close to zero = "
372     //      << sgScalarProductVec3(surface_south, surface_east) << endl;
373 }
374
375
376 // Destructor
377 FGView::~FGView( void ) {
378 }