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 "../constants.h"
31 #include "../Flight/flight.h"
32 #include "../Math/mat3.h"
33 #include "../Math/polar.h"
34 #include "../Scenery/scenery.h"
37 /* This is a record containing current view parameters */
38 struct fgVIEW current_view;
41 /* Initialize a view structure */
42 void fgViewInit(struct fgVIEW *v) {
44 v->goal_view_offset = 0.0;
48 /* Update the view parameters */
49 void fgViewUpdate(struct fgFLIGHT *f, struct fgVIEW *v) {
51 MAT3mat R, TMP, UP, LOCAL, VIEW;
53 /* calculate view position in current FG view coordinate system */
54 v->view_pos = fgPolarToCart(FG_Longitude, FG_Lat_geocentric,
55 FG_Radius_to_vehicle * FEET_TO_METER + 1.0);
56 v->view_pos.x -= scenery.center.x;
57 v->view_pos.y -= scenery.center.y;
58 v->view_pos.z -= scenery.center.z;
60 printf("View pos = %.4f, %.4f, %.4f\n",
61 v->view_pos.x, v->view_pos.y, v->view_pos.z);
63 /* Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw) */
64 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
65 MAT3rotate(R, vec, FG_Phi);
66 /* printf("Roll matrix\n"); */
67 /* MAT3print(R, stdout); */
69 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
70 /* MAT3mult_vec(vec, vec, R); */
71 MAT3rotate(TMP, vec, FG_Theta);
72 /* printf("Pitch matrix\n"); */
73 /* MAT3print(TMP, stdout); */
76 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
77 /* MAT3mult_vec(vec, vec, R); */
78 /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
79 MAT3rotate(TMP, vec, -FG_Psi);
80 /* printf("Yaw matrix\n");
81 MAT3print(TMP, stdout); */
82 MAT3mult(LOCAL, R, TMP);
83 /* printf("LOCAL matrix\n"); */
84 /* MAT3print(LOCAL, stdout); */
86 /* Derive the local UP transformation matrix based on *geodetic*
88 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
89 MAT3rotate(R, vec, FG_Longitude); /* R = rotate about Z axis */
90 /* printf("Longitude matrix\n"); */
91 /* MAT3print(R, stdout); */
93 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
94 MAT3mult_vec(vec, vec, R);
95 MAT3rotate(TMP, vec, -FG_Latitude); /* TMP = rotate about X axis */
96 /* printf("Latitude matrix\n"); */
97 /* MAT3print(TMP, stdout); */
100 /* printf("Local up matrix\n"); */
101 /* MAT3print(UP, stdout); */
103 MAT3_SET_VEC(v->local_up, 1.0, 0.0, 0.0);
104 MAT3mult_vec(v->local_up, v->local_up, UP);
106 printf(" Local Up = (%.4f, %.4f, %.4f)\n",
107 v->local_up[0], v->local_up[1], v->local_up[2]);
109 /* Alternative method to Derive local up vector based on
110 * *geodetic* coordinates */
111 /* alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0); */
112 /* printf(" Alt Up = (%.4f, %.4f, %.4f)\n",
113 alt_up.x, alt_up.y, alt_up.z); */
115 /* Derive the VIEW matrix */
116 MAT3mult(VIEW, LOCAL, UP);
117 /* printf("VIEW matrix\n"); */
118 /* MAT3print(VIEW, stdout); */
120 /* generate the current up, forward, and fwrd-view vectors */
121 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
122 MAT3mult_vec(v->view_up, vec, VIEW);
124 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
125 MAT3mult_vec(forward, vec, VIEW);
126 printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
129 MAT3rotate(TMP, v->view_up, v->view_offset);
130 MAT3mult_vec(v->view_forward, forward, TMP);
136 /* Revision 1.2 1997/12/10 22:37:48 curt
137 /* Prepended "fg" on the name of all global structures that didn't have it yet.
138 /* i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
140 * Revision 1.1 1997/08/27 21:31:17 curt