]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.cxx
0fc291b5472f838b86fed3d2e01d924f3c8d6418
[flightgear.git] / src / Main / viewer.cxx
1 // viewer.cxx -- class for managing a viewer in the flightgear world.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //                          overhaul started October 2000.
5 //   partially rewritten by Jim Wilson jim@kelcomaine.com using interface
6 //                          by David Megginson March 2002
7 //
8 // Copyright (C) 1997 - 2000  Curtis L. Olson  - curt@flightgear.org
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #include <simgear/compiler.h>
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/constants.h>
35 #include <simgear/math/point3d.hxx>
36 #include <simgear/math/polar3d.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38
39 #include <Scenery/scenery.hxx>
40
41 /* from lookat */
42 #include <simgear/math/vector.hxx>
43 #include "globals.hxx"
44 /* end from lookat */
45
46 #include "viewer.hxx"
47
48 \f
49 ////////////////////////////////////////////////////////////////////////
50 // Implementation of FGViewer.
51 ////////////////////////////////////////////////////////////////////////
52
53 // Constructor
54 FGViewer::FGViewer( void ):
55     _scaling_type(FG_SCALING_MAX),
56     _fov_deg(55.0),
57     _dirty(true),
58     _lon_deg(0),
59     _lat_deg(0),
60     _alt_ft(0),
61     _target_lon_deg(0),
62     _target_lat_deg(0),
63     _target_alt_ft(0),
64     _roll_deg(0),
65     _pitch_deg(0),
66     _heading_deg(0),
67     _x_offset_m(0),
68     _y_offset_m(0),
69     _z_offset_m(0),
70     _heading_offset_deg(0),
71     _pitch_offset_deg(0),
72     _roll_offset_deg(0),
73     _goal_heading_offset_deg(0.0),
74     _goal_pitch_offset_deg(0.0)
75 {
76     sgdZeroVec3(_absolute_view_pos);
77     //a reasonable guess for init, so that the math doesn't blow up
78 }
79
80
81 // Destructor
82 FGViewer::~FGViewer( void ) {
83 }
84
85 void
86 FGViewer::init ()
87 {
88   if ( _type == FG_LOOKAT ) {
89       set_reverse_view_offset(true);
90   }
91
92   if ( _type == FG_RPH ) {
93       set_reverse_view_offset(false);
94   }
95 }
96
97 void
98 FGViewer::bind ()
99 {
100 }
101
102 void
103 FGViewer::unbind ()
104 {
105 }
106
107 void
108 FGViewer::setType ( int type )
109 {
110   if (type == 0)
111     _type = FG_RPH;
112   if (type == 1)
113     _type = FG_LOOKAT;
114 }
115
116 void
117 FGViewer::setLongitude_deg (double lon_deg)
118 {
119   _dirty = true;
120   _lon_deg = lon_deg;
121 }
122
123 void
124 FGViewer::setLatitude_deg (double lat_deg)
125 {
126   _dirty = true;
127   _lat_deg = lat_deg;
128 }
129
130 void
131 FGViewer::setAltitude_ft (double alt_ft)
132 {
133   _dirty = true;
134   _alt_ft = alt_ft;
135 }
136
137 void
138 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
139 {
140   _dirty = true;
141   _lon_deg = lon_deg;
142   _lat_deg = lat_deg;
143   _alt_ft = alt_ft;
144 }
145
146 void
147 FGViewer::setTargetLongitude_deg (double lon_deg)
148 {
149   _dirty = true;
150   _target_lon_deg = lon_deg;
151 }
152
153 void
154 FGViewer::setTargetLatitude_deg (double lat_deg)
155 {
156   _dirty = true;
157   _target_lat_deg = lat_deg;
158 }
159
160 void
161 FGViewer::setTargetAltitude_ft (double alt_ft)
162 {
163   _dirty = true;
164   _target_alt_ft = alt_ft;
165 }
166
167 void
168 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
169 {
170   _dirty = true;
171   _target_lon_deg = lon_deg;
172   _target_lat_deg = lat_deg;
173   _target_alt_ft = alt_ft;
174 }
175
176 void
177 FGViewer::setRoll_deg (double roll_deg)
178 {
179   _dirty = true;
180   _roll_deg = roll_deg;
181 }
182
183 void
184 FGViewer::setPitch_deg (double pitch_deg)
185 {
186   _dirty = true;
187   _pitch_deg = pitch_deg;
188 }
189
190 void
191 FGViewer::setHeading_deg (double heading_deg)
192 {
193   _dirty = true;
194   _heading_deg = heading_deg;
195 }
196
197 void
198 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
199 {
200   _dirty = true;
201   _roll_deg = roll_deg;
202   _pitch_deg = pitch_deg;
203   _heading_deg = heading_deg;
204 }
205
206 void
207 FGViewer::setXOffset_m (double x_offset_m)
208 {
209   _dirty = true;
210   _x_offset_m = x_offset_m;
211 }
212
213 void
214 FGViewer::setYOffset_m (double y_offset_m)
215 {
216   _dirty = true;
217   _y_offset_m = y_offset_m;
218 }
219
220 void
221 FGViewer::setZOffset_m (double z_offset_m)
222 {
223   _dirty = true;
224   _z_offset_m = z_offset_m;
225 }
226
227 void
228 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
229 {
230   _dirty = true;
231   _x_offset_m = x_offset_m;
232   _y_offset_m = y_offset_m;
233   _z_offset_m = z_offset_m;
234 }
235
236 void
237 FGViewer::setRollOffset_deg (double roll_offset_deg)
238 {
239   _dirty = true;
240   _roll_offset_deg = roll_offset_deg;
241 }
242
243 void
244 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
245 {
246   _dirty = true;
247   _pitch_offset_deg = pitch_offset_deg;
248 }
249
250 void
251 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
252 {
253   _dirty = true;
254   _heading_offset_deg = heading_offset_deg;
255 }
256
257 void
258 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
259 {
260   _dirty = true;
261   _goal_roll_offset_deg = goal_roll_offset_deg;
262 }
263
264 void
265 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
266 {
267   _dirty = true;
268   _goal_pitch_offset_deg = goal_pitch_offset_deg;
269   if ( _goal_pitch_offset_deg < -90 ) {
270     _goal_pitch_offset_deg = -90.0;
271   }
272   if ( _goal_pitch_offset_deg > 90.0 ) {
273     _goal_pitch_offset_deg = 90.0;
274   }
275
276 }
277
278 void
279 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
280 {
281   _dirty = true;
282   _goal_heading_offset_deg = goal_heading_offset_deg;
283   while ( _goal_heading_offset_deg < 0.0 ) {
284     _goal_heading_offset_deg += 360;
285   }
286   while ( _goal_heading_offset_deg > 360 ) {
287     _goal_heading_offset_deg -= 360;
288   }
289 }
290
291 void
292 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
293 {
294   _dirty = true;
295   _roll_offset_deg = roll_offset_deg;
296   _pitch_offset_deg = pitch_offset_deg;
297   _heading_offset_deg = heading_offset_deg;
298 }
299
300 double *
301 FGViewer::get_absolute_view_pos () 
302 {
303   if (_dirty)
304     recalc();
305   return _absolute_view_pos;
306 }
307
308 float *
309 FGViewer::getRelativeViewPos () 
310 {
311   if (_dirty)
312     recalc();
313   return _relative_view_pos;
314 }
315
316 float *
317 FGViewer::getZeroElevViewPos () 
318 {
319   if (_dirty)
320     recalc();
321   return _zero_elev_view_pos;
322 }
323
324
325 // recalc() is done every time one of the setters is called (making the 
326 // cached data "dirty") on the next "get".  It calculates all the outputs 
327 // for viewer.
328 void
329 FGViewer::recalc ()
330 {
331   sgVec3 minus_z, right, forward, tilt;
332   sgMat4 tmpROT;  // temp rotation work matrices
333   sgMat4 VIEW_HEADINGOFFSET, VIEW_PITCHOFFSET;
334   sgVec3 tmpVec3;  // temp work vector (3)
335
336
337   // The position vectors originate from the view point or target location
338   // depending on the type of view.
339   // FIXME: In particular this routine will need to support both locations
340   //        and chase view (aka lookat) is only unique in that the 
341   //        eye position is calculated in relation to the object's position.
342   // FIXME: Later note: actually the object (target) info needs to be held
343   //        by the model class.
344   if (_type == FG_RPH) {
345     // position is the location of the pilot
346     recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
347     // Make the world up rotation matrix for rph
348     sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
349   } else {
350     // position is the location of the object being looked at
351     recalcPositionVectors( _target_lon_deg, _target_lat_deg, _target_alt_ft );
352     // Make the world up rotation matrix for lookat
353     sgMakeRotMat4( UP, _target_lon_deg, 0.0, -_target_lat_deg );
354   }
355   // the coordinates generated by the above "recalcPositionVectors"
356   sgCopyVec3(_zero_elev, _zero_elev_view_pos);
357   sgCopyVec3(_view_pos, _relative_view_pos);
358
359
360
361   // get the world up radial vector from planet center
362   // (ie. effect of aircraft location on earth "sphere" approximation)
363   sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
364
365
366
367   // Creat local matrix with current geodetic position.  Converting
368   // the orientation (pitch/roll/heading) to vectors.
369   fgMakeLOCAL( LOCAL, _pitch_deg * SG_DEGREES_TO_RADIANS,
370                       _roll_deg * SG_DEGREES_TO_RADIANS,
371                       -_heading_deg * SG_DEGREES_TO_RADIANS);
372   // Adjust LOCAL to current world_up vector (adjustment for planet location)
373   sgPostMultMat4( LOCAL, UP );
374   // make sg vectors view up, right and forward vectors from LOCAL
375   sgSetVec3( _view_up, LOCAL[0][0], LOCAL[0][1], LOCAL[0][2] );
376   sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
377   sgSetVec3( forward, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
378
379
380
381   // create xyz offsets Vector
382   sgVec3 position_offset;
383   sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
384
385
386
387   // generate the heading offset matrix using heading_offset angle(s)
388   if (_type == FG_LOOKAT) {
389     // Note that when in "chase view" the offset is in relation to the 
390     // orientation heading (_heading_deg) of the model being looked at as 
391     // it is used to rotate around the model.
392     sgMakeRotMat4( VIEW_HEADINGOFFSET, _heading_offset_deg -_heading_deg, _world_up );
393   }
394   if (_type == FG_RPH) {
395     // generate the view offset matrix using orientation offset (heading)
396     sgMakeRotMat4( VIEW_HEADINGOFFSET, _heading_offset_deg, _view_up );
397   }
398
399
400   // create a tilt matrix using orientation offset (pitch)
401   sgMakeRotMat4( VIEW_PITCHOFFSET, _pitch_offset_deg, right );
402
403   sgCopyMat4(VIEW_OFFSET, VIEW_HEADINGOFFSET);
404   sgPreMultMat4(VIEW_OFFSET, VIEW_PITCHOFFSET);
405
406
407   if (_type == FG_LOOKAT) {
408    
409     // transfrom "offset" and "orientation offset" to vector
410     sgXformVec3( position_offset, position_offset, UP );
411     sgXformVec3( position_offset, position_offset, VIEW_HEADINGOFFSET );
412     sgXformPnt3( position_offset, position_offset, VIEW_PITCHOFFSET );
413
414     sgVec3 object_pos, eye_pos;
415     // copy to coordinates to object...
416     sgCopyVec3( object_pos, _view_pos );
417
418     // add the offsets from object to the coordinates to get "eye"
419     sgAddVec3( eye_pos, _view_pos, position_offset );
420
421     // Make the VIEW matrix for "lookat".
422     sgMakeLookAtMat4( VIEW, eye_pos, object_pos, _view_up );
423   }
424
425   if (_type == FG_RPH) {
426
427     sgXformVec3( position_offset, position_offset, LOCAL);
428     // add the offsets including rotations to the coordinates
429     sgAddVec3( _view_pos, position_offset );
430
431     // Make the VIEW matrix.
432     VIEW[0][0] = right[0];
433     VIEW[0][1] = right[1];
434     VIEW[0][2] = right[2];
435     VIEW[1][0] = forward[0];
436     VIEW[1][1] = forward[1];
437     VIEW[1][2] = forward[2];
438     VIEW[2][0] = _view_up[0];
439     VIEW[2][1] = _view_up[1];
440     VIEW[2][2] = _view_up[2];
441     // multiply the OFFSETS (for heading and pitch) into the VIEW
442     sgPostMultMat4(VIEW, VIEW_OFFSET);
443
444     // add the position data to the matrix
445     VIEW[3][0] = _view_pos[0];
446     VIEW[3][1] = _view_pos[1];
447     VIEW[3][2] = _view_pos[2];
448     VIEW[3][3] = 1.0f;
449
450
451     // copy the LOCAL matrix to COCKPIT_ROT for publication...
452     sgCopyMat4( COCKPIT_ROT, LOCAL );
453   }
454
455   // the VIEW matrix includes both rotation and translation.  Let's
456   // knock out the translation part to make the VIEW_ROT matrix
457   sgCopyMat4( VIEW_ROT, VIEW );
458   VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
459
460   // Given a vector pointing straight down (-Z), map into onto the
461   // local plane representing "horizontal".  This should give us the
462   // local direction for moving "south".
463   sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
464
465   sgmap_vec_onto_cur_surface_plane(_world_up, _view_pos, minus_z,
466                                      _surface_south);
467   sgNormalizeVec3(_surface_south);
468
469   // now calculate the surface east vector
470   sgVec3 world_down;
471   sgNegateVec3(world_down, _world_up);
472   sgVectorProductVec3(_surface_east, _surface_south, world_down);
473
474   set_clean();
475 }
476
477 void
478 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
479 {
480   double sea_level_radius_m;
481   double lat_geoc_rad;
482
483
484                                 // Convert from geodetic to geocentric
485                                 // coordinates.
486   sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
487                alt_ft * SG_FEET_TO_METER,
488                &sea_level_radius_m,
489                &lat_geoc_rad);
490
491                                 // Calculate the cartesian coordinates
492                                 // of point directly below at sea level.
493                                 // aka Zero Elevation Position
494   Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
495                       lat_geoc_rad,
496                       sea_level_radius_m);
497   Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
498   sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
499
500                                 // Calculate the absolute view position
501                                 // in fgfs coordinates.
502                                 // aka Absolute View Position
503   p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
504   tmp = sgPolarToCart3d(p);
505   sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
506
507                                 // Calculate the relative view position
508                                 // from the scenery center.
509                                 // aka Relative View Position
510   sgdVec3 scenery_center;
511   sgdSetVec3(scenery_center,
512              scenery.get_next_center().x(),
513              scenery.get_next_center().y(),
514              scenery.get_next_center().z());
515   sgdVec3 view_pos;
516   sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
517   sgSetVec3(_relative_view_pos, view_pos);
518
519 }
520
521 double
522 FGViewer::get_h_fov()
523 {
524     switch (_scaling_type) {
525     case FG_SCALING_WIDTH:  // h_fov == fov
526         return _fov_deg;
527     case FG_SCALING_MAX:
528         if (_aspect_ratio < 1.0) {
529             // h_fov == fov
530             return _fov_deg;
531         } else {
532             // v_fov == fov
533             return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
534                 SG_RADIANS_TO_DEGREES * 2;
535         }
536     default:
537         assert(false);
538     }
539 }
540
541 double
542 FGViewer::get_v_fov()
543 {
544     switch (_scaling_type) {
545     case FG_SCALING_WIDTH:  // h_fov == fov
546         return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
547             SG_RADIANS_TO_DEGREES * 2;
548     case FG_SCALING_MAX:
549         if (_aspect_ratio < 1.0) {
550             // h_fov == fov
551             return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
552                 SG_RADIANS_TO_DEGREES * 2;
553         } else {
554             // v_fov == fov
555             return _fov_deg;
556         }
557     default:
558         assert(false);
559     }
560 }
561
562 void
563 FGViewer::update (int dt)
564 {
565   int i;
566   for ( i = 0; i < dt; i++ ) {
567     if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
568       setHeadingOffset_deg( _goal_heading_offset_deg );
569       break;
570     } else {
571       // move current_view.headingoffset towards
572       // current_view.goal_view_offset
573       if ( _goal_heading_offset_deg > _heading_offset_deg )
574         {
575           if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
576             incHeadingOffset_deg( 0.5 );
577           } else {
578             incHeadingOffset_deg( -0.5 );
579           }
580         } else {
581           if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
582             incHeadingOffset_deg( -0.5 );
583           } else {
584             incHeadingOffset_deg( 0.5 );
585           }
586         }
587       if ( _heading_offset_deg > 360 ) {
588         incHeadingOffset_deg( -360 );
589       } else if ( _heading_offset_deg < 0 ) {
590         incHeadingOffset_deg( 360 );
591       }
592     }
593   }
594
595   for ( i = 0; i < dt; i++ ) {
596     if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
597       setPitchOffset_deg( _goal_pitch_offset_deg );
598       break;
599     } else {
600       // move current_view.pitch_offset_deg towards
601       // current_view.goal_pitch_offset
602       if ( _goal_pitch_offset_deg > _pitch_offset_deg )
603         {
604           incPitchOffset_deg( 1.0 );
605         } else {
606             incPitchOffset_deg( -1.0 );
607         }
608       if ( _pitch_offset_deg > 90 ) {
609         setPitchOffset_deg(90);
610       } else if ( _pitch_offset_deg < -90 ) {
611         setPitchOffset_deg( -90 );
612       }
613     }
614   }
615 }
616
617 void FGViewer::fgMakeLOCAL( sgMat4 dst, const double Theta,
618                                 const double Phi, const double Psi)
619 {
620     SGfloat cosTheta = (SGfloat) cos(Theta);
621     SGfloat sinTheta = (SGfloat) sin(Theta);
622     SGfloat cosPhi   = (SGfloat) cos(Phi);
623     SGfloat sinPhi   = (SGfloat) sin(Phi);
624     SGfloat sinPsi   = (SGfloat) sin(Psi) ;
625     SGfloat cosPsi   = (SGfloat) cos(Psi) ;
626         
627     dst[0][0] = cosPhi * cosTheta;
628     dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
629     dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
630     dst[0][3] = SG_ZERO;
631
632     dst[1][0] = -sinPhi * cosTheta;
633     dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
634     dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
635     dst[1][3] = SG_ZERO ;
636         
637     dst[2][0] = sinTheta;
638     dst[2][1] = cosTheta * -sinPsi;
639     dst[2][2] = cosTheta * cosPsi;
640     dst[2][3] = SG_ZERO;
641         
642     dst[3][0] = SG_ZERO;
643     dst[3][1] = SG_ZERO;
644     dst[3][2] = SG_ZERO;
645     dst[3][3] = SG_ONE ;
646 }
647
648