1 /**************************************************************************
2 * views.c -- data structures and routines for managing and view parameters.
4 * Written by Curtis Olson, started August 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
29 #include "../Include/constants.h"
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"
39 /* This is a record containing current view parameters */
40 struct fgVIEW current_view;
43 /* Initialize a view structure */
44 void fgViewInit(struct fgVIEW *v) {
46 v->goal_view_offset = 0.0;
50 /* Update the view parameters */
51 void fgViewUpdate(struct fgFLIGHT *f, struct fgVIEW *v, struct fgLIGHT *l) {
52 MAT3vec vec, forward, v0, minus_z;
53 MAT3mat R, TMP, UP, LOCAL, VIEW;
56 /* calculate the cartesion coords of the current lat/lon/0 elev */
57 v->cur_zero_elev = fgPolarToCart(FG_Longitude, FG_Lat_geocentric,
58 FG_Sea_level_radius * FEET_TO_METER);
59 v->cur_zero_elev.x -= scenery.center.x;
60 v->cur_zero_elev.y -= scenery.center.y;
61 v->cur_zero_elev.z -= scenery.center.z;
63 /* calculate view position in current FG view coordinate system */
64 v->view_pos = fgPolarToCart(FG_Longitude, FG_Lat_geocentric,
65 FG_Radius_to_vehicle * FEET_TO_METER + 1.0);
66 v->view_pos.x -= scenery.center.x;
67 v->view_pos.y -= scenery.center.y;
68 v->view_pos.z -= scenery.center.z;
70 printf("View pos = %.4f, %.4f, %.4f\n",
71 v->view_pos.x, v->view_pos.y, v->view_pos.z);
73 /* make a vector to the current view position */
74 MAT3_SET_VEC(v0, v->view_pos.x, v->view_pos.y, v->view_pos.z);
76 /* calculate vector to sun's position on the earth's surface */
77 v->to_sun[0] = l->fg_sunpos.x - (v->view_pos.x + scenery.center.x);
78 v->to_sun[1] = l->fg_sunpos.y - (v->view_pos.y + scenery.center.y);
79 v->to_sun[2] = l->fg_sunpos.z - (v->view_pos.z + scenery.center.z);
80 printf("Vector to sun = %.2f %.2f %.2f\n",
81 v->to_sun[0], v->to_sun[1], v->to_sun[2]);
83 /* Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw) */
84 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
85 MAT3rotate(R, vec, FG_Phi);
86 /* printf("Roll matrix\n"); */
87 /* MAT3print(R, stdout); */
89 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
90 /* MAT3mult_vec(vec, vec, R); */
91 MAT3rotate(TMP, vec, FG_Theta);
92 /* printf("Pitch matrix\n"); */
93 /* MAT3print(TMP, stdout); */
96 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
97 /* MAT3mult_vec(vec, vec, R); */
98 /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
99 MAT3rotate(TMP, vec, -FG_Psi);
100 /* printf("Yaw matrix\n");
101 MAT3print(TMP, stdout); */
102 MAT3mult(LOCAL, R, TMP);
103 /* printf("LOCAL matrix\n"); */
104 /* MAT3print(LOCAL, stdout); */
106 /* Derive the local UP transformation matrix based on *geodetic*
108 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
109 MAT3rotate(R, vec, FG_Longitude); /* R = rotate about Z axis */
110 /* printf("Longitude matrix\n"); */
111 /* MAT3print(R, stdout); */
113 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
114 MAT3mult_vec(vec, vec, R);
115 MAT3rotate(TMP, vec, -FG_Latitude); /* TMP = rotate about X axis */
116 /* printf("Latitude matrix\n"); */
117 /* MAT3print(TMP, stdout); */
119 MAT3mult(UP, R, TMP);
120 /* printf("Local up matrix\n"); */
121 /* MAT3print(UP, stdout); */
123 MAT3_SET_VEC(v->local_up, 1.0, 0.0, 0.0);
124 MAT3mult_vec(v->local_up, v->local_up, UP);
126 printf(" Local Up = (%.4f, %.4f, %.4f)\n",
127 v->local_up[0], v->local_up[1], v->local_up[2]);
129 /* Alternative method to Derive local up vector based on
130 * *geodetic* coordinates */
131 /* alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0); */
132 /* printf(" Alt Up = (%.4f, %.4f, %.4f)\n",
133 alt_up.x, alt_up.y, alt_up.z); */
135 /* Derive the VIEW matrix */
136 MAT3mult(VIEW, LOCAL, UP);
137 /* printf("VIEW matrix\n"); */
138 /* MAT3print(VIEW, stdout); */
140 /* generate the current up, forward, and fwrd-view vectors */
141 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
142 MAT3mult_vec(v->view_up, vec, VIEW);
144 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
145 MAT3mult_vec(forward, vec, VIEW);
146 printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
149 MAT3rotate(TMP, v->view_up, v->view_offset);
150 MAT3mult_vec(v->view_forward, forward, TMP);
152 /* Given a vector from the view position to the point on the
153 * earth's surface the sun is directly over, map into onto the
154 * local plane representing "horizontal". */
155 map_vec_onto_cur_surface_plane(v->local_up, v0, v->to_sun,
157 MAT3_NORMALIZE_VEC(v->surface_to_sun, ntmp);
158 printf("Surface direction to sun is %.2f %.2f %.2f\n",
159 v->surface_to_sun[0], v->surface_to_sun[1], v->surface_to_sun[2]);
161 /* printf("Should be close to zero = %.2f\n",
162 MAT3_DOT_PRODUCT(v->local_up, v->surface_to_sun)); */
164 /* Given a vector pointing straight down (-Z), map into onto the
165 * local plane representing "horizontal". This should give us the
166 * local direction for moving "south". */
167 MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
168 map_vec_onto_cur_surface_plane(v->local_up, v0, minus_z, v->surface_south);
169 MAT3_NORMALIZE_VEC(v->surface_south, ntmp);
170 /* printf("Surface direction directly south %.2f %.2f %.2f\n",
171 v->surface_south[0], v->surface_south[1], v->surface_south[2]); */
173 /* now calculate the surface east vector */
174 MAT3rotate(TMP, v->view_up, FG_PI_2);
175 MAT3mult_vec(v->surface_east, v->surface_south, TMP);
176 /* printf("Surface direction directly east %.2f %.2f %.2f\n",
177 v->surface_east[0], v->surface_east[1], v->surface_east[2]); */
178 /* printf("Should be close to zero = %.2f\n",
179 MAT3_DOT_PRODUCT(v->surface_south, v->surface_east)); */
184 /* Revision 1.6 1997/12/22 04:14:32 curt
185 /* Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
187 * Revision 1.5 1997/12/18 04:07:02 curt
188 * Worked on properly translating and positioning the sky dome.
190 * Revision 1.4 1997/12/17 23:13:36 curt
191 * Began working on rendering a sky.
193 * Revision 1.3 1997/12/15 23:54:50 curt
194 * Add xgl wrappers for debugging.
195 * Generate terrain normals on the fly.
197 * Revision 1.2 1997/12/10 22:37:48 curt
198 * Prepended "fg" on the name of all global structures that didn't have it yet.
199 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
201 * Revision 1.1 1997/08/27 21:31:17 curt