]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer_lookat.cxx
Centralized most view-management code in FGViewMgr. It's still a
[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/sg_geodesy.hxx>
38 #include <simgear/math/vector.hxx>
39
40 #include <Scenery/scenery.hxx>
41
42 #include "globals.hxx"
43 #include "viewer_lookat.hxx"
44
45
46 // Constructor
47 FGViewerLookAt::FGViewerLookAt( void )
48 {
49     set_reverse_view_offset(true);
50 }
51
52
53 void fgMakeLookAtMat4 ( sgMat4 dst, const sgVec3 eye, const sgVec3 center,
54                         const sgVec3 up )
55 {
56   // Caveats:
57   // 1) In order to compute the line of sight, the eye point must not be equal
58   //    to the center point.
59   // 2) The up vector must not be parallel to the line of sight from the eye
60   //    to the center point.
61
62   /* Compute the direction vectors */
63   sgVec3 x,y,z;
64
65   /* Y vector = center - eye */
66   sgSubVec3 ( y, center, eye ) ;
67
68   /* Z vector = up */
69   sgCopyVec3 ( z, up ) ;
70
71   /* X vector = Y cross Z */
72   sgVectorProductVec3 ( x, y, z ) ;
73
74   /* Recompute Z = X cross Y */
75   sgVectorProductVec3 ( z, x, y ) ;
76
77   /* Normalize everything */
78   sgNormaliseVec3 ( x ) ;
79   sgNormaliseVec3 ( y ) ;
80   sgNormaliseVec3 ( z ) ;
81
82   /* Build the matrix */
83 #define M(row,col)  dst[row][col]
84   M(0,0) = x[0];    M(0,1) = x[1];    M(0,2) = x[2];    M(0,3) = 0.0;
85   M(1,0) = y[0];    M(1,1) = y[1];    M(1,2) = y[2];    M(1,3) = 0.0;
86   M(2,0) = z[0];    M(2,1) = z[1];    M(2,2) = z[2];    M(2,3) = 0.0;
87   M(3,0) = eye[0];  M(3,1) = eye[1];  M(3,2) = eye[2];  M(3,3) = 1.0;
88 #undef M
89 }
90
91
92 // Update the view parameters
93 void FGViewerLookAt::update() {
94     sgVec3 minus_z;
95
96     view_point.setPosition(geod_view_pos[0] * SGD_RADIANS_TO_DEGREES,
97                            geod_view_pos[1] * SGD_RADIANS_TO_DEGREES,
98                            geod_view_pos[2] * SG_METER_TO_FEET);
99     sgCopyVec3(zero_elev, view_point.getZeroElevViewPos());
100     sgdCopyVec3(abs_view_pos, view_point.getAbsoluteViewPos());
101     sgCopyVec3(view_pos, view_point.getRelativeViewPos());
102
103     sgVec3 tmp_offset;
104     sgCopyVec3( tmp_offset, pilot_offset );
105     SG_LOG( SG_VIEW, SG_DEBUG, "tmp offset = "
106             << tmp_offset[0] << "," << tmp_offset[1] << ","
107             << tmp_offset[2] );
108         
109     //!!!!!!!!!!!!!!!!!!!       
110     // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
111     // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
112     extern float GuiQuat_mat[4][4];
113     sgXformPnt3( tmp_offset, tmp_offset, GuiQuat_mat );
114     SG_LOG( SG_VIEW, SG_DEBUG, "tmp_offset = "
115             << tmp_offset[0] << "," << tmp_offset[1] << ","
116             << tmp_offset[2] );
117         
118     sgAddVec3( view_pos, tmp_offset );
119     // !!!!!!!!!! testing
120         
121     // Make the VIEW matrix.
122     fgMakeLookAtMat4( VIEW, view_pos, view_forward, view_up );
123
124     // the VIEW matrix includes both rotation and translation.  Let's
125     // knock out the translation part to make the VIEW_ROT matrix
126     sgCopyMat4( VIEW_ROT, VIEW );
127     VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
128
129     // Make the world up rotation matrix
130     sgMakeRotMat4( UP, 
131                    geod_view_pos[0] * SGD_RADIANS_TO_DEGREES,
132                    0.0,
133                    -geod_view_pos[1] * SGD_RADIANS_TO_DEGREES );
134
135     // use a clever observation into the nature of our tranformation
136     // matrix to grab the world_up vector
137     sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
138
139     // Given a vector pointing straight down (-Z), map into onto the
140     // local plane representing "horizontal".  This should give us the
141     // local direction for moving "south".
142     sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
143
144     sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
145                                      surface_south);
146     sgNormalizeVec3(surface_south);
147
148     // now calculate the surface east vector
149     sgVec3 world_down;
150     sgNegateVec3(world_down, world_up);
151     sgVectorProductVec3(surface_east, surface_south, world_down);
152
153     set_clean();
154 }
155
156
157 // Destructor
158 FGViewerLookAt::~FGViewerLookAt( void ) {
159 }