1 /**************************************************************************
2 * GLmain.c -- top level sim routines
4 * Written by Curtis Olson for OpenGL, started May 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 **************************************************************************/
37 /* assumes -I/usr/include/mesa in compile command */
42 #include "../constants.h"
44 #include "../Aircraft/aircraft.h"
45 #include "../Scenery/mesh.h"
46 #include "../Scenery/scenery.h"
47 #include "../Math/mat3.h"
48 #include "../Math/polar.h"
49 #include "../Timer/fg_timer.h"
50 #include "../Utils/fg_random.h"
53 /* This is a record containing all the info for the aircraft currently
55 struct aircraft_params current_aircraft;
58 static GLfloat win_ratio = 1.0;
61 static GLfloat sun_vec[4] = {-3.0, 1.0, 2.0, 0.0 };
64 /* extern struct mesh *mesh_ptr; */
65 /* Function prototypes */
66 /* GLint fgSceneryCompile_OLD(); */
67 /* static void fgSceneryDraw_OLD(); */
69 /* pointer to scenery structure */
70 /* static GLint scenery, runway; */
73 double fogDensity = 60.0; /* in meters = about 70 miles */
74 double view_offset = 0.0;
75 double goal_view_offset = 0.0;
78 #define DEFAULT_TIMER_HZ 20
79 #define DEFAULT_MULTILOOP 6
80 #define DEFAULT_MODEL_HZ (DEFAULT_TIMER_HZ * DEFAULT_MULTILOOP)
88 /**************************************************************************
89 * fgInitVisuals() -- Initialize various GL/view parameters
90 **************************************************************************/
92 static void fgInitVisuals() {
93 /* if the 4th field is 0.0, this specifies a direction ... */
94 static GLfloat fogColor[4] = {0.65, 0.65, 0.85, 1.0};
96 glEnable( GL_DEPTH_TEST );
97 /* glFrontFace(GL_CW); */
98 glEnable( GL_CULL_FACE );
100 /* If enabled, normal vectors specified with glNormal are scaled
101 to unit length after transformation. See glNormal. */
102 glEnable( GL_NORMALIZE );
104 glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
105 glEnable( GL_LIGHTING );
106 glEnable( GL_LIGHT0 );
108 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
111 glFogi (GL_FOG_MODE, GL_LINEAR);
112 /* glFogf (GL_FOG_START, 1.0); */
113 glFogf (GL_FOG_END, fogDensity);
114 glFogfv (GL_FOG_COLOR, fogColor);
115 /* glFogf (GL_FOG_DENSITY, fogDensity); */
116 /* glHint (GL_FOG_HINT, GL_FASTEST); */
118 glClearColor(0.6, 0.6, 0.9, 1.0);
122 /**************************************************************************
123 * Update the view volume, position, and orientation
124 **************************************************************************/
126 static void fgUpdateViewParams() {
127 struct fgCartesianPoint view_pos;
128 struct flight_params *f;
130 MAT3vec vec, up, forward, fwrd_view;
132 f = ¤t_aircraft.flight;
134 /* Tell GL we are about to modify the projection parameters */
135 glMatrixMode(GL_PROJECTION);
137 gluPerspective(45.0, 1.0/win_ratio, 0.1, 200000.0);
139 glMatrixMode(GL_MODELVIEW);
142 /* calculate position in current FG view coordinate system */
143 view_pos = fgGeodetic2Cartesian(FG_Longitude, FG_Latitude);
144 view_pos = fgRotateCartesianPoint(view_pos);
146 printf("*** Altitude = %.2f meters\n", FG_Altitude * FEET_TO_METER);
148 /* build current rotation matrix */
149 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
150 MAT3rotate(R, vec, FG_Phi);
151 /* printf("Roll matrix\n"); */
152 /* MAT3print(R, stdout); */
154 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
155 /* MAT3mult_vec(vec, vec, R); */
156 MAT3rotate(TMP, vec, -FG_Theta);
157 /* printf("Pitch matrix\n"); */
158 /* MAT3print(TMP, stdout); */
161 MAT3_SET_VEC(vec, 0.0, 0.0, -1.0);
162 /* MAT3mult_vec(vec, vec, R); */
163 /* MAT3rotate(TMP, vec, FG_PI + FG_PI_2 + FG_Psi + view_offset); */
164 MAT3rotate(TMP, vec, FG_Psi - FG_PI_2);
165 /* printf("Yaw matrix\n");
166 MAT3print(TMP, stdout); */
169 /* MAT3print(R, stdout); */
171 /* generate the current up, forward, and fwrd-view vectors */
172 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
173 MAT3mult_vec(up, vec, R);
175 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
176 MAT3mult_vec(forward, vec, R);
177 printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
180 MAT3rotate(TMP, up, view_offset);
181 MAT3mult_vec(fwrd_view, forward, TMP);
183 printf("View pos = %.4f, %.4f, %.4f\n", view_pos.y, view_pos.z,
184 FG_Altitude * FEET_TO_METER);
185 gluLookAt(view_pos.y, view_pos.z, FG_Altitude*FEET_TO_METER * 0.0011,
186 view_pos.y + fwrd_view[0], view_pos.z + fwrd_view[1],
187 FG_Altitude*FEET_TO_METER * 0.001 + fwrd_view[2],
188 up[0], up[1], up[2]);
190 glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
194 /**************************************************************************
195 * Update all Visuals (redraws anything graphics related)
196 **************************************************************************/
198 static void fgUpdateVisuals( void ) {
199 /* update view volume parameters */
200 fgUpdateViewParams();
202 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
204 /* Tell GL we are switching to model view parameters */
205 glMatrixMode(GL_MODELVIEW);
206 /* glLoadIdentity(); */
219 /**************************************************************************
220 * Update internal time dependent calculations (i.e. flight model)
221 **************************************************************************/
223 void fgUpdateTimeDepCalcs(int multi_loop) {
224 struct flight_params *f;
227 f = ¤t_aircraft.flight;
229 /* update the flight model */
230 if ( multi_loop < 0 ) {
231 multi_loop = DEFAULT_MULTILOOP;
234 /* printf("updating flight model x %d\n", multi_loop); */
235 fgFlightModelUpdate(FG_LARCSIM, f, multi_loop);
237 for ( i = 0; i < multi_loop; i++ ) {
238 if ( fabs(goal_view_offset - view_offset) < 0.05 ) {
239 view_offset = goal_view_offset;
242 /* move view_offset towards goal_view_offset */
243 if ( goal_view_offset > view_offset ) {
244 if ( goal_view_offset - view_offset < FG_PI ) {
250 if ( view_offset - goal_view_offset < FG_PI ) {
256 if ( view_offset > FG_2PI ) {
257 view_offset -= FG_2PI;
258 } else if ( view_offset < 0 ) {
259 view_offset += FG_2PI;
266 void fgInitTimeDepCalcs() {
267 /* initialize timer */
270 fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
276 /**************************************************************************
277 * Scenery management routines
278 **************************************************************************/
280 /* static void fgSceneryInit_OLD() { */
282 /* scenery = fgSceneryCompile_OLD();
283 runway = fgRunwayHack_OLD(0.69, 53.07);
287 /* create the scenery */
288 /* GLint fgSceneryCompile_OLD() {
291 scenery = mesh2GL(mesh_ptr_OLD);
297 /* hack in a runway */
298 /* GLint fgRunwayHack_OLD(double width, double length) {
299 static GLfloat concrete[4] = { 0.5, 0.5, 0.5, 1.0 };
300 static GLfloat line[4] = { 0.9, 0.9, 0.9, 1.0 };
303 float line_len, line_width_2, cur_pos;
305 runway = glGenLists(1);
306 glNewList(runway, GL_COMPILE);
309 /* glBegin(GL_POLYGON);
310 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, concrete );
311 glNormal3f(0.0, 0.0, 1.0);
313 glVertex3d( 0.0, -width/2.0, 0.0);
314 glVertex3d( 0.0, width/2.0, 0.0);
315 glVertex3d(length, width/2.0, 0.0);
316 glVertex3d(length, -width/2.0, 0.0);
319 /* draw center line */
320 /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, line );
321 line_len = length / ( 2 * num_lines + 1);
322 printf("line_len = %.3f\n", line_len);
325 for ( i = 0; i < num_lines; i++ ) {
327 glVertex3d( cur_pos, -line_width_2, 0.005);
328 glVertex3d( cur_pos, line_width_2, 0.005);
330 glVertex3d( cur_pos, line_width_2, 0.005);
331 glVertex3d( cur_pos, -line_width_2, 0.005);
342 /* draw the scenery */
343 /*static void fgSceneryDraw_OLD() {
344 static float z = 32.35;
350 printf("*** Drawing runway at %.2f\n", z);
352 glTranslatef( -398391.28, 120070.41, 32.35);
353 glRotatef(170.0, 0.0, 0.0, 1.0);
360 /* What should we do when we have nothing else to do? How about get
361 * ready for the next move and update the display? */
362 static void fgMainLoop( void ) {
363 static int remainder = 0;
364 int elapsed, multi_loop;
366 struct flight_params *f;
368 f = ¤t_aircraft.flight;
370 elapsed = fgGetTimeInterval();
371 printf("Time interval is = %d, previous remainder is = %d\n", elapsed,
373 printf("--> Frame rate is = %.2f\n", 1000.0 / (float)elapsed);
374 elapsed += remainder;
376 multi_loop = ((float)elapsed * 0.001) * DEFAULT_MODEL_HZ;
377 remainder = elapsed - ((multi_loop*1000) / DEFAULT_MODEL_HZ);
378 printf("Model iterations needed = %d, new remainder = %d\n", multi_loop,
384 if ( ! use_signals ) {
385 fgUpdateTimeDepCalcs(multi_loop);
388 /* I'm just sticking this here for now, it should probably move
390 rough_elev = mesh_altitude(FG_Longitude * RAD_TO_DEG * 3600.0,
391 FG_Latitude * RAD_TO_DEG * 3600.0);
392 printf("Ground elevation is about %.2f meters here.\n", rough_elev);
393 /* FG_Runway_altitude = rough_elev * METER_TO_FEET; */
395 if ( FG_Altitude * FEET_TO_METER < rough_elev ) {
396 /* set this here, otherwise if we set runway height above our
397 current height we get a really nasty bounce. */
398 FG_Runway_altitude = FG_Altitude - 3.758099;
400 /* now set aircraft altitude above ground */
401 FG_Altitude = rough_elev * METER_TO_FEET + 3.758099;
402 printf("<*> resetting altitude to %.0f meters\n",
403 FG_Altitude * FEET_TO_METER);
406 FG_U_gust = fg_random() * 1.0 - 0.5;
407 FG_V_gust = fg_random() * 1.0 - 0.5;
408 FG_W_gust = fg_random() * 1.0 - 0.5;
412 /**************************************************************************
413 * Handle new window size or exposure
414 **************************************************************************/
416 static void fgReshape( int width, int height ) {
417 /* Do this so we can call fgReshape(0,0) ourselves without having to know
418 * what the values of width & height are. */
419 if ( (height > 0) && (width > 0) ) {
420 win_ratio = (GLfloat) height / (GLfloat) width;
423 /* Inform gl of our view window size */
424 glViewport(0, 0, (GLint)width, (GLint)height);
426 fgUpdateViewParams();
428 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
432 /**************************************************************************
434 **************************************************************************/
436 int main( int argc, char *argv[] ) {
437 struct flight_params *f;
440 f = ¤t_aircraft.flight;
442 /* might as well do this first thing ... seed the random number generater */
445 printf("Flight Gear: prototype code to test OpenGL, LaRCsim, and VRML\n\n");
448 /**********************************************************************
449 * Initialize the Window/Graphics environment.
450 **********************************************************************/
453 /* initialize GLUT */
454 glutInit(&argc, argv);
456 /* Define Display Parameters */
457 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
459 /* Define initial window size */
460 glutInitWindowSize(640, 480);
462 /* Initialize the main window */
463 glutCreateWindow("Flight Gear");
465 /* Define initial window size */
466 tkInitPosition(0, 0, 640, 480);
468 /* Define Display Parameters */
469 tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
471 /* Initialize the main window */
472 if (tkInitWindow("Flight Gear") == GL_FALSE) {
477 /* setup view parameters, only makes GL calls */
481 /****************************************************************
482 * The following section sets up the flight model EOM parameters and
483 * should really be read in from one or more files.
484 ****************************************************************/
486 /* Globe Aiport, AZ */
487 FG_Runway_altitude = 3234.5;
488 FG_Runway_latitude = 120070.41;
489 FG_Runway_longitude = -398391.28;
490 FG_Runway_heading = 102.0 * DEG_TO_RAD;
492 /* Initial Position */
493 FG_Latitude = ( 120070.41 / 3600.0 ) * DEG_TO_RAD;
494 FG_Longitude = ( -398391.28 / 3600.0 ) * DEG_TO_RAD;
495 FG_Altitude = FG_Runway_altitude + 3.758099;
497 /* FG_Latitude = 0.0; */
498 /* FG_Longitude = 0.0; */
499 /* FG_Altitude = 15000.0; */
501 printf("Initial position is: (%.4f, %.4f, %.2f)\n", FG_Latitude,
502 FG_Longitude, FG_Altitude);
504 /* Initial Velocity */
505 FG_V_north = 0.0 /* 7.287719E+00 */;
506 FG_V_east = 0.0 /* 1.521770E+03 */;
507 FG_V_down = 0.0 /* -1.265722E-05 */;
509 /* Initial Orientation */
510 FG_Phi = -2.658474E-06;
511 FG_Theta = 7.401790E-03;
512 FG_Psi = 270.0 * DEG_TO_RAD;
514 /* Initial Angular B rates */
515 FG_P_body = 7.206685E-05;
516 FG_Q_body = 0.000000E+00;
517 FG_R_body = 9.492658E-05;
519 FG_Earth_position_angle = 0.000000E+00;
521 /* Mass properties and geometry values */
522 FG_Mass = 8.547270E+01;
523 FG_I_xx = 1.048000E+03;
524 FG_I_yy = 3.000000E+03;
525 FG_I_zz = 3.530000E+03;
526 FG_I_xz = 0.000000E+00;
528 /* CG position w.r.t. ref. point */
529 FG_Dx_cg = 0.000000E+00;
530 FG_Dy_cg = 0.000000E+00;
531 FG_Dz_cg = 0.000000E+00;
533 /* Configure some wind & turbulance */
534 FG_V_north_airmass = 15; /* ft/s =~ 10mph */
536 /* Set initial position and slew parameters */
537 /* fgSlewInit(-398391.3, 120070.41, 244, 3.1415); */ /* GLOBE Airport */
538 /* fgSlewInit(-335340,162540, 15, 4.38); */
539 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
541 /* Initialize the Scenery Management system */
544 /* Tell the Scenery Management system where we are so it can load
545 * the correct scenery data */
546 fgSceneryUpdate(FG_Latitude, FG_Longitude, FG_Altitude);
548 /* I'm just sticking this here for now, it should probably move
550 rough_elev = mesh_altitude(FG_Longitude * RAD_TO_DEG * 3600.0,
551 FG_Latitude * RAD_TO_DEG * 3600.0);
552 printf("Ground elevation is %.2f meters here.\n", rough_elev);
553 FG_Runway_altitude = rough_elev * METER_TO_FEET;
555 if ( FG_Altitude < FG_Runway_altitude ) {
556 FG_Altitude = FG_Runway_altitude + 3.758099;
558 /* end of thing that I just stuck in that I should probably move */
560 /* Initialize the flight model data structures base on above values */
561 fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
564 /* init timer routines, signals, etc. Arrange for an alarm
565 signal to be generated, etc. */
566 fgInitTimeDepCalcs();
569 /**********************************************************************
570 * Initialize the Event Handlers.
571 **********************************************************************/
574 /* call fgReshape() on window resizes */
575 glutReshapeFunc( fgReshape );
577 /* call key() on keyboard event */
578 glutKeyboardFunc( GLUTkey );
579 glutSpecialFunc( GLUTspecialkey );
581 /* call fgMainLoop() whenever there is nothing else to do */
582 glutIdleFunc( fgMainLoop );
585 glutDisplayFunc( fgUpdateVisuals );
587 /* pass control off to the GLUT event handler */
590 /* call fgReshape() on expose events */
591 tkExposeFunc( fgReshape );
593 /* call fgReshape() on window resizes */
594 tkReshapeFunc( fgReshape );
596 /* call key() on keyboard event */
597 tkKeyDownFunc( GLTKkey );
599 /* call fgMainLoop() whenever there is nothing else to do */
600 tkIdleFunc( fgMainLoop );
603 tkDisplayFunc( fgUpdateVisuals );
605 /* pass control off to the tk event handler */
615 int printf (const char *format, ...) {
622 /* Revision 1.37 1997/07/19 22:34:02 curt
623 /* Moved PI definitions to ../constants.h
624 /* Moved random() stuff to ../Utils/ and renamed fg_random()
626 * Revision 1.36 1997/07/18 23:41:25 curt
627 * Tweaks for building with Cygnus Win32 compiler.
629 * Revision 1.35 1997/07/18 14:28:34 curt
630 * Hacked in some support for wind/turbulence.
632 * Revision 1.34 1997/07/16 20:04:48 curt
633 * Minor tweaks to aid Win32 port.
635 * Revision 1.33 1997/07/12 03:50:20 curt
636 * Added an #include <Windows32/Base.h> to help compiling for Win32
638 * Revision 1.32 1997/07/11 03:23:18 curt
639 * Solved some scenery display/orientation problems. Still have a positioning
640 * (or transformation?) problem.
642 * Revision 1.31 1997/07/11 01:29:58 curt
643 * More tweaking of terrian floor.
645 * Revision 1.30 1997/07/10 04:26:37 curt
646 * We now can interpolated ground elevation for any position in the grid. We
647 * can use this to enforce a "hard" ground. We still need to enforce some
648 * bounds checking so that we don't try to lookup data points outside the
651 * Revision 1.29 1997/07/09 21:31:12 curt
652 * Working on making the ground "hard."
654 * Revision 1.28 1997/07/08 18:20:12 curt
655 * Working on establishing a hard ground.
657 * Revision 1.27 1997/07/07 20:59:49 curt
658 * Working on scenery transformations to enable us to fly fluidly over the
659 * poles with no discontinuity/distortion in scenery.
661 * Revision 1.26 1997/07/05 20:43:34 curt
662 * renamed mat3 directory to Math so we could add other math related routines.
664 * Revision 1.25 1997/06/29 21:19:17 curt
665 * Working on scenery management system.
667 * Revision 1.24 1997/06/26 22:14:53 curt
668 * Beginning work on a scenery management system.
670 * Revision 1.23 1997/06/26 19:08:33 curt
671 * Restructuring make, adding automatic "make dep" support.
673 * Revision 1.22 1997/06/25 15:39:47 curt
674 * Minor changes to compile with rsxnt/win32.
676 * Revision 1.21 1997/06/22 21:44:41 curt
677 * Working on intergrating the VRML (subset) parser.
679 * Revision 1.20 1997/06/21 17:12:53 curt
680 * Capitalized subdirectory names.
682 * Revision 1.19 1997/06/18 04:10:31 curt
683 * A couple more runway tweaks ...
685 * Revision 1.18 1997/06/18 02:21:24 curt
688 * Revision 1.17 1997/06/17 16:51:58 curt
689 * Timer interval stuff now uses gettimeofday() instead of ftime()
691 * Revision 1.16 1997/06/17 04:19:16 curt
692 * More timer related tweaks with respect to view direction changes.
694 * Revision 1.15 1997/06/17 03:41:10 curt
695 * Nonsignal based interval timing is now working.
696 * This would be a good time to look at cleaning up the code structure a bit.
698 * Revision 1.14 1997/06/16 19:32:51 curt
699 * Starting to add general timer support.
701 * Revision 1.13 1997/06/02 03:40:06 curt
702 * A tiny bit more view tweaking.
704 * Revision 1.12 1997/06/02 03:01:38 curt
705 * Working on views (side, front, back, transitions, etc.)
707 * Revision 1.11 1997/05/31 19:16:25 curt
708 * Elevator trim added.
710 * Revision 1.10 1997/05/31 04:13:52 curt
713 * Continuing work on the LaRCsim flight model integration.
714 * Added some MSFS-like keyboard input handling.
716 * Revision 1.9 1997/05/30 19:27:01 curt
717 * The LaRCsim flight model is starting to look like it is working.
719 * Revision 1.8 1997/05/30 03:54:10 curt
720 * Made a bit more progress towards integrating the LaRCsim flight model.
722 * Revision 1.7 1997/05/29 22:39:49 curt
723 * Working on incorporating the LaRCsim flight model.
725 * Revision 1.6 1997/05/29 12:31:39 curt
726 * Minor tweaks, moving towards general flight model integration.
728 * Revision 1.5 1997/05/29 02:33:23 curt
729 * Updated to reflect changing interfaces in other "modules."
731 * Revision 1.4 1997/05/27 17:44:31 curt
732 * Renamed & rearranged variables and routines. Added some initial simple
733 * timer/alarm routines so the flight model can be updated on a regular
736 * Revision 1.3 1997/05/23 15:40:25 curt
737 * Added GNU copyright headers.
740 * Revision 1.2 1997/05/23 00:35:12 curt
741 * Trying to get fog to work ...
743 * Revision 1.1 1997/05/21 15:57:51 curt
744 * Renamed due to added GLUT support.
746 * Revision 1.3 1997/05/19 18:22:42 curt
747 * Parameter tweaking ... starting to stub in fog support.
749 * Revision 1.2 1997/05/17 00:17:34 curt
750 * Trying to stub in support for standard OpenGL.
752 * Revision 1.1 1997/05/16 16:05:52 curt