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;
93 // convert sgMat4 to MAT3 and print
94 static void print_sgMat4( sgMat4 &in) {
96 for ( i = 0; i < 4; i++ ) {
97 for ( j = 0; j < 4; j++ ) {
98 printf("%10.4f ", in[i][j]);
106 // Update the view parameters
107 void FGViewerLookAt::update() {
111 // convert to geocentric coordinates
113 sgGeodToGeoc( geod_view_pos[1], geod_view_pos[2],
114 &sea_level_radius, &geoc_lat );
116 // calculate the cartesion coords of the current lat/lon/0 elev
117 Point3D p = Point3D( geod_view_pos[0], geoc_lat, sea_level_radius );
119 tmp = sgPolarToCart3d(p) - scenery.get_center();
120 sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
122 // calculate view position in current FG view coordinate system
123 // p.lon & p.lat are already defined earlier, p.radius was set to
124 // the sea level radius, so now we add in our altitude.
125 if ( geod_view_pos[2] > (scenery.get_cur_elev() + 0.5 * SG_METER_TO_FEET) ) {
126 p.setz( p.radius() + geod_view_pos[2] );
128 p.setz( p.radius() + scenery.get_cur_elev() + 0.5 * SG_METER_TO_FEET );
131 tmp = sgPolarToCart3d(p);
132 sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
134 // view_pos = abs_view_pos - scenery.center;
137 scenery.get_center().x(),
138 scenery.get_center().y(),
139 scenery.get_center().z() );
141 sgdSubVec3( vp, abs_view_pos, sc );
142 sgSetVec3( view_pos, vp );
145 sgCopyVec3( tmp_offset, pilot_offset );
146 SG_LOG( SG_VIEW, SG_DEBUG, "tmp offset = "
147 << tmp_offset[0] << "," << tmp_offset[1] << ","
150 //!!!!!!!!!!!!!!!!!!!
151 // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
152 // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
153 extern float GuiQuat_mat[4][4];
154 sgXformPnt3( tmp_offset, tmp_offset, GuiQuat_mat );
155 SG_LOG( SG_VIEW, SG_DEBUG, "tmp_offset = "
156 << tmp_offset[0] << "," << tmp_offset[1] << ","
159 sgAddVec3( view_pos, tmp_offset );
160 // !!!!!!!!!! testing
162 // sgAddVec3( view_pos, pilot_offset );
164 SG_LOG( SG_VIEW, SG_DEBUG, "sea level radius = " << sea_level_radius );
165 SG_LOG( SG_VIEW, SG_DEBUG, "Polar view pos = " << p );
166 SG_LOG( SG_VIEW, SG_DEBUG, "Absolute view pos = "
167 << abs_view_pos[0] << ","
168 << abs_view_pos[1] << ","
169 << abs_view_pos[2] );
170 SG_LOG( SG_VIEW, SG_DEBUG, "Relative view pos = "
171 << view_pos[0] << "," << view_pos[1] << "," << view_pos[2] );
172 SG_LOG( SG_VIEW, SG_DEBUG, "pilot offset = "
173 << pilot_offset[0] << "," << pilot_offset[1] << ","
174 << pilot_offset[2] );
175 SG_LOG( SG_VIEW, SG_DEBUG, "view forward = "
176 << view_forward[0] << "," << view_forward[1] << ","
177 << view_forward[2] );
178 SG_LOG( SG_VIEW, SG_DEBUG, "view up = "
179 << view_up[0] << "," << view_up[1] << ","
182 // Make the VIEW matrix.
183 fgMakeLookAtMat4( VIEW, view_pos, view_forward, view_up );
184 // cout << "VIEW matrix" << endl;
185 // print_sgMat4( VIEW );
187 // the VIEW matrix includes both rotation and translation. Let's
188 // knock out the translation part to make the VIEW_ROT matrix
189 sgCopyMat4( VIEW_ROT, VIEW );
190 VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
192 // Make the world up rotation matrix
194 geod_view_pos[0] * SGD_RADIANS_TO_DEGREES,
196 -geod_view_pos[1] * SGD_RADIANS_TO_DEGREES );
198 // use a clever observation into the nature of our tranformation
199 // matrix to grab the world_up vector
200 sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
201 // cout << "World Up = " << world_up[0] << "," << world_up[1] << ","
202 // << world_up[2] << endl;
205 // Given a vector pointing straight down (-Z), map into onto the
206 // local plane representing "horizontal". This should give us the
207 // local direction for moving "south".
208 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
210 sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
212 sgNormalizeVec3(surface_south);
213 // cout << "Surface direction directly south " << surface_south[0] << ","
214 // << surface_south[1] << "," << surface_south[2] << endl;
216 // now calculate the surface east vector
217 #define USE_FAST_SURFACE_EAST
218 #ifdef USE_FAST_SURFACE_EAST
220 sgNegateVec3(world_down, world_up);
221 sgVectorProductVec3(surface_east, surface_south, world_down);
223 sgMakeRotMat4( TMP, SGD_PI_2 * SGD_RADIANS_TO_DEGREES, world_up );
224 // cout << "sgMat4 TMP" << endl;
225 // print_sgMat4( TMP );
226 sgXformVec3(surface_east, surface_south, TMP);
227 #endif // USE_FAST_SURFACE_EAST
228 // cout << "Surface direction directly east " << surface_east[0] << ","
229 // << surface_east[1] << "," << surface_east[2] << endl;
230 // cout << "Should be close to zero = "
231 // << sgScalarProductVec3(surface_south, surface_east) << endl;
238 FGViewerLookAt::~FGViewerLookAt( void ) {