]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer_lookat.cxx
Fix a view bug that could make the aircraft appear rotated from an
[flightgear.git] / src / Main / viewer_lookat.cxx
1 // viewer_lookat.hxx -- class for managing a "look at" viewer in
2 //                      the flightgear world.
3 //
4 // Written by Curtis Olson, started October 2000.
5 //
6 // Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23
24
25 #include <simgear/compiler.h>
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <plib/ssg.h>           // plib include
32
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>
38
39 #include <Scenery/scenery.hxx>
40
41 #include "globals.hxx"
42 #include "viewer_lookat.hxx"
43
44
45 // Constructor
46 FGViewerLookAt::FGViewerLookAt( void )
47 {
48 }
49
50
51 void fgMakeLookAtMat4 ( sgMat4 dst, const sgVec3 eye, const sgVec3 center,
52                         const sgVec3 up )
53 {
54   // Caveats:
55   // 1) In order to compute the line of sight, the eye point must not be equal
56   //    to the center point.
57   // 2) The up vector must not be parallel to the line of sight from the eye
58   //    to the center point.
59
60   /* Compute the direction vectors */
61   sgVec3 x,y,z;
62
63   /* Y vector = center - eye */
64   sgSubVec3 ( y, center, eye ) ;
65
66   /* Z vector = up */
67   sgCopyVec3 ( z, up ) ;
68
69   /* X vector = Y cross Z */
70   sgVectorProductVec3 ( x, y, z ) ;
71
72   /* Recompute Z = X cross Y */
73   sgVectorProductVec3 ( z, x, y ) ;
74
75   /* Normalize everything */
76   sgNormaliseVec3 ( x ) ;
77   sgNormaliseVec3 ( y ) ;
78   sgNormaliseVec3 ( z ) ;
79
80   /* Build the matrix */
81 #define M(row,col)  dst[row][col]
82   M(0,0) = x[0];    M(0,1) = x[1];    M(0,2) = x[2];    M(0,3) = 0.0;
83   M(1,0) = y[0];    M(1,1) = y[1];    M(1,2) = y[2];    M(1,3) = 0.0;
84   M(2,0) = z[0];    M(2,1) = z[1];    M(2,2) = z[2];    M(2,3) = 0.0;
85   M(3,0) = eye[0];  M(3,1) = eye[1];  M(3,2) = eye[2];  M(3,3) = 1.0;
86 #undef M
87 }
88
89
90 // convert sgMat4 to MAT3 and print
91 static void print_sgMat4( sgMat4 &in) {
92     int i, j;
93     for ( i = 0; i < 4; i++ ) {
94         for ( j = 0; j < 4; j++ ) {
95             printf("%10.4f ", in[i][j]);
96         }
97         cout << endl;
98     }
99 }
100
101
102 // Update the view parameters
103 void FGViewerLookAt::update() {
104     Point3D tmp;
105     sgVec3 minus_z;
106
107     // calculate the cartesion coords of the current lat/lon/0 elev
108     Point3D p = Point3D( geod_view_pos[0], 
109                          geod_view_pos[1], 
110                          sea_level_radius );
111
112     tmp = sgPolarToCart3d(p) - scenery.center;
113     sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
114
115     // calculate view position in current FG view coordinate system
116     // p.lon & p.lat are already defined earlier, p.radius was set to
117     // the sea level radius, so now we add in our altitude.
118     if ( geod_view_pos[2] > (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
119         p.setz( p.radius() + geod_view_pos[2] );
120     } else {
121         p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
122     }
123
124     tmp = sgPolarToCart3d(p);
125     sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
126
127     // view_pos = abs_view_pos - scenery.center;
128     sgdVec3 sc;
129     sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
130     sgdVec3 vp;
131     sgdSubVec3( vp, abs_view_pos, sc );
132     sgSetVec3( view_pos, vp );
133     sgAddVec3( view_pos, pilot_offset );
134
135     FG_LOG( FG_VIEW, FG_DEBUG, "sea level radius = " << sea_level_radius );
136     FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
137     FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = "
138             << abs_view_pos[0] << ","
139             << abs_view_pos[1] << ","
140             << abs_view_pos[2] );
141     FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = "
142             << view_pos[0] << "," << view_pos[1] << "," << view_pos[2] );
143     FG_LOG( FG_VIEW, FG_DEBUG, "pilot offset = "
144             << pilot_offset[0] << "," << pilot_offset[1] << ","
145             << pilot_offset[2] );
146     FG_LOG( FG_VIEW, FG_DEBUG, "view forward = "
147             << view_forward[0] << "," << view_forward[1] << ","
148             << view_forward[2] );
149     FG_LOG( FG_VIEW, FG_DEBUG, "view up = "
150             << view_up[0] << "," << view_up[1] << ","
151             << view_up[2] );
152
153     // Make the VIEW matrix.
154     fgMakeLookAtMat4( VIEW, view_pos, view_forward, view_up );
155     // cout << "VIEW matrix" << endl;
156     // print_sgMat4( VIEW );
157
158     // the VIEW matrix includes both rotation and translation.  Let's
159     // knock out the translation part to make the VIEW_ROT matrix
160     sgCopyMat4( VIEW_ROT, VIEW );
161     VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
162
163     // Make the world up rotation matrix
164     sgMakeRotMat4( UP, 
165                    geod_view_pos[0] * RAD_TO_DEG,
166                    0.0,
167                    -geod_view_pos[1] * RAD_TO_DEG );
168
169     // use a clever observation into the nature of our tranformation
170     // matrix to grab the world_up vector
171     sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
172     // cout << "World Up = " << world_up[0] << "," << world_up[1] << ","
173     //      << world_up[2] << endl;
174     
175
176     //!!!!!!!!!!!!!!!!!!!       
177     // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
178     // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
179     // this in gui.cxx for now just testing
180     extern float quat_mat[4][4];
181     sgPreMultMat4( VIEW, quat_mat);
182     // !!!!!!!!!! testing       
183
184     // Given a vector pointing straight down (-Z), map into onto the
185     // local plane representing "horizontal".  This should give us the
186     // local direction for moving "south".
187     sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
188
189     sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
190                                      surface_south);
191     sgNormalizeVec3(surface_south);
192     // cout << "Surface direction directly south " << surface_south[0] << ","
193     //      << surface_south[1] << "," << surface_south[2] << endl;
194
195     // now calculate the surface east vector
196 #define USE_FAST_SURFACE_EAST
197 #ifdef USE_FAST_SURFACE_EAST
198     sgVec3 world_down;
199     sgNegateVec3(world_down, world_up);
200     sgVectorProductVec3(surface_east, surface_south, world_down);
201 #else
202     sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, world_up );
203     // cout << "sgMat4 TMP" << endl;
204     // print_sgMat4( TMP );
205     sgXformVec3(surface_east, surface_south, TMP);
206 #endif //  USE_FAST_SURFACE_EAST
207     // cout << "Surface direction directly east " << surface_east[0] << ","
208     //      << surface_east[1] << "," << surface_east[2] << endl;
209     // cout << "Should be close to zero = "
210     //      << sgScalarProductVec3(surface_south, surface_east) << endl;
211
212     set_clean();
213 }
214
215
216 // Destructor
217 FGViewerLookAt::~FGViewerLookAt( void ) {
218 }