1 /**************************************************************************
2 * GLUTmain.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 **************************************************************************/
38 #include "../constants.h"
39 #include "../general.h"
41 #include "../Aircraft/aircraft.h"
42 #include "../Cockpit/cockpit.h"
43 #include "../Joystick/joystick.h"
44 #include "../Math/fg_geodesy.h"
45 #include "../Math/mat3.h"
46 #include "../Math/polar.h"
47 #include "../Scenery/mesh.h"
48 #include "../Scenery/scenery.h"
49 #include "../Time/fg_time.h"
50 #include "../Time/fg_timer.h"
51 #include "../Time/sunpos.h"
52 #include "../Weather/weather.h"
55 /* This is a record containing all the info for the aircraft currently
57 struct AIRCRAFT current_aircraft;
59 /* This is a record containing global housekeeping information */
60 struct GENERAL general;
62 /* This is a record containing current weather info */
63 struct WEATHER current_weather;
65 /* This is a record containing current view parameters */
66 struct VIEW current_view;
69 static GLfloat win_ratio = 1.0;
72 /* static GLfloat sun_vec[4] = {1.0, 0.0, 0.0, 0.0 }; */
74 /* if the 4th field is 0.0, this specifies a direction ... */
75 /* clear color (sky) */
76 GLfloat fgClearColor[4] = {0.60, 0.60, 0.90, 1.0};
78 static GLfloat fgFogColor[4] = {0.65, 0.65, 0.85, 1.0};
81 /* extern struct mesh *mesh_ptr; */
82 /* Function prototypes */
83 /* GLint fgSceneryCompile_OLD(); */
84 /* static void fgSceneryDraw_OLD(); */
86 /* pointer to scenery structure */
87 /* static GLint scenery, runway; */
94 /* Yet another hack. This one used by the HUD code. Michele */
98 /**************************************************************************
99 * fgInitVisuals() -- Initialize various GL/view parameters
100 **************************************************************************/
102 static void fgInitVisuals() {
107 l = &cur_light_params;
108 t = &cur_time_params;
109 w = ¤t_weather;
111 glEnable( GL_DEPTH_TEST );
112 /* glFrontFace(GL_CW); */
113 glEnable( GL_CULL_FACE );
115 /* If enabled, normal vectors specified with glNormal are scaled
116 to unit length after transformation. See glNormal. */
117 glEnable( GL_NORMALIZE );
119 glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
120 glEnable( GL_LIGHTING );
121 glEnable( GL_LIGHT0 );
123 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
126 glFogi (GL_FOG_MODE, GL_LINEAR);
127 /* glFogf (GL_FOG_START, 1.0); */
128 glFogf (GL_FOG_END, w->visibility);
129 glFogfv (GL_FOG_COLOR, fgFogColor);
130 /* glFogf (GL_FOG_DENSITY, w->visibility); */
131 /* glHint (GL_FOG_HINT, GL_FASTEST); */
133 glClearColor(fgClearColor[0], fgClearColor[1], fgClearColor[2],
138 /**************************************************************************
139 * Update the view volume, position, and orientation
140 **************************************************************************/
142 static void fgUpdateViewParams() {
147 double x_2, x_4, x_8, x_10;
148 double ambient, diffuse, sky;
149 GLfloat color[4] = { 1.0, 1.0, 0.50, 1.0 };
150 GLfloat amb[3], diff[3], fog[4], clear[4];
152 f = ¤t_aircraft.flight;
153 l = &cur_light_params;
154 t = &cur_time_params;
159 /* Tell GL we are about to modify the projection parameters */
160 glMatrixMode(GL_PROJECTION);
162 gluPerspective(60.0, 1.0/win_ratio, 1.0, 200000.0);
164 glMatrixMode(GL_MODELVIEW);
167 /* set up our view volume */
168 gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
169 v->view_pos.x + v->view_forward[0],
170 v->view_pos.y + v->view_forward[1],
171 v->view_pos.z + v->view_forward[2],
172 v->view_up[0], v->view_up[1], v->view_up[2]);
174 /* set the sun position */
175 glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
177 /* calculate lighting parameters based on sun's relative angle to
179 /* ya kind'a have to plot this to see the magic */
181 /* x = t->sun_angle^8 */
182 x_2 = l->sun_angle * l->sun_angle;
187 ambient = 0.4 * pow(1.1, -x_10 / 30.0);
189 /* diffuse = 0.4 * cos(0.3 * x_2);
190 if ( t->sun_angle > FG_PI_2 + 0.05 ) {
197 sky = 0.85 * pow(1.2, -x_8 / 20.0) + 0.15;
199 /* sky = 0.15; */ /* to force a dark sky (for testing) */
201 if ( ambient < 0.1 ) { ambient = 0.1; }
202 if ( diffuse < 0.0 ) { diffuse = 0.0; }
204 if ( sky < 0.0 ) { sky = 0.0; }
206 amb[0] = color[0] * ambient;
207 amb[1] = color[1] * ambient;
208 amb[2] = color[2] * ambient;
210 diff[0] = color[0] * diffuse;
211 diff[1] = color[1] * diffuse;
212 diff[2] = color[2] * diffuse;
214 /* set lighting parameters */
215 glLightfv(GL_LIGHT0, GL_AMBIENT, amb );
216 glLightfv(GL_LIGHT0, GL_DIFFUSE, diff );
219 fog[0] = fgFogColor[0] * (ambient + diffuse);
220 fog[1] = fgFogColor[1] * (ambient + diffuse);
221 fog[2] = fgFogColor[2] * (ambient + diffuse);
222 fog[3] = fgFogColor[3];
223 glFogfv (GL_FOG_COLOR, fog);
226 clear[0] = fgClearColor[0] * sky;
227 clear[1] = fgClearColor[1] * sky;
228 clear[2] = fgClearColor[2] * sky;
229 clear[3] = fgClearColor[3];
230 glClearColor(clear[0], clear[1], clear[2], clear[3]);
234 /**************************************************************************
235 * Update all Visuals (redraws anything graphics related)
236 **************************************************************************/
238 static void fgUpdateVisuals( void ) {
239 /* update view volume parameters */
240 fgUpdateViewParams();
242 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
244 /* Tell GL we are switching to model view parameters */
245 glMatrixMode(GL_MODELVIEW);
246 /* glLoadIdentity(); */
261 /**************************************************************************
262 * Update internal time dependent calculations (i.e. flight model)
263 **************************************************************************/
265 void fgUpdateTimeDepCalcs(int multi_loop) {
271 f = ¤t_aircraft.flight;
272 t = &cur_time_params;
275 /* update the flight model */
276 if ( multi_loop < 0 ) {
277 multi_loop = DEFAULT_MULTILOOP;
280 /* printf("updating flight model x %d\n", multi_loop); */
281 fgFlightModelUpdate(FG_LARCSIM, f, multi_loop);
283 /* refresh shared sun position and sun_vec */
284 fgUpdateSunPos(scenery.center);
286 /* update the view angle */
287 for ( i = 0; i < multi_loop; i++ ) {
288 if ( fabs(v->goal_view_offset - v->view_offset) < 0.05 ) {
289 v->view_offset = v->goal_view_offset;
292 /* move v->view_offset towards v->goal_view_offset */
293 if ( v->goal_view_offset > v->view_offset ) {
294 if ( v->goal_view_offset - v->view_offset < FG_PI ) {
295 v->view_offset += 0.01;
297 v->view_offset -= 0.01;
300 if ( v->view_offset - v->goal_view_offset < FG_PI ) {
301 v->view_offset -= 0.01;
303 v->view_offset += 0.01;
306 if ( v->view_offset > FG_2PI ) {
307 v->view_offset -= FG_2PI;
308 } else if ( v->view_offset < 0 ) {
309 v->view_offset += FG_2PI;
316 void fgInitTimeDepCalcs() {
317 /* initialize timer */
320 fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
326 /**************************************************************************
327 * Scenery management routines
328 **************************************************************************/
330 /* static void fgSceneryInit_OLD() { */
332 /* scenery = fgSceneryCompile_OLD();
333 runway = fgRunwayHack_OLD(0.69, 53.07);
337 /* create the scenery */
338 /* GLint fgSceneryCompile_OLD() {
341 scenery = mesh2GL(mesh_ptr_OLD);
347 /* hack in a runway */
348 /* GLint fgRunwayHack_OLD(double width, double length) {
349 static GLfloat concrete[4] = { 0.5, 0.5, 0.5, 1.0 };
350 static GLfloat line[4] = { 0.9, 0.9, 0.9, 1.0 };
353 float line_len, line_width_2, cur_pos;
355 runway = glGenLists(1);
356 glNewList(runway, GL_COMPILE);
359 /* glBegin(GL_POLYGON);
360 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, concrete );
361 glNormal3f(0.0, 0.0, 1.0);
363 glVertex3d( 0.0, -width/2.0, 0.0);
364 glVertex3d( 0.0, width/2.0, 0.0);
365 glVertex3d(length, width/2.0, 0.0);
366 glVertex3d(length, -width/2.0, 0.0);
369 /* draw center line */
370 /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, line );
371 line_len = length / ( 2 * num_lines + 1);
372 printf("line_len = %.3f\n", line_len);
375 for ( i = 0; i < num_lines; i++ ) {
377 glVertex3d( cur_pos, -line_width_2, 0.005);
378 glVertex3d( cur_pos, line_width_2, 0.005);
380 glVertex3d( cur_pos, line_width_2, 0.005);
381 glVertex3d( cur_pos, -line_width_2, 0.005);
392 /* draw the scenery */
393 /*static void fgSceneryDraw_OLD() {
394 static float z = 32.35;
400 printf("*** Drawing runway at %.2f\n", z);
402 glTranslatef( -398391.28, 120070.41, 32.35);
403 glRotatef(170.0, 0.0, 0.0, 1.0);
410 /* What should we do when we have nothing else to do? How about get
411 * ready for the next move and update the display? */
412 static void fgMainLoop( void ) {
413 static int remainder = 0;
414 int elapsed, multi_loop;
422 a = ¤t_aircraft;
424 t = &cur_time_params;
430 /* fgJoystickRead( &joy_x, &joy_y, &joy_b1, &joy_b2 );
431 printf( "Joystick X %f Y %f B1 %d B2 %d\n",
432 joy_x, joy_y, joy_b1, joy_b2 );
434 fgAileronSet( joy_x ); */
436 /* update the weather for our current position */
437 fgWeatherUpdate(FG_Longitude * RAD_TO_ARCSEC,
438 FG_Latitude * RAD_TO_ARCSEC,
439 FG_Altitude * FEET_TO_METER);
441 /* Calculate model iterations needed */
442 elapsed = fgGetTimeInterval();
443 printf("Time interval is = %d, previous remainder is = %d\n", elapsed,
445 printf("--> Frame rate is = %.2f\n", 1000.0 / (float)elapsed);
446 elapsed += remainder;
448 multi_loop = ((float)elapsed * 0.001) * DEFAULT_MODEL_HZ;
449 remainder = elapsed - ((multi_loop*1000) / DEFAULT_MODEL_HZ);
450 printf("Model iterations needed = %d, new remainder = %d\n", multi_loop,
453 if ( ! use_signals ) {
455 fgUpdateTimeDepCalcs(multi_loop);
458 /* I'm just sticking this here for now, it should probably move
460 cur_elev = mesh_altitude(FG_Longitude * RAD_TO_ARCSEC,
461 FG_Latitude * RAD_TO_ARCSEC);
462 printf("Ground elevation is %.2f meters here.\n", cur_elev);
463 /* FG_Runway_altitude = cur_elev * METER_TO_FEET; */
465 if ( FG_Altitude * FEET_TO_METER < cur_elev + 3.758099) {
466 /* set this here, otherwise if we set runway height above our
467 current height we get a really nasty bounce. */
468 FG_Runway_altitude = FG_Altitude - 3.758099;
470 /* now set aircraft altitude above ground */
471 FG_Altitude = cur_elev * METER_TO_FEET + 3.758099;
472 printf("<*> resetting altitude to %.0f meters\n",
473 FG_Altitude * FEET_TO_METER);
476 fgAircraftOutputCurrent(a);
483 /**************************************************************************
484 * Handle new window size or exposure
485 **************************************************************************/
487 static void fgReshape( int width, int height ) {
488 /* Do this so we can call fgReshape(0,0) ourselves without having to know
489 * what the values of width & height are. */
490 if ( (height > 0) && (width > 0) ) {
491 win_ratio = (GLfloat) height / (GLfloat) width;
494 /* Inform gl of our view window size */
495 glViewport(0, 0, (GLint)width, (GLint)height);
497 fgUpdateViewParams();
499 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
503 /**************************************************************************
505 **************************************************************************/
507 int main( int argc, char *argv[] ) {
510 f = ¤t_aircraft.flight;
512 printf("Flight Gear: prototype version %s\n\n", VERSION);
514 /**********************************************************************
515 * Initialize the Window/Graphics environment.
516 **********************************************************************/
519 /* initialize GLUT */
520 glutInit(&argc, argv);
522 /* Define Display Parameters */
523 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
525 /* Define initial window size */
526 glutInitWindowSize(640, 480);
528 /* Initialize windows */
529 glutCreateWindow("Flight Gear");
532 /* This is the general house keeping init routine */
535 /* This is the top level init routine which calls all the other
536 * subsystem initialization routines. If you are adding a
537 * subsystem to flight gear, its initialization call should
538 * located in this routine.*/
541 /* setup view parameters, only makes GL calls */
545 /* init timer routines, signals, etc. Arrange for an alarm
546 signal to be generated, etc. */
547 fgInitTimeDepCalcs();
550 /**********************************************************************
551 * Initialize the Event Handlers.
552 **********************************************************************/
555 /* call fgReshape() on window resizes */
556 glutReshapeFunc( fgReshape );
558 /* call key() on keyboard event */
559 glutKeyboardFunc( GLUTkey );
560 glutSpecialFunc( GLUTspecialkey );
562 /* call fgMainLoop() whenever there is nothing else to do */
563 glutIdleFunc( fgMainLoop );
566 glutDisplayFunc( fgUpdateVisuals );
568 /* pass control off to the GLUT event handler */
578 int printf (const char *format, ...) {
584 /* Revision 1.26 1997/12/09 04:25:29 curt
585 /* Working on adding a global lighting params structure.
587 * Revision 1.25 1997/12/08 22:54:09 curt
588 * Enabled GL_CULL_FACE.
590 * Revision 1.24 1997/11/25 19:25:32 curt
591 * Changes to integrate Durk's moon/sun code updates + clean up.
593 * Revision 1.23 1997/11/15 18:16:34 curt
596 * Revision 1.22 1997/10/30 12:38:41 curt
597 * Working on new scenery subsystem.
599 * Revision 1.21 1997/09/23 00:29:38 curt
600 * Tweaks to get things to compile with gcc-win32.
602 * Revision 1.20 1997/09/22 14:44:19 curt
603 * Continuing to try to align stars correctly.
605 * Revision 1.19 1997/09/18 16:20:08 curt
606 * At dusk/dawn add/remove stars in stages.
608 * Revision 1.18 1997/09/16 22:14:51 curt
609 * Tweaked time of day lighting equations. Don't draw stars during the day.
611 * Revision 1.17 1997/09/16 15:50:29 curt
612 * Working on star alignment and time issues.
614 * Revision 1.16 1997/09/13 02:00:06 curt
615 * Mostly working on stars and generating sidereal time for accurate star
618 * Revision 1.15 1997/09/05 14:17:27 curt
619 * More tweaking with stars.
621 * Revision 1.14 1997/09/05 01:35:53 curt
622 * Working on getting stars right.
624 * Revision 1.13 1997/09/04 02:17:34 curt
627 * Revision 1.12 1997/08/27 21:32:24 curt
628 * Restructured view calculation code. Added stars.
630 * Revision 1.11 1997/08/27 03:30:16 curt
631 * Changed naming scheme of basic shared structures.
633 * Revision 1.10 1997/08/25 20:27:22 curt
634 * Merged in initial HUD and Joystick code.
636 * Revision 1.9 1997/08/22 21:34:39 curt
637 * Doing a bit of reorganizing and house cleaning.
639 * Revision 1.8 1997/08/19 23:55:03 curt
640 * Worked on better simulating real lighting.
642 * Revision 1.7 1997/08/16 12:22:38 curt
643 * Working on improving the lighting/shading.
645 * Revision 1.6 1997/08/13 20:24:56 curt
646 * Changes due to changing sunpos interface.
648 * Revision 1.5 1997/08/06 21:08:32 curt
649 * Sun position now *really* works (I think) ... I still have sun time warping
650 * code in place, probably should remove it soon.
652 * Revision 1.4 1997/08/06 15:41:26 curt
653 * Working on correct sun position.
655 * Revision 1.3 1997/08/06 00:24:22 curt
656 * Working on correct real time sun lighting.
658 * Revision 1.2 1997/08/04 20:25:15 curt
659 * Organizational tweaking.
661 * Revision 1.1 1997/08/02 18:45:00 curt
662 * Renamed GLmain.c GLUTmain.c
664 * Revision 1.43 1997/08/02 16:23:47 curt
667 * Revision 1.42 1997/08/01 19:43:33 curt
668 * Making progress with coordinate system overhaul.
670 * Revision 1.41 1997/07/31 22:52:37 curt
671 * Working on redoing internal coordinate systems & scenery transformations.
673 * Revision 1.40 1997/07/30 16:12:42 curt
674 * Moved fg_random routines from Util/ to Math/
676 * Revision 1.39 1997/07/21 14:45:01 curt
679 * Revision 1.38 1997/07/19 23:04:47 curt
680 * Added an initial weather section.
682 * Revision 1.37 1997/07/19 22:34:02 curt
683 * Moved PI definitions to ../constants.h
684 * Moved random() stuff to ../Utils/ and renamed fg_random()
686 * Revision 1.36 1997/07/18 23:41:25 curt
687 * Tweaks for building with Cygnus Win32 compiler.
689 * Revision 1.35 1997/07/18 14:28:34 curt
690 * Hacked in some support for wind/turbulence.
692 * Revision 1.34 1997/07/16 20:04:48 curt
693 * Minor tweaks to aid Win32 port.
695 * Revision 1.33 1997/07/12 03:50:20 curt
696 * Added an #include <Windows32/Base.h> to help compiling for Win32
698 * Revision 1.32 1997/07/11 03:23:18 curt
699 * Solved some scenery display/orientation problems. Still have a positioning
700 * (or transformation?) problem.
702 * Revision 1.31 1997/07/11 01:29:58 curt
703 * More tweaking of terrian floor.
705 * Revision 1.30 1997/07/10 04:26:37 curt
706 * We now can interpolated ground elevation for any position in the grid. We
707 * can use this to enforce a "hard" ground. We still need to enforce some
708 * bounds checking so that we don't try to lookup data points outside the
711 * Revision 1.29 1997/07/09 21:31:12 curt
712 * Working on making the ground "hard."
714 * Revision 1.28 1997/07/08 18:20:12 curt
715 * Working on establishing a hard ground.
717 * Revision 1.27 1997/07/07 20:59:49 curt
718 * Working on scenery transformations to enable us to fly fluidly over the
719 * poles with no discontinuity/distortion in scenery.
721 * Revision 1.26 1997/07/05 20:43:34 curt
722 * renamed mat3 directory to Math so we could add other math related routines.
724 * Revision 1.25 1997/06/29 21:19:17 curt
725 * Working on scenery management system.
727 * Revision 1.24 1997/06/26 22:14:53 curt
728 * Beginning work on a scenery management system.
730 * Revision 1.23 1997/06/26 19:08:33 curt
731 * Restructuring make, adding automatic "make dep" support.
733 * Revision 1.22 1997/06/25 15:39:47 curt
734 * Minor changes to compile with rsxnt/win32.
736 * Revision 1.21 1997/06/22 21:44:41 curt
737 * Working on intergrating the VRML (subset) parser.
739 * Revision 1.20 1997/06/21 17:12:53 curt
740 * Capitalized subdirectory names.
742 * Revision 1.19 1997/06/18 04:10:31 curt
743 * A couple more runway tweaks ...
745 * Revision 1.18 1997/06/18 02:21:24 curt
748 * Revision 1.17 1997/06/17 16:51:58 curt
749 * Timer interval stuff now uses gettimeofday() instead of ftime()
751 * Revision 1.16 1997/06/17 04:19:16 curt
752 * More timer related tweaks with respect to view direction changes.
754 * Revision 1.15 1997/06/17 03:41:10 curt
755 * Nonsignal based interval timing is now working.
756 * This would be a good time to look at cleaning up the code structure a bit.
758 * Revision 1.14 1997/06/16 19:32:51 curt
759 * Starting to add general timer support.
761 * Revision 1.13 1997/06/02 03:40:06 curt
762 * A tiny bit more view tweaking.
764 * Revision 1.12 1997/06/02 03:01:38 curt
765 * Working on views (side, front, back, transitions, etc.)
767 * Revision 1.11 1997/05/31 19:16:25 curt
768 * Elevator trim added.
770 * Revision 1.10 1997/05/31 04:13:52 curt
773 * Continuing work on the LaRCsim flight model integration.
774 * Added some MSFS-like keyboard input handling.
776 * Revision 1.9 1997/05/30 19:27:01 curt
777 * The LaRCsim flight model is starting to look like it is working.
779 * Revision 1.8 1997/05/30 03:54:10 curt
780 * Made a bit more progress towards integrating the LaRCsim flight model.
782 * Revision 1.7 1997/05/29 22:39:49 curt
783 * Working on incorporating the LaRCsim flight model.
785 * Revision 1.6 1997/05/29 12:31:39 curt
786 * Minor tweaks, moving towards general flight model integration.
788 * Revision 1.5 1997/05/29 02:33:23 curt
789 * Updated to reflect changing interfaces in other "modules."
791 * Revision 1.4 1997/05/27 17:44:31 curt
792 * Renamed & rearranged variables and routines. Added some initial simple
793 * timer/alarm routines so the flight model can be updated on a regular
796 * Revision 1.3 1997/05/23 15:40:25 curt
797 * Added GNU copyright headers.
800 * Revision 1.2 1997/05/23 00:35:12 curt
801 * Trying to get fog to work ...
803 * Revision 1.1 1997/05/21 15:57:51 curt
804 * Renamed due to added GLUT support.
806 * Revision 1.3 1997/05/19 18:22:42 curt
807 * Parameter tweaking ... starting to stub in fog support.
809 * Revision 1.2 1997/05/17 00:17:34 curt
810 * Trying to stub in support for standard OpenGL.
812 * Revision 1.1 1997/05/16 16:05:52 curt