]> git.mxchange.org Git - flightgear.git/blob - Main/views.cxx
Added command line rendering options:
[flightgear.git] / Main / views.cxx
1 //
2 // views.cxx -- 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 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <Debug/fg_debug.h>
32 #include <Flight/flight.h>
33 #include <Include/fg_constants.h>
34 #include <Math/mat3.h>
35 #include <Math/polar.h>
36 #include <Math/vector.h>
37 #include <Scenery/scenery.hxx>
38 #include <Time/fg_time.hxx>
39
40 #include "views.hxx"
41
42
43 // This is a record containing current view parameters
44 fgVIEW current_view;
45
46
47 // Initialize a view structure
48 void fgViewInit(fgVIEW *v) {
49     fgPrintf( FG_VIEW, FG_INFO, "Initializing View parameters\n");
50
51     v->view_offset = 0.0;
52     v->goal_view_offset = 0.0;
53 }
54
55
56 // Update the view parameters
57 void fgViewUpdate(fgFLIGHT *f, fgVIEW *v, fgLIGHT *l) {
58     MAT3vec vec, forward, v0, minus_z;
59     MAT3mat R, TMP, UP, LOCAL, VIEW;
60     double ntmp;
61
62     scenery.center.x = scenery.next_center.x;
63     scenery.center.y = scenery.next_center.y;
64     scenery.center.z = scenery.next_center.z;
65
66     // calculate the cartesion coords of the current lat/lon/0 elev
67     v->cur_zero_elev = fgPolarToCart(FG_Longitude, FG_Lat_geocentric, 
68                                      FG_Sea_level_radius * FEET_TO_METER);
69     v->cur_zero_elev.x -= scenery.center.x;
70     v->cur_zero_elev.y -= scenery.center.y;
71     v->cur_zero_elev.z -= scenery.center.z;
72
73     // calculate view position in current FG view coordinate system
74     v->abs_view_pos = fgPolarToCart(FG_Longitude, FG_Lat_geocentric, 
75                              FG_Radius_to_vehicle * FEET_TO_METER + 1.0);
76     v->view_pos.x = v->abs_view_pos.x - scenery.center.x;
77     v->view_pos.y = v->abs_view_pos.y - scenery.center.y;
78     v->view_pos.z = v->abs_view_pos.z - scenery.center.z;
79
80     fgPrintf( FG_VIEW, FG_DEBUG, "Absolute view pos = %.4f, %.4f, %.4f\n", 
81            v->abs_view_pos.x, v->abs_view_pos.y, v->abs_view_pos.z);
82     fgPrintf( FG_VIEW, FG_DEBUG, "Relative view pos = %.4f, %.4f, %.4f\n", 
83            v->view_pos.x, v->view_pos.y, v->view_pos.z);
84
85     // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
86     // from FG_T_local_to_body[3][3]
87
88     // Question: Why is the LaRCsim matrix arranged so differently
89     // than the one we need???
90     LOCAL[0][0] = FG_T_local_to_body_33;
91     LOCAL[0][1] = -FG_T_local_to_body_32;
92     LOCAL[0][2] = -FG_T_local_to_body_31;
93     LOCAL[0][3] = 0.0;
94     LOCAL[1][0] = -FG_T_local_to_body_23;
95     LOCAL[1][1] = FG_T_local_to_body_22;
96     LOCAL[1][2] = FG_T_local_to_body_21;
97     LOCAL[1][3] = 0.0;
98     LOCAL[2][0] = -FG_T_local_to_body_13;
99     LOCAL[2][1] = FG_T_local_to_body_12;
100     LOCAL[2][2] = FG_T_local_to_body_11;
101     LOCAL[2][3] = 0.0;
102     LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
103     LOCAL[3][3] = 1.0;
104     // printf("LaRCsim LOCAL matrix\n");
105     // MAT3print(LOCAL, stdout);
106
107 #ifdef OLD_LOCAL_TO_BODY_CODE
108         // old code to calculate LOCAL matrix calculated from Phi,
109         // Theta, and Psi (roll, pitch, yaw)
110
111         MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
112         MAT3rotate(R, vec, FG_Phi);
113         /* printf("Roll matrix\n"); */
114         /* MAT3print(R, stdout); */
115
116         MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
117         /* MAT3mult_vec(vec, vec, R); */
118         MAT3rotate(TMP, vec, FG_Theta);
119         /* printf("Pitch matrix\n"); */
120         /* MAT3print(TMP, stdout); */
121         MAT3mult(R, R, TMP);
122
123         MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
124         /* MAT3mult_vec(vec, vec, R); */
125         /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
126         MAT3rotate(TMP, vec, -FG_Psi);
127         /* printf("Yaw matrix\n");
128            MAT3print(TMP, stdout); */
129         MAT3mult(LOCAL, R, TMP);
130         // printf("FG derived LOCAL matrix\n");
131         // MAT3print(LOCAL, stdout);
132 #endif // OLD_LOCAL_TO_BODY_CODE
133
134     // Derive the local UP transformation matrix based on *geodetic*
135     // coordinates
136     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
137     MAT3rotate(R, vec, FG_Longitude);     // R = rotate about Z axis
138     // printf("Longitude matrix\n");
139     // MAT3print(R, stdout);
140
141     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
142     MAT3mult_vec(vec, vec, R);
143     MAT3rotate(TMP, vec, -FG_Latitude);  // TMP = rotate about X axis
144     // printf("Latitude matrix\n");
145     // MAT3print(TMP, stdout);
146
147     MAT3mult(UP, R, TMP);
148     // printf("Local up matrix\n");
149     // MAT3print(UP, stdout);
150
151     MAT3_SET_VEC(v->local_up, 1.0, 0.0, 0.0);
152     MAT3mult_vec(v->local_up, v->local_up, UP);
153
154     // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
155     //         v->local_up[0], v->local_up[1], v->local_up[2]);
156     
157     // Alternative method to Derive local up vector based on
158     // *geodetic* coordinates
159     // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
160     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
161     //         alt_up.x, alt_up.y, alt_up.z);
162
163     // Calculate the VIEW matrix
164     MAT3mult(VIEW, LOCAL, UP);
165     // printf("VIEW matrix\n");
166     // MAT3print(VIEW, stdout);
167
168     // generate the current up, forward, and fwrd-view vectors
169     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
170     MAT3mult_vec(v->view_up, vec, VIEW);
171
172     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
173     MAT3mult_vec(forward, vec, VIEW);
174     // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
175     //         forward[2]);
176
177     MAT3rotate(TMP, v->view_up, v->view_offset);
178     MAT3mult_vec(v->view_forward, forward, TMP);
179
180     // make a vector to the current view position
181     MAT3_SET_VEC(v0, v->view_pos.x, v->view_pos.y, v->view_pos.z);
182
183     // Given a vector pointing straight down (-Z), map into onto the
184     // local plane representing "horizontal".  This should give us the
185     // local direction for moving "south".
186     MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
187     map_vec_onto_cur_surface_plane(v->local_up, v0, minus_z, v->surface_south);
188     MAT3_NORMALIZE_VEC(v->surface_south, ntmp);
189     // printf( "Surface direction directly south %.2f %.2f %.2f\n",
190     //         v->surface_south[0], v->surface_south[1], v->surface_south[2]);
191
192     // now calculate the surface east vector
193     MAT3rotate(TMP, v->view_up, FG_PI_2);
194     MAT3mult_vec(v->surface_east, v->surface_south, TMP);
195     // printf( "Surface direction directly east %.2f %.2f %.2f\n",
196     //         v->surface_east[0], v->surface_east[1], v->surface_east[2]);
197     // printf( "Should be close to zero = %.2f\n", 
198     //         MAT3_DOT_PRODUCT(v->surface_south, v->surface_east));
199 }
200
201
202 // $Log$
203 // Revision 1.7  1998/04/30 12:34:20  curt
204 // Added command line rendering options:
205 //   enable/disable fog/haze
206 //   specify smooth/flat shading
207 //   disable sky blending and just use a solid color
208 //   enable wireframe drawing mode
209 //
210 // Revision 1.6  1998/04/28 01:20:23  curt
211 // Type-ified fgTIME and fgVIEW.
212 // Added a command line option to disable textures.
213 //
214 // Revision 1.5  1998/04/26 05:10:04  curt
215 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
216 //
217 // Revision 1.4  1998/04/25 22:04:53  curt
218 // Use already calculated LaRCsim values to create the roll/pitch/yaw
219 // transformation matrix (we call it LOCAL)
220 //
221 // Revision 1.3  1998/04/25 20:24:02  curt
222 // Cleaned up initialization sequence to eliminate interdependencies
223 // between sun position, lighting, and view position.  This creates a
224 // valid single pass initialization path.
225 //
226 // Revision 1.2  1998/04/24 00:49:22  curt
227 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
228 // Trying out some different option parsing code.
229 // Some code reorganization.
230 //
231 // Revision 1.1  1998/04/22 13:25:45  curt
232 // C++ - ifing the code.
233 // Starting a bit of reorganization of lighting code.
234 //
235 // Revision 1.16  1998/04/18 04:11:29  curt
236 // Moved fg_debug to it's own library, added zlib support.
237 //
238 // Revision 1.15  1998/02/20 00:16:24  curt
239 // Thursday's tweaks.
240 //
241 // Revision 1.14  1998/02/09 15:07:50  curt
242 // Minor tweaks.
243 //
244 // Revision 1.13  1998/02/07 15:29:45  curt
245 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
246 // <chotchkiss@namg.us.anritsu.com>
247 //
248 // Revision 1.12  1998/01/29 00:50:28  curt
249 // Added a view record field for absolute x, y, z position.
250 //
251 // Revision 1.11  1998/01/27 00:47:58  curt
252 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
253 // system and commandline/config file processing code.
254 //
255 // Revision 1.10  1998/01/19 19:27:09  curt
256 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
257 // This should simplify things tremendously.
258 //
259 // Revision 1.9  1998/01/13 00:23:09  curt
260 // Initial changes to support loading and management of scenery tiles.  Note,
261 // there's still a fair amount of work left to be done.
262 //
263 // Revision 1.8  1997/12/30 22:22:33  curt
264 // Further integration of event manager.
265 //
266 // Revision 1.7  1997/12/30 20:47:45  curt
267 // Integrated new event manager with subsystem initializations.
268 //
269 // Revision 1.6  1997/12/22 04:14:32  curt
270 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
271 //
272 // Revision 1.5  1997/12/18 04:07:02  curt
273 // Worked on properly translating and positioning the sky dome.
274 //
275 // Revision 1.4  1997/12/17 23:13:36  curt
276 // Began working on rendering a sky.
277 //
278 // Revision 1.3  1997/12/15 23:54:50  curt
279 // Add xgl wrappers for debugging.
280 // Generate terrain normals on the fly.
281 //
282 // Revision 1.2  1997/12/10 22:37:48  curt
283 // Prepended "fg" on the name of all global structures that didn't have it yet.
284 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
285 //
286 // Revision 1.1  1997/08/27 21:31:17  curt
287 // Initial revision.
288 //