]> git.mxchange.org Git - flightgear.git/blob - Main/views.c
ddee4460e1f62130636367ba0d837d6a33606796
[flightgear.git] / Main / views.c
1 /**************************************************************************
2  * views.c -- data structures and routines for managing and view parameters.
3  *
4  * Written by Curtis Olson, started August 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
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  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #include <Main/views.h>
28
29 #include <Include/fg_constants.h>
30
31 #include <Flight/flight.h>
32 #include <Math/mat3.h>
33 #include <Math/polar.h>
34 #include <Math/vector.h>
35 #include <Scenery/scenery.h>
36 #include <Time/fg_time.h>
37 #include <Main/fg_debug.h>
38
39 /* This is a record containing current view parameters */
40 struct fgVIEW current_view;
41
42
43 /* Initialize a view structure */
44 void fgViewInit(struct fgVIEW *v) {
45     fgPrintf( FG_VIEW, FG_INFO, "Initializing View parameters\n");
46
47     v->view_offset = 0.0;
48     v->goal_view_offset = 0.0;
49 }
50
51
52 /* Update the view parameters */
53 void fgViewUpdate(struct fgFLIGHT *f, struct fgVIEW *v, struct fgLIGHT *l) {
54     MAT3vec vec, forward, v0, minus_z;
55     MAT3mat R, TMP, UP, LOCAL, VIEW;
56     double ntmp;
57
58     /* calculate the cartesion coords of the current lat/lon/0 elev */
59     v->cur_zero_elev = fgPolarToCart(FG_Longitude, FG_Lat_geocentric, 
60                                      FG_Sea_level_radius * FEET_TO_METER);
61     v->cur_zero_elev.x -= scenery.center.x;
62     v->cur_zero_elev.y -= scenery.center.y;
63     v->cur_zero_elev.z -= scenery.center.z;
64
65     /* calculate view position in current FG view coordinate system */
66     v->abs_view_pos = fgPolarToCart(FG_Longitude, FG_Lat_geocentric, 
67                              FG_Radius_to_vehicle * FEET_TO_METER + 1.0);
68     v->view_pos.x = v->abs_view_pos.x - scenery.center.x;
69     v->view_pos.y = v->abs_view_pos.y - scenery.center.y;
70     v->view_pos.z = v->abs_view_pos.z - scenery.center.z;
71
72     printf( "View pos = %.4f, %.4f, %.4f\n", 
73            v->abs_view_pos.x, v->abs_view_pos.y, v->abs_view_pos.z);
74     fgPrintf( FG_VIEW, FG_DEBUG, "View pos = %.4f, %.4f, %.4f\n", 
75            v->view_pos.x, v->view_pos.y, v->view_pos.z);
76
77     /* make a vector to the current view position */
78     MAT3_SET_VEC(v0, v->view_pos.x, v->view_pos.y, v->view_pos.z);
79
80     /* calculate vector to sun's position on the earth's surface */
81     v->to_sun[0] = l->fg_sunpos.x - (v->view_pos.x + scenery.center.x);
82     v->to_sun[1] = l->fg_sunpos.y - (v->view_pos.y + scenery.center.y);
83     v->to_sun[2] = l->fg_sunpos.z - (v->view_pos.z + scenery.center.z);
84     /* printf("Vector to sun = %.2f %.2f %.2f\n",
85            v->to_sun[0], v->to_sun[1], v->to_sun[2]); */
86
87     /* Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw) */
88     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
89     MAT3rotate(R, vec, FG_Phi);
90     /* printf("Roll matrix\n"); */
91     /* MAT3print(R, stdout); */
92
93     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
94     /* MAT3mult_vec(vec, vec, R); */
95     MAT3rotate(TMP, vec, FG_Theta);
96     /* printf("Pitch matrix\n"); */
97     /* MAT3print(TMP, stdout); */
98     MAT3mult(R, R, TMP);
99
100     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
101     /* MAT3mult_vec(vec, vec, R); */
102     /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
103     MAT3rotate(TMP, vec, -FG_Psi);
104     /* printf("Yaw matrix\n");
105     MAT3print(TMP, stdout); */
106     MAT3mult(LOCAL, R, TMP);
107     /* printf("LOCAL matrix\n"); */
108     /* MAT3print(LOCAL, stdout); */
109
110     /* Derive the local UP transformation matrix based on *geodetic*
111      * coordinates */
112     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
113     MAT3rotate(R, vec, FG_Longitude);     /* R = rotate about Z axis */
114     /* printf("Longitude matrix\n"); */
115     /* MAT3print(R, stdout); */
116
117     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
118     MAT3mult_vec(vec, vec, R);
119     MAT3rotate(TMP, vec, -FG_Latitude);  /* TMP = rotate about X axis */
120     /* printf("Latitude matrix\n"); */
121     /* MAT3print(TMP, stdout); */
122
123     MAT3mult(UP, R, TMP);
124     /* printf("Local up matrix\n"); */
125     /* MAT3print(UP, stdout); */
126
127     MAT3_SET_VEC(v->local_up, 1.0, 0.0, 0.0);
128     MAT3mult_vec(v->local_up, v->local_up, UP);
129
130     /* printf("Local Up = (%.4f, %.4f, %.4f)\n",
131            v->local_up[0], v->local_up[1], v->local_up[2]); */
132     
133     /* Alternative method to Derive local up vector based on
134      * *geodetic* coordinates */
135     /* alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0); */
136     /* printf("    Alt Up = (%.4f, %.4f, %.4f)\n", 
137        alt_up.x, alt_up.y, alt_up.z); */
138
139     /* Derive the VIEW matrix */
140     MAT3mult(VIEW, LOCAL, UP);
141     /* printf("VIEW matrix\n"); */
142     /* MAT3print(VIEW, stdout); */
143
144     /* generate the current up, forward, and fwrd-view vectors */
145     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
146     MAT3mult_vec(v->view_up, vec, VIEW);
147
148     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
149     MAT3mult_vec(forward, vec, VIEW);
150     /* printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
151            forward[2]); */
152
153     MAT3rotate(TMP, v->view_up, v->view_offset);
154     MAT3mult_vec(v->view_forward, forward, TMP);
155
156     /* Given a vector from the view position to the point on the
157      * earth's surface the sun is directly over, map into onto the
158      * local plane representing "horizontal". */
159     map_vec_onto_cur_surface_plane(v->local_up, v0, v->to_sun, 
160                                    v->surface_to_sun);
161     MAT3_NORMALIZE_VEC(v->surface_to_sun, ntmp);
162     /* printf("Surface direction to sun is %.2f %.2f %.2f\n",
163            v->surface_to_sun[0], v->surface_to_sun[1], v->surface_to_sun[2]); */
164     /* printf("Should be close to zero = %.2f\n", 
165            MAT3_DOT_PRODUCT(v->local_up, v->surface_to_sun)); */
166
167     /* Given a vector pointing straight down (-Z), map into onto the
168      * local plane representing "horizontal".  This should give us the
169      * local direction for moving "south". */
170     MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
171     map_vec_onto_cur_surface_plane(v->local_up, v0, minus_z, v->surface_south);
172     MAT3_NORMALIZE_VEC(v->surface_south, ntmp);
173     /* printf("Surface direction directly south %.2f %.2f %.2f\n",
174            v->surface_south[0], v->surface_south[1], v->surface_south[2]); */
175
176     /* now calculate the surface east vector */
177     MAT3rotate(TMP, v->view_up, FG_PI_2);
178     MAT3mult_vec(v->surface_east, v->surface_south, TMP);
179     /* printf("Surface direction directly east %.2f %.2f %.2f\n",
180            v->surface_east[0], v->surface_east[1], v->surface_east[2]); */
181     /* printf("Should be close to zero = %.2f\n", 
182            MAT3_DOT_PRODUCT(v->surface_south, v->surface_east)); */
183 }
184
185
186 /* $Log$
187 /* Revision 1.12  1998/01/29 00:50:28  curt
188 /* Added a view record field for absolute x, y, z position.
189 /*
190  * Revision 1.11  1998/01/27 00:47:58  curt
191  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
192  * system and commandline/config file processing code.
193  *
194  * Revision 1.10  1998/01/19 19:27:09  curt
195  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
196  * This should simplify things tremendously.
197  *
198  * Revision 1.9  1998/01/13 00:23:09  curt
199  * Initial changes to support loading and management of scenery tiles.  Note,
200  * there's still a fair amount of work left to be done.
201  *
202  * Revision 1.8  1997/12/30 22:22:33  curt
203  * Further integration of event manager.
204  *
205  * Revision 1.7  1997/12/30 20:47:45  curt
206  * Integrated new event manager with subsystem initializations.
207  *
208  * Revision 1.6  1997/12/22 04:14:32  curt
209  * Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
210  *
211  * Revision 1.5  1997/12/18 04:07:02  curt
212  * Worked on properly translating and positioning the sky dome.
213  *
214  * Revision 1.4  1997/12/17 23:13:36  curt
215  * Began working on rendering a sky.
216  *
217  * Revision 1.3  1997/12/15 23:54:50  curt
218  * Add xgl wrappers for debugging.
219  * Generate terrain normals on the fly.
220  *
221  * Revision 1.2  1997/12/10 22:37:48  curt
222  * Prepended "fg" on the name of all global structures that didn't have it yet.
223  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
224  *
225  * Revision 1.1  1997/08/27 21:31:17  curt
226  * Initial revision.
227  *
228  */