]> git.mxchange.org Git - flightgear.git/blob - Main/views.cxx
C++ - ified views.[ch]xx
[flightgear.git] / Main / views.cxx
1 // views.cxx -- data structures and routines for managing and view
2 //               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 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <Debug/fg_debug.h>
31 #include <Flight/flight.h>
32 #include <Include/fg_constants.h>
33 #include <Math/mat3.h>
34 #include <Math/polar3d.h>
35 #include <Math/vector.h>
36 #include <Scenery/scenery.hxx>
37 #include <Time/fg_time.hxx>
38
39 #include "options.hxx"
40 #include "views.hxx"
41
42
43 // This is a record containing current view parameters
44 fgVIEW current_view;
45
46
47 // Constructor
48 fgVIEW::fgVIEW( void ) {
49 }
50
51
52 // Initialize a view structure
53 void fgVIEW::Init( void ) {
54     fgPrintf( FG_VIEW, FG_INFO, "Initializing View parameters\n");
55
56     view_offset = 0.0;
57     goal_view_offset = 0.0;
58 }
59
60
61 // Update the view parameters
62 void fgVIEW::Update( fgFLIGHT *f ) {
63     fgOPTIONS *o;
64     fgPolarPoint3d p;
65     MAT3vec vec, forward, v0, minus_z;
66     MAT3mat R, TMP, UP, LOCAL, VIEW;
67     double theta_x, theta_y, ntmp;
68
69     o = &current_options;
70
71     scenery.center.x = scenery.next_center.x;
72     scenery.center.y = scenery.next_center.y;
73     scenery.center.z = scenery.next_center.z;
74
75     printf("win_ratio = %.2f\n", win_ratio);
76
77     // calculate sin() and cos() of fov / 2 in X direction;
78     theta_x = FG_PI_2 - (o->fov * win_ratio * DEG_TO_RAD) / 2.0;
79     printf("theta_x = %.2f\n", theta_x);
80     sin_fov_x = sin(theta_x);
81     cos_fov_x = cos(theta_x);
82     slope_x = sin_fov_x / cos_fov_x;
83     printf("slope_x = %.2f\n", slope_x);
84
85     // calculate sin() and cos() of fov / 2 in Y direction;
86     theta_y = FG_PI_2 - (o->fov * DEG_TO_RAD) / 2.0;
87     printf("theta_y = %.2f\n", theta_y);
88     sin_fov_y = sin(theta_y);
89     cos_fov_y = cos(theta_y);
90     slope_y = sin_fov_y / cos_fov_y;
91     printf("slope_y = %.2f\n", slope_y);
92
93     // calculate the cartesion coords of the current lat/lon/0 elev
94     p.lon = FG_Longitude;
95     p.lat = FG_Lat_geocentric;
96     p.radius = FG_Sea_level_radius * FEET_TO_METER;
97
98     cur_zero_elev = fgPolarToCart3d(p);
99
100     cur_zero_elev.x -= scenery.center.x;
101     cur_zero_elev.y -= scenery.center.y;
102     cur_zero_elev.z -= scenery.center.z;
103
104     // calculate view position in current FG view coordinate system
105     // p.lon & p.lat are already defined earlier
106     p.radius = FG_Radius_to_vehicle * FEET_TO_METER + 1.0;
107
108     abs_view_pos = fgPolarToCart3d(p);
109
110     view_pos.x = abs_view_pos.x - scenery.center.x;
111     view_pos.y = abs_view_pos.y - scenery.center.y;
112     view_pos.z = abs_view_pos.z - scenery.center.z;
113
114     fgPrintf( FG_VIEW, FG_DEBUG, "Absolute view pos = %.4f, %.4f, %.4f\n", 
115            abs_view_pos.x, abs_view_pos.y, abs_view_pos.z);
116     fgPrintf( FG_VIEW, FG_DEBUG, "Relative view pos = %.4f, %.4f, %.4f\n", 
117            view_pos.x, view_pos.y, view_pos.z);
118
119     // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
120     // from FG_T_local_to_body[3][3]
121
122     // Question: Why is the LaRCsim matrix arranged so differently
123     // than the one we need???
124     LOCAL[0][0] = FG_T_local_to_body_33;
125     LOCAL[0][1] = -FG_T_local_to_body_32;
126     LOCAL[0][2] = -FG_T_local_to_body_31;
127     LOCAL[0][3] = 0.0;
128     LOCAL[1][0] = -FG_T_local_to_body_23;
129     LOCAL[1][1] = FG_T_local_to_body_22;
130     LOCAL[1][2] = FG_T_local_to_body_21;
131     LOCAL[1][3] = 0.0;
132     LOCAL[2][0] = -FG_T_local_to_body_13;
133     LOCAL[2][1] = FG_T_local_to_body_12;
134     LOCAL[2][2] = FG_T_local_to_body_11;
135     LOCAL[2][3] = 0.0;
136     LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
137     LOCAL[3][3] = 1.0;
138     // printf("LaRCsim LOCAL matrix\n");
139     // MAT3print(LOCAL, stdout);
140
141 #ifdef OLD_LOCAL_TO_BODY_CODE
142         // old code to calculate LOCAL matrix calculated from Phi,
143         // Theta, and Psi (roll, pitch, yaw)
144
145         MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
146         MAT3rotate(R, vec, FG_Phi);
147         /* printf("Roll matrix\n"); */
148         /* MAT3print(R, stdout); */
149
150         MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
151         /* MAT3mult_vec(vec, vec, R); */
152         MAT3rotate(TMP, vec, FG_Theta);
153         /* printf("Pitch matrix\n"); */
154         /* MAT3print(TMP, stdout); */
155         MAT3mult(R, R, TMP);
156
157         MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
158         /* MAT3mult_vec(vec, vec, R); */
159         /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
160         MAT3rotate(TMP, vec, -FG_Psi);
161         /* printf("Yaw matrix\n");
162            MAT3print(TMP, stdout); */
163         MAT3mult(LOCAL, R, TMP);
164         // printf("FG derived LOCAL matrix\n");
165         // MAT3print(LOCAL, stdout);
166 #endif // OLD_LOCAL_TO_BODY_CODE
167
168     // Derive the local UP transformation matrix based on *geodetic*
169     // coordinates
170     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
171     MAT3rotate(R, vec, FG_Longitude);     // R = rotate about Z axis
172     // printf("Longitude matrix\n");
173     // MAT3print(R, stdout);
174
175     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
176     MAT3mult_vec(vec, vec, R);
177     MAT3rotate(TMP, vec, -FG_Latitude);  // TMP = rotate about X axis
178     // printf("Latitude matrix\n");
179     // MAT3print(TMP, stdout);
180
181     MAT3mult(UP, R, TMP);
182     // printf("Local up matrix\n");
183     // MAT3print(UP, stdout);
184
185     MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
186     MAT3mult_vec(local_up, local_up, UP);
187
188     // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
189     //         local_up[0], local_up[1], local_up[2]);
190     
191     // Alternative method to Derive local up vector based on
192     // *geodetic* coordinates
193     // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
194     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
195     //         alt_up.x, alt_up.y, alt_up.z);
196
197     // Calculate the VIEW matrix
198     MAT3mult(VIEW, LOCAL, UP);
199     // printf("VIEW matrix\n");
200     // MAT3print(VIEW, stdout);
201
202     // generate the current up, forward, and fwrd-view vectors
203     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
204     MAT3mult_vec(view_up, vec, VIEW);
205
206     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
207     MAT3mult_vec(forward, vec, VIEW);
208     // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
209     //         forward[2]);
210
211     MAT3rotate(TMP, view_up, view_offset);
212     MAT3mult_vec(view_forward, forward, TMP);
213
214     // make a vector to the current view position
215     MAT3_SET_VEC(v0, view_pos.x, view_pos.y, view_pos.z);
216
217     // Given a vector pointing straight down (-Z), map into onto the
218     // local plane representing "horizontal".  This should give us the
219     // local direction for moving "south".
220     MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
221     map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
222     MAT3_NORMALIZE_VEC(surface_south, ntmp);
223     // printf( "Surface direction directly south %.2f %.2f %.2f\n",
224     //         surface_south[0], surface_south[1], surface_south[2]);
225
226     // now calculate the surface east vector
227     MAT3rotate(TMP, view_up, FG_PI_2);
228     MAT3mult_vec(surface_east, surface_south, TMP);
229     // printf( "Surface direction directly east %.2f %.2f %.2f\n",
230     //         surface_east[0], surface_east[1], surface_east[2]);
231     // printf( "Should be close to zero = %.2f\n", 
232     //         MAT3_DOT_PRODUCT(surface_south, surface_east));
233 }
234
235
236 // Update the "World to Eye" transformation matrix
237 // This is most useful for view frustum culling
238 void fgVIEW::UpdateWorldToEye( fgFLIGHT *f ) {
239     MAT3mat R_Phi, R_Theta, R_Psi, R_Lat, R_Lon, T_view;
240     MAT3mat TMP;
241     MAT3hvec vec;
242
243     // Roll Matrix
244     MAT3_SET_HVEC(vec, 0.0, 0.0, -1.0, 1.0);
245     MAT3rotate(R_Phi, vec, FG_Phi);
246     // printf("Roll matrix (Phi)\n");
247     // MAT3print(R_Phi, stdout);
248
249     // Pitch Matrix
250     MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
251     MAT3rotate(R_Theta, vec, FG_Theta);
252     // printf("\nPitch matrix (Theta)\n");
253     // MAT3print(R_Theta, stdout);
254
255     // Yaw Matrix
256     MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
257     MAT3rotate(R_Psi, vec, FG_Psi + FG_PI - view_offset );
258     // printf("\nYaw matrix (Psi)\n");
259     // MAT3print(R_Psi, stdout);
260
261     // Latitude
262     MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
263     // R_Lat = rotate about X axis
264     MAT3rotate(R_Lat, vec, FG_Latitude);
265     // printf("\nLatitude matrix\n");
266     // MAT3print(R_Lat, stdout);
267
268     // Longitude
269     MAT3_SET_HVEC(vec, 0.0, 0.0, 1.0, 1.0);
270     // R_Lon = rotate about Z axis
271     MAT3rotate(R_Lon, vec, FG_Longitude - FG_PI_2 );
272     // printf("\nLongitude matrix\n");
273     // MAT3print(R_Lon, stdout);
274
275     // View position in scenery centered coordinates
276     MAT3_SET_HVEC(vec, view_pos.x, view_pos.y, view_pos.z, 1.0);
277     MAT3translate(T_view, vec);
278     // printf("\nTranslation matrix\n");
279     // MAT3print(T_view, stdout);
280
281     // aircraft roll/pitch/yaw
282     MAT3mult(TMP, R_Phi, R_Theta);
283     MAT3mult(AIRCRAFT, TMP, R_Psi);
284     // printf("\naircraft roll pitch yaw\n");
285     // MAT3print(AIRCRAFT, stdout);
286
287     // lon/lat
288     MAT3mult(WORLD, R_Lat, R_Lon);
289     // printf("\nworld\n");
290     // MAT3print(WORLD, stdout);
291
292     MAT3mult(EYE_TO_WORLD, AIRCRAFT, WORLD);
293     MAT3mult(EYE_TO_WORLD, EYE_TO_WORLD, T_view);
294     // printf("\nEye to world\n");
295     // MAT3print(EYE_TO_WORLD, stdout);
296
297     MAT3invert(WORLD_TO_EYE, EYE_TO_WORLD);
298     // printf("\nWorld to eye\n");
299     // MAT3print(WORLD_TO_EYE, stdout);
300
301     // printf( "\nview_pos = %.2f %.2f %.2f\n", 
302     //         view_pos.x, view_pos.y, view_pos.z );
303
304     // MAT3_SET_HVEC(eye, 0.0, 0.0, 0.0, 1.0);
305     // MAT3mult_vec(vec, eye, EYE_TO_WORLD);
306     // printf("\neye -> world = %.2f %.2f %.2f\n", vec[0], vec[1], vec[2]);
307
308     // MAT3_SET_HVEC(vec1, view_pos.x, view_pos.y, view_pos.z, 1.0);
309     // MAT3mult_vec(vec, vec1, WORLD_TO_EYE);
310     // printf( "\nabs_view_pos -> eye = %.2f %.2f %.2f\n", 
311     //         vec[0], vec[1], vec[2]);
312 }
313
314
315 // Destructor
316 fgVIEW::~fgVIEW( void ) {
317 }
318
319
320 // $Log$
321 // Revision 1.9  1998/05/16 13:08:37  curt
322 // C++ - ified views.[ch]xx
323 // Shuffled some additional view parameters into the fgVIEW class.
324 // Changed tile-radius to tile-diameter because it is a much better
325 //   name.
326 // Added a WORLD_TO_EYE transformation to views.cxx.  This allows us
327 //  to transform world space to eye space for view frustum culling.
328 //
329 // Revision 1.8  1998/05/02 01:51:01  curt
330 // Updated polartocart conversion routine.
331 //
332 // Revision 1.7  1998/04/30 12:34:20  curt
333 // Added command line rendering options:
334 //   enable/disable fog/haze
335 //   specify smooth/flat shading
336 //   disable sky blending and just use a solid color
337 //   enable wireframe drawing mode
338 //
339 // Revision 1.6  1998/04/28 01:20:23  curt
340 // Type-ified fgTIME and fgVIEW.
341 // Added a command line option to disable textures.
342 //
343 // Revision 1.5  1998/04/26 05:10:04  curt
344 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
345 //
346 // Revision 1.4  1998/04/25 22:04:53  curt
347 // Use already calculated LaRCsim values to create the roll/pitch/yaw
348 // transformation matrix (we call it LOCAL)
349 //
350 // Revision 1.3  1998/04/25 20:24:02  curt
351 // Cleaned up initialization sequence to eliminate interdependencies
352 // between sun position, lighting, and view position.  This creates a
353 // valid single pass initialization path.
354 //
355 // Revision 1.2  1998/04/24 00:49:22  curt
356 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
357 // Trying out some different option parsing code.
358 // Some code reorganization.
359 //
360 // Revision 1.1  1998/04/22 13:25:45  curt
361 // C++ - ifing the code.
362 // Starting a bit of reorganization of lighting code.
363 //
364 // Revision 1.16  1998/04/18 04:11:29  curt
365 // Moved fg_debug to it's own library, added zlib support.
366 //
367 // Revision 1.15  1998/02/20 00:16:24  curt
368 // Thursday's tweaks.
369 //
370 // Revision 1.14  1998/02/09 15:07:50  curt
371 // Minor tweaks.
372 //
373 // Revision 1.13  1998/02/07 15:29:45  curt
374 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
375 // <chotchkiss@namg.us.anritsu.com>
376 //
377 // Revision 1.12  1998/01/29 00:50:28  curt
378 // Added a view record field for absolute x, y, z position.
379 //
380 // Revision 1.11  1998/01/27 00:47:58  curt
381 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
382 // system and commandline/config file processing code.
383 //
384 // Revision 1.10  1998/01/19 19:27:09  curt
385 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
386 // This should simplify things tremendously.
387 //
388 // Revision 1.9  1998/01/13 00:23:09  curt
389 // Initial changes to support loading and management of scenery tiles.  Note,
390 // there's still a fair amount of work left to be done.
391 //
392 // Revision 1.8  1997/12/30 22:22:33  curt
393 // Further integration of event manager.
394 //
395 // Revision 1.7  1997/12/30 20:47:45  curt
396 // Integrated new event manager with subsystem initializations.
397 //
398 // Revision 1.6  1997/12/22 04:14:32  curt
399 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
400 //
401 // Revision 1.5  1997/12/18 04:07:02  curt
402 // Worked on properly translating and positioning the sky dome.
403 //
404 // Revision 1.4  1997/12/17 23:13:36  curt
405 // Began working on rendering a sky.
406 //
407 // Revision 1.3  1997/12/15 23:54:50  curt
408 // Add xgl wrappers for debugging.
409 // Generate terrain normals on the fly.
410 //
411 // Revision 1.2  1997/12/10 22:37:48  curt
412 // Prepended "fg" on the name of all global structures that didn't have it yet.
413 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
414 //
415 // Revision 1.1  1997/08/27 21:31:17  curt
416 // Initial revision.
417 //