]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer_lookat.cxx
Added a viewmgr system and made corresponding changes to support it.
[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 static void fgLookAt( sgVec3 eye, sgVec3 center, sgVec3 up, sgMat4 &m ) {
52     double x[3], y[3], z[3];
53     double mag;
54
55     /* Make rotation matrix */
56
57     /* Z vector */
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 */
63         z[0] /= mag;
64         z[1] /= mag;
65         z[2] /= mag;
66     }
67
68     /* Y vector */
69     y[0] = up[0];
70     y[1] = up[1];
71     y[2] = up[2];
72
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];
77
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];
82
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
86      */
87
88     mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
89     if (mag) {
90         x[0] /= mag;
91         x[1] /= mag;
92         x[2] /= mag;
93     }
94
95     mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
96     if (mag) {
97         y[0] /= mag;
98         y[1] /= mag;
99         y[2] /= mag;
100     }
101
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;
107 #undef M
108 }
109
110
111 // convert sgMat4 to MAT3 and print
112 static void print_sgMat4( sgMat4 &in) {
113     int i, j;
114     for ( i = 0; i < 4; i++ ) {
115         for ( j = 0; j < 4; j++ ) {
116             printf("%10.4f ", in[i][j]);
117         }
118         cout << endl;
119     }
120 }
121
122
123 // Update the view parameters
124 void FGViewerLookAt::update() {
125     Point3D tmp;
126     sgVec3 minus_z, forward;
127     sgMat4 VIEWo;
128
129     // calculate the cartesion coords of the current lat/lon/0 elev
130     Point3D p = Point3D( geod_view_pos[0], 
131                          geod_view_pos[1], 
132                          sea_level_radius );
133
134     tmp = sgPolarToCart3d(p) - scenery.center;
135     sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
136
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] );
142     } else {
143         p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
144     }
145
146     tmp = sgPolarToCart3d(p);
147     sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
148
149     // view_pos = abs_view_pos - scenery.center;
150     sgdVec3 sc;
151     sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
152     sgdVec3 vp;
153     sgdSubVec3( vp, abs_view_pos, sc );
154     sgSetVec3( view_pos, vp );
155
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] << ","
169             << view_up[2] );
170
171     // Make the VIEW matrix.
172     fgLookAt( view_pos, view_forward, view_up, VIEW );
173     // cout << "VIEW matrix" << endl;
174     // print_sgMat4( VIEW );
175
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;
180
181     // Make the world up rotation matrix
182     sgMakeRotMat4( UP, 
183                    geod_view_pos[0] * RAD_TO_DEG,
184                    0.0,
185                    -geod_view_pos[1] * RAD_TO_DEG );
186
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;
192     
193
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       
201
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 );
206
207     sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
208                                      surface_south);
209     sgNormalizeVec3(surface_south);
210     // cout << "Surface direction directly south " << surface_south[0] << ","
211     //      << surface_south[1] << "," << surface_south[2] << endl;
212
213     // now calculate the surface east vector
214 #define USE_FAST_SURFACE_EAST
215 #ifdef USE_FAST_SURFACE_EAST
216     sgVec3 world_down;
217     sgNegateVec3(world_down, world_up);
218     sgVectorProductVec3(surface_east, surface_south, world_down);
219 #else
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;
229
230     set_clean();
231 }
232
233
234 // Destructor
235 FGViewerLookAt::~FGViewerLookAt( void ) {
236 }