1 // viewer_lookat.hxx -- class for managing a "look at" viewer in
2 // the flightgear world.
4 // Written by Curtis Olson, started October 2000.
6 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
25 #include <simgear/compiler.h>
31 #include <plib/ssg.h> // plib include
33 #include <simgear/constants.h>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/math/point3d.hxx>
36 #include <simgear/math/polar3d.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38 #include <simgear/math/vector.hxx>
40 #include <Scenery/scenery.hxx>
42 #include "globals.hxx"
43 #include "viewer_lookat.hxx"
47 FGViewerLookAt::FGViewerLookAt( void )
49 set_reverse_view_offset(true);
53 void fgMakeLookAtMat4 ( sgMat4 dst, const sgVec3 eye, const sgVec3 center,
57 // 1) In order to compute the line of sight, the eye point must not be equal
58 // to the center point.
59 // 2) The up vector must not be parallel to the line of sight from the eye
60 // to the center point.
62 /* Compute the direction vectors */
65 /* Y vector = center - eye */
66 sgSubVec3 ( y, center, eye ) ;
69 sgCopyVec3 ( z, up ) ;
71 /* X vector = Y cross Z */
72 sgVectorProductVec3 ( x, y, z ) ;
74 /* Recompute Z = X cross Y */
75 sgVectorProductVec3 ( z, x, y ) ;
77 /* Normalize everything */
78 sgNormaliseVec3 ( x ) ;
79 sgNormaliseVec3 ( y ) ;
80 sgNormaliseVec3 ( z ) ;
82 /* Build the matrix */
83 #define M(row,col) dst[row][col]
84 M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
85 M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
86 M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
87 M(3,0) = eye[0]; M(3,1) = eye[1]; M(3,2) = eye[2]; M(3,3) = 1.0;
92 // Update the view parameters
93 void FGViewerLookAt::update() {
96 view_point.setPosition(geod_view_pos[0] * SGD_RADIANS_TO_DEGREES,
97 geod_view_pos[1] * SGD_RADIANS_TO_DEGREES,
98 geod_view_pos[2] * SG_METER_TO_FEET);
99 sgCopyVec3(zero_elev, view_point.getZeroElevViewPos());
100 sgdCopyVec3(abs_view_pos, view_point.getAbsoluteViewPos());
101 sgCopyVec3(view_pos, view_point.getRelativeViewPos());
104 sgCopyVec3( tmp_offset, pilot_offset );
105 SG_LOG( SG_VIEW, SG_DEBUG, "tmp offset = "
106 << tmp_offset[0] << "," << tmp_offset[1] << ","
109 //!!!!!!!!!!!!!!!!!!!
110 // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
111 // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
112 extern float GuiQuat_mat[4][4];
113 sgXformPnt3( tmp_offset, tmp_offset, GuiQuat_mat );
114 SG_LOG( SG_VIEW, SG_DEBUG, "tmp_offset = "
115 << tmp_offset[0] << "," << tmp_offset[1] << ","
118 sgAddVec3( view_pos, tmp_offset );
119 // !!!!!!!!!! testing
121 // Make the VIEW matrix.
122 fgMakeLookAtMat4( VIEW, view_pos, view_forward, view_up );
124 // the VIEW matrix includes both rotation and translation. Let's
125 // knock out the translation part to make the VIEW_ROT matrix
126 sgCopyMat4( VIEW_ROT, VIEW );
127 VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
129 // Make the world up rotation matrix
131 geod_view_pos[0] * SGD_RADIANS_TO_DEGREES,
133 -geod_view_pos[1] * SGD_RADIANS_TO_DEGREES );
135 // use a clever observation into the nature of our tranformation
136 // matrix to grab the world_up vector
137 sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
139 // Given a vector pointing straight down (-Z), map into onto the
140 // local plane representing "horizontal". This should give us the
141 // local direction for moving "south".
142 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
144 sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
146 sgNormalizeVec3(surface_south);
148 // now calculate the surface east vector
150 sgNegateVec3(world_down, world_up);
151 sgVectorProductVec3(surface_east, surface_south, world_down);
158 FGViewerLookAt::~FGViewerLookAt( void ) {