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/vector.hxx>
39 #include <Scenery/scenery.hxx>
41 #include "globals.hxx"
42 #include "viewer_lookat.hxx"
46 FGViewerLookAt::FGViewerLookAt( void )
48 set_reverse_view_offset(true);
52 void fgMakeLookAtMat4 ( sgMat4 dst, const sgVec3 eye, const sgVec3 center,
56 // 1) In order to compute the line of sight, the eye point must not be equal
57 // to the center point.
58 // 2) The up vector must not be parallel to the line of sight from the eye
59 // to the center point.
61 /* Compute the direction vectors */
64 /* Y vector = center - eye */
65 sgSubVec3 ( y, center, eye ) ;
68 sgCopyVec3 ( z, up ) ;
70 /* X vector = Y cross Z */
71 sgVectorProductVec3 ( x, y, z ) ;
73 /* Recompute Z = X cross Y */
74 sgVectorProductVec3 ( z, x, y ) ;
76 /* Normalize everything */
77 sgNormaliseVec3 ( x ) ;
78 sgNormaliseVec3 ( y ) ;
79 sgNormaliseVec3 ( z ) ;
81 /* Build the matrix */
82 #define M(row,col) dst[row][col]
83 M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
84 M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
85 M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
86 M(3,0) = eye[0]; M(3,1) = eye[1]; M(3,2) = eye[2]; M(3,3) = 1.0;
91 // convert sgMat4 to MAT3 and print
92 static void print_sgMat4( sgMat4 &in) {
94 for ( i = 0; i < 4; i++ ) {
95 for ( j = 0; j < 4; j++ ) {
96 printf("%10.4f ", in[i][j]);
103 // Update the view parameters
104 void FGViewerLookAt::update() {
108 // calculate the cartesion coords of the current lat/lon/0 elev
109 Point3D p = Point3D( geod_view_pos[0],
113 tmp = sgPolarToCart3d(p) - scenery.center;
114 sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
116 // calculate view position in current FG view coordinate system
117 // p.lon & p.lat are already defined earlier, p.radius was set to
118 // the sea level radius, so now we add in our altitude.
119 if ( geod_view_pos[2] > (scenery.cur_elev + 0.5 * SG_METER_TO_FEET) ) {
120 p.setz( p.radius() + geod_view_pos[2] );
122 p.setz( p.radius() + scenery.cur_elev + 0.5 * SG_METER_TO_FEET );
125 tmp = sgPolarToCart3d(p);
126 sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
128 // view_pos = abs_view_pos - scenery.center;
130 sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
132 sgdSubVec3( vp, abs_view_pos, sc );
133 sgSetVec3( view_pos, vp );
134 sgAddVec3( view_pos, pilot_offset );
136 SG_LOG( SG_VIEW, SG_DEBUG, "sea level radius = " << sea_level_radius );
137 SG_LOG( SG_VIEW, SG_DEBUG, "Polar view pos = " << p );
138 SG_LOG( SG_VIEW, SG_DEBUG, "Absolute view pos = "
139 << abs_view_pos[0] << ","
140 << abs_view_pos[1] << ","
141 << abs_view_pos[2] );
142 SG_LOG( SG_VIEW, SG_DEBUG, "Relative view pos = "
143 << view_pos[0] << "," << view_pos[1] << "," << view_pos[2] );
144 SG_LOG( SG_VIEW, SG_DEBUG, "pilot offset = "
145 << pilot_offset[0] << "," << pilot_offset[1] << ","
146 << pilot_offset[2] );
147 SG_LOG( SG_VIEW, SG_DEBUG, "view forward = "
148 << view_forward[0] << "," << view_forward[1] << ","
149 << view_forward[2] );
150 SG_LOG( SG_VIEW, SG_DEBUG, "view up = "
151 << view_up[0] << "," << view_up[1] << ","
154 // Make the VIEW matrix.
155 fgMakeLookAtMat4( VIEW, view_pos, view_forward, view_up );
156 // cout << "VIEW matrix" << endl;
157 // print_sgMat4( VIEW );
159 // the VIEW matrix includes both rotation and translation. Let's
160 // knock out the translation part to make the VIEW_ROT matrix
161 sgCopyMat4( VIEW_ROT, VIEW );
162 VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
164 // Make the world up rotation matrix
166 geod_view_pos[0] * SGD_RADIANS_TO_DEGREES,
168 -geod_view_pos[1] * SGD_RADIANS_TO_DEGREES );
170 // use a clever observation into the nature of our tranformation
171 // matrix to grab the world_up vector
172 sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
173 // cout << "World Up = " << world_up[0] << "," << world_up[1] << ","
174 // << world_up[2] << endl;
177 //!!!!!!!!!!!!!!!!!!!
178 // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
179 // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
180 // this in gui.cxx for now just testing
181 extern float GuiQuat_mat[4][4];
182 sgPreMultMat4( VIEW, GuiQuat_mat);
183 // !!!!!!!!!! testing
185 // Given a vector pointing straight down (-Z), map into onto the
186 // local plane representing "horizontal". This should give us the
187 // local direction for moving "south".
188 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
190 sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
192 sgNormalizeVec3(surface_south);
193 // cout << "Surface direction directly south " << surface_south[0] << ","
194 // << surface_south[1] << "," << surface_south[2] << endl;
196 // now calculate the surface east vector
197 #define USE_FAST_SURFACE_EAST
198 #ifdef USE_FAST_SURFACE_EAST
200 sgNegateVec3(world_down, world_up);
201 sgVectorProductVec3(surface_east, surface_south, world_down);
203 sgMakeRotMat4( TMP, SGD_PI_2 * SGD_RADIANS_TO_DEGREES, world_up );
204 // cout << "sgMat4 TMP" << endl;
205 // print_sgMat4( TMP );
206 sgXformVec3(surface_east, surface_south, TMP);
207 #endif // USE_FAST_SURFACE_EAST
208 // cout << "Surface direction directly east " << surface_east[0] << ","
209 // << surface_east[1] << "," << surface_east[2] << endl;
210 // cout << "Should be close to zero = "
211 // << sgScalarProductVec3(surface_south, surface_east) << endl;
218 FGViewerLookAt::~FGViewerLookAt( void ) {