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 )
51 static void fgLookAt( sgVec3 eye, sgVec3 center, sgVec3 up, sgMat4 &m ) {
52 double x[3], y[3], z[3];
55 /* Make rotation matrix */
58 z[0] = eye[0] - center[0];
59 z[1] = eye[1] - center[1];
60 z[2] = eye[2] - center[2];
61 mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
62 if (mag) { /* mpichler, 19950515 */
73 /* X vector = Y cross Z */
74 x[0] = y[1]*z[2] - y[2]*z[1];
75 x[1] = -y[0]*z[2] + y[2]*z[0];
76 x[2] = y[0]*z[1] - y[1]*z[0];
78 /* Recompute Y = Z cross X */
79 y[0] = z[1]*x[2] - z[2]*x[1];
80 y[1] = -z[0]*x[2] + z[2]*x[0];
81 y[2] = z[0]*x[1] - z[1]*x[0];
83 /* mpichler, 19950515 */
84 /* cross product gives area of parallelogram, which is < 1.0 for
85 * non-perpendicular unit-length vectors; so normalize x, y here
88 mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
95 mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
102 #define M(row,col) m[row][col]
103 M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
104 M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
105 M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
106 M(3,0) = -eye[0]; M(3,1) = -eye[1]; M(3,2) = -eye[2]; M(3,3) = 1.0;
111 // convert sgMat4 to MAT3 and print
112 static void print_sgMat4( sgMat4 &in) {
114 for ( i = 0; i < 4; i++ ) {
115 for ( j = 0; j < 4; j++ ) {
116 printf("%10.4f ", in[i][j]);
123 // Update the view parameters
124 void FGViewerLookAt::update() {
126 sgVec3 minus_z, forward;
129 // calculate the cartesion coords of the current lat/lon/0 elev
130 Point3D p = Point3D( geod_view_pos[0],
134 tmp = sgPolarToCart3d(p) - scenery.center;
135 sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
137 // calculate view position in current FG view coordinate system
138 // p.lon & p.lat are already defined earlier, p.radius was set to
139 // the sea level radius, so now we add in our altitude.
140 if ( geod_view_pos[2] > (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
141 p.setz( p.radius() + geod_view_pos[2] );
143 p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
146 tmp = sgPolarToCart3d(p);
147 sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
149 // view_pos = abs_view_pos - scenery.center;
151 sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
153 sgdSubVec3( vp, abs_view_pos, sc );
154 sgSetVec3( view_pos, vp );
156 FG_LOG( FG_VIEW, FG_DEBUG, "sea level radius = " << sea_level_radius );
157 FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
158 FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = "
159 << abs_view_pos[0] << ","
160 << abs_view_pos[1] << ","
161 << abs_view_pos[2] );
162 FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = "
163 << view_pos[0] << "," << view_pos[1] << "," << view_pos[2] );
164 FG_LOG( FG_VIEW, FG_DEBUG, "view forward = "
165 << view_forward[0] << "," << view_forward[1] << ","
166 << view_forward[2] );
167 FG_LOG( FG_VIEW, FG_DEBUG, "view up = "
168 << view_up[0] << "," << view_up[1] << ","
171 // Make the VIEW matrix.
172 fgLookAt( view_pos, view_forward, view_up, VIEW );
173 // cout << "VIEW matrix" << endl;
174 // print_sgMat4( VIEW );
176 // the VIEW matrix includes both rotation and translation. Let's
177 // knock out the translation part to make the VIEW_ROT matrix
178 sgCopyMat4( VIEW_ROT, VIEW );
179 VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
181 // Make the world up rotation matrix
183 geod_view_pos[0] * RAD_TO_DEG,
185 -geod_view_pos[1] * RAD_TO_DEG );
187 // use a clever observation into the nature of our tranformation
188 // matrix to grab the world_up vector
189 sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
190 // cout << "World Up = " << world_up[0] << "," << world_up[1] << ","
191 // << world_up[2] << endl;
194 //!!!!!!!!!!!!!!!!!!!
195 // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
196 // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
197 // this in gui.cxx for now just testing
198 extern float quat_mat[4][4];
199 sgPreMultMat4( VIEW, quat_mat);
200 // !!!!!!!!!! testing
202 // Given a vector pointing straight down (-Z), map into onto the
203 // local plane representing "horizontal". This should give us the
204 // local direction for moving "south".
205 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
207 sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
209 sgNormalizeVec3(surface_south);
210 // cout << "Surface direction directly south " << surface_south[0] << ","
211 // << surface_south[1] << "," << surface_south[2] << endl;
213 // now calculate the surface east vector
214 #define USE_FAST_SURFACE_EAST
215 #ifdef USE_FAST_SURFACE_EAST
217 sgNegateVec3(world_down, world_up);
218 sgVectorProductVec3(surface_east, surface_south, world_down);
220 sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, world_up );
221 // cout << "sgMat4 TMP" << endl;
222 // print_sgMat4( TMP );
223 sgXformVec3(surface_east, surface_south, TMP);
224 #endif // USE_FAST_SURFACE_EAST
225 // cout << "Surface direction directly east " << surface_east[0] << ","
226 // << surface_east[1] << "," << surface_east[2] << endl;
227 // cout << "Should be close to zero = "
228 // << sgScalarProductVec3(surface_south, surface_east) << endl;
235 FGViewerLookAt::~FGViewerLookAt( void ) {