1 // viewer.cxx -- class for managing a viewer in the flightgear world.
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
8 // Copyright (C) 1997 - 2000 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
27 #include <simgear/compiler.h>
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>
39 #include <Scenery/scenery.hxx>
42 #include <simgear/math/vector.hxx>
43 #include "globals.hxx"
49 ////////////////////////////////////////////////////////////////////////
50 // Implementation of FGViewer.
51 ////////////////////////////////////////////////////////////////////////
54 FGViewer::FGViewer( void ):
55 _scaling_type(FG_SCALING_MAX),
70 _heading_offset_deg(0),
73 _goal_heading_offset_deg(0.0),
74 _goal_pitch_offset_deg(0.0)
76 sgdZeroVec3(_absolute_view_pos);
77 //a reasonable guess for init, so that the math doesn't blow up
82 FGViewer::~FGViewer( void ) {
88 if ( _type == FG_LOOKAT ) {
89 set_reverse_view_offset(true);
92 if ( _type == FG_RPH ) {
93 set_reverse_view_offset(false);
108 FGViewer::setType ( int type )
117 FGViewer::setLongitude_deg (double lon_deg)
124 FGViewer::setLatitude_deg (double lat_deg)
131 FGViewer::setAltitude_ft (double alt_ft)
138 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
147 FGViewer::setTargetLongitude_deg (double lon_deg)
150 _target_lon_deg = lon_deg;
154 FGViewer::setTargetLatitude_deg (double lat_deg)
157 _target_lat_deg = lat_deg;
161 FGViewer::setTargetAltitude_ft (double alt_ft)
164 _target_alt_ft = alt_ft;
168 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
171 _target_lon_deg = lon_deg;
172 _target_lat_deg = lat_deg;
173 _target_alt_ft = alt_ft;
177 FGViewer::setRoll_deg (double roll_deg)
180 _roll_deg = roll_deg;
184 FGViewer::setPitch_deg (double pitch_deg)
187 _pitch_deg = pitch_deg;
191 FGViewer::setHeading_deg (double heading_deg)
194 _heading_deg = heading_deg;
198 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
201 _roll_deg = roll_deg;
202 _pitch_deg = pitch_deg;
203 _heading_deg = heading_deg;
207 FGViewer::setXOffset_m (double x_offset_m)
210 _x_offset_m = x_offset_m;
214 FGViewer::setYOffset_m (double y_offset_m)
217 _y_offset_m = y_offset_m;
221 FGViewer::setZOffset_m (double z_offset_m)
224 _z_offset_m = z_offset_m;
228 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
231 _x_offset_m = x_offset_m;
232 _y_offset_m = y_offset_m;
233 _z_offset_m = z_offset_m;
237 FGViewer::setRollOffset_deg (double roll_offset_deg)
240 _roll_offset_deg = roll_offset_deg;
244 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
247 _pitch_offset_deg = pitch_offset_deg;
251 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
254 _heading_offset_deg = heading_offset_deg;
258 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
261 _goal_roll_offset_deg = goal_roll_offset_deg;
265 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
268 _goal_pitch_offset_deg = goal_pitch_offset_deg;
269 if ( _goal_pitch_offset_deg < -90 ) {
270 _goal_pitch_offset_deg = -90.0;
272 if ( _goal_pitch_offset_deg > 90.0 ) {
273 _goal_pitch_offset_deg = 90.0;
279 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
282 _goal_heading_offset_deg = goal_heading_offset_deg;
283 while ( _goal_heading_offset_deg < 0.0 ) {
284 _goal_heading_offset_deg += 360;
286 while ( _goal_heading_offset_deg > 360 ) {
287 _goal_heading_offset_deg -= 360;
292 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
295 _roll_offset_deg = roll_offset_deg;
296 _pitch_offset_deg = pitch_offset_deg;
297 _heading_offset_deg = heading_offset_deg;
301 FGViewer::get_absolute_view_pos ()
305 return _absolute_view_pos;
309 FGViewer::getRelativeViewPos ()
313 return _relative_view_pos;
317 FGViewer::getZeroElevViewPos ()
321 return _zero_elev_view_pos;
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
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)
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 );
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 );
355 // the coordinates generated by the above "recalcPositionVectors"
356 sgCopyVec3(_zero_elev, _zero_elev_view_pos);
357 sgCopyVec3(_view_pos, _relative_view_pos);
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] );
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] );
381 // create xyz offsets Vector
382 sgVec3 position_offset;
383 sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
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 );
394 if (_type == FG_RPH) {
395 // generate the view offset matrix using orientation offset (heading)
396 sgMakeRotMat4( VIEW_HEADINGOFFSET, _heading_offset_deg, _view_up );
400 // create a tilt matrix using orientation offset (pitch)
401 sgMakeRotMat4( VIEW_PITCHOFFSET, _pitch_offset_deg, right );
403 sgCopyMat4(VIEW_OFFSET, VIEW_HEADINGOFFSET);
404 sgPreMultMat4(VIEW_OFFSET, VIEW_PITCHOFFSET);
407 if (_type == FG_LOOKAT) {
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 );
414 sgVec3 object_pos, eye_pos;
415 // copy to coordinates to object...
416 sgCopyVec3( object_pos, _view_pos );
418 // add the offsets from object to the coordinates to get "eye"
419 sgAddVec3( eye_pos, _view_pos, position_offset );
421 // Make the VIEW matrix for "lookat".
422 sgMakeLookAtMat4( VIEW, eye_pos, object_pos, _view_up );
425 if (_type == FG_RPH) {
427 sgXformVec3( position_offset, position_offset, LOCAL);
428 // add the offsets including rotations to the coordinates
429 sgAddVec3( _view_pos, position_offset );
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);
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];
451 // copy the LOCAL matrix to COCKPIT_ROT for publication...
452 sgCopyMat4( COCKPIT_ROT, LOCAL );
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;
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 );
465 sgmap_vec_onto_cur_surface_plane(_world_up, _view_pos, minus_z,
467 sgNormalizeVec3(_surface_south);
469 // now calculate the surface east vector
471 sgNegateVec3(world_down, _world_up);
472 sgVectorProductVec3(_surface_east, _surface_south, world_down);
478 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
480 double sea_level_radius_m;
484 // Convert from geodetic to geocentric
486 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
487 alt_ft * SG_FEET_TO_METER,
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,
497 Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
498 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
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]);
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());
516 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
517 sgSetVec3(_relative_view_pos, view_pos);
522 FGViewer::get_h_fov()
524 switch (_scaling_type) {
525 case FG_SCALING_WIDTH: // h_fov == fov
528 if (_aspect_ratio < 1.0) {
533 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
534 SG_RADIANS_TO_DEGREES * 2;
542 FGViewer::get_v_fov()
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;
549 if (_aspect_ratio < 1.0) {
551 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
552 SG_RADIANS_TO_DEGREES * 2;
563 FGViewer::update (int dt)
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 );
571 // move current_view.headingoffset towards
572 // current_view.goal_view_offset
573 if ( _goal_heading_offset_deg > _heading_offset_deg )
575 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
576 incHeadingOffset_deg( 0.5 );
578 incHeadingOffset_deg( -0.5 );
581 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
582 incHeadingOffset_deg( -0.5 );
584 incHeadingOffset_deg( 0.5 );
587 if ( _heading_offset_deg > 360 ) {
588 incHeadingOffset_deg( -360 );
589 } else if ( _heading_offset_deg < 0 ) {
590 incHeadingOffset_deg( 360 );
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 );
600 // move current_view.pitch_offset_deg towards
601 // current_view.goal_pitch_offset
602 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
604 incPitchOffset_deg( 1.0 );
606 incPitchOffset_deg( -1.0 );
608 if ( _pitch_offset_deg > 90 ) {
609 setPitchOffset_deg(90);
610 } else if ( _pitch_offset_deg < -90 ) {
611 setPitchOffset_deg( -90 );
617 void FGViewer::fgMakeLOCAL( sgMat4 dst, const double Theta,
618 const double Phi, const double Psi)
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) ;
627 dst[0][0] = cosPhi * cosTheta;
628 dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
629 dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
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 ;
637 dst[2][0] = sinTheta;
638 dst[2][1] = cosTheta * -sinPsi;
639 dst[2][2] = cosTheta * cosPsi;