]> git.mxchange.org Git - flightgear.git/blob - Main/views.c
Began working on rendering a sky.
[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 "views.h"
28
29 #include "../Include/constants.h"
30
31 #include "../Flight/flight.h"
32 #include "../Math/mat3.h"
33 #include "../Math/polar.h"
34 #include "../Scenery/scenery.h"
35
36
37 /* This is a record containing current view parameters */
38 struct fgVIEW current_view;
39
40
41 /* Initialize a view structure */
42 void fgViewInit(struct fgVIEW *v) {
43     v->view_offset = 0.0;
44     v->goal_view_offset = 0.0;
45 }
46
47
48 /* Update the view parameters */
49 void fgViewUpdate(struct fgFLIGHT *f, struct fgVIEW *v) {
50     MAT3vec vec, forward;
51     MAT3mat R, TMP, UP, LOCAL, VIEW;
52
53     /* calculate the cartesion coords of the current lat/lon/0 elev */
54     v->cur_zero_elev = fgPolarToCart(FG_Longitude, FG_Lat_geocentric, 
55                                      FG_Sea_level_radius);
56
57     /* calculate view position in current FG view coordinate system */
58     v->view_pos = fgPolarToCart(FG_Longitude, FG_Lat_geocentric, 
59                              FG_Radius_to_vehicle * FEET_TO_METER + 1.0);
60     v->view_pos.x -= scenery.center.x;
61     v->view_pos.y -= scenery.center.y;
62     v->view_pos.z -= scenery.center.z;
63
64     printf("View pos = %.4f, %.4f, %.4f\n", 
65            v->view_pos.x, v->view_pos.y, v->view_pos.z);
66
67     /* Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw) */
68     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
69     MAT3rotate(R, vec, FG_Phi);
70     /* printf("Roll matrix\n"); */
71     /* MAT3print(R, stdout); */
72
73     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
74     /* MAT3mult_vec(vec, vec, R); */
75     MAT3rotate(TMP, vec, FG_Theta);
76     /* printf("Pitch matrix\n"); */
77     /* MAT3print(TMP, stdout); */
78     MAT3mult(R, R, TMP);
79
80     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
81     /* MAT3mult_vec(vec, vec, R); */
82     /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
83     MAT3rotate(TMP, vec, -FG_Psi);
84     /* printf("Yaw matrix\n");
85     MAT3print(TMP, stdout); */
86     MAT3mult(LOCAL, R, TMP);
87     /* printf("LOCAL matrix\n"); */
88     /* MAT3print(LOCAL, stdout); */
89
90     /* Derive the local UP transformation matrix based on *geodetic*
91      * coordinates */
92     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
93     MAT3rotate(R, vec, FG_Longitude);     /* R = rotate about Z axis */
94     /* printf("Longitude matrix\n"); */
95     /* MAT3print(R, stdout); */
96
97     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
98     MAT3mult_vec(vec, vec, R);
99     MAT3rotate(TMP, vec, -FG_Latitude);  /* TMP = rotate about X axis */
100     /* printf("Latitude matrix\n"); */
101     /* MAT3print(TMP, stdout); */
102
103     MAT3mult(UP, R, TMP);
104     /* printf("Local up matrix\n"); */
105     /* MAT3print(UP, stdout); */
106
107     MAT3_SET_VEC(v->local_up, 1.0, 0.0, 0.0);
108     MAT3mult_vec(v->local_up, v->local_up, UP);
109
110     printf("    Local Up = (%.4f, %.4f, %.4f)\n", 
111            v->local_up[0], v->local_up[1], v->local_up[2]);
112     
113     /* Alternative method to Derive local up vector based on
114      * *geodetic* coordinates */
115     /* alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0); */
116     /* printf("    Alt Up = (%.4f, %.4f, %.4f)\n", 
117        alt_up.x, alt_up.y, alt_up.z); */
118
119     /* Derive the VIEW matrix */
120     MAT3mult(VIEW, LOCAL, UP);
121     /* printf("VIEW matrix\n"); */
122     /* MAT3print(VIEW, stdout); */
123
124     /* generate the current up, forward, and fwrd-view vectors */
125     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
126     MAT3mult_vec(v->view_up, vec, VIEW);
127
128     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
129     MAT3mult_vec(forward, vec, VIEW);
130     printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
131            forward[2]);
132
133     MAT3rotate(TMP, v->view_up, v->view_offset);
134     MAT3mult_vec(v->view_forward, forward, TMP);
135
136 }
137
138
139 /* $Log$
140 /* Revision 1.4  1997/12/17 23:13:36  curt
141 /* Began working on rendering a sky.
142 /*
143  * Revision 1.3  1997/12/15 23:54:50  curt
144  * Add xgl wrappers for debugging.
145  * Generate terrain normals on the fly.
146  *
147  * Revision 1.2  1997/12/10 22:37:48  curt
148  * Prepended "fg" on the name of all global structures that didn't have it yet.
149  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
150  *
151  * Revision 1.1  1997/08/27 21:31:17  curt
152  * Initial revision.
153  *
154  */