]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer_lookat.cxx
MSVC tweaks.
[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     set_reverse_view_offset(true);
49 }
50
51
52 void fgMakeLookAtMat4 ( sgMat4 dst, const sgVec3 eye, const sgVec3 center,
53                         const sgVec3 up )
54 {
55   // Caveats:
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.
60
61   /* Compute the direction vectors */
62   sgVec3 x,y,z;
63
64   /* Y vector = center - eye */
65   sgSubVec3 ( y, center, eye ) ;
66
67   /* Z vector = up */
68   sgCopyVec3 ( z, up ) ;
69
70   /* X vector = Y cross Z */
71   sgVectorProductVec3 ( x, y, z ) ;
72
73   /* Recompute Z = X cross Y */
74   sgVectorProductVec3 ( z, x, y ) ;
75
76   /* Normalize everything */
77   sgNormaliseVec3 ( x ) ;
78   sgNormaliseVec3 ( y ) ;
79   sgNormaliseVec3 ( z ) ;
80
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;
87 #undef M
88 }
89
90
91 #if 0
92 // convert sgMat4 to MAT3 and print
93 static void print_sgMat4( sgMat4 &in) {
94     int i, j;
95     for ( i = 0; i < 4; i++ ) {
96         for ( j = 0; j < 4; j++ ) {
97             printf("%10.4f ", in[i][j]);
98         }
99         cout << endl;
100     }
101 }
102 #endif
103
104
105 // Update the view parameters
106 void FGViewerLookAt::update() {
107     Point3D tmp;
108     sgVec3 minus_z;
109
110     // calculate the cartesion coords of the current lat/lon/0 elev
111     Point3D p = Point3D( geod_view_pos[0], 
112                          geod_view_pos[1], 
113                          sea_level_radius );
114
115     tmp = sgPolarToCart3d(p) - scenery.center;
116     sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
117
118     // calculate view position in current FG view coordinate system
119     // p.lon & p.lat are already defined earlier, p.radius was set to
120     // the sea level radius, so now we add in our altitude.
121     if ( geod_view_pos[2] > (scenery.cur_elev + 0.5 * SG_METER_TO_FEET) ) {
122         p.setz( p.radius() + geod_view_pos[2] );
123     } else {
124         p.setz( p.radius() + scenery.cur_elev + 0.5 * SG_METER_TO_FEET );
125     }
126
127     tmp = sgPolarToCart3d(p);
128     sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
129
130     // view_pos = abs_view_pos - scenery.center;
131     sgdVec3 sc;
132     sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
133     sgdVec3 vp;
134     sgdSubVec3( vp, abs_view_pos, sc );
135     sgSetVec3( view_pos, vp );
136     sgAddVec3( view_pos, pilot_offset );
137
138     SG_LOG( SG_VIEW, SG_DEBUG, "sea level radius = " << sea_level_radius );
139     SG_LOG( SG_VIEW, SG_DEBUG, "Polar view pos = " << p );
140     SG_LOG( SG_VIEW, SG_DEBUG, "Absolute view pos = "
141             << abs_view_pos[0] << ","
142             << abs_view_pos[1] << ","
143             << abs_view_pos[2] );
144     SG_LOG( SG_VIEW, SG_DEBUG, "Relative view pos = "
145             << view_pos[0] << "," << view_pos[1] << "," << view_pos[2] );
146     SG_LOG( SG_VIEW, SG_DEBUG, "pilot offset = "
147             << pilot_offset[0] << "," << pilot_offset[1] << ","
148             << pilot_offset[2] );
149     SG_LOG( SG_VIEW, SG_DEBUG, "view forward = "
150             << view_forward[0] << "," << view_forward[1] << ","
151             << view_forward[2] );
152     SG_LOG( SG_VIEW, SG_DEBUG, "view up = "
153             << view_up[0] << "," << view_up[1] << ","
154             << view_up[2] );
155
156     // Make the VIEW matrix.
157     fgMakeLookAtMat4( VIEW, view_pos, view_forward, view_up );
158     // cout << "VIEW matrix" << endl;
159     // print_sgMat4( VIEW );
160
161     // the VIEW matrix includes both rotation and translation.  Let's
162     // knock out the translation part to make the VIEW_ROT matrix
163     sgCopyMat4( VIEW_ROT, VIEW );
164     VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
165
166     // Make the world up rotation matrix
167     sgMakeRotMat4( UP, 
168                    geod_view_pos[0] * SGD_RADIANS_TO_DEGREES,
169                    0.0,
170                    -geod_view_pos[1] * SGD_RADIANS_TO_DEGREES );
171
172     // use a clever observation into the nature of our tranformation
173     // matrix to grab the world_up vector
174     sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
175     // cout << "World Up = " << world_up[0] << "," << world_up[1] << ","
176     //      << world_up[2] << endl;
177     
178
179     //!!!!!!!!!!!!!!!!!!!       
180     // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
181     // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
182     // this in gui.cxx for now just testing
183     extern float GuiQuat_mat[4][4];
184     sgPreMultMat4( VIEW, GuiQuat_mat);
185     // !!!!!!!!!! testing       
186
187     // Given a vector pointing straight down (-Z), map into onto the
188     // local plane representing "horizontal".  This should give us the
189     // local direction for moving "south".
190     sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
191
192     sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
193                                      surface_south);
194     sgNormalizeVec3(surface_south);
195     // cout << "Surface direction directly south " << surface_south[0] << ","
196     //      << surface_south[1] << "," << surface_south[2] << endl;
197
198     // now calculate the surface east vector
199 #define USE_FAST_SURFACE_EAST
200 #ifdef USE_FAST_SURFACE_EAST
201     sgVec3 world_down;
202     sgNegateVec3(world_down, world_up);
203     sgVectorProductVec3(surface_east, surface_south, world_down);
204 #else
205     sgMakeRotMat4( TMP, SGD_PI_2 * SGD_RADIANS_TO_DEGREES, world_up );
206     // cout << "sgMat4 TMP" << endl;
207     // print_sgMat4( TMP );
208     sgXformVec3(surface_east, surface_south, TMP);
209 #endif //  USE_FAST_SURFACE_EAST
210     // cout << "Surface direction directly east " << surface_east[0] << ","
211     //      << surface_east[1] << "," << surface_east[2] << endl;
212     // cout << "Should be close to zero = "
213     //      << sgScalarProductVec3(surface_south, surface_east) << endl;
214
215     set_clean();
216 }
217
218
219 // Destructor
220 FGViewerLookAt::~FGViewerLookAt( void ) {
221 }