~ubuntu-branches/ubuntu/quantal/flightgear/quantal

« back to all changes in this revision

Viewing changes to src/Main/viewer_rph.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Ove Kaaven
  • Date: 2002-03-27 21:50:15 UTC
  • Revision ID: james.westby@ubuntu.com-20020327215015-0rvi3o8iml0a8s93
Tags: upstream-0.7.9
ImportĀ upstreamĀ versionĀ 0.7.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// viewer_rph.cxx -- class for managing a Roll/Pitch/Heading viewer in
 
2
//                   the flightgear world.
 
3
//
 
4
// Written by Curtis Olson, started August 1997.
 
5
//                          overhaul started October 2000.
 
6
//
 
7
// Copyright (C) 1997 - 2000  Curtis L. Olson  - curt@flightgear.org
 
8
//
 
9
// This program is free software; you can redistribute it and/or
 
10
// modify it under the terms of the GNU General Public License as
 
11
// published by the Free Software Foundation; either version 2 of the
 
12
// License, or (at your option) any later version.
 
13
//
 
14
// This program is distributed in the hope that it will be useful, but
 
15
// WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
// General Public License for more details.
 
18
//
 
19
// You should have received a copy of the GNU General Public License
 
20
// along with this program; if not, write to the Free Software
 
21
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
22
//
 
23
// $Id: viewer_rph.cxx,v 1.15 2002/01/19 22:38:24 david Exp $
 
24
 
 
25
 
 
26
#include <simgear/compiler.h>
 
27
 
 
28
#ifdef HAVE_CONFIG_H
 
29
#  include <config.h>
 
30
#endif
 
31
 
 
32
#include <plib/ssg.h>           // plib include
 
33
 
 
34
#include <simgear/constants.h>
 
35
#include <simgear/debug/logstream.hxx>
 
36
#include <simgear/math/point3d.hxx>
 
37
#include <simgear/math/polar3d.hxx>
 
38
#include <simgear/math/sg_geodesy.hxx>
 
39
#include <simgear/math/vector.hxx>
 
40
 
 
41
#include <Scenery/scenery.hxx>
 
42
 
 
43
#include "globals.hxx"
 
44
#include "viewer_rph.hxx"
 
45
 
 
46
 
 
47
// Constructor
 
48
FGViewerRPH::FGViewerRPH( void )
 
49
{
 
50
    set_reverse_view_offset(false);
 
51
#ifndef USE_FAST_VIEWROT
 
52
    // This never changes -- NHV
 
53
    LARC_TO_SSG[0][0] = 0.0; 
 
54
    LARC_TO_SSG[0][1] = 1.0; 
 
55
    LARC_TO_SSG[0][2] = -0.0; 
 
56
    LARC_TO_SSG[0][3] = 0.0; 
 
57
 
 
58
    LARC_TO_SSG[1][0] = 0.0; 
 
59
    LARC_TO_SSG[1][1] = 0.0; 
 
60
    LARC_TO_SSG[1][2] = 1.0; 
 
61
    LARC_TO_SSG[1][3] = 0.0;
 
62
        
 
63
    LARC_TO_SSG[2][0] = 1.0; 
 
64
    LARC_TO_SSG[2][1] = -0.0; 
 
65
    LARC_TO_SSG[2][2] = 0.0; 
 
66
    LARC_TO_SSG[2][3] = 0.0;
 
67
        
 
68
    LARC_TO_SSG[3][0] = 0.0; 
 
69
    LARC_TO_SSG[3][1] = 0.0; 
 
70
    LARC_TO_SSG[3][2] = 0.0; 
 
71
    LARC_TO_SSG[3][3] = 1.0; 
 
72
#endif // USE_FAST_VIEWROT
 
73
}
 
74
 
 
75
 
 
76
#define USE_FAST_VIEWROT
 
77
#ifdef USE_FAST_VIEWROT
 
78
// VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
 
79
// This takes advantage of the fact that VIEWo and VIEW_OFFSET
 
80
// only have entries in the upper 3x3 block
 
81
// and that LARC_TO_SSG is just a shift of rows   NHV
 
82
inline static void fgMakeViewRot( sgMat4 dst, const sgMat4 m1, const sgMat4 m2 )
 
83
{
 
84
    for ( int j = 0 ; j < 3 ; j++ ) {
 
85
        dst[2][j] = m2[0][0] * m1[0][j] +
 
86
            m2[0][1] * m1[1][j] +
 
87
            m2[0][2] * m1[2][j];
 
88
 
 
89
        dst[0][j] = m2[1][0] * m1[0][j] +
 
90
            m2[1][1] * m1[1][j] +
 
91
            m2[1][2] * m1[2][j];
 
92
 
 
93
        dst[1][j] = m2[2][0] * m1[0][j] +
 
94
            m2[2][1] * m1[1][j] +
 
95
            m2[2][2] * m1[2][j];
 
96
    }
 
97
    dst[0][3] = 
 
98
        dst[1][3] = 
 
99
        dst[2][3] = 
 
100
        dst[3][0] = 
 
101
        dst[3][1] = 
 
102
        dst[3][2] = SG_ZERO;
 
103
    dst[3][3] = SG_ONE;
 
104
}
 
105
#endif
 
106
 
 
107
 
 
108
#define USE_FAST_LOCAL
 
109
#ifdef USE_FAST_LOCAL
 
110
inline static void fgMakeLOCAL( sgMat4 dst, const double Theta,
 
111
                                const double Phi, const double Psi)
 
112
{
 
113
    SGfloat cosTheta = (SGfloat) cos(Theta);
 
114
    SGfloat sinTheta = (SGfloat) sin(Theta);
 
115
    SGfloat cosPhi   = (SGfloat) cos(Phi);
 
116
    SGfloat sinPhi   = (SGfloat) sin(Phi);
 
117
    SGfloat sinPsi   = (SGfloat) sin(Psi) ;
 
118
    SGfloat cosPsi   = (SGfloat) cos(Psi) ;
 
119
        
 
120
    dst[0][0] = cosPhi * cosTheta;
 
121
    dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
 
122
    dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
 
123
    dst[0][3] = SG_ZERO;
 
124
 
 
125
    dst[1][0] = -sinPhi * cosTheta;
 
126
    dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
 
127
    dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
 
128
    dst[1][3] = SG_ZERO ;
 
129
        
 
130
    dst[2][0] = sinTheta;
 
131
    dst[2][1] = cosTheta * -sinPsi;
 
132
    dst[2][2] = cosTheta * cosPsi;
 
133
    dst[2][3] = SG_ZERO;
 
134
        
 
135
    dst[3][0] = SG_ZERO;
 
136
    dst[3][1] = SG_ZERO;
 
137
    dst[3][2] = SG_ZERO;
 
138
    dst[3][3] = SG_ONE ;
 
139
}
 
140
#endif
 
141
 
 
142
 
 
143
#if 0
 
144
// convert sgMat4 to MAT3 and print
 
145
static void print_sgMat4( sgMat4 &in) {
 
146
    int i, j;
 
147
    for ( i = 0; i < 4; i++ ) {
 
148
        for ( j = 0; j < 4; j++ ) {
 
149
            printf("%10.4f ", in[i][j]);
 
150
        }
 
151
        cout << endl;
 
152
    }
 
153
}
 
154
#endif
 
155
 
 
156
 
 
157
// Update the view parameters
 
158
void FGViewerRPH::update() {
 
159
    Point3D tmp;
 
160
    sgVec3 minus_z, right, forward, tilt;
 
161
    sgMat4 VIEWo;
 
162
 
 
163
    // convert to geocentric coordinates
 
164
    double geoc_lat;
 
165
    sgGeodToGeoc( geod_view_pos[1], geod_view_pos[2],
 
166
                  &sea_level_radius, &geoc_lat );
 
167
 
 
168
    // calculate the cartesion coords of the current lat/lon/0 elev
 
169
    Point3D p = Point3D( geod_view_pos[0], geoc_lat, sea_level_radius );
 
170
 
 
171
    tmp = sgPolarToCart3d(p) - scenery.get_center();
 
172
    sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
 
173
 
 
174
    // calculate view position in current FG view coordinate system
 
175
    // p.lon & p.lat are already defined earlier, p.radius was set to
 
176
    // the sea level radius, so now we add in our altitude.
 
177
    if ( geod_view_pos[2] > (scenery.get_cur_elev() + 0.5 * SG_METER_TO_FEET) ) {
 
178
        p.setz( p.radius() + geod_view_pos[2] );
 
179
    } else {
 
180
        p.setz( p.radius() + scenery.get_cur_elev() + 0.5 * SG_METER_TO_FEET );
 
181
    }
 
182
 
 
183
    tmp = sgPolarToCart3d(p);
 
184
    sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
 
185
 
 
186
    // view_pos = abs_view_pos - scenery.center;
 
187
    sgdVec3 sc;
 
188
    sgdSetVec3( sc,
 
189
                scenery.get_center().x(),
 
190
                scenery.get_center().y(),
 
191
                scenery.get_center().z() );
 
192
    sgdVec3 vp;
 
193
    sgdSubVec3( vp, abs_view_pos, sc );
 
194
    sgSetVec3( view_pos, vp );
 
195
 
 
196
    SG_LOG( SG_VIEW, SG_DEBUG, "sea level radius = " << sea_level_radius );
 
197
    SG_LOG( SG_VIEW, SG_DEBUG, "Polar view pos = " << p );
 
198
    SG_LOG( SG_VIEW, SG_DEBUG, "Absolute view pos = "
 
199
            << abs_view_pos[0] << ","
 
200
            << abs_view_pos[1] << ","
 
201
            << abs_view_pos[2] );
 
202
    SG_LOG( SG_VIEW, SG_DEBUG, "(RPH) Relative view pos = "
 
203
            << view_pos[0] << "," << view_pos[1] << "," << view_pos[2] );
 
204
 
 
205
    // code to calculate LOCAL matrix calculated from Phi, Theta, and
 
206
    // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
 
207
    // flight model
 
208
        
 
209
#ifdef USE_FAST_LOCAL
 
210
        
 
211
    fgMakeLOCAL( LOCAL, rph[1], rph[0], -rph[2] );
 
212
        
 
213
#else // USE_TEXT_BOOK_METHOD
 
214
        
 
215
    sgVec3 rollvec;
 
216
    sgSetVec3( rollvec, 0.0, 0.0, 1.0 );
 
217
    sgMat4 PHI;         // roll
 
218
    sgMakeRotMat4( PHI, rph[0] * SGD_RADIANS_TO_DEGREES, rollvec );
 
219
 
 
220
    sgVec3 pitchvec;
 
221
    sgSetVec3( pitchvec, 0.0, 1.0, 0.0 );
 
222
    sgMat4 THETA;               // pitch
 
223
    sgMakeRotMat4( THETA, rph[1] * SGD_RADIANS_TO_DEGREES, pitchvec );
 
224
 
 
225
    // ROT = PHI * THETA
 
226
    sgMat4 ROT;
 
227
    // sgMultMat4( ROT, PHI, THETA );
 
228
    sgCopyMat4( ROT, PHI );
 
229
    sgPostMultMat4( ROT, THETA );
 
230
 
 
231
    sgVec3 yawvec;
 
232
    sgSetVec3( yawvec, 1.0, 0.0, 0.0 );
 
233
    sgMat4 PSI;         // heading
 
234
    sgMakeRotMat4( PSI, -rph[2] * SGD_RADIANS_TO_DEGREES, yawvec );
 
235
 
 
236
    // LOCAL = ROT * PSI
 
237
    // sgMultMat4( LOCAL, ROT, PSI );
 
238
    sgCopyMat4( LOCAL, ROT );
 
239
    sgPostMultMat4( LOCAL, PSI );
 
240
 
 
241
#endif // USE_FAST_LOCAL
 
242
        
 
243
    // cout << "LOCAL matrix" << endl;
 
244
    // print_sgMat4( LOCAL );
 
245
        
 
246
    sgMakeRotMat4( UP, 
 
247
                   geod_view_pos[0] * SGD_RADIANS_TO_DEGREES,
 
248
                   0.0,
 
249
                   -geod_view_pos[1] * SGD_RADIANS_TO_DEGREES );
 
250
 
 
251
    sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
 
252
    // sgXformVec3( world_up, UP );
 
253
    // cout << "World Up = " << world_up[0] << "," << world_up[1] << ","
 
254
    //      << world_up[2] << endl;
 
255
    
 
256
    // Alternative method to Derive world up vector based on
 
257
    // *geodetic* coordinates
 
258
    // alt_up = sgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
 
259
    // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
 
260
    //         alt_up.x, alt_up.y, alt_up.z);
 
261
 
 
262
    // VIEWo = LOCAL * UP
 
263
    // sgMultMat4( VIEWo, LOCAL, UP );
 
264
    sgCopyMat4( VIEWo, LOCAL );
 
265
    sgPostMultMat4( VIEWo, UP );
 
266
    // cout << "VIEWo matrix" << endl;
 
267
    // print_sgMat4( VIEWo );
 
268
 
 
269
    // generate the sg view up and forward vectors
 
270
    sgSetVec3( view_up, VIEWo[0][0], VIEWo[0][1], VIEWo[0][2] );
 
271
    // cout << "view = " << view[0] << ","
 
272
    //      << view[1] << "," << view[2] << endl;
 
273
    sgSetVec3( right, VIEWo[1][0], VIEWo[1][1], VIEWo[1][2] );
 
274
    sgSetVec3( forward, VIEWo[2][0], VIEWo[2][1], VIEWo[2][2] );
 
275
    // cout << "forward = " << forward[0] << ","
 
276
    //      << forward[1] << "," << forward[2] << endl;
 
277
 
 
278
    // generate the pilot offset vector in world coordinates
 
279
    sgVec3 pilot_offset_world;
 
280
    sgSetVec3( pilot_offset_world, 
 
281
               pilot_offset[2], pilot_offset[1], -pilot_offset[0] );
 
282
    sgXformVec3( pilot_offset_world, pilot_offset_world, VIEWo );
 
283
 
 
284
    // generate the view offset matrix
 
285
    sgMakeRotMat4( VIEW_OFFSET, view_offset * SGD_RADIANS_TO_DEGREES, view_up );
 
286
 
 
287
    sgMat4 VIEW_TILT;
 
288
    sgMakeRotMat4( VIEW_TILT, view_tilt * SGD_RADIANS_TO_DEGREES, right );
 
289
    sgPreMultMat4(VIEW_OFFSET, VIEW_TILT);
 
290
    // cout << "VIEW_OFFSET matrix" << endl;
 
291
    // print_sgMat4( VIEW_OFFSET );
 
292
    sgXformVec3( view_forward, forward, VIEW_OFFSET );
 
293
    SG_LOG( SG_VIEW, SG_DEBUG, "(RPH) view forward = "
 
294
            << view_forward[0] << "," << view_forward[1] << ","
 
295
            << view_forward[2] );
 
296
        
 
297
    // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
 
298
#ifdef USE_FAST_VIEWROT
 
299
    fgMakeViewRot( VIEW_ROT, VIEW_OFFSET, VIEWo );
 
300
#else
 
301
    // sgMultMat4( VIEW_ROT, VIEW_OFFSET, VIEWo );
 
302
    // sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
 
303
    sgCopyMat4( VIEW_ROT, VIEWo );
 
304
    sgPostMultMat4( VIEW_ROT, VIEW_OFFSET );
 
305
    sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
 
306
#endif
 
307
    // cout << "VIEW_ROT matrix" << endl;
 
308
    // print_sgMat4( VIEW_ROT );
 
309
 
 
310
    sgVec3 trans_vec;
 
311
    sgAddVec3( trans_vec, view_pos, pilot_offset_world );
 
312
 
 
313
    // VIEW = VIEW_ROT * TRANS
 
314
    sgCopyMat4( VIEW, VIEW_ROT );
 
315
    sgPostMultMat4ByTransMat4( VIEW, trans_vec );
 
316
 
 
317
    //!!!!!!!!!!!!!!!!!!!       
 
318
    // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
 
319
    // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
 
320
    // this in gui.cxx for now just testing
 
321
    extern float GuiQuat_mat[4][4];
 
322
    sgPreMultMat4( VIEW, GuiQuat_mat);
 
323
    // !!!!!!!!!! testing       
 
324
 
 
325
    // Given a vector pointing straight down (-Z), map into onto the
 
326
    // local plane representing "horizontal".  This should give us the
 
327
    // local direction for moving "south".
 
328
    sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
 
329
 
 
330
    sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
 
331
                                     surface_south);
 
332
    sgNormalizeVec3(surface_south);
 
333
    // cout << "Surface direction directly south " << surface_south[0] << ","
 
334
    //      << surface_south[1] << "," << surface_south[2] << endl;
 
335
 
 
336
    // now calculate the surface east vector
 
337
#define USE_FAST_SURFACE_EAST
 
338
#ifdef USE_FAST_SURFACE_EAST
 
339
    sgVec3 world_down;
 
340
    sgNegateVec3(world_down, world_up);
 
341
    sgVectorProductVec3(surface_east, surface_south, world_down);
 
342
#else
 
343
    sgMakeRotMat4( TMP, SGD_PI_2 * SGD_RADIANS_TO_DEGREES, world_up );
 
344
    // cout << "sgMat4 TMP" << endl;
 
345
    // print_sgMat4( TMP );
 
346
    sgXformVec3(surface_east, surface_south, TMP);
 
347
#endif //  USE_FAST_SURFACE_EAST
 
348
    // cout << "Surface direction directly east " << surface_east[0] << ","
 
349
    //      << surface_east[1] << "," << surface_east[2] << endl;
 
350
    // cout << "Should be close to zero = "
 
351
    //      << sgScalarProductVec3(surface_south, surface_east) << endl;
 
352
 
 
353
    set_clean();
 
354
}
 
355
 
 
356
 
 
357
// Destructor
 
358
FGViewerRPH::~FGViewerRPH( void ) {
 
359
}