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 /* Initialize a view structure */
38 void fgViewInit(struct VIEW *v) {
40 v->goal_view_offset = 0.0;
44 /* Update the view parameters */
45 void fgViewUpdate(struct FLIGHT *f, struct VIEW *v) {
47 MAT3mat R, TMP, UP, LOCAL, VIEW;
49 /* calculate view position in current FG view coordinate system */
50 v->view_pos = fgPolarToCart(FG_Longitude, FG_Lat_geocentric,
51 FG_Radius_to_vehicle * FEET_TO_METER + 1.0);
52 v->view_pos.x -= scenery.center.x;
53 v->view_pos.y -= scenery.center.y;
54 v->view_pos.z -= scenery.center.z;
56 printf("View pos = %.4f, %.4f, %.4f\n",
57 v->view_pos.x, v->view_pos.y, v->view_pos.z);
59 /* Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw) */
60 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
61 MAT3rotate(R, vec, FG_Phi);
62 /* printf("Roll matrix\n"); */
63 /* MAT3print(R, stdout); */
65 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
66 /* MAT3mult_vec(vec, vec, R); */
67 MAT3rotate(TMP, vec, FG_Theta);
68 /* printf("Pitch matrix\n"); */
69 /* MAT3print(TMP, stdout); */
72 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
73 /* MAT3mult_vec(vec, vec, R); */
74 /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
75 MAT3rotate(TMP, vec, -FG_Psi);
76 /* printf("Yaw matrix\n");
77 MAT3print(TMP, stdout); */
78 MAT3mult(LOCAL, R, TMP);
79 /* printf("LOCAL matrix\n"); */
80 /* MAT3print(LOCAL, stdout); */
82 /* Derive the local UP transformation matrix based on *geodetic*
84 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
85 MAT3rotate(R, vec, FG_Longitude); /* R = rotate about Z axis */
86 /* printf("Longitude 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_Latitude); /* TMP = rotate about X axis */
92 /* printf("Latitude matrix\n"); */
93 /* MAT3print(TMP, stdout); */
96 /* printf("Local up matrix\n"); */
97 /* MAT3print(UP, stdout); */
99 MAT3_SET_VEC(v->local_up, 1.0, 0.0, 0.0);
100 MAT3mult_vec(v->local_up, v->local_up, UP);
102 printf(" Local Up = (%.4f, %.4f, %.4f)\n",
103 v->local_up[0], v->local_up[1], v->local_up[2]);
105 /* Alternative method to Derive local up vector based on
106 * *geodetic* coordinates */
107 /* alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0); */
108 /* printf(" Alt Up = (%.4f, %.4f, %.4f)\n",
109 alt_up.x, alt_up.y, alt_up.z); */
111 /* Derive the VIEW matrix */
112 MAT3mult(VIEW, LOCAL, UP);
113 /* printf("VIEW matrix\n"); */
114 /* MAT3print(VIEW, stdout); */
116 /* generate the current up, forward, and fwrd-view vectors */
117 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
118 MAT3mult_vec(v->view_up, vec, VIEW);
120 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
121 MAT3mult_vec(forward, vec, VIEW);
122 printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
125 MAT3rotate(TMP, v->view_up, v->view_offset);
126 MAT3mult_vec(v->view_forward, forward, TMP);
132 /* Revision 1.1 1997/08/27 21:31:17 curt